Re-adding getDeclaringClass method, but throwing a security exception when it is called
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 org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
30 import java.io.InputStream;
31 import java.lang.annotation.Annotation;
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.MethodImpl;
41 * Instances of the class {@code Class} represent classes and
42 * interfaces in a running Java application. An enum is a kind of
43 * class and an annotation is a kind of interface. Every array also
44 * belongs to a class that is reflected as a {@code Class} object
45 * that is shared by all arrays with the same element type and number
46 * of dimensions. The primitive Java types ({@code boolean},
47 * {@code byte}, {@code char}, {@code short},
48 * {@code int}, {@code long}, {@code float}, and
49 * {@code double}), and the keyword {@code void} are also
50 * represented as {@code Class} objects.
52 * <p> {@code Class} has no public constructor. Instead {@code Class}
53 * objects are constructed automatically by the Java Virtual Machine as classes
54 * are loaded and by calls to the {@code defineClass} method in the class
57 * <p> The following example uses a {@code Class} object to print the
58 * class name of an object:
60 * <p> <blockquote><pre>
61 * void printClassName(Object obj) {
62 * System.out.println("The class of " + obj +
63 * " is " + obj.getClass().getName());
67 * <p> It is also possible to get the {@code Class} object for a named
68 * type (or for void) using a class literal. See Section 15.8.2 of
69 * <cite>The Java™ Language Specification</cite>.
73 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
76 * @param <T> the type of the class modeled by this {@code Class}
77 * object. For example, the type of {@code String.class} is {@code
78 * Class<String>}. Use {@code Class<?>} if the class being modeled is
82 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
86 class Class<T> implements java.io.Serializable,
87 java.lang.reflect.GenericDeclaration,
88 java.lang.reflect.Type,
89 java.lang.reflect.AnnotatedElement {
90 private static final int ANNOTATION= 0x00002000;
91 private static final int ENUM = 0x00004000;
92 private static final int SYNTHETIC = 0x00001000;
95 * Constructor. Only the Java Virtual Machine creates Class
102 * Converts the object to a string. The string representation is the
103 * string "class" or "interface", followed by a space, and then by the
104 * fully qualified name of the class in the format returned by
105 * {@code getName}. If this {@code Class} object represents a
106 * primitive type, this method returns the name of the primitive type. If
107 * this {@code Class} object represents void this method returns
110 * @return a string representation of this class object.
112 public String toString() {
113 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
119 * Returns the {@code Class} object associated with the class or
120 * interface with the given string name. Invoking this method is
124 * {@code Class.forName(className, true, currentLoader)}
127 * where {@code currentLoader} denotes the defining class loader of
130 * <p> For example, the following code fragment returns the
131 * runtime {@code Class} descriptor for the class named
132 * {@code java.lang.Thread}:
135 * {@code Class t = Class.forName("java.lang.Thread")}
138 * A call to {@code forName("X")} causes the class named
139 * {@code X} to be initialized.
141 * @param className the fully qualified name of the desired class.
142 * @return the {@code Class} object for the class with the
144 * @exception LinkageError if the linkage fails
145 * @exception ExceptionInInitializerError if the initialization provoked
146 * by this method fails
147 * @exception ClassNotFoundException if the class cannot be located
149 public static Class<?> forName(String className)
150 throws ClassNotFoundException {
151 if (className.startsWith("[")) {
152 Class<?> arrType = defineArray(className);
153 Class<?> c = arrType;
154 while (c != null && c.isArray()) {
155 c = c.getComponentType0(); // verify component type is sane
160 Class<?> c = loadCls(className, className.replace('.', '_'));
162 throw new ClassNotFoundException(className);
165 } catch (Throwable ex) {
166 throw new ClassNotFoundException(className, ex);
172 * Returns the {@code Class} object associated with the class or
173 * interface with the given string name, using the given class loader.
174 * Given the fully qualified name for a class or interface (in the same
175 * format returned by {@code getName}) this method attempts to
176 * locate, load, and link the class or interface. The specified class
177 * loader is used to load the class or interface. If the parameter
178 * {@code loader} is null, the class is loaded through the bootstrap
179 * class loader. The class is initialized only if the
180 * {@code initialize} parameter is {@code true} and if it has
181 * not been initialized earlier.
183 * <p> If {@code name} denotes a primitive type or void, an attempt
184 * will be made to locate a user-defined class in the unnamed package whose
185 * name is {@code name}. Therefore, this method cannot be used to
186 * obtain any of the {@code Class} objects representing primitive
189 * <p> If {@code name} denotes an array class, the component type of
190 * the array class is loaded but not initialized.
192 * <p> For example, in an instance method the expression:
195 * {@code Class.forName("Foo")}
201 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
204 * Note that this method throws errors related to loading, linking or
205 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
206 * Java Language Specification</em>.
207 * Note that this method does not check whether the requested class
208 * is accessible to its caller.
210 * <p> If the {@code loader} is {@code null}, and a security
211 * manager is present, and the caller's class loader is not null, then this
212 * method calls the security manager's {@code checkPermission} method
213 * with a {@code RuntimePermission("getClassLoader")} permission to
214 * ensure it's ok to access the bootstrap class loader.
216 * @param name fully qualified name of the desired class
217 * @param initialize whether the class must be initialized
218 * @param loader class loader from which the class must be loaded
219 * @return class object representing the desired class
221 * @exception LinkageError if the linkage fails
222 * @exception ExceptionInInitializerError if the initialization provoked
223 * by this method fails
224 * @exception ClassNotFoundException if the class cannot be located by
225 * the specified class loader
227 * @see java.lang.Class#forName(String)
228 * @see java.lang.ClassLoader
231 public static Class<?> forName(String name, boolean initialize,
233 throws ClassNotFoundException
235 return forName(name);
238 @JavaScriptBody(args = {"n", "c" }, body =
240 + " if (vm.loadClass) {\n"
241 + " vm.loadClass(n);\n"
243 + " if (!vm[c]) return null;\n"
246 + "return vm[c].$class;"
248 private static native Class<?> loadCls(String n, String c);
252 * Creates a new instance of the class represented by this {@code Class}
253 * object. The class is instantiated as if by a {@code new}
254 * expression with an empty argument list. The class is initialized if it
255 * has not already been initialized.
257 * <p>Note that this method propagates any exception thrown by the
258 * nullary constructor, including a checked exception. Use of
259 * this method effectively bypasses the compile-time exception
260 * checking that would otherwise be performed by the compiler.
262 * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
263 * Constructor.newInstance} method avoids this problem by wrapping
264 * any exception thrown by the constructor in a (checked) {@link
265 * java.lang.reflect.InvocationTargetException}.
267 * @return a newly allocated instance of the class represented by this
269 * @exception IllegalAccessException if the class or its nullary
270 * constructor is not accessible.
271 * @exception InstantiationException
272 * if this {@code Class} represents an abstract class,
273 * an interface, an array class, a primitive type, or void;
274 * or if the class has no nullary constructor;
275 * or if the instantiation fails for some other reason.
276 * @exception ExceptionInInitializerError if the initialization
277 * provoked by this method fails.
278 * @exception SecurityException
279 * If a security manager, <i>s</i>, is present and any of the
280 * following conditions is met:
285 * {@link SecurityManager#checkMemberAccess
286 * s.checkMemberAccess(this, Member.PUBLIC)} denies
287 * creation of new instances of this class
289 * <li> the caller's class loader is not the same as or an
290 * ancestor of the class loader for the current class and
291 * invocation of {@link SecurityManager#checkPackageAccess
292 * s.checkPackageAccess()} denies access to the package
298 @JavaScriptBody(args = { "self", "illegal" }, body =
299 "\nvar c = self.cnstr;"
300 + "\nif (c['cons__V']) {"
301 + "\n if ((c.cons__V.access & 0x1) != 0) {"
302 + "\n var inst = c();"
303 + "\n c.cons__V.call(inst);"
306 + "\n return illegal;"
310 private static native Object newInstance0(Class<?> self, Object illegal);
312 public T newInstance()
313 throws InstantiationException, IllegalAccessException
315 Object illegal = new Object();
316 Object inst = newInstance0(this, illegal);
318 throw new InstantiationException(getName());
320 if (inst == illegal) {
321 throw new IllegalAccessException();
327 * Determines if the specified {@code Object} is assignment-compatible
328 * with the object represented by this {@code Class}. This method is
329 * the dynamic equivalent of the Java language {@code instanceof}
330 * operator. The method returns {@code true} if the specified
331 * {@code Object} argument is non-null and can be cast to the
332 * reference type represented by this {@code Class} object without
333 * raising a {@code ClassCastException.} It returns {@code false}
336 * <p> Specifically, if this {@code Class} object represents a
337 * declared class, this method returns {@code true} if the specified
338 * {@code Object} argument is an instance of the represented class (or
339 * of any of its subclasses); it returns {@code false} otherwise. If
340 * this {@code Class} object represents an array class, this method
341 * returns {@code true} if the specified {@code Object} argument
342 * can be converted to an object of the array class by an identity
343 * conversion or by a widening reference conversion; it returns
344 * {@code false} otherwise. If this {@code Class} object
345 * represents an interface, this method returns {@code true} if the
346 * class or any superclass of the specified {@code Object} argument
347 * implements this interface; it returns {@code false} otherwise. If
348 * this {@code Class} object represents a primitive type, this method
349 * returns {@code false}.
351 * @param obj the object to check
352 * @return true if {@code obj} is an instance of this class
356 public boolean isInstance(Object obj) {
361 return isAssignableFrom(obj.getClass());
364 String prop = "$instOf_" + getName().replace('.', '_');
365 return hasProperty(obj, prop);
368 @JavaScriptBody(args = { "who", "prop" }, body =
369 "if (who[prop]) return true; else return false;"
371 private static native boolean hasProperty(Object who, String prop);
375 * Determines if the class or interface represented by this
376 * {@code Class} object is either the same as, or is a superclass or
377 * superinterface of, the class or interface represented by the specified
378 * {@code Class} parameter. It returns {@code true} if so;
379 * otherwise it returns {@code false}. If this {@code Class}
380 * object represents a primitive type, this method returns
381 * {@code true} if the specified {@code Class} parameter is
382 * exactly this {@code Class} object; otherwise it returns
385 * <p> Specifically, this method tests whether the type represented by the
386 * specified {@code Class} parameter can be converted to the type
387 * represented by this {@code Class} object via an identity conversion
388 * or via a widening reference conversion. See <em>The Java Language
389 * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
391 * @param cls the {@code Class} object to be checked
392 * @return the {@code boolean} value indicating whether objects of the
393 * type {@code cls} can be assigned to objects of this class
394 * @exception NullPointerException if the specified Class parameter is
398 public boolean isAssignableFrom(Class<?> cls) {
404 final Class<?> cmpType = cls.getComponentType();
406 return this == cmpType;
408 return cmpType != null && getComponentType().isAssignableFrom(cmpType);
413 if (cls.isPrimitive()) {
416 String prop = "$instOf_" + getName().replace('.', '_');
417 return hasCnstrProperty(cls, prop);
421 @JavaScriptBody(args = { "who", "prop" }, body =
422 "if (who.cnstr.prototype[prop]) return true; else return false;"
424 private static native boolean hasCnstrProperty(Object who, String prop);
428 * Determines if the specified {@code Class} object represents an
431 * @return {@code true} if this object represents an interface;
432 * {@code false} otherwise.
434 public boolean isInterface() {
435 return (getAccess() & 0x200) != 0;
438 @JavaScriptBody(args = {}, body = "return this.access;")
439 private native int getAccess();
443 * Determines if this {@code Class} object represents an array class.
445 * @return {@code true} if this object represents an array class;
446 * {@code false} otherwise.
449 public boolean isArray() {
450 return hasProperty(this, "array"); // NOI18N
455 * Determines if the specified {@code Class} object represents a
458 * <p> There are nine predefined {@code Class} objects to represent
459 * the eight primitive types and void. These are created by the Java
460 * Virtual Machine, and have the same names as the primitive types that
461 * they represent, namely {@code boolean}, {@code byte},
462 * {@code char}, {@code short}, {@code int},
463 * {@code long}, {@code float}, and {@code double}.
465 * <p> These objects may only be accessed via the following public static
466 * final variables, and are the only {@code Class} objects for which
467 * this method returns {@code true}.
469 * @return true if and only if this class represents a primitive type
471 * @see java.lang.Boolean#TYPE
472 * @see java.lang.Character#TYPE
473 * @see java.lang.Byte#TYPE
474 * @see java.lang.Short#TYPE
475 * @see java.lang.Integer#TYPE
476 * @see java.lang.Long#TYPE
477 * @see java.lang.Float#TYPE
478 * @see java.lang.Double#TYPE
479 * @see java.lang.Void#TYPE
482 @JavaScriptBody(args = {}, body =
483 "if (this.primitive) return true;"
484 + "else return false;"
486 public native boolean isPrimitive();
489 * Returns true if this {@code Class} object represents an annotation
490 * type. Note that if this method returns true, {@link #isInterface()}
491 * would also return true, as all annotation types are also interfaces.
493 * @return {@code true} if this class object represents an annotation
494 * type; {@code false} otherwise
497 public boolean isAnnotation() {
498 return (getModifiers() & ANNOTATION) != 0;
502 * Returns {@code true} if this class is a synthetic class;
503 * returns {@code false} otherwise.
504 * @return {@code true} if and only if this class is a synthetic class as
505 * defined by the Java Language Specification.
508 public boolean isSynthetic() {
509 return (getModifiers() & SYNTHETIC) != 0;
513 * Returns the name of the entity (class, interface, array class,
514 * primitive type, or void) represented by this {@code Class} object,
515 * as a {@code String}.
517 * <p> If this class object represents a reference type that is not an
518 * array type then the binary name of the class is returned, as specified
520 * <cite>The Java™ Language Specification</cite>.
522 * <p> If this class object represents a primitive type or void, then the
523 * name returned is a {@code String} equal to the Java language
524 * keyword corresponding to the primitive type or void.
526 * <p> If this class object represents a class of arrays, then the internal
527 * form of the name consists of the name of the element type preceded by
528 * one or more '{@code [}' characters representing the depth of the array
529 * nesting. The encoding of element type names is as follows:
531 * <blockquote><table summary="Element types and encodings">
532 * <tr><th> Element Type <th> <th> Encoding
533 * <tr><td> boolean <td> <td align=center> Z
534 * <tr><td> byte <td> <td align=center> B
535 * <tr><td> char <td> <td align=center> C
536 * <tr><td> class or interface
537 * <td> <td align=center> L<i>classname</i>;
538 * <tr><td> double <td> <td align=center> D
539 * <tr><td> float <td> <td align=center> F
540 * <tr><td> int <td> <td align=center> I
541 * <tr><td> long <td> <td align=center> J
542 * <tr><td> short <td> <td align=center> S
543 * </table></blockquote>
545 * <p> The class or interface name <i>classname</i> is the binary name of
546 * the class specified above.
550 * String.class.getName()
551 * returns "java.lang.String"
552 * byte.class.getName()
554 * (new Object[3]).getClass().getName()
555 * returns "[Ljava.lang.Object;"
556 * (new int[3][4][5][6][7][8][9]).getClass().getName()
558 * </pre></blockquote>
560 * @return the name of the class or interface
561 * represented by this object.
563 public String getName() {
564 return jvmName().replace('/', '.');
567 @JavaScriptBody(args = {}, body = "return this.jvmName;")
568 private native String jvmName();
572 * Returns an array of {@code TypeVariable} objects that represent the
573 * type variables declared by the generic declaration represented by this
574 * {@code GenericDeclaration} object, in declaration order. Returns an
575 * array of length 0 if the underlying generic declaration declares no type
578 * @return an array of {@code TypeVariable} objects that represent
579 * the type variables declared by this generic declaration
580 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
581 * signature of this generic declaration does not conform to
582 * the format specified in
583 * <cite>The Java™ Virtual Machine Specification</cite>
586 public TypeVariable<Class<T>>[] getTypeParameters() {
587 throw new UnsupportedOperationException();
591 * Returns the {@code Class} representing the superclass of the entity
592 * (class, interface, primitive type or void) represented by this
593 * {@code Class}. If this {@code Class} represents either the
594 * {@code Object} class, an interface, a primitive type, or void, then
595 * null is returned. If this object represents an array class then the
596 * {@code Class} object representing the {@code Object} class is
599 * @return the superclass of the class represented by this object.
601 @JavaScriptBody(args = {}, body = "return this.superclass;")
602 public native Class<? super T> getSuperclass();
605 * Returns the Java language modifiers for this class or interface, encoded
606 * in an integer. The modifiers consist of the Java Virtual Machine's
607 * constants for {@code public}, {@code protected},
608 * {@code private}, {@code final}, {@code static},
609 * {@code abstract} and {@code interface}; they should be decoded
610 * using the methods of class {@code Modifier}.
612 * <p> If the underlying class is an array class, then its
613 * {@code public}, {@code private} and {@code protected}
614 * modifiers are the same as those of its component type. If this
615 * {@code Class} represents a primitive type or void, its
616 * {@code public} modifier is always {@code true}, and its
617 * {@code protected} and {@code private} modifiers are always
618 * {@code false}. If this object represents an array class, a
619 * primitive type or void, then its {@code final} modifier is always
620 * {@code true} and its interface modifier is always
621 * {@code false}. The values of its other modifiers are not determined
622 * by this specification.
624 * <p> The modifier encodings are defined in <em>The Java Virtual Machine
625 * Specification</em>, table 4.1.
627 * @return the {@code int} representing the modifiers for this class
628 * @see java.lang.reflect.Modifier
631 public int getModifiers() {
636 * If the class or interface represented by this {@code Class} object
637 * is a member of another class, returns the {@code Class} object
638 * representing the class in which it was declared. This method returns
639 * null if this class or interface is not a member of any other class. If
640 * this {@code Class} object represents an array class, a primitive
641 * type, or void,then this method returns null.
643 * @return the declaring class for this class
646 public Class<?> getDeclaringClass() {
647 throw new SecurityException();
651 * Returns the simple name of the underlying class as given in the
652 * source code. Returns an empty string if the underlying class is
655 * <p>The simple name of an array is the simple name of the
656 * component type with "[]" appended. In particular the simple
657 * name of an array whose component type is anonymous is "[]".
659 * @return the simple name of the underlying class
662 public String getSimpleName() {
664 return getComponentType().getSimpleName()+"[]";
666 String simpleName = getSimpleBinaryName();
667 if (simpleName == null) { // top level class
668 simpleName = getName();
669 return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
671 // According to JLS3 "Binary Compatibility" (13.1) the binary
672 // name of non-package classes (not top level) is the binary
673 // name of the immediately enclosing class followed by a '$' followed by:
674 // (for nested and inner classes): the simple name.
675 // (for local classes): 1 or more digits followed by the simple name.
676 // (for anonymous classes): 1 or more digits.
678 // Since getSimpleBinaryName() will strip the binary name of
679 // the immediatly enclosing class, we are now looking at a
680 // string that matches the regular expression "\$[0-9]*"
681 // followed by a simple name (considering the simple of an
682 // anonymous class to be the empty string).
684 // Remove leading "\$[0-9]*" from the name
685 int length = simpleName.length();
686 if (length < 1 || simpleName.charAt(0) != '$')
687 throw new IllegalStateException("Malformed class name");
689 while (index < length && isAsciiDigit(simpleName.charAt(index)))
691 // Eventually, this is the empty string iff this is an anonymous class
692 return simpleName.substring(index);
696 * Returns the "simple binary name" of the underlying class, i.e.,
697 * the binary name without the leading enclosing class name.
698 * Returns {@code null} if the underlying class is a top level
701 private String getSimpleBinaryName() {
702 Class<?> enclosingClass = null; // XXX getEnclosingClass();
703 if (enclosingClass == null) // top level class
705 // Otherwise, strip the enclosing class' name
707 return getName().substring(enclosingClass.getName().length());
708 } catch (IndexOutOfBoundsException ex) {
709 throw new IllegalStateException("Malformed class name");
714 * Returns an array containing {@code Field} objects reflecting all
715 * the accessible public fields of the class or interface represented by
716 * this {@code Class} object. The elements in the array returned are
717 * not sorted and are not in any particular order. This method returns an
718 * array of length 0 if the class or interface has no accessible public
719 * fields, or if it represents an array class, a primitive type, or void.
721 * <p> Specifically, if this {@code Class} object represents a class,
722 * this method returns the public fields of this class and of all its
723 * superclasses. If this {@code Class} object represents an
724 * interface, this method returns the fields of this interface and of all
725 * its superinterfaces.
727 * <p> The implicit length field for array class is not reflected by this
728 * method. User code should use the methods of class {@code Array} to
731 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
733 * @return the array of {@code Field} objects representing the
735 * @exception SecurityException
736 * If a security manager, <i>s</i>, is present and any of the
737 * following conditions is met:
742 * {@link SecurityManager#checkMemberAccess
743 * s.checkMemberAccess(this, Member.PUBLIC)} denies
744 * access to the fields within this class
746 * <li> the caller's class loader is not the same as or an
747 * ancestor of the class loader for the current class and
748 * invocation of {@link SecurityManager#checkPackageAccess
749 * s.checkPackageAccess()} denies access to the package
756 public Field[] getFields() throws SecurityException {
757 throw new SecurityException();
761 * Returns an array containing {@code Method} objects reflecting all
762 * the public <em>member</em> methods of the class or interface represented
763 * by this {@code Class} object, including those declared by the class
764 * or interface and those inherited from superclasses and
765 * superinterfaces. Array classes return all the (public) member methods
766 * inherited from the {@code Object} class. The elements in the array
767 * returned are not sorted and are not in any particular order. This
768 * method returns an array of length 0 if this {@code Class} object
769 * represents a class or interface that has no public member methods, or if
770 * this {@code Class} object represents a primitive type or void.
772 * <p> The class initialization method {@code <clinit>} is not
773 * included in the returned array. If the class declares multiple public
774 * member methods with the same parameter types, they are all included in
775 * the returned array.
777 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
779 * @return the array of {@code Method} objects representing the
780 * public methods of this class
781 * @exception SecurityException
782 * If a security manager, <i>s</i>, is present and any of the
783 * following conditions is met:
788 * {@link SecurityManager#checkMemberAccess
789 * s.checkMemberAccess(this, Member.PUBLIC)} denies
790 * access to the methods within this class
792 * <li> the caller's class loader is not the same as or an
793 * ancestor of the class loader for the current class and
794 * invocation of {@link SecurityManager#checkPackageAccess
795 * s.checkPackageAccess()} denies access to the package
802 public Method[] getMethods() throws SecurityException {
803 return MethodImpl.findMethods(this, 0x01);
807 * Returns a {@code Field} object that reflects the specified public
808 * member field of the class or interface represented by this
809 * {@code Class} object. The {@code name} parameter is a
810 * {@code String} specifying the simple name of the desired field.
812 * <p> The field to be reflected is determined by the algorithm that
813 * follows. Let C be the class represented by this object:
815 * <LI> If C declares a public field with the name specified, that is the
816 * field to be reflected.</LI>
817 * <LI> If no field was found in step 1 above, this algorithm is applied
818 * recursively to each direct superinterface of C. The direct
819 * superinterfaces are searched in the order they were declared.</LI>
820 * <LI> If no field was found in steps 1 and 2 above, and C has a
821 * superclass S, then this algorithm is invoked recursively upon S.
822 * If C has no superclass, then a {@code NoSuchFieldException}
826 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
828 * @param name the field name
829 * @return the {@code Field} object of this class specified by
831 * @exception NoSuchFieldException if a field with the specified name is
833 * @exception NullPointerException if {@code name} is {@code null}
834 * @exception SecurityException
835 * If a security manager, <i>s</i>, is present and any of the
836 * following conditions is met:
841 * {@link SecurityManager#checkMemberAccess
842 * s.checkMemberAccess(this, Member.PUBLIC)} denies
843 * access to the field
845 * <li> the caller's class loader is not the same as or an
846 * ancestor of the class loader for the current class and
847 * invocation of {@link SecurityManager#checkPackageAccess
848 * s.checkPackageAccess()} denies access to the package
855 public Field getField(String name)
856 throws SecurityException {
857 throw new SecurityException();
862 * Returns a {@code Method} object that reflects the specified public
863 * member method of the class or interface represented by this
864 * {@code Class} object. The {@code name} parameter is a
865 * {@code String} specifying the simple name of the desired method. The
866 * {@code parameterTypes} parameter is an array of {@code Class}
867 * objects that identify the method's formal parameter types, in declared
868 * order. If {@code parameterTypes} is {@code null}, it is
869 * treated as if it were an empty array.
871 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
872 * {@code NoSuchMethodException} is raised. Otherwise, the method to
873 * be reflected is determined by the algorithm that follows. Let C be the
874 * class represented by this object:
876 * <LI> C is searched for any <I>matching methods</I>. If no matching
877 * method is found, the algorithm of step 1 is invoked recursively on
878 * the superclass of C.</LI>
879 * <LI> If no method was found in step 1 above, the superinterfaces of C
880 * are searched for a matching method. If any such method is found, it
884 * To find a matching method in a class C: If C declares exactly one
885 * public method with the specified name and exactly the same formal
886 * parameter types, that is the method reflected. If more than one such
887 * method is found in C, and one of these methods has a return type that is
888 * more specific than any of the others, that method is reflected;
889 * otherwise one of the methods is chosen arbitrarily.
891 * <p>Note that there may be more than one matching method in a
892 * class because while the Java language forbids a class to
893 * declare multiple methods with the same signature but different
894 * return types, the Java virtual machine does not. This
895 * increased flexibility in the virtual machine can be used to
896 * implement various language features. For example, covariant
897 * returns can be implemented with {@linkplain
898 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
899 * method and the method being overridden would have the same
900 * signature but different return types.
902 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
904 * @param name the name of the method
905 * @param parameterTypes the list of parameters
906 * @return the {@code Method} object that matches the specified
907 * {@code name} and {@code parameterTypes}
908 * @exception NoSuchMethodException if a matching method is not found
909 * or if the name is "<init>"or "<clinit>".
910 * @exception NullPointerException if {@code name} is {@code null}
911 * @exception SecurityException
912 * If a security manager, <i>s</i>, is present and any of the
913 * following conditions is met:
918 * {@link SecurityManager#checkMemberAccess
919 * s.checkMemberAccess(this, Member.PUBLIC)} denies
920 * access to the method
922 * <li> the caller's class loader is not the same as or an
923 * ancestor of the class loader for the current class and
924 * invocation of {@link SecurityManager#checkPackageAccess
925 * s.checkPackageAccess()} denies access to the package
932 public Method getMethod(String name, Class<?>... parameterTypes)
933 throws SecurityException, NoSuchMethodException {
934 Method m = MethodImpl.findMethod(this, name, parameterTypes);
936 StringBuilder sb = new StringBuilder();
937 sb.append(getName()).append('.').append(name).append('(');
939 for (int i = 0; i < parameterTypes.length; i++) {
940 sb.append(sep).append(parameterTypes[i].getName());
944 throw new NoSuchMethodException(sb.toString());
950 * Returns an array of {@code Method} objects reflecting all the
951 * methods declared by the class or interface represented by this
952 * {@code Class} object. This includes public, protected, default
953 * (package) access, and private methods, but excludes inherited methods.
954 * The elements in the array returned are not sorted and are not in any
955 * particular order. This method returns an array of length 0 if the class
956 * or interface declares no methods, or if this {@code Class} object
957 * represents a primitive type, an array class, or void. The class
958 * initialization method {@code <clinit>} is not included in the
959 * returned array. If the class declares multiple public member methods
960 * with the same parameter types, they are all included in the returned
963 * <p> See <em>The Java Language Specification</em>, section 8.2.
965 * @return the array of {@code Method} objects representing all the
966 * declared methods of this class
967 * @exception SecurityException
968 * If a security manager, <i>s</i>, is present and any of the
969 * following conditions is met:
974 * {@link SecurityManager#checkMemberAccess
975 * s.checkMemberAccess(this, Member.DECLARED)} denies
976 * access to the declared methods within this class
978 * <li> the caller's class loader is not the same as or an
979 * ancestor of the class loader for the current class and
980 * invocation of {@link SecurityManager#checkPackageAccess
981 * s.checkPackageAccess()} denies access to the package
988 public Method[] getDeclaredMethods() throws SecurityException {
989 throw new SecurityException();
993 * Returns an array of {@code Field} objects reflecting all the fields
994 * declared by the class or interface represented by this
995 * {@code Class} object. This includes public, protected, default
996 * (package) access, and private fields, but excludes inherited fields.
997 * The elements in the array returned are not sorted and are not in any
998 * particular order. This method returns an array of length 0 if the class
999 * or interface declares no fields, or if this {@code Class} object
1000 * represents a primitive type, an array class, or void.
1002 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1004 * @return the array of {@code Field} objects representing all the
1005 * declared fields of this class
1006 * @exception SecurityException
1007 * If a security manager, <i>s</i>, is present and any of the
1008 * following conditions is met:
1012 * <li> invocation of
1013 * {@link SecurityManager#checkMemberAccess
1014 * s.checkMemberAccess(this, Member.DECLARED)} denies
1015 * access to the declared fields within this class
1017 * <li> the caller's class loader is not the same as or an
1018 * ancestor of the class loader for the current class and
1019 * invocation of {@link SecurityManager#checkPackageAccess
1020 * s.checkPackageAccess()} denies access to the package
1027 public Field[] getDeclaredFields() throws SecurityException {
1028 throw new SecurityException();
1032 * <b>Bck2Brwsr</b> emulation can only seek public methods, otherwise it
1033 * throws a {@code SecurityException}.
1035 * Returns a {@code Method} object that reflects the specified
1036 * declared method of the class or interface represented by this
1037 * {@code Class} object. The {@code name} parameter is a
1038 * {@code String} that specifies the simple name of the desired
1039 * method, and the {@code parameterTypes} parameter is an array of
1040 * {@code Class} objects that identify the method's formal parameter
1041 * types, in declared order. If more than one method with the same
1042 * parameter types is declared in a class, and one of these methods has a
1043 * return type that is more specific than any of the others, that method is
1044 * returned; otherwise one of the methods is chosen arbitrarily. If the
1045 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
1048 * @param name the name of the method
1049 * @param parameterTypes the parameter array
1050 * @return the {@code Method} object for the method of this class
1051 * matching the specified name and parameters
1052 * @exception NoSuchMethodException if a matching method is not found.
1053 * @exception NullPointerException if {@code name} is {@code null}
1054 * @exception SecurityException
1055 * If a security manager, <i>s</i>, is present and any of the
1056 * following conditions is met:
1060 * <li> invocation of
1061 * {@link SecurityManager#checkMemberAccess
1062 * s.checkMemberAccess(this, Member.DECLARED)} denies
1063 * access to the declared method
1065 * <li> the caller's class loader is not the same as or an
1066 * ancestor of the class loader for the current class and
1067 * invocation of {@link SecurityManager#checkPackageAccess
1068 * s.checkPackageAccess()} denies access to the package
1075 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1076 throws NoSuchMethodException, SecurityException {
1078 return getMethod(name, parameterTypes);
1079 } catch (NoSuchMethodException ex) {
1080 throw new SecurityException();
1085 * Returns a {@code Field} object that reflects the specified declared
1086 * field of the class or interface represented by this {@code Class}
1087 * object. The {@code name} parameter is a {@code String} that
1088 * specifies the simple name of the desired field. Note that this method
1089 * will not reflect the {@code length} field of an array class.
1091 * @param name the name of the field
1092 * @return the {@code Field} object for the specified field in this
1094 * @exception NoSuchFieldException if a field with the specified name is
1096 * @exception NullPointerException if {@code name} is {@code null}
1097 * @exception SecurityException
1098 * If a security manager, <i>s</i>, is present and any of the
1099 * following conditions is met:
1103 * <li> invocation of
1104 * {@link SecurityManager#checkMemberAccess
1105 * s.checkMemberAccess(this, Member.DECLARED)} denies
1106 * access to the declared field
1108 * <li> the caller's class loader is not the same as or an
1109 * ancestor of the class loader for the current class and
1110 * invocation of {@link SecurityManager#checkPackageAccess
1111 * s.checkPackageAccess()} denies access to the package
1118 public Field getDeclaredField(String name)
1119 throws SecurityException {
1120 throw new SecurityException();
1124 * Returns an array containing {@code Constructor} objects reflecting
1125 * all the public constructors of the class represented by this
1126 * {@code Class} object. An array of length 0 is returned if the
1127 * class has no public constructors, or if the class is an array class, or
1128 * if the class reflects a primitive type or void.
1130 * Note that while this method returns an array of {@code
1131 * Constructor<T>} objects (that is an array of constructors from
1132 * this class), the return type of this method is {@code
1133 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1134 * might be expected. This less informative return type is
1135 * necessary since after being returned from this method, the
1136 * array could be modified to hold {@code Constructor} objects for
1137 * different classes, which would violate the type guarantees of
1138 * {@code Constructor<T>[]}.
1140 * @return the array of {@code Constructor} objects representing the
1141 * public constructors of this class
1142 * @exception SecurityException
1143 * If a security manager, <i>s</i>, is present and any of the
1144 * following conditions is met:
1148 * <li> invocation of
1149 * {@link SecurityManager#checkMemberAccess
1150 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1151 * access to the constructors within this class
1153 * <li> the caller's class loader is not the same as or an
1154 * ancestor of the class loader for the current class and
1155 * invocation of {@link SecurityManager#checkPackageAccess
1156 * s.checkPackageAccess()} denies access to the package
1163 public Constructor<?>[] getConstructors() throws SecurityException {
1164 return MethodImpl.findConstructors(this, 0x01);
1168 * Returns a {@code Constructor} object that reflects the specified
1169 * public constructor of the class represented by this {@code Class}
1170 * object. The {@code parameterTypes} parameter is an array of
1171 * {@code Class} objects that identify the constructor's formal
1172 * parameter types, in declared order.
1174 * If this {@code Class} object represents an inner class
1175 * declared in a non-static context, the formal parameter types
1176 * include the explicit enclosing instance as the first parameter.
1178 * <p> The constructor to reflect is the public constructor of the class
1179 * represented by this {@code Class} object whose formal parameter
1180 * types match those specified by {@code parameterTypes}.
1182 * @param parameterTypes the parameter array
1183 * @return the {@code Constructor} object of the public constructor that
1184 * matches the specified {@code parameterTypes}
1185 * @exception NoSuchMethodException if a matching method is not found.
1186 * @exception SecurityException
1187 * If a security manager, <i>s</i>, is present and any of the
1188 * following conditions is met:
1192 * <li> invocation of
1193 * {@link SecurityManager#checkMemberAccess
1194 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1195 * access to the constructor
1197 * <li> the caller's class loader is not the same as or an
1198 * ancestor of the class loader for the current class and
1199 * invocation of {@link SecurityManager#checkPackageAccess
1200 * s.checkPackageAccess()} denies access to the package
1207 public Constructor<T> getConstructor(Class<?>... parameterTypes)
1208 throws NoSuchMethodException, SecurityException {
1209 Constructor c = MethodImpl.findConstructor(this, parameterTypes);
1211 StringBuilder sb = new StringBuilder();
1212 sb.append(getName()).append('(');
1214 for (int i = 0; i < parameterTypes.length; i++) {
1215 sb.append(sep).append(parameterTypes[i].getName());
1219 throw new NoSuchMethodException(sb.toString());
1225 * Returns an array of {@code Constructor} objects reflecting all the
1226 * constructors declared by the class represented by this
1227 * {@code Class} object. These are public, protected, default
1228 * (package) access, and private constructors. The elements in the array
1229 * returned are not sorted and are not in any particular order. If the
1230 * class has a default constructor, it is included in the returned array.
1231 * This method returns an array of length 0 if this {@code Class}
1232 * object represents an interface, a primitive type, an array class, or
1235 * <p> See <em>The Java Language Specification</em>, section 8.2.
1237 * @return the array of {@code Constructor} objects representing all the
1238 * declared constructors of this class
1239 * @exception SecurityException
1240 * If a security manager, <i>s</i>, is present and any of the
1241 * following conditions is met:
1245 * <li> invocation of
1246 * {@link SecurityManager#checkMemberAccess
1247 * s.checkMemberAccess(this, Member.DECLARED)} denies
1248 * access to the declared constructors within this class
1250 * <li> the caller's class loader is not the same as or an
1251 * ancestor of the class loader for the current class and
1252 * invocation of {@link SecurityManager#checkPackageAccess
1253 * s.checkPackageAccess()} denies access to the package
1260 public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
1261 throw new SecurityException();
1264 * Returns a {@code Constructor} object that reflects the specified
1265 * constructor of the class or interface represented by this
1266 * {@code Class} object. The {@code parameterTypes} parameter is
1267 * an array of {@code Class} objects that identify the constructor's
1268 * formal parameter types, in declared order.
1270 * If this {@code Class} object represents an inner class
1271 * declared in a non-static context, the formal parameter types
1272 * include the explicit enclosing instance as the first parameter.
1274 * @param parameterTypes the parameter array
1275 * @return The {@code Constructor} object for the constructor with the
1276 * specified parameter list
1277 * @exception NoSuchMethodException if a matching method is not found.
1278 * @exception SecurityException
1279 * If a security manager, <i>s</i>, is present and any of the
1280 * following conditions is met:
1284 * <li> invocation of
1285 * {@link SecurityManager#checkMemberAccess
1286 * s.checkMemberAccess(this, Member.DECLARED)} denies
1287 * access to the declared constructor
1289 * <li> the caller's class loader is not the same as or an
1290 * ancestor of the class loader for the current class and
1291 * invocation of {@link SecurityManager#checkPackageAccess
1292 * s.checkPackageAccess()} denies access to the package
1299 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
1300 throws NoSuchMethodException, SecurityException {
1301 return getConstructor(parameterTypes);
1306 * Character.isDigit answers {@code true} to some non-ascii
1307 * digits. This one does not.
1309 private static boolean isAsciiDigit(char c) {
1310 return '0' <= c && c <= '9';
1314 * Returns the canonical name of the underlying class as
1315 * defined by the Java Language Specification. Returns null if
1316 * the underlying class does not have a canonical name (i.e., if
1317 * it is a local or anonymous class or an array whose component
1318 * type does not have a canonical name).
1319 * @return the canonical name of the underlying class if it exists, and
1320 * {@code null} otherwise.
1323 public String getCanonicalName() {
1325 String canonicalName = getComponentType().getCanonicalName();
1326 if (canonicalName != null)
1327 return canonicalName + "[]";
1331 // if (isLocalOrAnonymousClass())
1333 // Class<?> enclosingClass = getEnclosingClass();
1334 Class<?> enclosingClass = null;
1335 if (enclosingClass == null) { // top level class
1338 String enclosingName = enclosingClass.getCanonicalName();
1339 if (enclosingName == null)
1341 return enclosingName + "." + getSimpleName();
1346 * Finds a resource with a given name. The rules for searching resources
1347 * associated with a given class are implemented by the defining
1348 * {@linkplain ClassLoader class loader} of the class. This method
1349 * delegates to this object's class loader. If this object was loaded by
1350 * the bootstrap class loader, the method delegates to {@link
1351 * ClassLoader#getSystemResourceAsStream}.
1353 * <p> Before delegation, an absolute resource name is constructed from the
1354 * given resource name using this algorithm:
1358 * <li> If the {@code name} begins with a {@code '/'}
1359 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1360 * portion of the {@code name} following the {@code '/'}.
1362 * <li> Otherwise, the absolute name is of the following form:
1365 * {@code modified_package_name/name}
1368 * <p> Where the {@code modified_package_name} is the package name of this
1369 * object with {@code '/'} substituted for {@code '.'}
1370 * (<tt>'\u002e'</tt>).
1374 * @param name name of the desired resource
1375 * @return A {@link java.io.InputStream} object or {@code null} if
1376 * no resource with this name is found
1377 * @throws NullPointerException If {@code name} is {@code null}
1380 public InputStream getResourceAsStream(String name) {
1381 name = resolveName(name);
1382 byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
1383 return arr == null ? null : new ByteArrayInputStream(arr);
1387 * Finds a resource with a given name. The rules for searching resources
1388 * associated with a given class are implemented by the defining
1389 * {@linkplain ClassLoader class loader} of the class. This method
1390 * delegates to this object's class loader. If this object was loaded by
1391 * the bootstrap class loader, the method delegates to {@link
1392 * ClassLoader#getSystemResource}.
1394 * <p> Before delegation, an absolute resource name is constructed from the
1395 * given resource name using this algorithm:
1399 * <li> If the {@code name} begins with a {@code '/'}
1400 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1401 * portion of the {@code name} following the {@code '/'}.
1403 * <li> Otherwise, the absolute name is of the following form:
1406 * {@code modified_package_name/name}
1409 * <p> Where the {@code modified_package_name} is the package name of this
1410 * object with {@code '/'} substituted for {@code '.'}
1411 * (<tt>'\u002e'</tt>).
1415 * @param name name of the desired resource
1416 * @return A {@link java.net.URL} object or {@code null} if no
1417 * resource with this name is found
1420 public java.net.URL getResource(String name) {
1421 return newResourceURL(name, getResourceAsStream(name));
1424 static URL newResourceURL(String name, InputStream is) {
1425 return is == null ? null : newResourceURL0(URL.class, "res:/" + name, is);
1428 @JavaScriptBody(args = { "url", "spec", "is" }, body =
1429 "var u = url.cnstr(true);\n"
1430 + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n"
1433 private static native URL newResourceURL0(Class<URL> url, String spec, InputStream is);
1436 * Add a package name prefix if the name is not absolute Remove leading "/"
1437 * if name is absolute
1439 private String resolveName(String name) {
1443 if (!name.startsWith("/")) {
1445 while (c.isArray()) {
1446 c = c.getComponentType();
1448 String baseName = c.getName();
1449 int index = baseName.lastIndexOf('.');
1451 name = baseName.substring(0, index).replace('.', '/')
1455 name = name.substring(1);
1461 * Returns the class loader for the class. Some implementations may use
1462 * null to represent the bootstrap class loader. This method will return
1463 * null in such implementations if this class was loaded by the bootstrap
1466 * <p> If a security manager is present, and the caller's class loader is
1467 * not null and the caller's class loader is not the same as or an ancestor of
1468 * the class loader for the class whose class loader is requested, then
1469 * this method calls the security manager's {@code checkPermission}
1470 * method with a {@code RuntimePermission("getClassLoader")}
1471 * permission to ensure it's ok to access the class loader for the class.
1474 * represents a primitive type or void, null is returned.
1476 * @return the class loader that loaded the class or interface
1477 * represented by this object.
1478 * @throws SecurityException
1479 * if a security manager exists and its
1480 * {@code checkPermission} method denies
1481 * access to the class loader for the class.
1482 * @see java.lang.ClassLoader
1483 * @see SecurityManager#checkPermission
1484 * @see java.lang.RuntimePermission
1486 public ClassLoader getClassLoader() {
1487 return ClassLoader.getSystemClassLoader();
1491 * Determines the interfaces implemented by the class or interface
1492 * represented by this object.
1494 * <p> If this object represents a class, the return value is an array
1495 * containing objects representing all interfaces implemented by the
1496 * class. The order of the interface objects in the array corresponds to
1497 * the order of the interface names in the {@code implements} clause
1498 * of the declaration of the class represented by this object. For
1499 * example, given the declaration:
1501 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1503 * suppose the value of {@code s} is an instance of
1504 * {@code Shimmer}; the value of the expression:
1506 * {@code s.getClass().getInterfaces()[0]}
1508 * is the {@code Class} object that represents interface
1509 * {@code FloorWax}; and the value of:
1511 * {@code s.getClass().getInterfaces()[1]}
1513 * is the {@code Class} object that represents interface
1514 * {@code DessertTopping}.
1516 * <p> If this object represents an interface, the array contains objects
1517 * representing all interfaces extended by the interface. The order of the
1518 * interface objects in the array corresponds to the order of the interface
1519 * names in the {@code extends} clause of the declaration of the
1520 * interface represented by this object.
1522 * <p> If this object represents a class or interface that implements no
1523 * interfaces, the method returns an array of length 0.
1525 * <p> If this object represents a primitive type or void, the method
1526 * returns an array of length 0.
1528 * @return an array of interfaces implemented by this class.
1530 public native Class<?>[] getInterfaces();
1533 * Returns the {@code Class} representing the component type of an
1534 * array. If this class does not represent an array class this method
1537 * @return the {@code Class} representing the component type of this
1538 * class if this class is an array
1539 * @see java.lang.reflect.Array
1542 public Class<?> getComponentType() {
1545 return getComponentType0();
1546 } catch (ClassNotFoundException cnfe) {
1547 throw new IllegalStateException(cnfe);
1553 private Class<?> getComponentType0() throws ClassNotFoundException {
1554 String n = getName().substring(1);
1555 switch (n.charAt(0)) {
1557 n = n.substring(1, n.length() - 1);
1558 return Class.forName(n);
1560 return Integer.TYPE;
1570 return Boolean.TYPE;
1576 return Character.TYPE;
1578 return defineArray(n);
1580 throw new ClassNotFoundException("Unknown component type of " + getName());
1584 @JavaScriptBody(args = { "sig" }, body =
1585 "if (!sig) sig = '[Ljava/lang/Object;';\n" +
1586 "var c = Array[sig];\n" +
1587 "if (c) return c;\n" +
1588 "c = vm.java_lang_Class(true);\n" +
1589 "c.jvmName = sig;\n" +
1590 "c.superclass = vm.java_lang_Object(false).$class;\n" +
1591 "c.array = true;\n" +
1592 "Array[sig] = c;\n" +
1595 private static native Class<?> defineArray(String sig);
1598 * Returns true if and only if this class was declared as an enum in the
1601 * @return true if and only if this class was declared as an enum in the
1605 public boolean isEnum() {
1606 // An enum must both directly extend java.lang.Enum and have
1607 // the ENUM bit set; classes for specialized enum constants
1608 // don't do the former.
1609 return (this.getModifiers() & ENUM) != 0 &&
1610 this.getSuperclass() == java.lang.Enum.class;
1614 * Casts an object to the class or interface represented
1615 * by this {@code Class} object.
1617 * @param obj the object to be cast
1618 * @return the object after casting, or null if obj is null
1620 * @throws ClassCastException if the object is not
1621 * null and is not assignable to the type T.
1625 public T cast(Object obj) {
1626 if (obj != null && !isInstance(obj))
1627 throw new ClassCastException(cannotCastMsg(obj));
1631 private String cannotCastMsg(Object obj) {
1632 return "Cannot cast " + obj.getClass().getName() + " to " + getName();
1636 * Casts this {@code Class} object to represent a subclass of the class
1637 * represented by the specified class object. Checks that that the cast
1638 * is valid, and throws a {@code ClassCastException} if it is not. If
1639 * this method succeeds, it always returns a reference to this class object.
1641 * <p>This method is useful when a client needs to "narrow" the type of
1642 * a {@code Class} object to pass it to an API that restricts the
1643 * {@code Class} objects that it is willing to accept. A cast would
1644 * generate a compile-time warning, as the correctness of the cast
1645 * could not be checked at runtime (because generic types are implemented
1648 * @return this {@code Class} object, cast to represent a subclass of
1649 * the specified class object.
1650 * @throws ClassCastException if this {@code Class} object does not
1651 * represent a subclass of the specified class (here "subclass" includes
1652 * the class itself).
1655 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
1656 if (clazz.isAssignableFrom(this))
1657 return (Class<? extends U>) this;
1659 throw new ClassCastException(this.toString());
1662 @JavaScriptBody(args = { "ac" },
1664 "if (this.anno) {\n"
1665 + " var r = this.anno['L' + ac.jvmName + ';'];\n"
1666 + " if (typeof r === 'undefined') r = null;\n"
1668 + "} else return null;\n"
1670 private Object getAnnotationData(Class<?> annotationClass) {
1671 throw new UnsupportedOperationException();
1674 * @throws NullPointerException {@inheritDoc}
1677 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
1678 Object data = getAnnotationData(annotationClass);
1679 return data == null ? null : AnnotationImpl.create(annotationClass, data);
1683 * @throws NullPointerException {@inheritDoc}
1686 @JavaScriptBody(args = { "ac" },
1687 body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
1688 + "else return false;"
1690 public boolean isAnnotationPresent(
1691 Class<? extends Annotation> annotationClass) {
1692 if (annotationClass == null)
1693 throw new NullPointerException();
1695 return getAnnotation(annotationClass) != null;
1698 @JavaScriptBody(args = {}, body = "return this.anno;")
1699 private Object getAnnotationData() {
1700 throw new UnsupportedOperationException();
1706 public Annotation[] getAnnotations() {
1707 Object data = getAnnotationData();
1708 return data == null ? new Annotation[0] : AnnotationImpl.create(data);
1714 public Annotation[] getDeclaredAnnotations() {
1715 throw new UnsupportedOperationException();
1718 @JavaScriptBody(args = "type", body = ""
1719 + "var c = vm.java_lang_Class(true);"
1720 + "c.jvmName = type;"
1721 + "c.primitive = true;"
1724 native static Class getPrimitiveClass(String type);
1726 @JavaScriptBody(args = {}, body =
1727 "return vm.desiredAssertionStatus ? vm.desiredAssertionStatus : false;"
1729 public native boolean desiredAssertionStatus();