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.JavaScriptBody;
38 import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
39 import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
42 * Instances of the class {@code Class} represent classes and
43 * interfaces in a running Java application. An enum is a kind of
44 * class and an annotation is a kind of interface. Every array also
45 * belongs to a class that is reflected as a {@code Class} object
46 * that is shared by all arrays with the same element type and number
47 * of dimensions. The primitive Java types ({@code boolean},
48 * {@code byte}, {@code char}, {@code short},
49 * {@code int}, {@code long}, {@code float}, and
50 * {@code double}), and the keyword {@code void} are also
51 * represented as {@code Class} objects.
53 * <p> {@code Class} has no public constructor. Instead {@code Class}
54 * objects are constructed automatically by the Java Virtual Machine as classes
55 * are loaded and by calls to the {@code defineClass} method in the class
58 * <p> The following example uses a {@code Class} object to print the
59 * class name of an object:
61 * <p> <blockquote><pre>
62 * void printClassName(Object obj) {
63 * System.out.println("The class of " + obj +
64 * " is " + obj.getClass().getName());
68 * <p> It is also possible to get the {@code Class} object for a named
69 * type (or for void) using a class literal. See Section 15.8.2 of
70 * <cite>The Java™ Language Specification</cite>.
74 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
77 * @param <T> the type of the class modeled by this {@code Class}
78 * object. For example, the type of {@code String.class} is {@code
79 * Class<String>}. Use {@code Class<?>} if the class being modeled is
83 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
87 class Class<T> implements java.io.Serializable,
88 java.lang.reflect.GenericDeclaration,
89 java.lang.reflect.Type,
90 java.lang.reflect.AnnotatedElement {
91 private static final int ANNOTATION= 0x00002000;
92 private static final int ENUM = 0x00004000;
93 private static final int SYNTHETIC = 0x00001000;
96 * Constructor. Only the Java Virtual Machine creates Class
103 * Converts the object to a string. The string representation is the
104 * string "class" or "interface", followed by a space, and then by the
105 * fully qualified name of the class in the format returned by
106 * {@code getName}. If this {@code Class} object represents a
107 * primitive type, this method returns the name of the primitive type. If
108 * this {@code Class} object represents void this method returns
111 * @return a string representation of this class object.
113 public String toString() {
114 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
120 * Returns the {@code Class} object associated with the class or
121 * interface with the given string name. Invoking this method is
125 * {@code Class.forName(className, true, currentLoader)}
128 * where {@code currentLoader} denotes the defining class loader of
131 * <p> For example, the following code fragment returns the
132 * runtime {@code Class} descriptor for the class named
133 * {@code java.lang.Thread}:
136 * {@code Class t = Class.forName("java.lang.Thread")}
139 * A call to {@code forName("X")} causes the class named
140 * {@code X} to be initialized.
142 * @param className the fully qualified name of the desired class.
143 * @return the {@code Class} object for the class with the
145 * @exception LinkageError if the linkage fails
146 * @exception ExceptionInInitializerError if the initialization provoked
147 * by this method fails
148 * @exception ClassNotFoundException if the class cannot be located
150 public static Class<?> forName(String className)
151 throws ClassNotFoundException {
152 if (className.startsWith("[")) {
153 Class<?> arrType = defineArray(className);
154 Class<?> c = arrType;
155 while (c != null && c.isArray()) {
156 c = c.getComponentType0(); // verify component type is sane
161 Class<?> c = loadCls(className, className.replace('.', '_'));
163 throw new ClassNotFoundException(className);
166 } catch (Throwable ex) {
167 throw new ClassNotFoundException(className, ex);
173 * Returns the {@code Class} object associated with the class or
174 * interface with the given string name, using the given class loader.
175 * Given the fully qualified name for a class or interface (in the same
176 * format returned by {@code getName}) this method attempts to
177 * locate, load, and link the class or interface. The specified class
178 * loader is used to load the class or interface. If the parameter
179 * {@code loader} is null, the class is loaded through the bootstrap
180 * class loader. The class is initialized only if the
181 * {@code initialize} parameter is {@code true} and if it has
182 * not been initialized earlier.
184 * <p> If {@code name} denotes a primitive type or void, an attempt
185 * will be made to locate a user-defined class in the unnamed package whose
186 * name is {@code name}. Therefore, this method cannot be used to
187 * obtain any of the {@code Class} objects representing primitive
190 * <p> If {@code name} denotes an array class, the component type of
191 * the array class is loaded but not initialized.
193 * <p> For example, in an instance method the expression:
196 * {@code Class.forName("Foo")}
202 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
205 * Note that this method throws errors related to loading, linking or
206 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
207 * Java Language Specification</em>.
208 * Note that this method does not check whether the requested class
209 * is accessible to its caller.
211 * <p> If the {@code loader} is {@code null}, and a security
212 * manager is present, and the caller's class loader is not null, then this
213 * method calls the security manager's {@code checkPermission} method
214 * with a {@code RuntimePermission("getClassLoader")} permission to
215 * ensure it's ok to access the bootstrap class loader.
217 * @param name fully qualified name of the desired class
218 * @param initialize whether the class must be initialized
219 * @param loader class loader from which the class must be loaded
220 * @return class object representing the desired class
222 * @exception LinkageError if the linkage fails
223 * @exception ExceptionInInitializerError if the initialization provoked
224 * by this method fails
225 * @exception ClassNotFoundException if the class cannot be located by
226 * the specified class loader
228 * @see java.lang.Class#forName(String)
229 * @see java.lang.ClassLoader
232 public static Class<?> forName(String name, boolean initialize,
234 throws ClassNotFoundException
236 return forName(name);
239 @JavaScriptBody(args = {"n", "c" }, body =
241 + " if (vm.loadClass) {\n"
242 + " vm.loadClass(n);\n"
244 + " if (!vm[c]) return null;\n"
247 + "return vm[c].$class;"
249 private static native Class<?> loadCls(String n, String c);
253 * Creates a new instance of the class represented by this {@code Class}
254 * object. The class is instantiated as if by a {@code new}
255 * expression with an empty argument list. The class is initialized if it
256 * has not already been initialized.
258 * <p>Note that this method propagates any exception thrown by the
259 * nullary constructor, including a checked exception. Use of
260 * this method effectively bypasses the compile-time exception
261 * checking that would otherwise be performed by the compiler.
263 * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
264 * Constructor.newInstance} method avoids this problem by wrapping
265 * any exception thrown by the constructor in a (checked) {@link
266 * java.lang.reflect.InvocationTargetException}.
268 * @return a newly allocated instance of the class represented by this
270 * @exception IllegalAccessException if the class or its nullary
271 * constructor is not accessible.
272 * @exception InstantiationException
273 * if this {@code Class} represents an abstract class,
274 * an interface, an array class, a primitive type, or void;
275 * or if the class has no nullary constructor;
276 * or if the instantiation fails for some other reason.
277 * @exception ExceptionInInitializerError if the initialization
278 * provoked by this method fails.
279 * @exception SecurityException
280 * If a security manager, <i>s</i>, is present and any of the
281 * following conditions is met:
286 * {@link SecurityManager#checkMemberAccess
287 * s.checkMemberAccess(this, Member.PUBLIC)} denies
288 * creation of new instances of this class
290 * <li> the caller's class loader is not the same as or an
291 * ancestor of the class loader for the current class and
292 * invocation of {@link SecurityManager#checkPackageAccess
293 * s.checkPackageAccess()} denies access to the package
299 @JavaScriptBody(args = { "self", "illegal" }, body =
300 "\nvar c = self.cnstr;"
301 + "\nif (c['cons__V']) {"
302 + "\n if ((c.cons__V.access & 0x1) != 0) {"
303 + "\n var inst = c();"
304 + "\n c.cons__V.call(inst);"
307 + "\n return illegal;"
311 private static native Object newInstance0(Class<?> self, Object illegal);
313 public T newInstance()
314 throws InstantiationException, IllegalAccessException
316 Object illegal = new Object();
317 Object inst = newInstance0(this, illegal);
319 throw new InstantiationException(getName());
321 if (inst == illegal) {
322 throw new IllegalAccessException();
328 * Determines if the specified {@code Object} is assignment-compatible
329 * with the object represented by this {@code Class}. This method is
330 * the dynamic equivalent of the Java language {@code instanceof}
331 * operator. The method returns {@code true} if the specified
332 * {@code Object} argument is non-null and can be cast to the
333 * reference type represented by this {@code Class} object without
334 * raising a {@code ClassCastException.} It returns {@code false}
337 * <p> Specifically, if this {@code Class} object represents a
338 * declared class, this method returns {@code true} if the specified
339 * {@code Object} argument is an instance of the represented class (or
340 * of any of its subclasses); it returns {@code false} otherwise. If
341 * this {@code Class} object represents an array class, this method
342 * returns {@code true} if the specified {@code Object} argument
343 * can be converted to an object of the array class by an identity
344 * conversion or by a widening reference conversion; it returns
345 * {@code false} otherwise. If this {@code Class} object
346 * represents an interface, this method returns {@code true} if the
347 * class or any superclass of the specified {@code Object} argument
348 * implements this interface; it returns {@code false} otherwise. If
349 * this {@code Class} object represents a primitive type, this method
350 * returns {@code false}.
352 * @param obj the object to check
353 * @return true if {@code obj} is an instance of this class
357 public boolean isInstance(Object obj) {
362 return isAssignableFrom(obj.getClass());
365 String prop = "$instOf_" + getName().replace('.', '_');
366 return hasProperty(obj, prop);
369 @JavaScriptBody(args = { "who", "prop" }, body =
370 "if (who[prop]) return true; else return false;"
372 private static native boolean hasProperty(Object who, String prop);
376 * Determines if the class or interface represented by this
377 * {@code Class} object is either the same as, or is a superclass or
378 * superinterface of, the class or interface represented by the specified
379 * {@code Class} parameter. It returns {@code true} if so;
380 * otherwise it returns {@code false}. If this {@code Class}
381 * object represents a primitive type, this method returns
382 * {@code true} if the specified {@code Class} parameter is
383 * exactly this {@code Class} object; otherwise it returns
386 * <p> Specifically, this method tests whether the type represented by the
387 * specified {@code Class} parameter can be converted to the type
388 * represented by this {@code Class} object via an identity conversion
389 * or via a widening reference conversion. See <em>The Java Language
390 * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
392 * @param cls the {@code Class} object to be checked
393 * @return the {@code boolean} value indicating whether objects of the
394 * type {@code cls} can be assigned to objects of this class
395 * @exception NullPointerException if the specified Class parameter is
399 public boolean isAssignableFrom(Class<?> cls) {
405 final Class<?> cmpType = cls.getComponentType();
407 return this == cmpType;
409 return cmpType != null && getComponentType().isAssignableFrom(cmpType);
414 if (cls.isPrimitive()) {
417 String prop = "$instOf_" + getName().replace('.', '_');
418 return hasCnstrProperty(cls, prop);
422 @JavaScriptBody(args = { "who", "prop" }, body =
423 "if (who.cnstr.prototype[prop]) return true; else return false;"
425 private static native boolean hasCnstrProperty(Object who, String prop);
429 * Determines if the specified {@code Class} object represents an
432 * @return {@code true} if this object represents an interface;
433 * {@code false} otherwise.
435 public boolean isInterface() {
436 return (getAccess() & 0x200) != 0;
439 @JavaScriptBody(args = {}, body = "return this.access;")
440 private native int getAccess();
444 * Determines if this {@code Class} object represents an array class.
446 * @return {@code true} if this object represents an array class;
447 * {@code false} otherwise.
450 public boolean isArray() {
451 return hasProperty(this, "array"); // NOI18N
456 * Determines if the specified {@code Class} object represents a
459 * <p> There are nine predefined {@code Class} objects to represent
460 * the eight primitive types and void. These are created by the Java
461 * Virtual Machine, and have the same names as the primitive types that
462 * they represent, namely {@code boolean}, {@code byte},
463 * {@code char}, {@code short}, {@code int},
464 * {@code long}, {@code float}, and {@code double}.
466 * <p> These objects may only be accessed via the following public static
467 * final variables, and are the only {@code Class} objects for which
468 * this method returns {@code true}.
470 * @return true if and only if this class represents a primitive type
472 * @see java.lang.Boolean#TYPE
473 * @see java.lang.Character#TYPE
474 * @see java.lang.Byte#TYPE
475 * @see java.lang.Short#TYPE
476 * @see java.lang.Integer#TYPE
477 * @see java.lang.Long#TYPE
478 * @see java.lang.Float#TYPE
479 * @see java.lang.Double#TYPE
480 * @see java.lang.Void#TYPE
483 @JavaScriptBody(args = {}, body =
484 "if (this.primitive) return true;"
485 + "else return false;"
487 public native boolean isPrimitive();
490 * Returns true if this {@code Class} object represents an annotation
491 * type. Note that if this method returns true, {@link #isInterface()}
492 * would also return true, as all annotation types are also interfaces.
494 * @return {@code true} if this class object represents an annotation
495 * type; {@code false} otherwise
498 public boolean isAnnotation() {
499 return (getModifiers() & ANNOTATION) != 0;
503 * Returns {@code true} if this class is a synthetic class;
504 * returns {@code false} otherwise.
505 * @return {@code true} if and only if this class is a synthetic class as
506 * defined by the Java Language Specification.
509 public boolean isSynthetic() {
510 return (getModifiers() & SYNTHETIC) != 0;
514 * Returns the name of the entity (class, interface, array class,
515 * primitive type, or void) represented by this {@code Class} object,
516 * as a {@code String}.
518 * <p> If this class object represents a reference type that is not an
519 * array type then the binary name of the class is returned, as specified
521 * <cite>The Java™ Language Specification</cite>.
523 * <p> If this class object represents a primitive type or void, then the
524 * name returned is a {@code String} equal to the Java language
525 * keyword corresponding to the primitive type or void.
527 * <p> If this class object represents a class of arrays, then the internal
528 * form of the name consists of the name of the element type preceded by
529 * one or more '{@code [}' characters representing the depth of the array
530 * nesting. The encoding of element type names is as follows:
532 * <blockquote><table summary="Element types and encodings">
533 * <tr><th> Element Type <th> <th> Encoding
534 * <tr><td> boolean <td> <td align=center> Z
535 * <tr><td> byte <td> <td align=center> B
536 * <tr><td> char <td> <td align=center> C
537 * <tr><td> class or interface
538 * <td> <td align=center> L<i>classname</i>;
539 * <tr><td> double <td> <td align=center> D
540 * <tr><td> float <td> <td align=center> F
541 * <tr><td> int <td> <td align=center> I
542 * <tr><td> long <td> <td align=center> J
543 * <tr><td> short <td> <td align=center> S
544 * </table></blockquote>
546 * <p> The class or interface name <i>classname</i> is the binary name of
547 * the class specified above.
551 * String.class.getName()
552 * returns "java.lang.String"
553 * byte.class.getName()
555 * (new Object[3]).getClass().getName()
556 * returns "[Ljava.lang.Object;"
557 * (new int[3][4][5][6][7][8][9]).getClass().getName()
559 * </pre></blockquote>
561 * @return the name of the class or interface
562 * represented by this object.
564 public String getName() {
565 return jvmName().replace('/', '.');
568 @JavaScriptBody(args = {}, body = "return this.jvmName;")
569 private native String jvmName();
573 * Returns an array of {@code TypeVariable} objects that represent the
574 * type variables declared by the generic declaration represented by this
575 * {@code GenericDeclaration} object, in declaration order. Returns an
576 * array of length 0 if the underlying generic declaration declares no type
579 * @return an array of {@code TypeVariable} objects that represent
580 * the type variables declared by this generic declaration
581 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
582 * signature of this generic declaration does not conform to
583 * the format specified in
584 * <cite>The Java™ Virtual Machine Specification</cite>
587 public TypeVariable<Class<T>>[] getTypeParameters() {
588 throw new UnsupportedOperationException();
592 * Returns the {@code Class} representing the superclass of the entity
593 * (class, interface, primitive type or void) represented by this
594 * {@code Class}. If this {@code Class} represents either the
595 * {@code Object} class, an interface, a primitive type, or void, then
596 * null is returned. If this object represents an array class then the
597 * {@code Class} object representing the {@code Object} class is
600 * @return the superclass of the class represented by this object.
602 @JavaScriptBody(args = {}, body = "return this.superclass;")
603 public native Class<? super T> getSuperclass();
606 * Returns the Java language modifiers for this class or interface, encoded
607 * in an integer. The modifiers consist of the Java Virtual Machine's
608 * constants for {@code public}, {@code protected},
609 * {@code private}, {@code final}, {@code static},
610 * {@code abstract} and {@code interface}; they should be decoded
611 * using the methods of class {@code Modifier}.
613 * <p> If the underlying class is an array class, then its
614 * {@code public}, {@code private} and {@code protected}
615 * modifiers are the same as those of its component type. If this
616 * {@code Class} represents a primitive type or void, its
617 * {@code public} modifier is always {@code true}, and its
618 * {@code protected} and {@code private} modifiers are always
619 * {@code false}. If this object represents an array class, a
620 * primitive type or void, then its {@code final} modifier is always
621 * {@code true} and its interface modifier is always
622 * {@code false}. The values of its other modifiers are not determined
623 * by this specification.
625 * <p> The modifier encodings are defined in <em>The Java Virtual Machine
626 * Specification</em>, table 4.1.
628 * @return the {@code int} representing the modifiers for this class
629 * @see java.lang.reflect.Modifier
632 public int getModifiers() {
637 * If the class or interface represented by this {@code Class} object
638 * is a member of another class, returns the {@code Class} object
639 * representing the class in which it was declared. This method returns
640 * null if this class or interface is not a member of any other class. If
641 * this {@code Class} object represents an array class, a primitive
642 * type, or void,then this method returns null.
644 * @return the declaring class for this class
647 public Class<?> getDeclaringClass() {
648 throw new SecurityException();
652 * Returns the simple name of the underlying class as given in the
653 * source code. Returns an empty string if the underlying class is
656 * <p>The simple name of an array is the simple name of the
657 * component type with "[]" appended. In particular the simple
658 * name of an array whose component type is anonymous is "[]".
660 * @return the simple name of the underlying class
663 public String getSimpleName() {
665 return getComponentType().getSimpleName()+"[]";
667 String simpleName = getSimpleBinaryName();
668 if (simpleName == null) { // top level class
669 simpleName = getName();
670 return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
672 // According to JLS3 "Binary Compatibility" (13.1) the binary
673 // name of non-package classes (not top level) is the binary
674 // name of the immediately enclosing class followed by a '$' followed by:
675 // (for nested and inner classes): the simple name.
676 // (for local classes): 1 or more digits followed by the simple name.
677 // (for anonymous classes): 1 or more digits.
679 // Since getSimpleBinaryName() will strip the binary name of
680 // the immediatly enclosing class, we are now looking at a
681 // string that matches the regular expression "\$[0-9]*"
682 // followed by a simple name (considering the simple of an
683 // anonymous class to be the empty string).
685 // Remove leading "\$[0-9]*" from the name
686 int length = simpleName.length();
687 if (length < 1 || simpleName.charAt(0) != '$')
688 throw new IllegalStateException("Malformed class name");
690 while (index < length && isAsciiDigit(simpleName.charAt(index)))
692 // Eventually, this is the empty string iff this is an anonymous class
693 return simpleName.substring(index);
697 * Returns the "simple binary name" of the underlying class, i.e.,
698 * the binary name without the leading enclosing class name.
699 * Returns {@code null} if the underlying class is a top level
702 private String getSimpleBinaryName() {
703 Class<?> enclosingClass = null; // XXX getEnclosingClass();
704 if (enclosingClass == null) // top level class
706 // Otherwise, strip the enclosing class' name
708 return getName().substring(enclosingClass.getName().length());
709 } catch (IndexOutOfBoundsException ex) {
710 throw new IllegalStateException("Malformed class name");
715 * Returns an array containing {@code Field} objects reflecting all
716 * the accessible public fields of the class or interface represented by
717 * this {@code Class} object. The elements in the array returned are
718 * not sorted and are not in any particular order. This method returns an
719 * array of length 0 if the class or interface has no accessible public
720 * fields, or if it represents an array class, a primitive type, or void.
722 * <p> Specifically, if this {@code Class} object represents a class,
723 * this method returns the public fields of this class and of all its
724 * superclasses. If this {@code Class} object represents an
725 * interface, this method returns the fields of this interface and of all
726 * its superinterfaces.
728 * <p> The implicit length field for array class is not reflected by this
729 * method. User code should use the methods of class {@code Array} to
732 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
734 * @return the array of {@code Field} objects representing the
736 * @exception SecurityException
737 * If a security manager, <i>s</i>, is present and any of the
738 * following conditions is met:
743 * {@link SecurityManager#checkMemberAccess
744 * s.checkMemberAccess(this, Member.PUBLIC)} denies
745 * access to the fields within this class
747 * <li> the caller's class loader is not the same as or an
748 * ancestor of the class loader for the current class and
749 * invocation of {@link SecurityManager#checkPackageAccess
750 * s.checkPackageAccess()} denies access to the package
757 public Field[] getFields() throws SecurityException {
758 throw new SecurityException();
762 * Returns an array containing {@code Method} objects reflecting all
763 * the public <em>member</em> methods of the class or interface represented
764 * by this {@code Class} object, including those declared by the class
765 * or interface and those inherited from superclasses and
766 * superinterfaces. Array classes return all the (public) member methods
767 * inherited from the {@code Object} class. The elements in the array
768 * returned are not sorted and are not in any particular order. This
769 * method returns an array of length 0 if this {@code Class} object
770 * represents a class or interface that has no public member methods, or if
771 * this {@code Class} object represents a primitive type or void.
773 * <p> The class initialization method {@code <clinit>} is not
774 * included in the returned array. If the class declares multiple public
775 * member methods with the same parameter types, they are all included in
776 * the returned array.
778 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
780 * @return the array of {@code Method} objects representing the
781 * public methods of this class
782 * @exception SecurityException
783 * If a security manager, <i>s</i>, is present and any of the
784 * following conditions is met:
789 * {@link SecurityManager#checkMemberAccess
790 * s.checkMemberAccess(this, Member.PUBLIC)} denies
791 * access to the methods within this class
793 * <li> the caller's class loader is not the same as or an
794 * ancestor of the class loader for the current class and
795 * invocation of {@link SecurityManager#checkPackageAccess
796 * s.checkPackageAccess()} denies access to the package
803 public Method[] getMethods() throws SecurityException {
804 return MethodImpl.findMethods(this, 0x01);
808 * Returns a {@code Field} object that reflects the specified public
809 * member field of the class or interface represented by this
810 * {@code Class} object. The {@code name} parameter is a
811 * {@code String} specifying the simple name of the desired field.
813 * <p> The field to be reflected is determined by the algorithm that
814 * follows. Let C be the class represented by this object:
816 * <LI> If C declares a public field with the name specified, that is the
817 * field to be reflected.</LI>
818 * <LI> If no field was found in step 1 above, this algorithm is applied
819 * recursively to each direct superinterface of C. The direct
820 * superinterfaces are searched in the order they were declared.</LI>
821 * <LI> If no field was found in steps 1 and 2 above, and C has a
822 * superclass S, then this algorithm is invoked recursively upon S.
823 * If C has no superclass, then a {@code NoSuchFieldException}
827 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
829 * @param name the field name
830 * @return the {@code Field} object of this class specified by
832 * @exception NoSuchFieldException if a field with the specified name is
834 * @exception NullPointerException if {@code name} is {@code null}
835 * @exception SecurityException
836 * If a security manager, <i>s</i>, is present and any of the
837 * following conditions is met:
842 * {@link SecurityManager#checkMemberAccess
843 * s.checkMemberAccess(this, Member.PUBLIC)} denies
844 * access to the field
846 * <li> the caller's class loader is not the same as or an
847 * ancestor of the class loader for the current class and
848 * invocation of {@link SecurityManager#checkPackageAccess
849 * s.checkPackageAccess()} denies access to the package
856 public Field getField(String name)
857 throws SecurityException {
858 throw new SecurityException();
863 * Returns a {@code Method} object that reflects the specified public
864 * member method of the class or interface represented by this
865 * {@code Class} object. The {@code name} parameter is a
866 * {@code String} specifying the simple name of the desired method. The
867 * {@code parameterTypes} parameter is an array of {@code Class}
868 * objects that identify the method's formal parameter types, in declared
869 * order. If {@code parameterTypes} is {@code null}, it is
870 * treated as if it were an empty array.
872 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
873 * {@code NoSuchMethodException} is raised. Otherwise, the method to
874 * be reflected is determined by the algorithm that follows. Let C be the
875 * class represented by this object:
877 * <LI> C is searched for any <I>matching methods</I>. If no matching
878 * method is found, the algorithm of step 1 is invoked recursively on
879 * the superclass of C.</LI>
880 * <LI> If no method was found in step 1 above, the superinterfaces of C
881 * are searched for a matching method. If any such method is found, it
885 * To find a matching method in a class C: If C declares exactly one
886 * public method with the specified name and exactly the same formal
887 * parameter types, that is the method reflected. If more than one such
888 * method is found in C, and one of these methods has a return type that is
889 * more specific than any of the others, that method is reflected;
890 * otherwise one of the methods is chosen arbitrarily.
892 * <p>Note that there may be more than one matching method in a
893 * class because while the Java language forbids a class to
894 * declare multiple methods with the same signature but different
895 * return types, the Java virtual machine does not. This
896 * increased flexibility in the virtual machine can be used to
897 * implement various language features. For example, covariant
898 * returns can be implemented with {@linkplain
899 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
900 * method and the method being overridden would have the same
901 * signature but different return types.
903 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
905 * @param name the name of the method
906 * @param parameterTypes the list of parameters
907 * @return the {@code Method} object that matches the specified
908 * {@code name} and {@code parameterTypes}
909 * @exception NoSuchMethodException if a matching method is not found
910 * or if the name is "<init>"or "<clinit>".
911 * @exception NullPointerException if {@code name} is {@code null}
912 * @exception SecurityException
913 * If a security manager, <i>s</i>, is present and any of the
914 * following conditions is met:
919 * {@link SecurityManager#checkMemberAccess
920 * s.checkMemberAccess(this, Member.PUBLIC)} denies
921 * access to the method
923 * <li> the caller's class loader is not the same as or an
924 * ancestor of the class loader for the current class and
925 * invocation of {@link SecurityManager#checkPackageAccess
926 * s.checkPackageAccess()} denies access to the package
933 public Method getMethod(String name, Class<?>... parameterTypes)
934 throws SecurityException, NoSuchMethodException {
935 Method m = MethodImpl.findMethod(this, name, parameterTypes);
937 StringBuilder sb = new StringBuilder();
938 sb.append(getName()).append('.').append(name).append('(');
940 for (int i = 0; i < parameterTypes.length; i++) {
941 sb.append(sep).append(parameterTypes[i].getName());
945 throw new NoSuchMethodException(sb.toString());
951 * Returns an array of {@code Method} objects reflecting all the
952 * methods declared by the class or interface represented by this
953 * {@code Class} object. This includes public, protected, default
954 * (package) access, and private methods, but excludes inherited methods.
955 * The elements in the array returned are not sorted and are not in any
956 * particular order. This method returns an array of length 0 if the class
957 * or interface declares no methods, or if this {@code Class} object
958 * represents a primitive type, an array class, or void. The class
959 * initialization method {@code <clinit>} is not included in the
960 * returned array. If the class declares multiple public member methods
961 * with the same parameter types, they are all included in the returned
964 * <p> See <em>The Java Language Specification</em>, section 8.2.
966 * @return the array of {@code Method} objects representing all the
967 * declared methods of this class
968 * @exception SecurityException
969 * If a security manager, <i>s</i>, is present and any of the
970 * following conditions is met:
975 * {@link SecurityManager#checkMemberAccess
976 * s.checkMemberAccess(this, Member.DECLARED)} denies
977 * access to the declared methods within this class
979 * <li> the caller's class loader is not the same as or an
980 * ancestor of the class loader for the current class and
981 * invocation of {@link SecurityManager#checkPackageAccess
982 * s.checkPackageAccess()} denies access to the package
989 public Method[] getDeclaredMethods() throws SecurityException {
990 throw new SecurityException();
994 * Returns an array of {@code Field} objects reflecting all the fields
995 * declared by the class or interface represented by this
996 * {@code Class} object. This includes public, protected, default
997 * (package) access, and private fields, but excludes inherited fields.
998 * The elements in the array returned are not sorted and are not in any
999 * particular order. This method returns an array of length 0 if the class
1000 * or interface declares no fields, or if this {@code Class} object
1001 * represents a primitive type, an array class, or void.
1003 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1005 * @return the array of {@code Field} objects representing all the
1006 * declared fields of this class
1007 * @exception SecurityException
1008 * If a security manager, <i>s</i>, is present and any of the
1009 * following conditions is met:
1013 * <li> invocation of
1014 * {@link SecurityManager#checkMemberAccess
1015 * s.checkMemberAccess(this, Member.DECLARED)} denies
1016 * access to the declared fields within this class
1018 * <li> the caller's class loader is not the same as or an
1019 * ancestor of the class loader for the current class and
1020 * invocation of {@link SecurityManager#checkPackageAccess
1021 * s.checkPackageAccess()} denies access to the package
1028 public Field[] getDeclaredFields() throws SecurityException {
1029 throw new SecurityException();
1033 * <b>Bck2Brwsr</b> emulation can only seek public methods, otherwise it
1034 * throws a {@code SecurityException}.
1036 * Returns a {@code Method} object that reflects the specified
1037 * declared method of the class or interface represented by this
1038 * {@code Class} object. The {@code name} parameter is a
1039 * {@code String} that specifies the simple name of the desired
1040 * method, and the {@code parameterTypes} parameter is an array of
1041 * {@code Class} objects that identify the method's formal parameter
1042 * types, in declared order. If more than one method with the same
1043 * parameter types is declared in a class, and one of these methods has a
1044 * return type that is more specific than any of the others, that method is
1045 * returned; otherwise one of the methods is chosen arbitrarily. If the
1046 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
1049 * @param name the name of the method
1050 * @param parameterTypes the parameter array
1051 * @return the {@code Method} object for the method of this class
1052 * matching the specified name and parameters
1053 * @exception NoSuchMethodException if a matching method is not found.
1054 * @exception NullPointerException if {@code name} is {@code null}
1055 * @exception SecurityException
1056 * If a security manager, <i>s</i>, is present and any of the
1057 * following conditions is met:
1061 * <li> invocation of
1062 * {@link SecurityManager#checkMemberAccess
1063 * s.checkMemberAccess(this, Member.DECLARED)} denies
1064 * access to the declared method
1066 * <li> the caller's class loader is not the same as or an
1067 * ancestor of the class loader for the current class and
1068 * invocation of {@link SecurityManager#checkPackageAccess
1069 * s.checkPackageAccess()} denies access to the package
1076 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1077 throws NoSuchMethodException, SecurityException {
1079 return getMethod(name, parameterTypes);
1080 } catch (NoSuchMethodException ex) {
1081 throw new SecurityException();
1086 * Returns a {@code Field} object that reflects the specified declared
1087 * field of the class or interface represented by this {@code Class}
1088 * object. The {@code name} parameter is a {@code String} that
1089 * specifies the simple name of the desired field. Note that this method
1090 * will not reflect the {@code length} field of an array class.
1092 * @param name the name of the field
1093 * @return the {@code Field} object for the specified field in this
1095 * @exception NoSuchFieldException if a field with the specified name is
1097 * @exception NullPointerException if {@code name} is {@code null}
1098 * @exception SecurityException
1099 * If a security manager, <i>s</i>, is present and any of the
1100 * following conditions is met:
1104 * <li> invocation of
1105 * {@link SecurityManager#checkMemberAccess
1106 * s.checkMemberAccess(this, Member.DECLARED)} denies
1107 * access to the declared field
1109 * <li> the caller's class loader is not the same as or an
1110 * ancestor of the class loader for the current class and
1111 * invocation of {@link SecurityManager#checkPackageAccess
1112 * s.checkPackageAccess()} denies access to the package
1119 public Field getDeclaredField(String name)
1120 throws SecurityException {
1121 throw new SecurityException();
1125 * Returns an array containing {@code Constructor} objects reflecting
1126 * all the public constructors of the class represented by this
1127 * {@code Class} object. An array of length 0 is returned if the
1128 * class has no public constructors, or if the class is an array class, or
1129 * if the class reflects a primitive type or void.
1131 * Note that while this method returns an array of {@code
1132 * Constructor<T>} objects (that is an array of constructors from
1133 * this class), the return type of this method is {@code
1134 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1135 * might be expected. This less informative return type is
1136 * necessary since after being returned from this method, the
1137 * array could be modified to hold {@code Constructor} objects for
1138 * different classes, which would violate the type guarantees of
1139 * {@code Constructor<T>[]}.
1141 * @return the array of {@code Constructor} objects representing the
1142 * public constructors of this class
1143 * @exception SecurityException
1144 * If a security manager, <i>s</i>, is present and any of the
1145 * following conditions is met:
1149 * <li> invocation of
1150 * {@link SecurityManager#checkMemberAccess
1151 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1152 * access to the constructors within this class
1154 * <li> the caller's class loader is not the same as or an
1155 * ancestor of the class loader for the current class and
1156 * invocation of {@link SecurityManager#checkPackageAccess
1157 * s.checkPackageAccess()} denies access to the package
1164 public Constructor<?>[] getConstructors() throws SecurityException {
1165 return MethodImpl.findConstructors(this, 0x01);
1169 * Returns a {@code Constructor} object that reflects the specified
1170 * public constructor of the class represented by this {@code Class}
1171 * object. The {@code parameterTypes} parameter is an array of
1172 * {@code Class} objects that identify the constructor's formal
1173 * parameter types, in declared order.
1175 * If this {@code Class} object represents an inner class
1176 * declared in a non-static context, the formal parameter types
1177 * include the explicit enclosing instance as the first parameter.
1179 * <p> The constructor to reflect is the public constructor of the class
1180 * represented by this {@code Class} object whose formal parameter
1181 * types match those specified by {@code parameterTypes}.
1183 * @param parameterTypes the parameter array
1184 * @return the {@code Constructor} object of the public constructor that
1185 * matches the specified {@code parameterTypes}
1186 * @exception NoSuchMethodException if a matching method is not found.
1187 * @exception SecurityException
1188 * If a security manager, <i>s</i>, is present and any of the
1189 * following conditions is met:
1193 * <li> invocation of
1194 * {@link SecurityManager#checkMemberAccess
1195 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1196 * access to the constructor
1198 * <li> the caller's class loader is not the same as or an
1199 * ancestor of the class loader for the current class and
1200 * invocation of {@link SecurityManager#checkPackageAccess
1201 * s.checkPackageAccess()} denies access to the package
1208 public Constructor<T> getConstructor(Class<?>... parameterTypes)
1209 throws NoSuchMethodException, SecurityException {
1210 Constructor c = MethodImpl.findConstructor(this, parameterTypes);
1212 StringBuilder sb = new StringBuilder();
1213 sb.append(getName()).append('(');
1215 for (int i = 0; i < parameterTypes.length; i++) {
1216 sb.append(sep).append(parameterTypes[i].getName());
1220 throw new NoSuchMethodException(sb.toString());
1226 * Returns an array of {@code Constructor} objects reflecting all the
1227 * constructors declared by the class represented by this
1228 * {@code Class} object. These are public, protected, default
1229 * (package) access, and private constructors. The elements in the array
1230 * returned are not sorted and are not in any particular order. If the
1231 * class has a default constructor, it is included in the returned array.
1232 * This method returns an array of length 0 if this {@code Class}
1233 * object represents an interface, a primitive type, an array class, or
1236 * <p> See <em>The Java Language Specification</em>, section 8.2.
1238 * @return the array of {@code Constructor} objects representing all the
1239 * declared constructors of this class
1240 * @exception SecurityException
1241 * If a security manager, <i>s</i>, is present and any of the
1242 * following conditions is met:
1246 * <li> invocation of
1247 * {@link SecurityManager#checkMemberAccess
1248 * s.checkMemberAccess(this, Member.DECLARED)} denies
1249 * access to the declared constructors within this class
1251 * <li> the caller's class loader is not the same as or an
1252 * ancestor of the class loader for the current class and
1253 * invocation of {@link SecurityManager#checkPackageAccess
1254 * s.checkPackageAccess()} denies access to the package
1261 public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
1262 throw new SecurityException();
1265 * Returns a {@code Constructor} object that reflects the specified
1266 * constructor of the class or interface represented by this
1267 * {@code Class} object. The {@code parameterTypes} parameter is
1268 * an array of {@code Class} objects that identify the constructor's
1269 * formal parameter types, in declared order.
1271 * If this {@code Class} object represents an inner class
1272 * declared in a non-static context, the formal parameter types
1273 * include the explicit enclosing instance as the first parameter.
1275 * @param parameterTypes the parameter array
1276 * @return The {@code Constructor} object for the constructor with the
1277 * specified parameter list
1278 * @exception NoSuchMethodException if a matching method is not found.
1279 * @exception SecurityException
1280 * If a security manager, <i>s</i>, is present and any of the
1281 * following conditions is met:
1285 * <li> invocation of
1286 * {@link SecurityManager#checkMemberAccess
1287 * s.checkMemberAccess(this, Member.DECLARED)} denies
1288 * access to the declared constructor
1290 * <li> the caller's class loader is not the same as or an
1291 * ancestor of the class loader for the current class and
1292 * invocation of {@link SecurityManager#checkPackageAccess
1293 * s.checkPackageAccess()} denies access to the package
1300 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
1301 throws NoSuchMethodException, SecurityException {
1302 return getConstructor(parameterTypes);
1307 * Character.isDigit answers {@code true} to some non-ascii
1308 * digits. This one does not.
1310 private static boolean isAsciiDigit(char c) {
1311 return '0' <= c && c <= '9';
1315 * Returns the canonical name of the underlying class as
1316 * defined by the Java Language Specification. Returns null if
1317 * the underlying class does not have a canonical name (i.e., if
1318 * it is a local or anonymous class or an array whose component
1319 * type does not have a canonical name).
1320 * @return the canonical name of the underlying class if it exists, and
1321 * {@code null} otherwise.
1324 public String getCanonicalName() {
1326 String canonicalName = getComponentType().getCanonicalName();
1327 if (canonicalName != null)
1328 return canonicalName + "[]";
1332 // if (isLocalOrAnonymousClass())
1334 // Class<?> enclosingClass = getEnclosingClass();
1335 Class<?> enclosingClass = null;
1336 if (enclosingClass == null) { // top level class
1339 String enclosingName = enclosingClass.getCanonicalName();
1340 if (enclosingName == null)
1342 return enclosingName + "." + getSimpleName();
1347 * Finds a resource with a given name. The rules for searching resources
1348 * associated with a given class are implemented by the defining
1349 * {@linkplain ClassLoader class loader} of the class. This method
1350 * delegates to this object's class loader. If this object was loaded by
1351 * the bootstrap class loader, the method delegates to {@link
1352 * ClassLoader#getSystemResourceAsStream}.
1354 * <p> Before delegation, an absolute resource name is constructed from the
1355 * given resource name using this algorithm:
1359 * <li> If the {@code name} begins with a {@code '/'}
1360 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1361 * portion of the {@code name} following the {@code '/'}.
1363 * <li> Otherwise, the absolute name is of the following form:
1366 * {@code modified_package_name/name}
1369 * <p> Where the {@code modified_package_name} is the package name of this
1370 * object with {@code '/'} substituted for {@code '.'}
1371 * (<tt>'\u002e'</tt>).
1375 * @param name name of the desired resource
1376 * @return A {@link java.io.InputStream} object or {@code null} if
1377 * no resource with this name is found
1378 * @throws NullPointerException If {@code name} is {@code null}
1381 public InputStream getResourceAsStream(String name) {
1382 name = resolveName(name);
1383 byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
1384 return arr == null ? null : new ByteArrayInputStream(arr);
1388 * Finds a resource with a given name. The rules for searching resources
1389 * associated with a given class are implemented by the defining
1390 * {@linkplain ClassLoader class loader} of the class. This method
1391 * delegates to this object's class loader. If this object was loaded by
1392 * the bootstrap class loader, the method delegates to {@link
1393 * ClassLoader#getSystemResource}.
1395 * <p> Before delegation, an absolute resource name is constructed from the
1396 * given resource name using this algorithm:
1400 * <li> If the {@code name} begins with a {@code '/'}
1401 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1402 * portion of the {@code name} following the {@code '/'}.
1404 * <li> Otherwise, the absolute name is of the following form:
1407 * {@code modified_package_name/name}
1410 * <p> Where the {@code modified_package_name} is the package name of this
1411 * object with {@code '/'} substituted for {@code '.'}
1412 * (<tt>'\u002e'</tt>).
1416 * @param name name of the desired resource
1417 * @return A {@link java.net.URL} object or {@code null} if no
1418 * resource with this name is found
1421 public java.net.URL getResource(String name) {
1422 return newResourceURL(name, getResourceAsStream(name));
1425 static URL newResourceURL(String name, InputStream is) {
1426 return is == null ? null : newResourceURL0(URL.class, "res:/" + name, is);
1429 @JavaScriptBody(args = { "url", "spec", "is" }, body =
1430 "var u = url.cnstr(true);\n"
1431 + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n"
1434 private static native URL newResourceURL0(Class<URL> url, String spec, InputStream is);
1437 * Add a package name prefix if the name is not absolute Remove leading "/"
1438 * if name is absolute
1440 private String resolveName(String name) {
1444 if (!name.startsWith("/")) {
1446 while (c.isArray()) {
1447 c = c.getComponentType();
1449 String baseName = c.getName();
1450 int index = baseName.lastIndexOf('.');
1452 name = baseName.substring(0, index).replace('.', '/')
1456 name = name.substring(1);
1462 * Returns the class loader for the class. Some implementations may use
1463 * null to represent the bootstrap class loader. This method will return
1464 * null in such implementations if this class was loaded by the bootstrap
1467 * <p> If a security manager is present, and the caller's class loader is
1468 * not null and the caller's class loader is not the same as or an ancestor of
1469 * the class loader for the class whose class loader is requested, then
1470 * this method calls the security manager's {@code checkPermission}
1471 * method with a {@code RuntimePermission("getClassLoader")}
1472 * permission to ensure it's ok to access the class loader for the class.
1475 * represents a primitive type or void, null is returned.
1477 * @return the class loader that loaded the class or interface
1478 * represented by this object.
1479 * @throws SecurityException
1480 * if a security manager exists and its
1481 * {@code checkPermission} method denies
1482 * access to the class loader for the class.
1483 * @see java.lang.ClassLoader
1484 * @see SecurityManager#checkPermission
1485 * @see java.lang.RuntimePermission
1487 public ClassLoader getClassLoader() {
1488 return ClassLoader.getSystemClassLoader();
1492 * Determines the interfaces implemented by the class or interface
1493 * represented by this object.
1495 * <p> If this object represents a class, the return value is an array
1496 * containing objects representing all interfaces implemented by the
1497 * class. The order of the interface objects in the array corresponds to
1498 * the order of the interface names in the {@code implements} clause
1499 * of the declaration of the class represented by this object. For
1500 * example, given the declaration:
1502 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1504 * suppose the value of {@code s} is an instance of
1505 * {@code Shimmer}; the value of the expression:
1507 * {@code s.getClass().getInterfaces()[0]}
1509 * is the {@code Class} object that represents interface
1510 * {@code FloorWax}; and the value of:
1512 * {@code s.getClass().getInterfaces()[1]}
1514 * is the {@code Class} object that represents interface
1515 * {@code DessertTopping}.
1517 * <p> If this object represents an interface, the array contains objects
1518 * representing all interfaces extended by the interface. The order of the
1519 * interface objects in the array corresponds to the order of the interface
1520 * names in the {@code extends} clause of the declaration of the
1521 * interface represented by this object.
1523 * <p> If this object represents a class or interface that implements no
1524 * interfaces, the method returns an array of length 0.
1526 * <p> If this object represents a primitive type or void, the method
1527 * returns an array of length 0.
1529 * @return an array of interfaces implemented by this class.
1531 public native Class<?>[] getInterfaces();
1534 * Returns the {@code Class} representing the component type of an
1535 * array. If this class does not represent an array class this method
1538 * @return the {@code Class} representing the component type of this
1539 * class if this class is an array
1540 * @see java.lang.reflect.Array
1543 public Class<?> getComponentType() {
1546 return getComponentType0();
1547 } catch (ClassNotFoundException cnfe) {
1548 throw new IllegalStateException(cnfe);
1554 private Class<?> getComponentType0() throws ClassNotFoundException {
1555 String n = getName().substring(1);
1556 switch (n.charAt(0)) {
1558 n = n.substring(1, n.length() - 1);
1559 return Class.forName(n);
1561 return Integer.TYPE;
1571 return Boolean.TYPE;
1577 return Character.TYPE;
1579 return defineArray(n);
1581 throw new ClassNotFoundException("Unknown component type of " + getName());
1585 @JavaScriptBody(args = { "sig" }, body =
1586 "if (!sig) sig = '[Ljava/lang/Object;';\n" +
1587 "var c = Array[sig];\n" +
1588 "if (c) return c;\n" +
1589 "c = vm.java_lang_Class(true);\n" +
1590 "c.jvmName = sig;\n" +
1591 "c.superclass = vm.java_lang_Object(false).$class;\n" +
1592 "c.array = true;\n" +
1593 "Array[sig] = c;\n" +
1596 private static native Class<?> defineArray(String sig);
1599 * Returns true if and only if this class was declared as an enum in the
1602 * @return true if and only if this class was declared as an enum in the
1606 public boolean isEnum() {
1607 // An enum must both directly extend java.lang.Enum and have
1608 // the ENUM bit set; classes for specialized enum constants
1609 // don't do the former.
1610 return (this.getModifiers() & ENUM) != 0 &&
1611 this.getSuperclass() == java.lang.Enum.class;
1615 * Casts an object to the class or interface represented
1616 * by this {@code Class} object.
1618 * @param obj the object to be cast
1619 * @return the object after casting, or null if obj is null
1621 * @throws ClassCastException if the object is not
1622 * null and is not assignable to the type T.
1626 public T cast(Object obj) {
1627 if (obj != null && !isInstance(obj))
1628 throw new ClassCastException(cannotCastMsg(obj));
1632 private String cannotCastMsg(Object obj) {
1633 return "Cannot cast " + obj.getClass().getName() + " to " + getName();
1637 * Casts this {@code Class} object to represent a subclass of the class
1638 * represented by the specified class object. Checks that that the cast
1639 * is valid, and throws a {@code ClassCastException} if it is not. If
1640 * this method succeeds, it always returns a reference to this class object.
1642 * <p>This method is useful when a client needs to "narrow" the type of
1643 * a {@code Class} object to pass it to an API that restricts the
1644 * {@code Class} objects that it is willing to accept. A cast would
1645 * generate a compile-time warning, as the correctness of the cast
1646 * could not be checked at runtime (because generic types are implemented
1649 * @return this {@code Class} object, cast to represent a subclass of
1650 * the specified class object.
1651 * @throws ClassCastException if this {@code Class} object does not
1652 * represent a subclass of the specified class (here "subclass" includes
1653 * the class itself).
1656 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
1657 if (clazz.isAssignableFrom(this))
1658 return (Class<? extends U>) this;
1660 throw new ClassCastException(this.toString());
1663 @JavaScriptBody(args = { "ac" },
1665 "if (this.anno) {\n"
1666 + " var r = this.anno['L' + ac.jvmName + ';'];\n"
1667 + " if (typeof r === 'undefined') r = null;\n"
1669 + "} else return null;\n"
1671 private Object getAnnotationData(Class<?> annotationClass) {
1672 throw new UnsupportedOperationException();
1675 * @throws NullPointerException {@inheritDoc}
1678 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
1679 Object data = getAnnotationData(annotationClass);
1680 return data == null ? null : AnnotationImpl.create(annotationClass, data);
1684 * @throws NullPointerException {@inheritDoc}
1687 @JavaScriptBody(args = { "ac" },
1688 body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
1689 + "else return false;"
1691 public boolean isAnnotationPresent(
1692 Class<? extends Annotation> annotationClass) {
1693 if (annotationClass == null)
1694 throw new NullPointerException();
1696 return getAnnotation(annotationClass) != null;
1699 @JavaScriptBody(args = {}, body = "return this.anno;")
1700 private Object getAnnotationData() {
1701 throw new UnsupportedOperationException();
1707 public Annotation[] getAnnotations() {
1708 Object data = getAnnotationData();
1709 return data == null ? new Annotation[0] : AnnotationImpl.create(data);
1715 public Annotation[] getDeclaredAnnotations() {
1716 throw new UnsupportedOperationException();
1719 @JavaScriptBody(args = "type", body = ""
1720 + "var c = vm.java_lang_Class(true);"
1721 + "c.jvmName = type;"
1722 + "c.primitive = true;"
1725 native static Class getPrimitiveClass(String type);
1727 @JavaScriptBody(args = {}, body =
1728 "return vm.desiredAssertionStatus ? vm.desiredAssertionStatus : false;"
1730 public native boolean desiredAssertionStatus();
1732 static void registerNatives() {
1733 boolean assertsOn = false;
1734 // assert assertsOn = true;
1738 } catch (Throwable ex) {
1744 @JavaScriptBody(args = {}, body = "var p = vm.java_lang_Object(false);"
1745 + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
1747 static native void registerToString();
1749 @JavaScriptBody(args = {"self"}, body
1750 = "var c = self.constructor.$class;\n"
1751 + "return c ? c : null;\n"
1753 static native Class<?> classFor(Object self);
1755 @JavaScriptBody(args = { "self" }, body
1756 = "if (self.$hashCode) return self.$hashCode;\n"
1757 + "var h = self.computeHashCode__I ? self.computeHashCode__I() : Math.random() * Math.pow(2, 31);\n"
1758 + "return self.$hashCode = h & h;"
1760 static native int defaultHashCode(Object self);
1762 @JavaScriptBody(args = "self", body
1763 = "\nif (!self.$instOf_java_lang_Cloneable) {"
1766 + "\n var clone = self.constructor(true);"
1767 + "\n var props = Object.getOwnPropertyNames(self);"
1768 + "\n for (var i = 0; i < props.length; i++) {"
1769 + "\n var p = props[i];"
1770 + "\n clone[p] = self[p];"
1772 + "\n return clone;"
1775 static native Object clone(Object self) throws CloneNotSupportedException;