Adopting to new names of SPI packages to be introduced by version 1.0 of html4j API
2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.io.ByteArrayInputStream;
29 import java.io.InputStream;
30 import java.lang.annotation.Annotation;
31 import java.lang.reflect.Array;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.TypeVariable;
37 import org.apidesign.bck2brwsr.core.Exported;
38 import org.apidesign.bck2brwsr.core.JavaScriptBody;
39 import org.apidesign.bck2brwsr.core.JavaScriptOnly;
40 import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
41 import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
44 * Instances of the class {@code Class} represent classes and
45 * interfaces in a running Java application. An enum is a kind of
46 * class and an annotation is a kind of interface. Every array also
47 * belongs to a class that is reflected as a {@code Class} object
48 * that is shared by all arrays with the same element type and number
49 * of dimensions. The primitive Java types ({@code boolean},
50 * {@code byte}, {@code char}, {@code short},
51 * {@code int}, {@code long}, {@code float}, and
52 * {@code double}), and the keyword {@code void} are also
53 * represented as {@code Class} objects.
55 * <p> {@code Class} has no public constructor. Instead {@code Class}
56 * objects are constructed automatically by the Java Virtual Machine as classes
57 * are loaded and by calls to the {@code defineClass} method in the class
60 * <p> The following example uses a {@code Class} object to print the
61 * class name of an object:
63 * <p> <blockquote><pre>
64 * void printClassName(Object obj) {
65 * System.out.println("The class of " + obj +
66 * " is " + obj.getClass().getName());
70 * <p> It is also possible to get the {@code Class} object for a named
71 * type (or for void) using a class literal. See Section 15.8.2 of
72 * <cite>The Java™ Language Specification</cite>.
76 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
79 * @param <T> the type of the class modeled by this {@code Class}
80 * object. For example, the type of {@code String.class} is {@code
81 * Class<String>}. Use {@code Class<?>} if the class being modeled is
85 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
89 class Class<T> implements java.io.Serializable,
90 java.lang.reflect.GenericDeclaration,
91 java.lang.reflect.Type,
92 java.lang.reflect.AnnotatedElement {
93 private static final int ANNOTATION= 0x00002000;
94 private static final int ENUM = 0x00004000;
95 private static final int SYNTHETIC = 0x00001000;
98 * Constructor. Only the Java Virtual Machine creates Class
105 * Converts the object to a string. The string representation is the
106 * string "class" or "interface", followed by a space, and then by the
107 * fully qualified name of the class in the format returned by
108 * {@code getName}. If this {@code Class} object represents a
109 * primitive type, this method returns the name of the primitive type. If
110 * this {@code Class} object represents void this method returns
113 * @return a string representation of this class object.
115 public String toString() {
116 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
122 * Returns the {@code Class} object associated with the class or
123 * interface with the given string name. Invoking this method is
127 * {@code Class.forName(className, true, currentLoader)}
130 * where {@code currentLoader} denotes the defining class loader of
133 * <p> For example, the following code fragment returns the
134 * runtime {@code Class} descriptor for the class named
135 * {@code java.lang.Thread}:
138 * {@code Class t = Class.forName("java.lang.Thread")}
141 * A call to {@code forName("X")} causes the class named
142 * {@code X} to be initialized.
144 * @param className the fully qualified name of the desired class.
145 * @return the {@code Class} object for the class with the
147 * @exception LinkageError if the linkage fails
148 * @exception ExceptionInInitializerError if the initialization provoked
149 * by this method fails
150 * @exception ClassNotFoundException if the class cannot be located
152 public static Class<?> forName(String className)
153 throws ClassNotFoundException {
154 if (className.startsWith("[")) {
155 Class<?> arrType = defineArray(className, null);
156 Class<?> c = arrType;
157 while (c != null && c.isArray()) {
158 c = c.getComponentType0(); // verify component type is sane
163 Class<?> c = loadCls(className, className.replace('.', '_'));
165 throw new ClassNotFoundException(className);
168 } catch (Throwable ex) {
169 throw new ClassNotFoundException(className, ex);
175 * Returns the {@code Class} object associated with the class or
176 * interface with the given string name, using the given class loader.
177 * Given the fully qualified name for a class or interface (in the same
178 * format returned by {@code getName}) this method attempts to
179 * locate, load, and link the class or interface. The specified class
180 * loader is used to load the class or interface. If the parameter
181 * {@code loader} is null, the class is loaded through the bootstrap
182 * class loader. The class is initialized only if the
183 * {@code initialize} parameter is {@code true} and if it has
184 * not been initialized earlier.
186 * <p> If {@code name} denotes a primitive type or void, an attempt
187 * will be made to locate a user-defined class in the unnamed package whose
188 * name is {@code name}. Therefore, this method cannot be used to
189 * obtain any of the {@code Class} objects representing primitive
192 * <p> If {@code name} denotes an array class, the component type of
193 * the array class is loaded but not initialized.
195 * <p> For example, in an instance method the expression:
198 * {@code Class.forName("Foo")}
204 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
207 * Note that this method throws errors related to loading, linking or
208 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
209 * Java Language Specification</em>.
210 * Note that this method does not check whether the requested class
211 * is accessible to its caller.
213 * <p> If the {@code loader} is {@code null}, and a security
214 * manager is present, and the caller's class loader is not null, then this
215 * method calls the security manager's {@code checkPermission} method
216 * with a {@code RuntimePermission("getClassLoader")} permission to
217 * ensure it's ok to access the bootstrap class loader.
219 * @param name fully qualified name of the desired class
220 * @param initialize whether the class must be initialized
221 * @param loader class loader from which the class must be loaded
222 * @return class object representing the desired class
224 * @exception LinkageError if the linkage fails
225 * @exception ExceptionInInitializerError if the initialization provoked
226 * by this method fails
227 * @exception ClassNotFoundException if the class cannot be located by
228 * the specified class loader
230 * @see java.lang.Class#forName(String)
231 * @see java.lang.ClassLoader
234 public static Class<?> forName(String name, boolean initialize,
236 throws ClassNotFoundException
238 return forName(name);
241 @JavaScriptBody(args = {"n", "c" }, body =
244 + " var l = vm.loadClass ? vm.loadClass : exports ? exports.loadClass : null;\n"
248 + " if (vm[c]) m = vm[c];\n"
249 + " else if (exports[c]) m = exports[c];\n"
251 + "if (!m) return null;"
255 private static native Class<?> loadCls(String n, String c);
259 * Creates a new instance of the class represented by this {@code Class}
260 * object. The class is instantiated as if by a {@code new}
261 * expression with an empty argument list. The class is initialized if it
262 * has not already been initialized.
264 * <p>Note that this method propagates any exception thrown by the
265 * nullary constructor, including a checked exception. Use of
266 * this method effectively bypasses the compile-time exception
267 * checking that would otherwise be performed by the compiler.
269 * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
270 * Constructor.newInstance} method avoids this problem by wrapping
271 * any exception thrown by the constructor in a (checked) {@link
272 * java.lang.reflect.InvocationTargetException}.
274 * @return a newly allocated instance of the class represented by this
276 * @exception IllegalAccessException if the class or its nullary
277 * constructor is not accessible.
278 * @exception InstantiationException
279 * if this {@code Class} represents an abstract class,
280 * an interface, an array class, a primitive type, or void;
281 * or if the class has no nullary constructor;
282 * or if the instantiation fails for some other reason.
283 * @exception ExceptionInInitializerError if the initialization
284 * provoked by this method fails.
285 * @exception SecurityException
286 * If a security manager, <i>s</i>, is present and any of the
287 * following conditions is met:
292 * {@link SecurityManager#checkMemberAccess
293 * s.checkMemberAccess(this, Member.PUBLIC)} denies
294 * creation of new instances of this class
296 * <li> the caller's class loader is not the same as or an
297 * ancestor of the class loader for the current class and
298 * invocation of {@link SecurityManager#checkPackageAccess
299 * s.checkPackageAccess()} denies access to the package
305 @JavaScriptBody(args = { "self", "illegal" }, body =
306 "\nvar c = self.cnstr;"
307 + "\nif (c['cons__V']) {"
308 + "\n if ((c['cons__V'].access & 0x1) != 0) {"
309 + "\n var inst = c();"
310 + "\n c['cons__V'].call(inst);"
313 + "\n return illegal;"
317 private static native Object newInstance0(Class<?> self, Object illegal);
319 public T newInstance()
320 throws InstantiationException, IllegalAccessException
322 Object illegal = new Object();
323 Object inst = newInstance0(this, illegal);
325 throw new InstantiationException(getName());
327 if (inst == illegal) {
328 throw new IllegalAccessException();
334 * Determines if the specified {@code Object} is assignment-compatible
335 * with the object represented by this {@code Class}. This method is
336 * the dynamic equivalent of the Java language {@code instanceof}
337 * operator. The method returns {@code true} if the specified
338 * {@code Object} argument is non-null and can be cast to the
339 * reference type represented by this {@code Class} object without
340 * raising a {@code ClassCastException.} It returns {@code false}
343 * <p> Specifically, if this {@code Class} object represents a
344 * declared class, this method returns {@code true} if the specified
345 * {@code Object} argument is an instance of the represented class (or
346 * of any of its subclasses); it returns {@code false} otherwise. If
347 * this {@code Class} object represents an array class, this method
348 * returns {@code true} if the specified {@code Object} argument
349 * can be converted to an object of the array class by an identity
350 * conversion or by a widening reference conversion; it returns
351 * {@code false} otherwise. If this {@code Class} object
352 * represents an interface, this method returns {@code true} if the
353 * class or any superclass of the specified {@code Object} argument
354 * implements this interface; it returns {@code false} otherwise. If
355 * this {@code Class} object represents a primitive type, this method
356 * returns {@code false}.
358 * @param obj the object to check
359 * @return true if {@code obj} is an instance of this class
363 public boolean isInstance(Object obj) {
368 return isAssignableFrom(obj.getClass());
371 String prop = "$instOf_" + getName().replace('.', '_');
372 return hasProperty(obj, prop);
375 @JavaScriptBody(args = { "who", "prop" }, body =
376 "if (who[prop]) return true; else return false;"
378 private static native boolean hasProperty(Object who, String prop);
382 * Determines if the class or interface represented by this
383 * {@code Class} object is either the same as, or is a superclass or
384 * superinterface of, the class or interface represented by the specified
385 * {@code Class} parameter. It returns {@code true} if so;
386 * otherwise it returns {@code false}. If this {@code Class}
387 * object represents a primitive type, this method returns
388 * {@code true} if the specified {@code Class} parameter is
389 * exactly this {@code Class} object; otherwise it returns
392 * <p> Specifically, this method tests whether the type represented by the
393 * specified {@code Class} parameter can be converted to the type
394 * represented by this {@code Class} object via an identity conversion
395 * or via a widening reference conversion. See <em>The Java Language
396 * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
398 * @param cls the {@code Class} object to be checked
399 * @return the {@code boolean} value indicating whether objects of the
400 * type {@code cls} can be assigned to objects of this class
401 * @exception NullPointerException if the specified Class parameter is
405 public boolean isAssignableFrom(Class<?> cls) {
409 if (this == Object.class) {
414 final Class<?> cmpType = cls.getComponentType();
416 return this == cmpType;
418 return cmpType != null && getComponentType().isAssignableFrom(cmpType);
423 if (cls.isPrimitive()) {
429 String prop = "$instOf_" + getName().replace('.', '_');
430 return hasCnstrProperty(cls, prop);
434 @JavaScriptBody(args = { "who", "prop" }, body =
435 "if (who.cnstr.prototype[prop]) return true; else return false;"
437 private static native boolean hasCnstrProperty(Object who, String prop);
441 * Determines if the specified {@code Class} object represents an
444 * @return {@code true} if this object represents an interface;
445 * {@code false} otherwise.
447 public boolean isInterface() {
448 return (getAccess() & 0x200) != 0;
451 @JavaScriptBody(args = {}, body = "return this.access;")
452 private native int getAccess();
456 * Determines if this {@code Class} object represents an array class.
458 * @return {@code true} if this object represents an array class;
459 * {@code false} otherwise.
462 public boolean isArray() {
463 return hasProperty(this, "array"); // NOI18N
468 * Determines if the specified {@code Class} object represents a
471 * <p> There are nine predefined {@code Class} objects to represent
472 * the eight primitive types and void. These are created by the Java
473 * Virtual Machine, and have the same names as the primitive types that
474 * they represent, namely {@code boolean}, {@code byte},
475 * {@code char}, {@code short}, {@code int},
476 * {@code long}, {@code float}, and {@code double}.
478 * <p> These objects may only be accessed via the following public static
479 * final variables, and are the only {@code Class} objects for which
480 * this method returns {@code true}.
482 * @return true if and only if this class represents a primitive type
484 * @see java.lang.Boolean#TYPE
485 * @see java.lang.Character#TYPE
486 * @see java.lang.Byte#TYPE
487 * @see java.lang.Short#TYPE
488 * @see java.lang.Integer#TYPE
489 * @see java.lang.Long#TYPE
490 * @see java.lang.Float#TYPE
491 * @see java.lang.Double#TYPE
492 * @see java.lang.Void#TYPE
495 @JavaScriptBody(args = {}, body =
496 "if (this.primitive) return true;"
497 + "else return false;"
499 public native boolean isPrimitive();
502 * Returns true if this {@code Class} object represents an annotation
503 * type. Note that if this method returns true, {@link #isInterface()}
504 * would also return true, as all annotation types are also interfaces.
506 * @return {@code true} if this class object represents an annotation
507 * type; {@code false} otherwise
510 public boolean isAnnotation() {
511 return (getModifiers() & ANNOTATION) != 0;
515 * Returns {@code true} if this class is a synthetic class;
516 * returns {@code false} otherwise.
517 * @return {@code true} if and only if this class is a synthetic class as
518 * defined by the Java Language Specification.
521 public boolean isSynthetic() {
522 return (getModifiers() & SYNTHETIC) != 0;
526 * Returns the name of the entity (class, interface, array class,
527 * primitive type, or void) represented by this {@code Class} object,
528 * as a {@code String}.
530 * <p> If this class object represents a reference type that is not an
531 * array type then the binary name of the class is returned, as specified
533 * <cite>The Java™ Language Specification</cite>.
535 * <p> If this class object represents a primitive type or void, then the
536 * name returned is a {@code String} equal to the Java language
537 * keyword corresponding to the primitive type or void.
539 * <p> If this class object represents a class of arrays, then the internal
540 * form of the name consists of the name of the element type preceded by
541 * one or more '{@code [}' characters representing the depth of the array
542 * nesting. The encoding of element type names is as follows:
544 * <blockquote><table summary="Element types and encodings">
545 * <tr><th> Element Type <th> <th> Encoding
546 * <tr><td> boolean <td> <td align=center> Z
547 * <tr><td> byte <td> <td align=center> B
548 * <tr><td> char <td> <td align=center> C
549 * <tr><td> class or interface
550 * <td> <td align=center> L<i>classname</i>;
551 * <tr><td> double <td> <td align=center> D
552 * <tr><td> float <td> <td align=center> F
553 * <tr><td> int <td> <td align=center> I
554 * <tr><td> long <td> <td align=center> J
555 * <tr><td> short <td> <td align=center> S
556 * </table></blockquote>
558 * <p> The class or interface name <i>classname</i> is the binary name of
559 * the class specified above.
563 * String.class.getName()
564 * returns "java.lang.String"
565 * byte.class.getName()
567 * (new Object[3]).getClass().getName()
568 * returns "[Ljava.lang.Object;"
569 * (new int[3][4][5][6][7][8][9]).getClass().getName()
571 * </pre></blockquote>
573 * @return the name of the class or interface
574 * represented by this object.
576 public String getName() {
577 return jvmName().replace('/', '.');
580 @JavaScriptBody(args = {}, body = "return this.jvmName;")
581 private native String jvmName();
585 * Returns an array of {@code TypeVariable} objects that represent the
586 * type variables declared by the generic declaration represented by this
587 * {@code GenericDeclaration} object, in declaration order. Returns an
588 * array of length 0 if the underlying generic declaration declares no type
591 * @return an array of {@code TypeVariable} objects that represent
592 * the type variables declared by this generic declaration
593 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
594 * signature of this generic declaration does not conform to
595 * the format specified in
596 * <cite>The Java™ Virtual Machine Specification</cite>
599 public TypeVariable<Class<T>>[] getTypeParameters() {
600 throw new UnsupportedOperationException();
604 * Returns the {@code Class} representing the superclass of the entity
605 * (class, interface, primitive type or void) represented by this
606 * {@code Class}. If this {@code Class} represents either the
607 * {@code Object} class, an interface, a primitive type, or void, then
608 * null is returned. If this object represents an array class then the
609 * {@code Class} object representing the {@code Object} class is
612 * @return the superclass of the class represented by this object.
614 @JavaScriptBody(args = {}, body = "return this.superclass;")
615 public native Class<? super T> getSuperclass();
618 * Returns the Java language modifiers for this class or interface, encoded
619 * in an integer. The modifiers consist of the Java Virtual Machine's
620 * constants for {@code public}, {@code protected},
621 * {@code private}, {@code final}, {@code static},
622 * {@code abstract} and {@code interface}; they should be decoded
623 * using the methods of class {@code Modifier}.
625 * <p> If the underlying class is an array class, then its
626 * {@code public}, {@code private} and {@code protected}
627 * modifiers are the same as those of its component type. If this
628 * {@code Class} represents a primitive type or void, its
629 * {@code public} modifier is always {@code true}, and its
630 * {@code protected} and {@code private} modifiers are always
631 * {@code false}. If this object represents an array class, a
632 * primitive type or void, then its {@code final} modifier is always
633 * {@code true} and its interface modifier is always
634 * {@code false}. The values of its other modifiers are not determined
635 * by this specification.
637 * <p> The modifier encodings are defined in <em>The Java Virtual Machine
638 * Specification</em>, table 4.1.
640 * @return the {@code int} representing the modifiers for this class
641 * @see java.lang.reflect.Modifier
644 public int getModifiers() {
649 * If the class or interface represented by this {@code Class} object
650 * is a member of another class, returns the {@code Class} object
651 * representing the class in which it was declared. This method returns
652 * null if this class or interface is not a member of any other class. If
653 * this {@code Class} object represents an array class, a primitive
654 * type, or void,then this method returns null.
656 * @return the declaring class for this class
659 public Class<?> getDeclaringClass() {
660 throw new SecurityException();
664 * Returns the simple name of the underlying class as given in the
665 * source code. Returns an empty string if the underlying class is
668 * <p>The simple name of an array is the simple name of the
669 * component type with "[]" appended. In particular the simple
670 * name of an array whose component type is anonymous is "[]".
672 * @return the simple name of the underlying class
675 public String getSimpleName() {
677 return getComponentType().getSimpleName()+"[]";
679 String simpleName = getSimpleBinaryName();
680 if (simpleName == null) { // top level class
681 simpleName = getName();
682 return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
684 // According to JLS3 "Binary Compatibility" (13.1) the binary
685 // name of non-package classes (not top level) is the binary
686 // name of the immediately enclosing class followed by a '$' followed by:
687 // (for nested and inner classes): the simple name.
688 // (for local classes): 1 or more digits followed by the simple name.
689 // (for anonymous classes): 1 or more digits.
691 // Since getSimpleBinaryName() will strip the binary name of
692 // the immediatly enclosing class, we are now looking at a
693 // string that matches the regular expression "\$[0-9]*"
694 // followed by a simple name (considering the simple of an
695 // anonymous class to be the empty string).
697 // Remove leading "\$[0-9]*" from the name
698 int length = simpleName.length();
699 if (length < 1 || simpleName.charAt(0) != '$')
700 throw new IllegalStateException("Malformed class name");
702 while (index < length && isAsciiDigit(simpleName.charAt(index)))
704 // Eventually, this is the empty string iff this is an anonymous class
705 return simpleName.substring(index);
709 * Returns the "simple binary name" of the underlying class, i.e.,
710 * the binary name without the leading enclosing class name.
711 * Returns {@code null} if the underlying class is a top level
714 private String getSimpleBinaryName() {
715 Class<?> enclosingClass = null; // XXX getEnclosingClass();
716 if (enclosingClass == null) // top level class
718 // Otherwise, strip the enclosing class' name
720 return getName().substring(enclosingClass.getName().length());
721 } catch (IndexOutOfBoundsException ex) {
722 throw new IllegalStateException("Malformed class name");
727 * Returns an array containing {@code Field} objects reflecting all
728 * the accessible public fields of the class or interface represented by
729 * this {@code Class} object. The elements in the array returned are
730 * not sorted and are not in any particular order. This method returns an
731 * array of length 0 if the class or interface has no accessible public
732 * fields, or if it represents an array class, a primitive type, or void.
734 * <p> Specifically, if this {@code Class} object represents a class,
735 * this method returns the public fields of this class and of all its
736 * superclasses. If this {@code Class} object represents an
737 * interface, this method returns the fields of this interface and of all
738 * its superinterfaces.
740 * <p> The implicit length field for array class is not reflected by this
741 * method. User code should use the methods of class {@code Array} to
744 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
746 * @return the array of {@code Field} objects representing the
748 * @exception SecurityException
749 * If a security manager, <i>s</i>, is present and any of the
750 * following conditions is met:
755 * {@link SecurityManager#checkMemberAccess
756 * s.checkMemberAccess(this, Member.PUBLIC)} denies
757 * access to the fields within this class
759 * <li> the caller's class loader is not the same as or an
760 * ancestor of the class loader for the current class and
761 * invocation of {@link SecurityManager#checkPackageAccess
762 * s.checkPackageAccess()} denies access to the package
769 public Field[] getFields() throws SecurityException {
770 throw new SecurityException();
774 * Returns an array containing {@code Method} objects reflecting all
775 * the public <em>member</em> methods of the class or interface represented
776 * by this {@code Class} object, including those declared by the class
777 * or interface and those inherited from superclasses and
778 * superinterfaces. Array classes return all the (public) member methods
779 * inherited from the {@code Object} class. The elements in the array
780 * returned are not sorted and are not in any particular order. This
781 * method returns an array of length 0 if this {@code Class} object
782 * represents a class or interface that has no public member methods, or if
783 * this {@code Class} object represents a primitive type or void.
785 * <p> The class initialization method {@code <clinit>} is not
786 * included in the returned array. If the class declares multiple public
787 * member methods with the same parameter types, they are all included in
788 * the returned array.
790 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
792 * @return the array of {@code Method} objects representing the
793 * public methods of this class
794 * @exception SecurityException
795 * If a security manager, <i>s</i>, is present and any of the
796 * following conditions is met:
801 * {@link SecurityManager#checkMemberAccess
802 * s.checkMemberAccess(this, Member.PUBLIC)} denies
803 * access to the methods within this class
805 * <li> the caller's class loader is not the same as or an
806 * ancestor of the class loader for the current class and
807 * invocation of {@link SecurityManager#checkPackageAccess
808 * s.checkPackageAccess()} denies access to the package
815 public Method[] getMethods() throws SecurityException {
816 return MethodImpl.findMethods(this, 0x01);
820 * Returns a {@code Field} object that reflects the specified public
821 * member field of the class or interface represented by this
822 * {@code Class} object. The {@code name} parameter is a
823 * {@code String} specifying the simple name of the desired field.
825 * <p> The field to be reflected is determined by the algorithm that
826 * follows. Let C be the class represented by this object:
828 * <LI> If C declares a public field with the name specified, that is the
829 * field to be reflected.</LI>
830 * <LI> If no field was found in step 1 above, this algorithm is applied
831 * recursively to each direct superinterface of C. The direct
832 * superinterfaces are searched in the order they were declared.</LI>
833 * <LI> If no field was found in steps 1 and 2 above, and C has a
834 * superclass S, then this algorithm is invoked recursively upon S.
835 * If C has no superclass, then a {@code NoSuchFieldException}
839 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
841 * @param name the field name
842 * @return the {@code Field} object of this class specified by
844 * @exception NoSuchFieldException if a field with the specified name is
846 * @exception NullPointerException if {@code name} is {@code null}
847 * @exception SecurityException
848 * If a security manager, <i>s</i>, is present and any of the
849 * following conditions is met:
854 * {@link SecurityManager#checkMemberAccess
855 * s.checkMemberAccess(this, Member.PUBLIC)} denies
856 * access to the field
858 * <li> the caller's class loader is not the same as or an
859 * ancestor of the class loader for the current class and
860 * invocation of {@link SecurityManager#checkPackageAccess
861 * s.checkPackageAccess()} denies access to the package
868 public Field getField(String name)
869 throws SecurityException {
870 throw new SecurityException();
875 * Returns a {@code Method} object that reflects the specified public
876 * member method of the class or interface represented by this
877 * {@code Class} object. The {@code name} parameter is a
878 * {@code String} specifying the simple name of the desired method. The
879 * {@code parameterTypes} parameter is an array of {@code Class}
880 * objects that identify the method's formal parameter types, in declared
881 * order. If {@code parameterTypes} is {@code null}, it is
882 * treated as if it were an empty array.
884 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
885 * {@code NoSuchMethodException} is raised. Otherwise, the method to
886 * be reflected is determined by the algorithm that follows. Let C be the
887 * class represented by this object:
889 * <LI> C is searched for any <I>matching methods</I>. If no matching
890 * method is found, the algorithm of step 1 is invoked recursively on
891 * the superclass of C.</LI>
892 * <LI> If no method was found in step 1 above, the superinterfaces of C
893 * are searched for a matching method. If any such method is found, it
897 * To find a matching method in a class C: If C declares exactly one
898 * public method with the specified name and exactly the same formal
899 * parameter types, that is the method reflected. If more than one such
900 * method is found in C, and one of these methods has a return type that is
901 * more specific than any of the others, that method is reflected;
902 * otherwise one of the methods is chosen arbitrarily.
904 * <p>Note that there may be more than one matching method in a
905 * class because while the Java language forbids a class to
906 * declare multiple methods with the same signature but different
907 * return types, the Java virtual machine does not. This
908 * increased flexibility in the virtual machine can be used to
909 * implement various language features. For example, covariant
910 * returns can be implemented with {@linkplain
911 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
912 * method and the method being overridden would have the same
913 * signature but different return types.
915 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
917 * @param name the name of the method
918 * @param parameterTypes the list of parameters
919 * @return the {@code Method} object that matches the specified
920 * {@code name} and {@code parameterTypes}
921 * @exception NoSuchMethodException if a matching method is not found
922 * or if the name is "<init>"or "<clinit>".
923 * @exception NullPointerException if {@code name} is {@code null}
924 * @exception SecurityException
925 * If a security manager, <i>s</i>, is present and any of the
926 * following conditions is met:
931 * {@link SecurityManager#checkMemberAccess
932 * s.checkMemberAccess(this, Member.PUBLIC)} denies
933 * access to the method
935 * <li> the caller's class loader is not the same as or an
936 * ancestor of the class loader for the current class and
937 * invocation of {@link SecurityManager#checkPackageAccess
938 * s.checkPackageAccess()} denies access to the package
945 public Method getMethod(String name, Class<?>... parameterTypes)
946 throws SecurityException, NoSuchMethodException {
947 Method m = MethodImpl.findMethod(this, name, parameterTypes);
949 StringBuilder sb = new StringBuilder();
950 sb.append(getName()).append('.').append(name).append('(');
952 for (int i = 0; i < parameterTypes.length; i++) {
953 sb.append(sep).append(parameterTypes[i].getName());
957 throw new NoSuchMethodException(sb.toString());
963 * Returns an array of {@code Method} objects reflecting all the
964 * methods declared by the class or interface represented by this
965 * {@code Class} object. This includes public, protected, default
966 * (package) access, and private methods, but excludes inherited methods.
967 * The elements in the array returned are not sorted and are not in any
968 * particular order. This method returns an array of length 0 if the class
969 * or interface declares no methods, or if this {@code Class} object
970 * represents a primitive type, an array class, or void. The class
971 * initialization method {@code <clinit>} is not included in the
972 * returned array. If the class declares multiple public member methods
973 * with the same parameter types, they are all included in the returned
976 * <p> See <em>The Java Language Specification</em>, section 8.2.
978 * @return the array of {@code Method} objects representing all the
979 * declared methods of this class
980 * @exception SecurityException
981 * If a security manager, <i>s</i>, is present and any of the
982 * following conditions is met:
987 * {@link SecurityManager#checkMemberAccess
988 * s.checkMemberAccess(this, Member.DECLARED)} denies
989 * access to the declared methods within this class
991 * <li> the caller's class loader is not the same as or an
992 * ancestor of the class loader for the current class and
993 * invocation of {@link SecurityManager#checkPackageAccess
994 * s.checkPackageAccess()} denies access to the package
1001 public Method[] getDeclaredMethods() throws SecurityException {
1002 throw new SecurityException();
1006 * Returns an array of {@code Field} objects reflecting all the fields
1007 * declared by the class or interface represented by this
1008 * {@code Class} object. This includes public, protected, default
1009 * (package) access, and private fields, but excludes inherited fields.
1010 * The elements in the array returned are not sorted and are not in any
1011 * particular order. This method returns an array of length 0 if the class
1012 * or interface declares no fields, or if this {@code Class} object
1013 * represents a primitive type, an array class, or void.
1015 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1017 * @return the array of {@code Field} objects representing all the
1018 * declared fields of this class
1019 * @exception SecurityException
1020 * If a security manager, <i>s</i>, is present and any of the
1021 * following conditions is met:
1025 * <li> invocation of
1026 * {@link SecurityManager#checkMemberAccess
1027 * s.checkMemberAccess(this, Member.DECLARED)} denies
1028 * access to the declared fields within this class
1030 * <li> the caller's class loader is not the same as or an
1031 * ancestor of the class loader for the current class and
1032 * invocation of {@link SecurityManager#checkPackageAccess
1033 * s.checkPackageAccess()} denies access to the package
1040 public Field[] getDeclaredFields() throws SecurityException {
1041 throw new SecurityException();
1045 * <b>Bck2Brwsr</b> emulation can only seek public methods, otherwise it
1046 * throws a {@code SecurityException}.
1048 * Returns a {@code Method} object that reflects the specified
1049 * declared method of the class or interface represented by this
1050 * {@code Class} object. The {@code name} parameter is a
1051 * {@code String} that specifies the simple name of the desired
1052 * method, and the {@code parameterTypes} parameter is an array of
1053 * {@code Class} objects that identify the method's formal parameter
1054 * types, in declared order. If more than one method with the same
1055 * parameter types is declared in a class, and one of these methods has a
1056 * return type that is more specific than any of the others, that method is
1057 * returned; otherwise one of the methods is chosen arbitrarily. If the
1058 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
1061 * @param name the name of the method
1062 * @param parameterTypes the parameter array
1063 * @return the {@code Method} object for the method of this class
1064 * matching the specified name and parameters
1065 * @exception NoSuchMethodException if a matching method is not found.
1066 * @exception NullPointerException if {@code name} is {@code null}
1067 * @exception SecurityException
1068 * If a security manager, <i>s</i>, is present and any of the
1069 * following conditions is met:
1073 * <li> invocation of
1074 * {@link SecurityManager#checkMemberAccess
1075 * s.checkMemberAccess(this, Member.DECLARED)} denies
1076 * access to the declared method
1078 * <li> the caller's class loader is not the same as or an
1079 * ancestor of the class loader for the current class and
1080 * invocation of {@link SecurityManager#checkPackageAccess
1081 * s.checkPackageAccess()} denies access to the package
1088 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1089 throws NoSuchMethodException, SecurityException {
1091 return getMethod(name, parameterTypes);
1092 } catch (NoSuchMethodException ex) {
1093 throw new SecurityException();
1098 * Returns a {@code Field} object that reflects the specified declared
1099 * field of the class or interface represented by this {@code Class}
1100 * object. The {@code name} parameter is a {@code String} that
1101 * specifies the simple name of the desired field. Note that this method
1102 * will not reflect the {@code length} field of an array class.
1104 * @param name the name of the field
1105 * @return the {@code Field} object for the specified field in this
1107 * @exception NoSuchFieldException if a field with the specified name is
1109 * @exception NullPointerException if {@code name} is {@code null}
1110 * @exception SecurityException
1111 * If a security manager, <i>s</i>, is present and any of the
1112 * following conditions is met:
1116 * <li> invocation of
1117 * {@link SecurityManager#checkMemberAccess
1118 * s.checkMemberAccess(this, Member.DECLARED)} denies
1119 * access to the declared field
1121 * <li> the caller's class loader is not the same as or an
1122 * ancestor of the class loader for the current class and
1123 * invocation of {@link SecurityManager#checkPackageAccess
1124 * s.checkPackageAccess()} denies access to the package
1131 public Field getDeclaredField(String name)
1132 throws SecurityException {
1133 throw new SecurityException();
1137 * Returns an array containing {@code Constructor} objects reflecting
1138 * all the public constructors of the class represented by this
1139 * {@code Class} object. An array of length 0 is returned if the
1140 * class has no public constructors, or if the class is an array class, or
1141 * if the class reflects a primitive type or void.
1143 * Note that while this method returns an array of {@code
1144 * Constructor<T>} objects (that is an array of constructors from
1145 * this class), the return type of this method is {@code
1146 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1147 * might be expected. This less informative return type is
1148 * necessary since after being returned from this method, the
1149 * array could be modified to hold {@code Constructor} objects for
1150 * different classes, which would violate the type guarantees of
1151 * {@code Constructor<T>[]}.
1153 * @return the array of {@code Constructor} objects representing the
1154 * public constructors of this class
1155 * @exception SecurityException
1156 * If a security manager, <i>s</i>, is present and any of the
1157 * following conditions is met:
1161 * <li> invocation of
1162 * {@link SecurityManager#checkMemberAccess
1163 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1164 * access to the constructors within this class
1166 * <li> the caller's class loader is not the same as or an
1167 * ancestor of the class loader for the current class and
1168 * invocation of {@link SecurityManager#checkPackageAccess
1169 * s.checkPackageAccess()} denies access to the package
1176 public Constructor<?>[] getConstructors() throws SecurityException {
1177 return MethodImpl.findConstructors(this, 0x01);
1181 * Returns a {@code Constructor} object that reflects the specified
1182 * public constructor of the class represented by this {@code Class}
1183 * object. The {@code parameterTypes} parameter is an array of
1184 * {@code Class} objects that identify the constructor's formal
1185 * parameter types, in declared order.
1187 * If this {@code Class} object represents an inner class
1188 * declared in a non-static context, the formal parameter types
1189 * include the explicit enclosing instance as the first parameter.
1191 * <p> The constructor to reflect is the public constructor of the class
1192 * represented by this {@code Class} object whose formal parameter
1193 * types match those specified by {@code parameterTypes}.
1195 * @param parameterTypes the parameter array
1196 * @return the {@code Constructor} object of the public constructor that
1197 * matches the specified {@code parameterTypes}
1198 * @exception NoSuchMethodException if a matching method is not found.
1199 * @exception SecurityException
1200 * If a security manager, <i>s</i>, is present and any of the
1201 * following conditions is met:
1205 * <li> invocation of
1206 * {@link SecurityManager#checkMemberAccess
1207 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1208 * access to the constructor
1210 * <li> the caller's class loader is not the same as or an
1211 * ancestor of the class loader for the current class and
1212 * invocation of {@link SecurityManager#checkPackageAccess
1213 * s.checkPackageAccess()} denies access to the package
1220 public Constructor<T> getConstructor(Class<?>... parameterTypes)
1221 throws NoSuchMethodException, SecurityException {
1222 Constructor c = MethodImpl.findConstructor(this, parameterTypes);
1224 StringBuilder sb = new StringBuilder();
1225 sb.append(getName()).append('(');
1227 for (int i = 0; i < parameterTypes.length; i++) {
1228 sb.append(sep).append(parameterTypes[i].getName());
1232 throw new NoSuchMethodException(sb.toString());
1238 * Returns an array of {@code Constructor} objects reflecting all the
1239 * constructors declared by the class represented by this
1240 * {@code Class} object. These are public, protected, default
1241 * (package) access, and private constructors. The elements in the array
1242 * returned are not sorted and are not in any particular order. If the
1243 * class has a default constructor, it is included in the returned array.
1244 * This method returns an array of length 0 if this {@code Class}
1245 * object represents an interface, a primitive type, an array class, or
1248 * <p> See <em>The Java Language Specification</em>, section 8.2.
1250 * @return the array of {@code Constructor} objects representing all the
1251 * declared constructors of this class
1252 * @exception SecurityException
1253 * If a security manager, <i>s</i>, is present and any of the
1254 * following conditions is met:
1258 * <li> invocation of
1259 * {@link SecurityManager#checkMemberAccess
1260 * s.checkMemberAccess(this, Member.DECLARED)} denies
1261 * access to the declared constructors within this class
1263 * <li> the caller's class loader is not the same as or an
1264 * ancestor of the class loader for the current class and
1265 * invocation of {@link SecurityManager#checkPackageAccess
1266 * s.checkPackageAccess()} denies access to the package
1273 public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
1274 throw new SecurityException();
1277 * Returns a {@code Constructor} object that reflects the specified
1278 * constructor of the class or interface represented by this
1279 * {@code Class} object. The {@code parameterTypes} parameter is
1280 * an array of {@code Class} objects that identify the constructor's
1281 * formal parameter types, in declared order.
1283 * If this {@code Class} object represents an inner class
1284 * declared in a non-static context, the formal parameter types
1285 * include the explicit enclosing instance as the first parameter.
1287 * @param parameterTypes the parameter array
1288 * @return The {@code Constructor} object for the constructor with the
1289 * specified parameter list
1290 * @exception NoSuchMethodException if a matching method is not found.
1291 * @exception SecurityException
1292 * If a security manager, <i>s</i>, is present and any of the
1293 * following conditions is met:
1297 * <li> invocation of
1298 * {@link SecurityManager#checkMemberAccess
1299 * s.checkMemberAccess(this, Member.DECLARED)} denies
1300 * access to the declared constructor
1302 * <li> the caller's class loader is not the same as or an
1303 * ancestor of the class loader for the current class and
1304 * invocation of {@link SecurityManager#checkPackageAccess
1305 * s.checkPackageAccess()} denies access to the package
1312 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
1313 throws NoSuchMethodException, SecurityException {
1314 return getConstructor(parameterTypes);
1319 * Character.isDigit answers {@code true} to some non-ascii
1320 * digits. This one does not.
1322 private static boolean isAsciiDigit(char c) {
1323 return '0' <= c && c <= '9';
1327 * Returns the canonical name of the underlying class as
1328 * defined by the Java Language Specification. Returns null if
1329 * the underlying class does not have a canonical name (i.e., if
1330 * it is a local or anonymous class or an array whose component
1331 * type does not have a canonical name).
1332 * @return the canonical name of the underlying class if it exists, and
1333 * {@code null} otherwise.
1336 public String getCanonicalName() {
1338 String canonicalName = getComponentType().getCanonicalName();
1339 if (canonicalName != null)
1340 return canonicalName + "[]";
1344 // if (isLocalOrAnonymousClass())
1346 // Class<?> enclosingClass = getEnclosingClass();
1347 Class<?> enclosingClass = null;
1348 if (enclosingClass == null) { // top level class
1351 String enclosingName = enclosingClass.getCanonicalName();
1352 if (enclosingName == null)
1354 return enclosingName + "." + getSimpleName();
1359 * Finds a resource with a given name. The rules for searching resources
1360 * associated with a given class are implemented by the defining
1361 * {@linkplain ClassLoader class loader} of the class. This method
1362 * delegates to this object's class loader. If this object was loaded by
1363 * the bootstrap class loader, the method delegates to {@link
1364 * ClassLoader#getSystemResourceAsStream}.
1366 * <p> Before delegation, an absolute resource name is constructed from the
1367 * given resource name using this algorithm:
1371 * <li> If the {@code name} begins with a {@code '/'}
1372 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1373 * portion of the {@code name} following the {@code '/'}.
1375 * <li> Otherwise, the absolute name is of the following form:
1378 * {@code modified_package_name/name}
1381 * <p> Where the {@code modified_package_name} is the package name of this
1382 * object with {@code '/'} substituted for {@code '.'}
1383 * (<tt>'\u002e'</tt>).
1387 * @param name name of the desired resource
1388 * @return A {@link java.io.InputStream} object or {@code null} if
1389 * no resource with this name is found
1390 * @throws NullPointerException If {@code name} is {@code null}
1393 public InputStream getResourceAsStream(String name) {
1394 name = resolveName(name);
1395 byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
1396 return arr == null ? null : new ByteArrayInputStream(arr);
1400 * Finds a resource with a given name. The rules for searching resources
1401 * associated with a given class are implemented by the defining
1402 * {@linkplain ClassLoader class loader} of the class. This method
1403 * delegates to this object's class loader. If this object was loaded by
1404 * the bootstrap class loader, the method delegates to {@link
1405 * ClassLoader#getSystemResource}.
1407 * <p> Before delegation, an absolute resource name is constructed from the
1408 * given resource name using this algorithm:
1412 * <li> If the {@code name} begins with a {@code '/'}
1413 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1414 * portion of the {@code name} following the {@code '/'}.
1416 * <li> Otherwise, the absolute name is of the following form:
1419 * {@code modified_package_name/name}
1422 * <p> Where the {@code modified_package_name} is the package name of this
1423 * object with {@code '/'} substituted for {@code '.'}
1424 * (<tt>'\u002e'</tt>).
1428 * @param name name of the desired resource
1429 * @return A {@link java.net.URL} object or {@code null} if no
1430 * resource with this name is found
1433 public java.net.URL getResource(String name) {
1434 return newResourceURL(name, getResourceAsStream(name));
1437 static URL newResourceURL(String name, InputStream is) {
1438 return is == null ? null : newResourceURL0(URL.class, "res:/" + name, is);
1441 @JavaScriptBody(args = { "url", "spec", "is" }, body =
1442 "var u = url.cnstr(true);\n"
1443 + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n"
1446 private static native URL newResourceURL0(Class<URL> url, String spec, InputStream is);
1449 * Add a package name prefix if the name is not absolute Remove leading "/"
1450 * if name is absolute
1452 private String resolveName(String name) {
1456 if (!name.startsWith("/")) {
1458 while (c.isArray()) {
1459 c = c.getComponentType();
1461 String baseName = c.getName();
1462 int index = baseName.lastIndexOf('.');
1464 name = baseName.substring(0, index).replace('.', '/')
1468 name = name.substring(1);
1474 * Returns the class loader for the class. Some implementations may use
1475 * null to represent the bootstrap class loader. This method will return
1476 * null in such implementations if this class was loaded by the bootstrap
1479 * <p> If a security manager is present, and the caller's class loader is
1480 * not null and the caller's class loader is not the same as or an ancestor of
1481 * the class loader for the class whose class loader is requested, then
1482 * this method calls the security manager's {@code checkPermission}
1483 * method with a {@code RuntimePermission("getClassLoader")}
1484 * permission to ensure it's ok to access the class loader for the class.
1487 * represents a primitive type or void, null is returned.
1489 * @return the class loader that loaded the class or interface
1490 * represented by this object.
1491 * @throws SecurityException
1492 * if a security manager exists and its
1493 * {@code checkPermission} method denies
1494 * access to the class loader for the class.
1495 * @see java.lang.ClassLoader
1496 * @see SecurityManager#checkPermission
1497 * @see java.lang.RuntimePermission
1499 public ClassLoader getClassLoader() {
1500 return ClassLoader.getSystemClassLoader();
1504 * Determines the interfaces implemented by the class or interface
1505 * represented by this object.
1507 * <p> If this object represents a class, the return value is an array
1508 * containing objects representing all interfaces implemented by the
1509 * class. The order of the interface objects in the array corresponds to
1510 * the order of the interface names in the {@code implements} clause
1511 * of the declaration of the class represented by this object. For
1512 * example, given the declaration:
1514 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1516 * suppose the value of {@code s} is an instance of
1517 * {@code Shimmer}; the value of the expression:
1519 * {@code s.getClass().getInterfaces()[0]}
1521 * is the {@code Class} object that represents interface
1522 * {@code FloorWax}; and the value of:
1524 * {@code s.getClass().getInterfaces()[1]}
1526 * is the {@code Class} object that represents interface
1527 * {@code DessertTopping}.
1529 * <p> If this object represents an interface, the array contains objects
1530 * representing all interfaces extended by the interface. The order of the
1531 * interface objects in the array corresponds to the order of the interface
1532 * names in the {@code extends} clause of the declaration of the
1533 * interface represented by this object.
1535 * <p> If this object represents a class or interface that implements no
1536 * interfaces, the method returns an array of length 0.
1538 * <p> If this object represents a primitive type or void, the method
1539 * returns an array of length 0.
1541 * @return an array of interfaces implemented by this class.
1543 public native Class<?>[] getInterfaces();
1546 * Returns the {@code Class} representing the component type of an
1547 * array. If this class does not represent an array class this method
1550 * @return the {@code Class} representing the component type of this
1551 * class if this class is an array
1552 * @see java.lang.reflect.Array
1555 public Class<?> getComponentType() {
1558 Class<?> c = getComponentTypeByFnc();
1559 return c != null ? c : getComponentType0();
1560 } catch (ClassNotFoundException cnfe) {
1561 throw new IllegalStateException(cnfe);
1567 @JavaScriptBody(args = { }, body =
1569 + " var c = this.fnc(false).constructor.$class;\n"
1570 // + " java.lang.System.out.println('will call: ' + (!!this.fnc) + ' res: ' + c.jvmName);\n"
1571 + " if (c) return c;\n"
1575 private native Class<?> getComponentTypeByFnc();
1577 private Class<?> getComponentType0() throws ClassNotFoundException {
1578 String n = getName().substring(1);
1579 switch (n.charAt(0)) {
1581 n = n.substring(1, n.length() - 1);
1582 return Class.forName(n);
1584 return Integer.TYPE;
1594 return Boolean.TYPE;
1600 return Character.TYPE;
1602 return defineArray(n, null);
1604 throw new ClassNotFoundException("Unknown component type of " + getName());
1608 @JavaScriptBody(args = { "sig", "fnc" }, body =
1609 "if (!sig) sig = '[Ljava/lang/Object;';\n" +
1610 "var c = Array[sig];\n" +
1612 " c = vm.java_lang_Class(true);\n" +
1613 " c.jvmName = sig;\n" +
1614 " c.superclass = vm.java_lang_Object(false).$class;\n" +
1615 " c.array = true;\n" +
1616 " Array[sig] = c;\n" +
1618 "if (!c.fnc) c.fnc = fnc;\n" +
1621 private static native Class<?> defineArray(String sig, Object fnc);
1624 * Returns true if and only if this class was declared as an enum in the
1627 * @return true if and only if this class was declared as an enum in the
1631 public boolean isEnum() {
1632 // An enum must both directly extend java.lang.Enum and have
1633 // the ENUM bit set; classes for specialized enum constants
1634 // don't do the former.
1635 return (this.getModifiers() & ENUM) != 0 &&
1636 this.getSuperclass() == java.lang.Enum.class;
1640 * Casts an object to the class or interface represented
1641 * by this {@code Class} object.
1643 * @param obj the object to be cast
1644 * @return the object after casting, or null if obj is null
1646 * @throws ClassCastException if the object is not
1647 * null and is not assignable to the type T.
1651 public T cast(Object obj) {
1652 if (obj != null && !isInstance(obj))
1653 throw new ClassCastException(cannotCastMsg(obj));
1657 private String cannotCastMsg(Object obj) {
1658 return "Cannot cast " + obj.getClass().getName() + " to " + getName();
1662 * Casts this {@code Class} object to represent a subclass of the class
1663 * represented by the specified class object. Checks that that the cast
1664 * is valid, and throws a {@code ClassCastException} if it is not. If
1665 * this method succeeds, it always returns a reference to this class object.
1667 * <p>This method is useful when a client needs to "narrow" the type of
1668 * a {@code Class} object to pass it to an API that restricts the
1669 * {@code Class} objects that it is willing to accept. A cast would
1670 * generate a compile-time warning, as the correctness of the cast
1671 * could not be checked at runtime (because generic types are implemented
1674 * @return this {@code Class} object, cast to represent a subclass of
1675 * the specified class object.
1676 * @throws ClassCastException if this {@code Class} object does not
1677 * represent a subclass of the specified class (here "subclass" includes
1678 * the class itself).
1681 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
1682 if (clazz.isAssignableFrom(this))
1683 return (Class<? extends U>) this;
1685 throw new ClassCastException(this.toString());
1688 @JavaScriptBody(args = { "ac" },
1690 "if (this.anno) {\n"
1691 + " var r = this.anno['L' + ac.jvmName + ';'];\n"
1692 + " if (typeof r === 'undefined') r = null;\n"
1694 + "} else return null;\n"
1696 private Object getAnnotationData(Class<?> annotationClass) {
1697 throw new UnsupportedOperationException();
1700 * @throws NullPointerException {@inheritDoc}
1703 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
1704 Object data = getAnnotationData(annotationClass);
1705 return data == null ? null : AnnotationImpl.create(annotationClass, data);
1709 * @throws NullPointerException {@inheritDoc}
1712 @JavaScriptBody(args = { "ac" },
1713 body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
1714 + "else return false;"
1716 public boolean isAnnotationPresent(
1717 Class<? extends Annotation> annotationClass) {
1718 if (annotationClass == null)
1719 throw new NullPointerException();
1721 return getAnnotation(annotationClass) != null;
1724 @JavaScriptBody(args = {}, body = "return this.anno;")
1725 private Object getAnnotationData() {
1726 throw new UnsupportedOperationException();
1732 public Annotation[] getAnnotations() {
1733 Object data = getAnnotationData();
1734 return data == null ? new Annotation[0] : AnnotationImpl.create(data);
1740 public Annotation[] getDeclaredAnnotations() {
1741 throw new UnsupportedOperationException();
1744 @JavaScriptBody(args = "type", body = ""
1745 + "var c = vm.java_lang_Class(true);"
1746 + "c.jvmName = type;"
1747 + "c.primitive = true;"
1750 native static Class getPrimitiveClass(String type);
1752 @JavaScriptBody(args = {}, body =
1753 "return this['desiredAssertionStatus'] ? this['desiredAssertionStatus'] : false;"
1755 public native boolean desiredAssertionStatus();
1757 public boolean equals(Object obj) {
1758 if (isPrimitive() && obj instanceof Class) {
1759 Class c = ((Class)obj);
1760 return c.isPrimitive() && getName().equals(c.getName());
1762 return super.equals(obj);
1765 static void registerNatives() {
1766 boolean assertsOn = false;
1767 // assert assertsOn = true;
1771 } catch (Throwable ex) {
1777 @JavaScriptBody(args = {}, body = "var p = vm.java_lang_Object(false);"
1778 + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
1780 static native void registerToString();
1782 @JavaScriptBody(args = {"self"}, body
1783 = "var c = self.constructor.$class;\n"
1784 + "return c ? c : null;\n"
1786 static native Class<?> classFor(Object self);
1789 @JavaScriptBody(args = { "self" }, body
1790 = "if (self['$hashCode']) return self['$hashCode'];\n"
1791 + "var h = self['computeHashCode__I'] ? self['computeHashCode__I']() : Math.random() * Math.pow(2, 31);\n"
1792 + "return self['$hashCode'] = h & h;"
1794 static native int defaultHashCode(Object self);
1796 @JavaScriptBody(args = "self", body
1797 = "\nif (!self['$instOf_java_lang_Cloneable']) {"
1800 + "\n var clone = self.constructor(true);"
1801 + "\n var props = Object.getOwnPropertyNames(self);"
1802 + "\n for (var i = 0; i < props.length; i++) {"
1803 + "\n var p = props[i];"
1804 + "\n clone[p] = self[p];"
1806 + "\n return clone;"
1809 static native Object clone(Object self) throws CloneNotSupportedException;
1811 @JavaScriptOnly(name = "toJS", value = "function(v) {\n"
1812 + " if (v === null) return null;\n"
1813 + " if (Object.prototype.toString.call(v) === '[object Array]') {\n"
1814 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).convArray__Ljava_lang_Object_2Ljava_lang_Object_2(v);\n"
1816 + " return v.valueOf();\n"
1819 static native int toJS();
1822 @JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_apidesign_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
1823 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
1826 static native int activate();
1829 @JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_netbeans_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
1830 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
1833 static native int activateNew();
1835 private static Object bck2BrwsrCnvrt(Object o) {
1836 if (o instanceof Throwable) {
1839 final String msg = msg(o);
1840 if (msg == null || msg.startsWith("TypeError")) {
1841 return new NullPointerException(msg);
1843 return new Throwable(msg);
1846 @JavaScriptBody(args = {"o"}, body = "return o ? o.toString() : null;")
1847 private static native String msg(Object o);
1850 @JavaScriptOnly(name = "bck2BrwsrThrwrbl", value = "c.bck2BrwsrCnvrt__Ljava_lang_Object_2Ljava_lang_Object_2")
1851 private static void bck2BrwsrCnvrtVM() {