Remove the dirty hacks with names. The fix with the compiler should be good enough.
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) {
411 final Class<?> cmpType = cls.getComponentType();
413 return this == cmpType;
415 return cmpType != null && getComponentType().isAssignableFrom(cmpType);
420 if (cls.isPrimitive()) {
423 String prop = "$instOf_" + getName().replace('.', '_');
424 return hasCnstrProperty(cls, prop);
428 @JavaScriptBody(args = { "who", "prop" }, body =
429 "if (who.cnstr.prototype[prop]) return true; else return false;"
431 private static native boolean hasCnstrProperty(Object who, String prop);
435 * Determines if the specified {@code Class} object represents an
438 * @return {@code true} if this object represents an interface;
439 * {@code false} otherwise.
441 public boolean isInterface() {
442 return (getAccess() & 0x200) != 0;
445 @JavaScriptBody(args = {}, body = "return this.access;")
446 private native int getAccess();
450 * Determines if this {@code Class} object represents an array class.
452 * @return {@code true} if this object represents an array class;
453 * {@code false} otherwise.
456 public boolean isArray() {
457 return hasProperty(this, "array"); // NOI18N
462 * Determines if the specified {@code Class} object represents a
465 * <p> There are nine predefined {@code Class} objects to represent
466 * the eight primitive types and void. These are created by the Java
467 * Virtual Machine, and have the same names as the primitive types that
468 * they represent, namely {@code boolean}, {@code byte},
469 * {@code char}, {@code short}, {@code int},
470 * {@code long}, {@code float}, and {@code double}.
472 * <p> These objects may only be accessed via the following public static
473 * final variables, and are the only {@code Class} objects for which
474 * this method returns {@code true}.
476 * @return true if and only if this class represents a primitive type
478 * @see java.lang.Boolean#TYPE
479 * @see java.lang.Character#TYPE
480 * @see java.lang.Byte#TYPE
481 * @see java.lang.Short#TYPE
482 * @see java.lang.Integer#TYPE
483 * @see java.lang.Long#TYPE
484 * @see java.lang.Float#TYPE
485 * @see java.lang.Double#TYPE
486 * @see java.lang.Void#TYPE
489 @JavaScriptBody(args = {}, body =
490 "if (this.primitive) return true;"
491 + "else return false;"
493 public native boolean isPrimitive();
496 * Returns true if this {@code Class} object represents an annotation
497 * type. Note that if this method returns true, {@link #isInterface()}
498 * would also return true, as all annotation types are also interfaces.
500 * @return {@code true} if this class object represents an annotation
501 * type; {@code false} otherwise
504 public boolean isAnnotation() {
505 return (getModifiers() & ANNOTATION) != 0;
509 * Returns {@code true} if this class is a synthetic class;
510 * returns {@code false} otherwise.
511 * @return {@code true} if and only if this class is a synthetic class as
512 * defined by the Java Language Specification.
515 public boolean isSynthetic() {
516 return (getModifiers() & SYNTHETIC) != 0;
520 * Returns the name of the entity (class, interface, array class,
521 * primitive type, or void) represented by this {@code Class} object,
522 * as a {@code String}.
524 * <p> If this class object represents a reference type that is not an
525 * array type then the binary name of the class is returned, as specified
527 * <cite>The Java™ Language Specification</cite>.
529 * <p> If this class object represents a primitive type or void, then the
530 * name returned is a {@code String} equal to the Java language
531 * keyword corresponding to the primitive type or void.
533 * <p> If this class object represents a class of arrays, then the internal
534 * form of the name consists of the name of the element type preceded by
535 * one or more '{@code [}' characters representing the depth of the array
536 * nesting. The encoding of element type names is as follows:
538 * <blockquote><table summary="Element types and encodings">
539 * <tr><th> Element Type <th> <th> Encoding
540 * <tr><td> boolean <td> <td align=center> Z
541 * <tr><td> byte <td> <td align=center> B
542 * <tr><td> char <td> <td align=center> C
543 * <tr><td> class or interface
544 * <td> <td align=center> L<i>classname</i>;
545 * <tr><td> double <td> <td align=center> D
546 * <tr><td> float <td> <td align=center> F
547 * <tr><td> int <td> <td align=center> I
548 * <tr><td> long <td> <td align=center> J
549 * <tr><td> short <td> <td align=center> S
550 * </table></blockquote>
552 * <p> The class or interface name <i>classname</i> is the binary name of
553 * the class specified above.
557 * String.class.getName()
558 * returns "java.lang.String"
559 * byte.class.getName()
561 * (new Object[3]).getClass().getName()
562 * returns "[Ljava.lang.Object;"
563 * (new int[3][4][5][6][7][8][9]).getClass().getName()
565 * </pre></blockquote>
567 * @return the name of the class or interface
568 * represented by this object.
570 public String getName() {
571 return jvmName().replace('/', '.');
574 @JavaScriptBody(args = {}, body = "return this.jvmName;")
575 private native String jvmName();
579 * Returns an array of {@code TypeVariable} objects that represent the
580 * type variables declared by the generic declaration represented by this
581 * {@code GenericDeclaration} object, in declaration order. Returns an
582 * array of length 0 if the underlying generic declaration declares no type
585 * @return an array of {@code TypeVariable} objects that represent
586 * the type variables declared by this generic declaration
587 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
588 * signature of this generic declaration does not conform to
589 * the format specified in
590 * <cite>The Java™ Virtual Machine Specification</cite>
593 public TypeVariable<Class<T>>[] getTypeParameters() {
594 throw new UnsupportedOperationException();
598 * Returns the {@code Class} representing the superclass of the entity
599 * (class, interface, primitive type or void) represented by this
600 * {@code Class}. If this {@code Class} represents either the
601 * {@code Object} class, an interface, a primitive type, or void, then
602 * null is returned. If this object represents an array class then the
603 * {@code Class} object representing the {@code Object} class is
606 * @return the superclass of the class represented by this object.
608 @JavaScriptBody(args = {}, body = "return this.superclass;")
609 public native Class<? super T> getSuperclass();
612 * Returns the Java language modifiers for this class or interface, encoded
613 * in an integer. The modifiers consist of the Java Virtual Machine's
614 * constants for {@code public}, {@code protected},
615 * {@code private}, {@code final}, {@code static},
616 * {@code abstract} and {@code interface}; they should be decoded
617 * using the methods of class {@code Modifier}.
619 * <p> If the underlying class is an array class, then its
620 * {@code public}, {@code private} and {@code protected}
621 * modifiers are the same as those of its component type. If this
622 * {@code Class} represents a primitive type or void, its
623 * {@code public} modifier is always {@code true}, and its
624 * {@code protected} and {@code private} modifiers are always
625 * {@code false}. If this object represents an array class, a
626 * primitive type or void, then its {@code final} modifier is always
627 * {@code true} and its interface modifier is always
628 * {@code false}. The values of its other modifiers are not determined
629 * by this specification.
631 * <p> The modifier encodings are defined in <em>The Java Virtual Machine
632 * Specification</em>, table 4.1.
634 * @return the {@code int} representing the modifiers for this class
635 * @see java.lang.reflect.Modifier
638 public int getModifiers() {
643 * If the class or interface represented by this {@code Class} object
644 * is a member of another class, returns the {@code Class} object
645 * representing the class in which it was declared. This method returns
646 * null if this class or interface is not a member of any other class. If
647 * this {@code Class} object represents an array class, a primitive
648 * type, or void,then this method returns null.
650 * @return the declaring class for this class
653 public Class<?> getDeclaringClass() {
654 throw new SecurityException();
658 * Returns the simple name of the underlying class as given in the
659 * source code. Returns an empty string if the underlying class is
662 * <p>The simple name of an array is the simple name of the
663 * component type with "[]" appended. In particular the simple
664 * name of an array whose component type is anonymous is "[]".
666 * @return the simple name of the underlying class
669 public String getSimpleName() {
671 return getComponentType().getSimpleName()+"[]";
673 String simpleName = getSimpleBinaryName();
674 if (simpleName == null) { // top level class
675 simpleName = getName();
676 return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
678 // According to JLS3 "Binary Compatibility" (13.1) the binary
679 // name of non-package classes (not top level) is the binary
680 // name of the immediately enclosing class followed by a '$' followed by:
681 // (for nested and inner classes): the simple name.
682 // (for local classes): 1 or more digits followed by the simple name.
683 // (for anonymous classes): 1 or more digits.
685 // Since getSimpleBinaryName() will strip the binary name of
686 // the immediatly enclosing class, we are now looking at a
687 // string that matches the regular expression "\$[0-9]*"
688 // followed by a simple name (considering the simple of an
689 // anonymous class to be the empty string).
691 // Remove leading "\$[0-9]*" from the name
692 int length = simpleName.length();
693 if (length < 1 || simpleName.charAt(0) != '$')
694 throw new IllegalStateException("Malformed class name");
696 while (index < length && isAsciiDigit(simpleName.charAt(index)))
698 // Eventually, this is the empty string iff this is an anonymous class
699 return simpleName.substring(index);
703 * Returns the "simple binary name" of the underlying class, i.e.,
704 * the binary name without the leading enclosing class name.
705 * Returns {@code null} if the underlying class is a top level
708 private String getSimpleBinaryName() {
709 Class<?> enclosingClass = null; // XXX getEnclosingClass();
710 if (enclosingClass == null) // top level class
712 // Otherwise, strip the enclosing class' name
714 return getName().substring(enclosingClass.getName().length());
715 } catch (IndexOutOfBoundsException ex) {
716 throw new IllegalStateException("Malformed class name");
721 * Returns an array containing {@code Field} objects reflecting all
722 * the accessible public fields of the class or interface represented by
723 * this {@code Class} object. The elements in the array returned are
724 * not sorted and are not in any particular order. This method returns an
725 * array of length 0 if the class or interface has no accessible public
726 * fields, or if it represents an array class, a primitive type, or void.
728 * <p> Specifically, if this {@code Class} object represents a class,
729 * this method returns the public fields of this class and of all its
730 * superclasses. If this {@code Class} object represents an
731 * interface, this method returns the fields of this interface and of all
732 * its superinterfaces.
734 * <p> The implicit length field for array class is not reflected by this
735 * method. User code should use the methods of class {@code Array} to
738 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
740 * @return the array of {@code Field} objects representing the
742 * @exception SecurityException
743 * If a security manager, <i>s</i>, is present and any of the
744 * following conditions is met:
749 * {@link SecurityManager#checkMemberAccess
750 * s.checkMemberAccess(this, Member.PUBLIC)} denies
751 * access to the fields within this class
753 * <li> the caller's class loader is not the same as or an
754 * ancestor of the class loader for the current class and
755 * invocation of {@link SecurityManager#checkPackageAccess
756 * s.checkPackageAccess()} denies access to the package
763 public Field[] getFields() throws SecurityException {
764 throw new SecurityException();
768 * Returns an array containing {@code Method} objects reflecting all
769 * the public <em>member</em> methods of the class or interface represented
770 * by this {@code Class} object, including those declared by the class
771 * or interface and those inherited from superclasses and
772 * superinterfaces. Array classes return all the (public) member methods
773 * inherited from the {@code Object} class. The elements in the array
774 * returned are not sorted and are not in any particular order. This
775 * method returns an array of length 0 if this {@code Class} object
776 * represents a class or interface that has no public member methods, or if
777 * this {@code Class} object represents a primitive type or void.
779 * <p> The class initialization method {@code <clinit>} is not
780 * included in the returned array. If the class declares multiple public
781 * member methods with the same parameter types, they are all included in
782 * the returned array.
784 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
786 * @return the array of {@code Method} objects representing the
787 * public methods of this class
788 * @exception SecurityException
789 * If a security manager, <i>s</i>, is present and any of the
790 * following conditions is met:
795 * {@link SecurityManager#checkMemberAccess
796 * s.checkMemberAccess(this, Member.PUBLIC)} denies
797 * access to the methods within this class
799 * <li> the caller's class loader is not the same as or an
800 * ancestor of the class loader for the current class and
801 * invocation of {@link SecurityManager#checkPackageAccess
802 * s.checkPackageAccess()} denies access to the package
809 public Method[] getMethods() throws SecurityException {
810 return MethodImpl.findMethods(this, 0x01);
814 * Returns a {@code Field} object that reflects the specified public
815 * member field of the class or interface represented by this
816 * {@code Class} object. The {@code name} parameter is a
817 * {@code String} specifying the simple name of the desired field.
819 * <p> The field to be reflected is determined by the algorithm that
820 * follows. Let C be the class represented by this object:
822 * <LI> If C declares a public field with the name specified, that is the
823 * field to be reflected.</LI>
824 * <LI> If no field was found in step 1 above, this algorithm is applied
825 * recursively to each direct superinterface of C. The direct
826 * superinterfaces are searched in the order they were declared.</LI>
827 * <LI> If no field was found in steps 1 and 2 above, and C has a
828 * superclass S, then this algorithm is invoked recursively upon S.
829 * If C has no superclass, then a {@code NoSuchFieldException}
833 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
835 * @param name the field name
836 * @return the {@code Field} object of this class specified by
838 * @exception NoSuchFieldException if a field with the specified name is
840 * @exception NullPointerException if {@code name} is {@code null}
841 * @exception SecurityException
842 * If a security manager, <i>s</i>, is present and any of the
843 * following conditions is met:
848 * {@link SecurityManager#checkMemberAccess
849 * s.checkMemberAccess(this, Member.PUBLIC)} denies
850 * access to the field
852 * <li> the caller's class loader is not the same as or an
853 * ancestor of the class loader for the current class and
854 * invocation of {@link SecurityManager#checkPackageAccess
855 * s.checkPackageAccess()} denies access to the package
862 public Field getField(String name)
863 throws SecurityException {
864 throw new SecurityException();
869 * Returns a {@code Method} object that reflects the specified public
870 * member method of the class or interface represented by this
871 * {@code Class} object. The {@code name} parameter is a
872 * {@code String} specifying the simple name of the desired method. The
873 * {@code parameterTypes} parameter is an array of {@code Class}
874 * objects that identify the method's formal parameter types, in declared
875 * order. If {@code parameterTypes} is {@code null}, it is
876 * treated as if it were an empty array.
878 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
879 * {@code NoSuchMethodException} is raised. Otherwise, the method to
880 * be reflected is determined by the algorithm that follows. Let C be the
881 * class represented by this object:
883 * <LI> C is searched for any <I>matching methods</I>. If no matching
884 * method is found, the algorithm of step 1 is invoked recursively on
885 * the superclass of C.</LI>
886 * <LI> If no method was found in step 1 above, the superinterfaces of C
887 * are searched for a matching method. If any such method is found, it
891 * To find a matching method in a class C: If C declares exactly one
892 * public method with the specified name and exactly the same formal
893 * parameter types, that is the method reflected. If more than one such
894 * method is found in C, and one of these methods has a return type that is
895 * more specific than any of the others, that method is reflected;
896 * otherwise one of the methods is chosen arbitrarily.
898 * <p>Note that there may be more than one matching method in a
899 * class because while the Java language forbids a class to
900 * declare multiple methods with the same signature but different
901 * return types, the Java virtual machine does not. This
902 * increased flexibility in the virtual machine can be used to
903 * implement various language features. For example, covariant
904 * returns can be implemented with {@linkplain
905 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
906 * method and the method being overridden would have the same
907 * signature but different return types.
909 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
911 * @param name the name of the method
912 * @param parameterTypes the list of parameters
913 * @return the {@code Method} object that matches the specified
914 * {@code name} and {@code parameterTypes}
915 * @exception NoSuchMethodException if a matching method is not found
916 * or if the name is "<init>"or "<clinit>".
917 * @exception NullPointerException if {@code name} is {@code null}
918 * @exception SecurityException
919 * If a security manager, <i>s</i>, is present and any of the
920 * following conditions is met:
925 * {@link SecurityManager#checkMemberAccess
926 * s.checkMemberAccess(this, Member.PUBLIC)} denies
927 * access to the method
929 * <li> the caller's class loader is not the same as or an
930 * ancestor of the class loader for the current class and
931 * invocation of {@link SecurityManager#checkPackageAccess
932 * s.checkPackageAccess()} denies access to the package
939 public Method getMethod(String name, Class<?>... parameterTypes)
940 throws SecurityException, NoSuchMethodException {
941 Method m = MethodImpl.findMethod(this, name, parameterTypes);
943 StringBuilder sb = new StringBuilder();
944 sb.append(getName()).append('.').append(name).append('(');
946 for (int i = 0; i < parameterTypes.length; i++) {
947 sb.append(sep).append(parameterTypes[i].getName());
951 throw new NoSuchMethodException(sb.toString());
957 * Returns an array of {@code Method} objects reflecting all the
958 * methods declared by the class or interface represented by this
959 * {@code Class} object. This includes public, protected, default
960 * (package) access, and private methods, but excludes inherited methods.
961 * The elements in the array returned are not sorted and are not in any
962 * particular order. This method returns an array of length 0 if the class
963 * or interface declares no methods, or if this {@code Class} object
964 * represents a primitive type, an array class, or void. The class
965 * initialization method {@code <clinit>} is not included in the
966 * returned array. If the class declares multiple public member methods
967 * with the same parameter types, they are all included in the returned
970 * <p> See <em>The Java Language Specification</em>, section 8.2.
972 * @return the array of {@code Method} objects representing all the
973 * declared methods of this class
974 * @exception SecurityException
975 * If a security manager, <i>s</i>, is present and any of the
976 * following conditions is met:
981 * {@link SecurityManager#checkMemberAccess
982 * s.checkMemberAccess(this, Member.DECLARED)} denies
983 * access to the declared methods within this class
985 * <li> the caller's class loader is not the same as or an
986 * ancestor of the class loader for the current class and
987 * invocation of {@link SecurityManager#checkPackageAccess
988 * s.checkPackageAccess()} denies access to the package
995 public Method[] getDeclaredMethods() throws SecurityException {
996 throw new SecurityException();
1000 * Returns an array of {@code Field} objects reflecting all the fields
1001 * declared by the class or interface represented by this
1002 * {@code Class} object. This includes public, protected, default
1003 * (package) access, and private fields, but excludes inherited fields.
1004 * The elements in the array returned are not sorted and are not in any
1005 * particular order. This method returns an array of length 0 if the class
1006 * or interface declares no fields, or if this {@code Class} object
1007 * represents a primitive type, an array class, or void.
1009 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1011 * @return the array of {@code Field} objects representing all the
1012 * declared fields of this class
1013 * @exception SecurityException
1014 * If a security manager, <i>s</i>, is present and any of the
1015 * following conditions is met:
1019 * <li> invocation of
1020 * {@link SecurityManager#checkMemberAccess
1021 * s.checkMemberAccess(this, Member.DECLARED)} denies
1022 * access to the declared fields within this class
1024 * <li> the caller's class loader is not the same as or an
1025 * ancestor of the class loader for the current class and
1026 * invocation of {@link SecurityManager#checkPackageAccess
1027 * s.checkPackageAccess()} denies access to the package
1034 public Field[] getDeclaredFields() throws SecurityException {
1035 throw new SecurityException();
1039 * <b>Bck2Brwsr</b> emulation can only seek public methods, otherwise it
1040 * throws a {@code SecurityException}.
1042 * Returns a {@code Method} object that reflects the specified
1043 * declared method of the class or interface represented by this
1044 * {@code Class} object. The {@code name} parameter is a
1045 * {@code String} that specifies the simple name of the desired
1046 * method, and the {@code parameterTypes} parameter is an array of
1047 * {@code Class} objects that identify the method's formal parameter
1048 * types, in declared order. If more than one method with the same
1049 * parameter types is declared in a class, and one of these methods has a
1050 * return type that is more specific than any of the others, that method is
1051 * returned; otherwise one of the methods is chosen arbitrarily. If the
1052 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
1055 * @param name the name of the method
1056 * @param parameterTypes the parameter array
1057 * @return the {@code Method} object for the method of this class
1058 * matching the specified name and parameters
1059 * @exception NoSuchMethodException if a matching method is not found.
1060 * @exception NullPointerException if {@code name} is {@code null}
1061 * @exception SecurityException
1062 * If a security manager, <i>s</i>, is present and any of the
1063 * following conditions is met:
1067 * <li> invocation of
1068 * {@link SecurityManager#checkMemberAccess
1069 * s.checkMemberAccess(this, Member.DECLARED)} denies
1070 * access to the declared method
1072 * <li> the caller's class loader is not the same as or an
1073 * ancestor of the class loader for the current class and
1074 * invocation of {@link SecurityManager#checkPackageAccess
1075 * s.checkPackageAccess()} denies access to the package
1082 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1083 throws NoSuchMethodException, SecurityException {
1085 return getMethod(name, parameterTypes);
1086 } catch (NoSuchMethodException ex) {
1087 throw new SecurityException();
1092 * Returns a {@code Field} object that reflects the specified declared
1093 * field of the class or interface represented by this {@code Class}
1094 * object. The {@code name} parameter is a {@code String} that
1095 * specifies the simple name of the desired field. Note that this method
1096 * will not reflect the {@code length} field of an array class.
1098 * @param name the name of the field
1099 * @return the {@code Field} object for the specified field in this
1101 * @exception NoSuchFieldException if a field with the specified name is
1103 * @exception NullPointerException if {@code name} is {@code null}
1104 * @exception SecurityException
1105 * If a security manager, <i>s</i>, is present and any of the
1106 * following conditions is met:
1110 * <li> invocation of
1111 * {@link SecurityManager#checkMemberAccess
1112 * s.checkMemberAccess(this, Member.DECLARED)} denies
1113 * access to the declared field
1115 * <li> the caller's class loader is not the same as or an
1116 * ancestor of the class loader for the current class and
1117 * invocation of {@link SecurityManager#checkPackageAccess
1118 * s.checkPackageAccess()} denies access to the package
1125 public Field getDeclaredField(String name)
1126 throws SecurityException {
1127 throw new SecurityException();
1131 * Returns an array containing {@code Constructor} objects reflecting
1132 * all the public constructors of the class represented by this
1133 * {@code Class} object. An array of length 0 is returned if the
1134 * class has no public constructors, or if the class is an array class, or
1135 * if the class reflects a primitive type or void.
1137 * Note that while this method returns an array of {@code
1138 * Constructor<T>} objects (that is an array of constructors from
1139 * this class), the return type of this method is {@code
1140 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1141 * might be expected. This less informative return type is
1142 * necessary since after being returned from this method, the
1143 * array could be modified to hold {@code Constructor} objects for
1144 * different classes, which would violate the type guarantees of
1145 * {@code Constructor<T>[]}.
1147 * @return the array of {@code Constructor} objects representing the
1148 * public constructors of this class
1149 * @exception SecurityException
1150 * If a security manager, <i>s</i>, is present and any of the
1151 * following conditions is met:
1155 * <li> invocation of
1156 * {@link SecurityManager#checkMemberAccess
1157 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1158 * access to the constructors within this class
1160 * <li> the caller's class loader is not the same as or an
1161 * ancestor of the class loader for the current class and
1162 * invocation of {@link SecurityManager#checkPackageAccess
1163 * s.checkPackageAccess()} denies access to the package
1170 public Constructor<?>[] getConstructors() throws SecurityException {
1171 return MethodImpl.findConstructors(this, 0x01);
1175 * Returns a {@code Constructor} object that reflects the specified
1176 * public constructor of the class represented by this {@code Class}
1177 * object. The {@code parameterTypes} parameter is an array of
1178 * {@code Class} objects that identify the constructor's formal
1179 * parameter types, in declared order.
1181 * If this {@code Class} object represents an inner class
1182 * declared in a non-static context, the formal parameter types
1183 * include the explicit enclosing instance as the first parameter.
1185 * <p> The constructor to reflect is the public constructor of the class
1186 * represented by this {@code Class} object whose formal parameter
1187 * types match those specified by {@code parameterTypes}.
1189 * @param parameterTypes the parameter array
1190 * @return the {@code Constructor} object of the public constructor that
1191 * matches the specified {@code parameterTypes}
1192 * @exception NoSuchMethodException if a matching method is not found.
1193 * @exception SecurityException
1194 * If a security manager, <i>s</i>, is present and any of the
1195 * following conditions is met:
1199 * <li> invocation of
1200 * {@link SecurityManager#checkMemberAccess
1201 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1202 * access to the constructor
1204 * <li> the caller's class loader is not the same as or an
1205 * ancestor of the class loader for the current class and
1206 * invocation of {@link SecurityManager#checkPackageAccess
1207 * s.checkPackageAccess()} denies access to the package
1214 public Constructor<T> getConstructor(Class<?>... parameterTypes)
1215 throws NoSuchMethodException, SecurityException {
1216 Constructor c = MethodImpl.findConstructor(this, parameterTypes);
1218 StringBuilder sb = new StringBuilder();
1219 sb.append(getName()).append('(');
1221 for (int i = 0; i < parameterTypes.length; i++) {
1222 sb.append(sep).append(parameterTypes[i].getName());
1226 throw new NoSuchMethodException(sb.toString());
1232 * Returns an array of {@code Constructor} objects reflecting all the
1233 * constructors declared by the class represented by this
1234 * {@code Class} object. These are public, protected, default
1235 * (package) access, and private constructors. The elements in the array
1236 * returned are not sorted and are not in any particular order. If the
1237 * class has a default constructor, it is included in the returned array.
1238 * This method returns an array of length 0 if this {@code Class}
1239 * object represents an interface, a primitive type, an array class, or
1242 * <p> See <em>The Java Language Specification</em>, section 8.2.
1244 * @return the array of {@code Constructor} objects representing all the
1245 * declared constructors of this class
1246 * @exception SecurityException
1247 * If a security manager, <i>s</i>, is present and any of the
1248 * following conditions is met:
1252 * <li> invocation of
1253 * {@link SecurityManager#checkMemberAccess
1254 * s.checkMemberAccess(this, Member.DECLARED)} denies
1255 * access to the declared constructors within this class
1257 * <li> the caller's class loader is not the same as or an
1258 * ancestor of the class loader for the current class and
1259 * invocation of {@link SecurityManager#checkPackageAccess
1260 * s.checkPackageAccess()} denies access to the package
1267 public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
1268 throw new SecurityException();
1271 * Returns a {@code Constructor} object that reflects the specified
1272 * constructor of the class or interface represented by this
1273 * {@code Class} object. The {@code parameterTypes} parameter is
1274 * an array of {@code Class} objects that identify the constructor's
1275 * formal parameter types, in declared order.
1277 * If this {@code Class} object represents an inner class
1278 * declared in a non-static context, the formal parameter types
1279 * include the explicit enclosing instance as the first parameter.
1281 * @param parameterTypes the parameter array
1282 * @return The {@code Constructor} object for the constructor with the
1283 * specified parameter list
1284 * @exception NoSuchMethodException if a matching method is not found.
1285 * @exception SecurityException
1286 * If a security manager, <i>s</i>, is present and any of the
1287 * following conditions is met:
1291 * <li> invocation of
1292 * {@link SecurityManager#checkMemberAccess
1293 * s.checkMemberAccess(this, Member.DECLARED)} denies
1294 * access to the declared constructor
1296 * <li> the caller's class loader is not the same as or an
1297 * ancestor of the class loader for the current class and
1298 * invocation of {@link SecurityManager#checkPackageAccess
1299 * s.checkPackageAccess()} denies access to the package
1306 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
1307 throws NoSuchMethodException, SecurityException {
1308 return getConstructor(parameterTypes);
1313 * Character.isDigit answers {@code true} to some non-ascii
1314 * digits. This one does not.
1316 private static boolean isAsciiDigit(char c) {
1317 return '0' <= c && c <= '9';
1321 * Returns the canonical name of the underlying class as
1322 * defined by the Java Language Specification. Returns null if
1323 * the underlying class does not have a canonical name (i.e., if
1324 * it is a local or anonymous class or an array whose component
1325 * type does not have a canonical name).
1326 * @return the canonical name of the underlying class if it exists, and
1327 * {@code null} otherwise.
1330 public String getCanonicalName() {
1332 String canonicalName = getComponentType().getCanonicalName();
1333 if (canonicalName != null)
1334 return canonicalName + "[]";
1338 // if (isLocalOrAnonymousClass())
1340 // Class<?> enclosingClass = getEnclosingClass();
1341 Class<?> enclosingClass = null;
1342 if (enclosingClass == null) { // top level class
1345 String enclosingName = enclosingClass.getCanonicalName();
1346 if (enclosingName == null)
1348 return enclosingName + "." + getSimpleName();
1353 * Finds a resource with a given name. The rules for searching resources
1354 * associated with a given class are implemented by the defining
1355 * {@linkplain ClassLoader class loader} of the class. This method
1356 * delegates to this object's class loader. If this object was loaded by
1357 * the bootstrap class loader, the method delegates to {@link
1358 * ClassLoader#getSystemResourceAsStream}.
1360 * <p> Before delegation, an absolute resource name is constructed from the
1361 * given resource name using this algorithm:
1365 * <li> If the {@code name} begins with a {@code '/'}
1366 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1367 * portion of the {@code name} following the {@code '/'}.
1369 * <li> Otherwise, the absolute name is of the following form:
1372 * {@code modified_package_name/name}
1375 * <p> Where the {@code modified_package_name} is the package name of this
1376 * object with {@code '/'} substituted for {@code '.'}
1377 * (<tt>'\u002e'</tt>).
1381 * @param name name of the desired resource
1382 * @return A {@link java.io.InputStream} object or {@code null} if
1383 * no resource with this name is found
1384 * @throws NullPointerException If {@code name} is {@code null}
1387 public InputStream getResourceAsStream(String name) {
1388 name = resolveName(name);
1389 byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
1390 return arr == null ? null : new ByteArrayInputStream(arr);
1394 * Finds a resource with a given name. The rules for searching resources
1395 * associated with a given class are implemented by the defining
1396 * {@linkplain ClassLoader class loader} of the class. This method
1397 * delegates to this object's class loader. If this object was loaded by
1398 * the bootstrap class loader, the method delegates to {@link
1399 * ClassLoader#getSystemResource}.
1401 * <p> Before delegation, an absolute resource name is constructed from the
1402 * given resource name using this algorithm:
1406 * <li> If the {@code name} begins with a {@code '/'}
1407 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1408 * portion of the {@code name} following the {@code '/'}.
1410 * <li> Otherwise, the absolute name is of the following form:
1413 * {@code modified_package_name/name}
1416 * <p> Where the {@code modified_package_name} is the package name of this
1417 * object with {@code '/'} substituted for {@code '.'}
1418 * (<tt>'\u002e'</tt>).
1422 * @param name name of the desired resource
1423 * @return A {@link java.net.URL} object or {@code null} if no
1424 * resource with this name is found
1427 public java.net.URL getResource(String name) {
1428 return newResourceURL(name, getResourceAsStream(name));
1431 static URL newResourceURL(String name, InputStream is) {
1432 return is == null ? null : newResourceURL0(URL.class, "res:/" + name, is);
1435 @JavaScriptBody(args = { "url", "spec", "is" }, body =
1436 "var u = url.cnstr(true);\n"
1437 + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n"
1440 private static native URL newResourceURL0(Class<URL> url, String spec, InputStream is);
1443 * Add a package name prefix if the name is not absolute Remove leading "/"
1444 * if name is absolute
1446 private String resolveName(String name) {
1450 if (!name.startsWith("/")) {
1452 while (c.isArray()) {
1453 c = c.getComponentType();
1455 String baseName = c.getName();
1456 int index = baseName.lastIndexOf('.');
1458 name = baseName.substring(0, index).replace('.', '/')
1462 name = name.substring(1);
1468 * Returns the class loader for the class. Some implementations may use
1469 * null to represent the bootstrap class loader. This method will return
1470 * null in such implementations if this class was loaded by the bootstrap
1473 * <p> If a security manager is present, and the caller's class loader is
1474 * not null and the caller's class loader is not the same as or an ancestor of
1475 * the class loader for the class whose class loader is requested, then
1476 * this method calls the security manager's {@code checkPermission}
1477 * method with a {@code RuntimePermission("getClassLoader")}
1478 * permission to ensure it's ok to access the class loader for the class.
1481 * represents a primitive type or void, null is returned.
1483 * @return the class loader that loaded the class or interface
1484 * represented by this object.
1485 * @throws SecurityException
1486 * if a security manager exists and its
1487 * {@code checkPermission} method denies
1488 * access to the class loader for the class.
1489 * @see java.lang.ClassLoader
1490 * @see SecurityManager#checkPermission
1491 * @see java.lang.RuntimePermission
1493 public ClassLoader getClassLoader() {
1494 return ClassLoader.getSystemClassLoader();
1498 * Determines the interfaces implemented by the class or interface
1499 * represented by this object.
1501 * <p> If this object represents a class, the return value is an array
1502 * containing objects representing all interfaces implemented by the
1503 * class. The order of the interface objects in the array corresponds to
1504 * the order of the interface names in the {@code implements} clause
1505 * of the declaration of the class represented by this object. For
1506 * example, given the declaration:
1508 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1510 * suppose the value of {@code s} is an instance of
1511 * {@code Shimmer}; the value of the expression:
1513 * {@code s.getClass().getInterfaces()[0]}
1515 * is the {@code Class} object that represents interface
1516 * {@code FloorWax}; and the value of:
1518 * {@code s.getClass().getInterfaces()[1]}
1520 * is the {@code Class} object that represents interface
1521 * {@code DessertTopping}.
1523 * <p> If this object represents an interface, the array contains objects
1524 * representing all interfaces extended by the interface. The order of the
1525 * interface objects in the array corresponds to the order of the interface
1526 * names in the {@code extends} clause of the declaration of the
1527 * interface represented by this object.
1529 * <p> If this object represents a class or interface that implements no
1530 * interfaces, the method returns an array of length 0.
1532 * <p> If this object represents a primitive type or void, the method
1533 * returns an array of length 0.
1535 * @return an array of interfaces implemented by this class.
1537 public native Class<?>[] getInterfaces();
1540 * Returns the {@code Class} representing the component type of an
1541 * array. If this class does not represent an array class this method
1544 * @return the {@code Class} representing the component type of this
1545 * class if this class is an array
1546 * @see java.lang.reflect.Array
1549 public Class<?> getComponentType() {
1552 Class<?> c = getComponentTypeByFnc();
1553 return c != null ? c : getComponentType0();
1554 } catch (ClassNotFoundException cnfe) {
1555 throw new IllegalStateException(cnfe);
1561 @JavaScriptBody(args = { }, body =
1563 + " var c = this.fnc(false).constructor.$class;\n"
1564 // + " java.lang.System.out.println('will call: ' + (!!this.fnc) + ' res: ' + c.jvmName);\n"
1565 + " if (c) return c;\n"
1569 private native Class<?> getComponentTypeByFnc();
1571 private Class<?> getComponentType0() throws ClassNotFoundException {
1572 String n = getName().substring(1);
1573 switch (n.charAt(0)) {
1575 n = n.substring(1, n.length() - 1);
1576 return Class.forName(n);
1578 return Integer.TYPE;
1588 return Boolean.TYPE;
1594 return Character.TYPE;
1596 return defineArray(n, null);
1598 throw new ClassNotFoundException("Unknown component type of " + getName());
1602 @JavaScriptBody(args = { "sig", "fnc" }, body =
1603 "if (!sig) sig = '[Ljava/lang/Object;';\n" +
1604 "var c = Array[sig];\n" +
1606 " c = vm.java_lang_Class(true);\n" +
1607 " c.jvmName = sig;\n" +
1608 " c.superclass = vm.java_lang_Object(false).$class;\n" +
1609 " c.array = true;\n" +
1610 " Array[sig] = c;\n" +
1612 "if (!c.fnc) c.fnc = fnc;\n" +
1615 private static native Class<?> defineArray(String sig, Object fnc);
1618 * Returns true if and only if this class was declared as an enum in the
1621 * @return true if and only if this class was declared as an enum in the
1625 public boolean isEnum() {
1626 // An enum must both directly extend java.lang.Enum and have
1627 // the ENUM bit set; classes for specialized enum constants
1628 // don't do the former.
1629 return (this.getModifiers() & ENUM) != 0 &&
1630 this.getSuperclass() == java.lang.Enum.class;
1634 * Casts an object to the class or interface represented
1635 * by this {@code Class} object.
1637 * @param obj the object to be cast
1638 * @return the object after casting, or null if obj is null
1640 * @throws ClassCastException if the object is not
1641 * null and is not assignable to the type T.
1645 public T cast(Object obj) {
1646 if (obj != null && !isInstance(obj))
1647 throw new ClassCastException(cannotCastMsg(obj));
1651 private String cannotCastMsg(Object obj) {
1652 return "Cannot cast " + obj.getClass().getName() + " to " + getName();
1656 * Casts this {@code Class} object to represent a subclass of the class
1657 * represented by the specified class object. Checks that that the cast
1658 * is valid, and throws a {@code ClassCastException} if it is not. If
1659 * this method succeeds, it always returns a reference to this class object.
1661 * <p>This method is useful when a client needs to "narrow" the type of
1662 * a {@code Class} object to pass it to an API that restricts the
1663 * {@code Class} objects that it is willing to accept. A cast would
1664 * generate a compile-time warning, as the correctness of the cast
1665 * could not be checked at runtime (because generic types are implemented
1668 * @return this {@code Class} object, cast to represent a subclass of
1669 * the specified class object.
1670 * @throws ClassCastException if this {@code Class} object does not
1671 * represent a subclass of the specified class (here "subclass" includes
1672 * the class itself).
1675 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
1676 if (clazz.isAssignableFrom(this))
1677 return (Class<? extends U>) this;
1679 throw new ClassCastException(this.toString());
1682 @JavaScriptBody(args = { "ac" },
1684 "if (this.anno) {\n"
1685 + " var r = this.anno['L' + ac.jvmName + ';'];\n"
1686 + " if (typeof r === 'undefined') r = null;\n"
1688 + "} else return null;\n"
1690 private Object getAnnotationData(Class<?> annotationClass) {
1691 throw new UnsupportedOperationException();
1694 * @throws NullPointerException {@inheritDoc}
1697 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
1698 Object data = getAnnotationData(annotationClass);
1699 return data == null ? null : AnnotationImpl.create(annotationClass, data);
1703 * @throws NullPointerException {@inheritDoc}
1706 @JavaScriptBody(args = { "ac" },
1707 body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
1708 + "else return false;"
1710 public boolean isAnnotationPresent(
1711 Class<? extends Annotation> annotationClass) {
1712 if (annotationClass == null)
1713 throw new NullPointerException();
1715 return getAnnotation(annotationClass) != null;
1718 @JavaScriptBody(args = {}, body = "return this.anno;")
1719 private Object getAnnotationData() {
1720 throw new UnsupportedOperationException();
1726 public Annotation[] getAnnotations() {
1727 Object data = getAnnotationData();
1728 return data == null ? new Annotation[0] : AnnotationImpl.create(data);
1734 public Annotation[] getDeclaredAnnotations() {
1735 throw new UnsupportedOperationException();
1738 @JavaScriptBody(args = "type", body = ""
1739 + "var c = vm.java_lang_Class(true);"
1740 + "c.jvmName = type;"
1741 + "c.primitive = true;"
1744 native static Class getPrimitiveClass(String type);
1746 @JavaScriptBody(args = {}, body =
1747 "return this['desiredAssertionStatus'] ? this['desiredAssertionStatus'] : false;"
1749 public native boolean desiredAssertionStatus();
1751 public boolean equals(Object obj) {
1752 if (isPrimitive() && obj instanceof Class) {
1753 Class c = ((Class)obj);
1754 return c.isPrimitive() && getName().equals(c.getName());
1756 return super.equals(obj);
1759 static void registerNatives() {
1760 boolean assertsOn = false;
1761 // assert assertsOn = true;
1765 } catch (Throwable ex) {
1771 @JavaScriptBody(args = {}, body = "var p = vm.java_lang_Object(false);"
1772 + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
1774 static native void registerToString();
1776 @JavaScriptBody(args = {"self"}, body
1777 = "var c = self.constructor.$class;\n"
1778 + "return c ? c : null;\n"
1780 static native Class<?> classFor(Object self);
1783 @JavaScriptBody(args = { "self" }, body
1784 = "if (self['$hashCode']) return self['$hashCode'];\n"
1785 + "var h = self['computeHashCode__I'] ? self['computeHashCode__I']() : Math.random() * Math.pow(2, 31);\n"
1786 + "return self['$hashCode'] = h & h;"
1788 static native int defaultHashCode(Object self);
1790 @JavaScriptBody(args = "self", body
1791 = "\nif (!self['$instOf_java_lang_Cloneable']) {"
1794 + "\n var clone = self.constructor(true);"
1795 + "\n var props = Object.getOwnPropertyNames(self);"
1796 + "\n for (var i = 0; i < props.length; i++) {"
1797 + "\n var p = props[i];"
1798 + "\n clone[p] = self[p];"
1800 + "\n return clone;"
1803 static native Object clone(Object self) throws CloneNotSupportedException;
1805 @JavaScriptOnly(name = "toJS", value = "function(v) {\n"
1806 + " if (v === null) return null;\n"
1807 + " if (Object.prototype.toString.call(v) === '[object Array]') {\n"
1808 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).convArray__Ljava_lang_Object_2Ljava_lang_Object_2(v);\n"
1810 + " return v.valueOf();\n"
1813 static native int toJS();
1816 @JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_apidesign_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
1817 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
1820 static native int activate();
1822 private static Object bck2BrwsrCnvrt(Object o) {
1823 if (o instanceof Throwable) {
1826 final String msg = msg(o);
1827 if (msg == null || msg.startsWith("TypeError")) {
1828 return new NullPointerException(msg);
1830 return new Throwable(msg);
1833 @JavaScriptBody(args = {"o"}, body = "return o ? o.toString() : null;")
1834 private static native String msg(Object o);
1837 @JavaScriptOnly(name = "bck2BrwsrThrwrbl", value = "c.bck2BrwsrCnvrt__Ljava_lang_Object_2Ljava_lang_Object_2")
1838 private static void bck2BrwsrCnvrtVM() {