Generate the ClassCastException via java.lang.Class as that one is always known to be exported
2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.io.ByteArrayInputStream;
29 import java.io.InputStream;
30 import java.lang.annotation.Annotation;
31 import java.lang.reflect.Array;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.TypeVariable;
37 import org.apidesign.bck2brwsr.core.Exported;
38 import org.apidesign.bck2brwsr.core.JavaScriptBody;
39 import org.apidesign.bck2brwsr.core.JavaScriptOnly;
40 import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
41 import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
44 * Instances of the class {@code Class} represent classes and
45 * interfaces in a running Java application. An enum is a kind of
46 * class and an annotation is a kind of interface. Every array also
47 * belongs to a class that is reflected as a {@code Class} object
48 * that is shared by all arrays with the same element type and number
49 * of dimensions. The primitive Java types ({@code boolean},
50 * {@code byte}, {@code char}, {@code short},
51 * {@code int}, {@code long}, {@code float}, and
52 * {@code double}), and the keyword {@code void} are also
53 * represented as {@code Class} objects.
55 * <p> {@code Class} has no public constructor. Instead {@code Class}
56 * objects are constructed automatically by the Java Virtual Machine as classes
57 * are loaded and by calls to the {@code defineClass} method in the class
60 * <p> The following example uses a {@code Class} object to print the
61 * class name of an object:
63 * <p> <blockquote><pre>
64 * void printClassName(Object obj) {
65 * System.out.println("The class of " + obj +
66 * " is " + obj.getClass().getName());
70 * <p> It is also possible to get the {@code Class} object for a named
71 * type (or for void) using a class literal. See Section 15.8.2 of
72 * <cite>The Java™ Language Specification</cite>.
76 * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
79 * @param <T> the type of the class modeled by this {@code Class}
80 * object. For example, the type of {@code String.class} is {@code
81 * Class<String>}. Use {@code Class<?>} if the class being modeled is
85 * @see java.lang.ClassLoader#defineClass(byte[], int, int)
89 class Class<T> implements java.io.Serializable,
90 java.lang.reflect.GenericDeclaration,
91 java.lang.reflect.Type,
92 java.lang.reflect.AnnotatedElement {
93 private static final int ANNOTATION= 0x00002000;
94 private static final int ENUM = 0x00004000;
95 private static final int SYNTHETIC = 0x00001000;
97 /* Backing store of user-defined values pertaining to this class.
98 * Maintained by the ClassValue class.
100 transient Object classValueMap;
103 * Constructor. Only the Java Virtual Machine creates Class
110 * Converts the object to a string. The string representation is the
111 * string "class" or "interface", followed by a space, and then by the
112 * fully qualified name of the class in the format returned by
113 * {@code getName}. If this {@code Class} object represents a
114 * primitive type, this method returns the name of the primitive type. If
115 * this {@code Class} object represents void this method returns
118 * @return a string representation of this class object.
120 public String toString() {
121 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
127 * Returns the {@code Class} object associated with the class or
128 * interface with the given string name. Invoking this method is
132 * {@code Class.forName(className, true, currentLoader)}
135 * where {@code currentLoader} denotes the defining class loader of
138 * <p> For example, the following code fragment returns the
139 * runtime {@code Class} descriptor for the class named
140 * {@code java.lang.Thread}:
143 * {@code Class t = Class.forName("java.lang.Thread")}
146 * A call to {@code forName("X")} causes the class named
147 * {@code X} to be initialized.
149 * @param className the fully qualified name of the desired class.
150 * @return the {@code Class} object for the class with the
152 * @exception LinkageError if the linkage fails
153 * @exception ExceptionInInitializerError if the initialization provoked
154 * by this method fails
155 * @exception ClassNotFoundException if the class cannot be located
157 public static Class<?> forName(String className)
158 throws ClassNotFoundException {
159 if (className.startsWith("[")) {
160 Class<?> arrType = defineArray(className, null);
161 Class<?> c = arrType;
162 while (c != null && c.isArray()) {
163 c = c.getComponentType0(); // verify component type is sane
168 Class<?> c = loadCls(className, className.replace('.', '_'));
170 throw new ClassNotFoundException(className);
173 } catch (Throwable ex) {
174 throw new ClassNotFoundException(className, ex);
180 * Returns the {@code Class} object associated with the class or
181 * interface with the given string name, using the given class loader.
182 * Given the fully qualified name for a class or interface (in the same
183 * format returned by {@code getName}) this method attempts to
184 * locate, load, and link the class or interface. The specified class
185 * loader is used to load the class or interface. If the parameter
186 * {@code loader} is null, the class is loaded through the bootstrap
187 * class loader. The class is initialized only if the
188 * {@code initialize} parameter is {@code true} and if it has
189 * not been initialized earlier.
191 * <p> If {@code name} denotes a primitive type or void, an attempt
192 * will be made to locate a user-defined class in the unnamed package whose
193 * name is {@code name}. Therefore, this method cannot be used to
194 * obtain any of the {@code Class} objects representing primitive
197 * <p> If {@code name} denotes an array class, the component type of
198 * the array class is loaded but not initialized.
200 * <p> For example, in an instance method the expression:
203 * {@code Class.forName("Foo")}
209 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
212 * Note that this method throws errors related to loading, linking or
213 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
214 * Java Language Specification</em>.
215 * Note that this method does not check whether the requested class
216 * is accessible to its caller.
218 * <p> If the {@code loader} is {@code null}, and a security
219 * manager is present, and the caller's class loader is not null, then this
220 * method calls the security manager's {@code checkPermission} method
221 * with a {@code RuntimePermission("getClassLoader")} permission to
222 * ensure it's ok to access the bootstrap class loader.
224 * @param name fully qualified name of the desired class
225 * @param initialize whether the class must be initialized
226 * @param loader class loader from which the class must be loaded
227 * @return class object representing the desired class
229 * @exception LinkageError if the linkage fails
230 * @exception ExceptionInInitializerError if the initialization provoked
231 * by this method fails
232 * @exception ClassNotFoundException if the class cannot be located by
233 * the specified class loader
235 * @see java.lang.Class#forName(String)
236 * @see java.lang.ClassLoader
239 public static Class<?> forName(String name, boolean initialize,
241 throws ClassNotFoundException
243 return forName(name);
246 @JavaScriptBody(args = {"n", "c" }, body =
249 + " var l = vm.loadClass ? vm.loadClass : exports ? exports.loadClass : null;\n"
253 + " if (vm[c]) m = vm[c];\n"
254 + " else if (exports[c]) m = exports[c];\n"
256 + "if (!m) return null;"
260 private static native Class<?> loadCls(String n, String c);
264 * Creates a new instance of the class represented by this {@code Class}
265 * object. The class is instantiated as if by a {@code new}
266 * expression with an empty argument list. The class is initialized if it
267 * has not already been initialized.
269 * <p>Note that this method propagates any exception thrown by the
270 * nullary constructor, including a checked exception. Use of
271 * this method effectively bypasses the compile-time exception
272 * checking that would otherwise be performed by the compiler.
274 * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
275 * Constructor.newInstance} method avoids this problem by wrapping
276 * any exception thrown by the constructor in a (checked) {@link
277 * java.lang.reflect.InvocationTargetException}.
279 * @return a newly allocated instance of the class represented by this
281 * @exception IllegalAccessException if the class or its nullary
282 * constructor is not accessible.
283 * @exception InstantiationException
284 * if this {@code Class} represents an abstract class,
285 * an interface, an array class, a primitive type, or void;
286 * or if the class has no nullary constructor;
287 * or if the instantiation fails for some other reason.
288 * @exception ExceptionInInitializerError if the initialization
289 * provoked by this method fails.
290 * @exception SecurityException
291 * If a security manager, <i>s</i>, is present and any of the
292 * following conditions is met:
297 * {@link SecurityManager#checkMemberAccess
298 * s.checkMemberAccess(this, Member.PUBLIC)} denies
299 * creation of new instances of this class
301 * <li> the caller's class loader is not the same as or an
302 * ancestor of the class loader for the current class and
303 * invocation of {@link SecurityManager#checkPackageAccess
304 * s.checkPackageAccess()} denies access to the package
310 @JavaScriptBody(args = { "self", "illegal" }, body =
311 "\nvar c = self.cnstr;"
312 + "\nif (c['cons__V']) {"
313 + "\n if ((c['cons__V'].access & 0x1) != 0) {"
314 + "\n var inst = c();"
315 + "\n c['cons__V'].call(inst);"
318 + "\n return illegal;"
322 private static native Object newInstance0(Class<?> self, Object illegal);
324 public T newInstance()
325 throws InstantiationException, IllegalAccessException
327 Object illegal = new Object();
328 Object inst = newInstance0(this, illegal);
330 throw new InstantiationException(getName());
332 if (inst == illegal) {
333 throw new IllegalAccessException();
339 * Determines if the specified {@code Object} is assignment-compatible
340 * with the object represented by this {@code Class}. This method is
341 * the dynamic equivalent of the Java language {@code instanceof}
342 * operator. The method returns {@code true} if the specified
343 * {@code Object} argument is non-null and can be cast to the
344 * reference type represented by this {@code Class} object without
345 * raising a {@code ClassCastException.} It returns {@code false}
348 * <p> Specifically, if this {@code Class} object represents a
349 * declared class, this method returns {@code true} if the specified
350 * {@code Object} argument is an instance of the represented class (or
351 * of any of its subclasses); it returns {@code false} otherwise. If
352 * this {@code Class} object represents an array class, this method
353 * returns {@code true} if the specified {@code Object} argument
354 * can be converted to an object of the array class by an identity
355 * conversion or by a widening reference conversion; it returns
356 * {@code false} otherwise. If this {@code Class} object
357 * represents an interface, this method returns {@code true} if the
358 * class or any superclass of the specified {@code Object} argument
359 * implements this interface; it returns {@code false} otherwise. If
360 * this {@code Class} object represents a primitive type, this method
361 * returns {@code false}.
363 * @param obj the object to check
364 * @return true if {@code obj} is an instance of this class
368 public boolean isInstance(Object obj) {
373 return isAssignableFrom(obj.getClass());
376 String prop = "$instOf_" + getName().replace('.', '_');
377 return hasProperty(obj, prop);
380 @JavaScriptBody(args = { "who", "prop" }, body =
381 "if (who[prop]) return true; else return false;"
383 private static native boolean hasProperty(Object who, String prop);
387 * Determines if the class or interface represented by this
388 * {@code Class} object is either the same as, or is a superclass or
389 * superinterface of, the class or interface represented by the specified
390 * {@code Class} parameter. It returns {@code true} if so;
391 * otherwise it returns {@code false}. If this {@code Class}
392 * object represents a primitive type, this method returns
393 * {@code true} if the specified {@code Class} parameter is
394 * exactly this {@code Class} object; otherwise it returns
397 * <p> Specifically, this method tests whether the type represented by the
398 * specified {@code Class} parameter can be converted to the type
399 * represented by this {@code Class} object via an identity conversion
400 * or via a widening reference conversion. See <em>The Java Language
401 * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
403 * @param cls the {@code Class} object to be checked
404 * @return the {@code boolean} value indicating whether objects of the
405 * type {@code cls} can be assigned to objects of this class
406 * @exception NullPointerException if the specified Class parameter is
410 public boolean isAssignableFrom(Class<?> cls) {
414 if (this == Object.class) {
419 final Class<?> cmpType = cls.getComponentType();
421 return this == cmpType;
423 return cmpType != null && getComponentType().isAssignableFrom(cmpType);
428 if (cls.isPrimitive()) {
434 String prop = "$instOf_" + getName().replace('.', '_');
435 return hasCnstrProperty(cls, prop);
439 @JavaScriptBody(args = { "who", "prop" }, body =
440 "if (who.cnstr.prototype[prop]) return true; else return false;"
442 private static native boolean hasCnstrProperty(Object who, String prop);
446 * Determines if the specified {@code Class} object represents an
449 * @return {@code true} if this object represents an interface;
450 * {@code false} otherwise.
452 public boolean isInterface() {
453 return (getAccess() & 0x200) != 0;
456 @JavaScriptBody(args = {}, body = "return this.access;")
457 private native int getAccess();
461 * Determines if this {@code Class} object represents an array class.
463 * @return {@code true} if this object represents an array class;
464 * {@code false} otherwise.
467 public boolean isArray() {
468 return hasProperty(this, "array"); // NOI18N
473 * Determines if the specified {@code Class} object represents a
476 * <p> There are nine predefined {@code Class} objects to represent
477 * the eight primitive types and void. These are created by the Java
478 * Virtual Machine, and have the same names as the primitive types that
479 * they represent, namely {@code boolean}, {@code byte},
480 * {@code char}, {@code short}, {@code int},
481 * {@code long}, {@code float}, and {@code double}.
483 * <p> These objects may only be accessed via the following public static
484 * final variables, and are the only {@code Class} objects for which
485 * this method returns {@code true}.
487 * @return true if and only if this class represents a primitive type
489 * @see java.lang.Boolean#TYPE
490 * @see java.lang.Character#TYPE
491 * @see java.lang.Byte#TYPE
492 * @see java.lang.Short#TYPE
493 * @see java.lang.Integer#TYPE
494 * @see java.lang.Long#TYPE
495 * @see java.lang.Float#TYPE
496 * @see java.lang.Double#TYPE
497 * @see java.lang.Void#TYPE
500 @JavaScriptBody(args = {}, body =
501 "if (this.primitive) return true;"
502 + "else return false;"
504 public native boolean isPrimitive();
507 * Returns true if this {@code Class} object represents an annotation
508 * type. Note that if this method returns true, {@link #isInterface()}
509 * would also return true, as all annotation types are also interfaces.
511 * @return {@code true} if this class object represents an annotation
512 * type; {@code false} otherwise
515 public boolean isAnnotation() {
516 return (getModifiers() & ANNOTATION) != 0;
520 * Returns {@code true} if this class is a synthetic class;
521 * returns {@code false} otherwise.
522 * @return {@code true} if and only if this class is a synthetic class as
523 * defined by the Java Language Specification.
526 public boolean isSynthetic() {
527 return (getModifiers() & SYNTHETIC) != 0;
531 * Returns the name of the entity (class, interface, array class,
532 * primitive type, or void) represented by this {@code Class} object,
533 * as a {@code String}.
535 * <p> If this class object represents a reference type that is not an
536 * array type then the binary name of the class is returned, as specified
538 * <cite>The Java™ Language Specification</cite>.
540 * <p> If this class object represents a primitive type or void, then the
541 * name returned is a {@code String} equal to the Java language
542 * keyword corresponding to the primitive type or void.
544 * <p> If this class object represents a class of arrays, then the internal
545 * form of the name consists of the name of the element type preceded by
546 * one or more '{@code [}' characters representing the depth of the array
547 * nesting. The encoding of element type names is as follows:
549 * <blockquote><table summary="Element types and encodings">
550 * <tr><th> Element Type <th> <th> Encoding
551 * <tr><td> boolean <td> <td align=center> Z
552 * <tr><td> byte <td> <td align=center> B
553 * <tr><td> char <td> <td align=center> C
554 * <tr><td> class or interface
555 * <td> <td align=center> L<i>classname</i>;
556 * <tr><td> double <td> <td align=center> D
557 * <tr><td> float <td> <td align=center> F
558 * <tr><td> int <td> <td align=center> I
559 * <tr><td> long <td> <td align=center> J
560 * <tr><td> short <td> <td align=center> S
561 * </table></blockquote>
563 * <p> The class or interface name <i>classname</i> is the binary name of
564 * the class specified above.
568 * String.class.getName()
569 * returns "java.lang.String"
570 * byte.class.getName()
572 * (new Object[3]).getClass().getName()
573 * returns "[Ljava.lang.Object;"
574 * (new int[3][4][5][6][7][8][9]).getClass().getName()
576 * </pre></blockquote>
578 * @return the name of the class or interface
579 * represented by this object.
581 public String getName() {
582 return jvmName().replace('/', '.');
585 @JavaScriptBody(args = {}, body = "return this.jvmName;")
586 private native String jvmName();
590 * Returns an array of {@code TypeVariable} objects that represent the
591 * type variables declared by the generic declaration represented by this
592 * {@code GenericDeclaration} object, in declaration order. Returns an
593 * array of length 0 if the underlying generic declaration declares no type
596 * @return an array of {@code TypeVariable} objects that represent
597 * the type variables declared by this generic declaration
598 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
599 * signature of this generic declaration does not conform to
600 * the format specified in
601 * <cite>The Java™ Virtual Machine Specification</cite>
604 public TypeVariable<Class<T>>[] getTypeParameters() {
605 throw new UnsupportedOperationException();
609 * Returns the {@code Class} representing the superclass of the entity
610 * (class, interface, primitive type or void) represented by this
611 * {@code Class}. If this {@code Class} represents either the
612 * {@code Object} class, an interface, a primitive type, or void, then
613 * null is returned. If this object represents an array class then the
614 * {@code Class} object representing the {@code Object} class is
617 * @return the superclass of the class represented by this object.
619 @JavaScriptBody(args = {}, body = "return this.superclass;")
620 public native Class<? super T> getSuperclass();
623 * Returns the Java language modifiers for this class or interface, encoded
624 * in an integer. The modifiers consist of the Java Virtual Machine's
625 * constants for {@code public}, {@code protected},
626 * {@code private}, {@code final}, {@code static},
627 * {@code abstract} and {@code interface}; they should be decoded
628 * using the methods of class {@code Modifier}.
630 * <p> If the underlying class is an array class, then its
631 * {@code public}, {@code private} and {@code protected}
632 * modifiers are the same as those of its component type. If this
633 * {@code Class} represents a primitive type or void, its
634 * {@code public} modifier is always {@code true}, and its
635 * {@code protected} and {@code private} modifiers are always
636 * {@code false}. If this object represents an array class, a
637 * primitive type or void, then its {@code final} modifier is always
638 * {@code true} and its interface modifier is always
639 * {@code false}. The values of its other modifiers are not determined
640 * by this specification.
642 * <p> The modifier encodings are defined in <em>The Java Virtual Machine
643 * Specification</em>, table 4.1.
645 * @return the {@code int} representing the modifiers for this class
646 * @see java.lang.reflect.Modifier
649 public int getModifiers() {
654 * If the class or interface represented by this {@code Class} object
655 * is a member of another class, returns the {@code Class} object
656 * representing the class in which it was declared. This method returns
657 * null if this class or interface is not a member of any other class. If
658 * this {@code Class} object represents an array class, a primitive
659 * type, or void,then this method returns null.
661 * @return the declaring class for this class
664 public Class<?> getDeclaringClass() {
665 throw new SecurityException();
669 * Returns the simple name of the underlying class as given in the
670 * source code. Returns an empty string if the underlying class is
673 * <p>The simple name of an array is the simple name of the
674 * component type with "[]" appended. In particular the simple
675 * name of an array whose component type is anonymous is "[]".
677 * @return the simple name of the underlying class
680 public String getSimpleName() {
682 return getComponentType().getSimpleName()+"[]";
684 String simpleName = getSimpleBinaryName();
685 if (simpleName == null) { // top level class
686 simpleName = getName();
687 return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
689 // According to JLS3 "Binary Compatibility" (13.1) the binary
690 // name of non-package classes (not top level) is the binary
691 // name of the immediately enclosing class followed by a '$' followed by:
692 // (for nested and inner classes): the simple name.
693 // (for local classes): 1 or more digits followed by the simple name.
694 // (for anonymous classes): 1 or more digits.
696 // Since getSimpleBinaryName() will strip the binary name of
697 // the immediatly enclosing class, we are now looking at a
698 // string that matches the regular expression "\$[0-9]*"
699 // followed by a simple name (considering the simple of an
700 // anonymous class to be the empty string).
702 // Remove leading "\$[0-9]*" from the name
703 int length = simpleName.length();
704 if (length < 1 || simpleName.charAt(0) != '$')
705 throw new IllegalStateException("Malformed class name");
707 while (index < length && isAsciiDigit(simpleName.charAt(index)))
709 // Eventually, this is the empty string iff this is an anonymous class
710 return simpleName.substring(index);
714 * Returns the "simple binary name" of the underlying class, i.e.,
715 * the binary name without the leading enclosing class name.
716 * Returns {@code null} if the underlying class is a top level
719 private String getSimpleBinaryName() {
720 Class<?> enclosingClass = null; // XXX getEnclosingClass();
721 if (enclosingClass == null) // top level class
723 // Otherwise, strip the enclosing class' name
725 return getName().substring(enclosingClass.getName().length());
726 } catch (IndexOutOfBoundsException ex) {
727 throw new IllegalStateException("Malformed class name");
732 * Returns an array containing {@code Field} objects reflecting all
733 * the accessible public fields of the class or interface represented by
734 * this {@code Class} object. The elements in the array returned are
735 * not sorted and are not in any particular order. This method returns an
736 * array of length 0 if the class or interface has no accessible public
737 * fields, or if it represents an array class, a primitive type, or void.
739 * <p> Specifically, if this {@code Class} object represents a class,
740 * this method returns the public fields of this class and of all its
741 * superclasses. If this {@code Class} object represents an
742 * interface, this method returns the fields of this interface and of all
743 * its superinterfaces.
745 * <p> The implicit length field for array class is not reflected by this
746 * method. User code should use the methods of class {@code Array} to
749 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
751 * @return the array of {@code Field} objects representing the
753 * @exception SecurityException
754 * If a security manager, <i>s</i>, is present and any of the
755 * following conditions is met:
760 * {@link SecurityManager#checkMemberAccess
761 * s.checkMemberAccess(this, Member.PUBLIC)} denies
762 * access to the fields within this class
764 * <li> the caller's class loader is not the same as or an
765 * ancestor of the class loader for the current class and
766 * invocation of {@link SecurityManager#checkPackageAccess
767 * s.checkPackageAccess()} denies access to the package
774 public Field[] getFields() throws SecurityException {
775 throw new SecurityException();
779 * Returns an array containing {@code Method} objects reflecting all
780 * the public <em>member</em> methods of the class or interface represented
781 * by this {@code Class} object, including those declared by the class
782 * or interface and those inherited from superclasses and
783 * superinterfaces. Array classes return all the (public) member methods
784 * inherited from the {@code Object} class. The elements in the array
785 * returned are not sorted and are not in any particular order. This
786 * method returns an array of length 0 if this {@code Class} object
787 * represents a class or interface that has no public member methods, or if
788 * this {@code Class} object represents a primitive type or void.
790 * <p> The class initialization method {@code <clinit>} is not
791 * included in the returned array. If the class declares multiple public
792 * member methods with the same parameter types, they are all included in
793 * the returned array.
795 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
797 * @return the array of {@code Method} objects representing the
798 * public methods of this class
799 * @exception SecurityException
800 * If a security manager, <i>s</i>, is present and any of the
801 * following conditions is met:
806 * {@link SecurityManager#checkMemberAccess
807 * s.checkMemberAccess(this, Member.PUBLIC)} denies
808 * access to the methods within this class
810 * <li> the caller's class loader is not the same as or an
811 * ancestor of the class loader for the current class and
812 * invocation of {@link SecurityManager#checkPackageAccess
813 * s.checkPackageAccess()} denies access to the package
820 public Method[] getMethods() throws SecurityException {
821 return MethodImpl.findMethods(this, 0x01);
825 * Returns a {@code Field} object that reflects the specified public
826 * member field of the class or interface represented by this
827 * {@code Class} object. The {@code name} parameter is a
828 * {@code String} specifying the simple name of the desired field.
830 * <p> The field to be reflected is determined by the algorithm that
831 * follows. Let C be the class represented by this object:
833 * <LI> If C declares a public field with the name specified, that is the
834 * field to be reflected.</LI>
835 * <LI> If no field was found in step 1 above, this algorithm is applied
836 * recursively to each direct superinterface of C. The direct
837 * superinterfaces are searched in the order they were declared.</LI>
838 * <LI> If no field was found in steps 1 and 2 above, and C has a
839 * superclass S, then this algorithm is invoked recursively upon S.
840 * If C has no superclass, then a {@code NoSuchFieldException}
844 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
846 * @param name the field name
847 * @return the {@code Field} object of this class specified by
849 * @exception NoSuchFieldException if a field with the specified name is
851 * @exception NullPointerException if {@code name} is {@code null}
852 * @exception SecurityException
853 * If a security manager, <i>s</i>, is present and any of the
854 * following conditions is met:
859 * {@link SecurityManager#checkMemberAccess
860 * s.checkMemberAccess(this, Member.PUBLIC)} denies
861 * access to the field
863 * <li> the caller's class loader is not the same as or an
864 * ancestor of the class loader for the current class and
865 * invocation of {@link SecurityManager#checkPackageAccess
866 * s.checkPackageAccess()} denies access to the package
873 public Field getField(String name)
874 throws SecurityException {
875 throw new SecurityException();
880 * Returns a {@code Method} object that reflects the specified public
881 * member method of the class or interface represented by this
882 * {@code Class} object. The {@code name} parameter is a
883 * {@code String} specifying the simple name of the desired method. The
884 * {@code parameterTypes} parameter is an array of {@code Class}
885 * objects that identify the method's formal parameter types, in declared
886 * order. If {@code parameterTypes} is {@code null}, it is
887 * treated as if it were an empty array.
889 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
890 * {@code NoSuchMethodException} is raised. Otherwise, the method to
891 * be reflected is determined by the algorithm that follows. Let C be the
892 * class represented by this object:
894 * <LI> C is searched for any <I>matching methods</I>. If no matching
895 * method is found, the algorithm of step 1 is invoked recursively on
896 * the superclass of C.</LI>
897 * <LI> If no method was found in step 1 above, the superinterfaces of C
898 * are searched for a matching method. If any such method is found, it
902 * To find a matching method in a class C: If C declares exactly one
903 * public method with the specified name and exactly the same formal
904 * parameter types, that is the method reflected. If more than one such
905 * method is found in C, and one of these methods has a return type that is
906 * more specific than any of the others, that method is reflected;
907 * otherwise one of the methods is chosen arbitrarily.
909 * <p>Note that there may be more than one matching method in a
910 * class because while the Java language forbids a class to
911 * declare multiple methods with the same signature but different
912 * return types, the Java virtual machine does not. This
913 * increased flexibility in the virtual machine can be used to
914 * implement various language features. For example, covariant
915 * returns can be implemented with {@linkplain
916 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
917 * method and the method being overridden would have the same
918 * signature but different return types.
920 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
922 * @param name the name of the method
923 * @param parameterTypes the list of parameters
924 * @return the {@code Method} object that matches the specified
925 * {@code name} and {@code parameterTypes}
926 * @exception NoSuchMethodException if a matching method is not found
927 * or if the name is "<init>"or "<clinit>".
928 * @exception NullPointerException if {@code name} is {@code null}
929 * @exception SecurityException
930 * If a security manager, <i>s</i>, is present and any of the
931 * following conditions is met:
936 * {@link SecurityManager#checkMemberAccess
937 * s.checkMemberAccess(this, Member.PUBLIC)} denies
938 * access to the method
940 * <li> the caller's class loader is not the same as or an
941 * ancestor of the class loader for the current class and
942 * invocation of {@link SecurityManager#checkPackageAccess
943 * s.checkPackageAccess()} denies access to the package
950 public Method getMethod(String name, Class<?>... parameterTypes)
951 throws SecurityException, NoSuchMethodException {
952 Method m = MethodImpl.findMethod(this, name, parameterTypes);
954 StringBuilder sb = new StringBuilder();
955 sb.append(getName()).append('.').append(name).append('(');
957 for (int i = 0; i < parameterTypes.length; i++) {
958 sb.append(sep).append(parameterTypes[i].getName());
962 throw new NoSuchMethodException(sb.toString());
968 * Returns an array of {@code Method} objects reflecting all the
969 * methods declared by the class or interface represented by this
970 * {@code Class} object. This includes public, protected, default
971 * (package) access, and private methods, but excludes inherited methods.
972 * The elements in the array returned are not sorted and are not in any
973 * particular order. This method returns an array of length 0 if the class
974 * or interface declares no methods, or if this {@code Class} object
975 * represents a primitive type, an array class, or void. The class
976 * initialization method {@code <clinit>} is not included in the
977 * returned array. If the class declares multiple public member methods
978 * with the same parameter types, they are all included in the returned
981 * <p> See <em>The Java Language Specification</em>, section 8.2.
983 * @return the array of {@code Method} objects representing all the
984 * declared methods of this class
985 * @exception SecurityException
986 * If a security manager, <i>s</i>, is present and any of the
987 * following conditions is met:
992 * {@link SecurityManager#checkMemberAccess
993 * s.checkMemberAccess(this, Member.DECLARED)} denies
994 * access to the declared methods within this class
996 * <li> the caller's class loader is not the same as or an
997 * ancestor of the class loader for the current class and
998 * invocation of {@link SecurityManager#checkPackageAccess
999 * s.checkPackageAccess()} denies access to the package
1006 public Method[] getDeclaredMethods() throws SecurityException {
1007 throw new SecurityException();
1011 * Returns an array of {@code Field} objects reflecting all the fields
1012 * declared by the class or interface represented by this
1013 * {@code Class} object. This includes public, protected, default
1014 * (package) access, and private fields, but excludes inherited fields.
1015 * The elements in the array returned are not sorted and are not in any
1016 * particular order. This method returns an array of length 0 if the class
1017 * or interface declares no fields, or if this {@code Class} object
1018 * represents a primitive type, an array class, or void.
1020 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1022 * @return the array of {@code Field} objects representing all the
1023 * declared fields of this class
1024 * @exception SecurityException
1025 * If a security manager, <i>s</i>, is present and any of the
1026 * following conditions is met:
1030 * <li> invocation of
1031 * {@link SecurityManager#checkMemberAccess
1032 * s.checkMemberAccess(this, Member.DECLARED)} denies
1033 * access to the declared fields within this class
1035 * <li> the caller's class loader is not the same as or an
1036 * ancestor of the class loader for the current class and
1037 * invocation of {@link SecurityManager#checkPackageAccess
1038 * s.checkPackageAccess()} denies access to the package
1045 public Field[] getDeclaredFields() throws SecurityException {
1046 throw new SecurityException();
1050 * <b>Bck2Brwsr</b> emulation can only seek public methods, otherwise it
1051 * throws a {@code SecurityException}.
1053 * Returns a {@code Method} object that reflects the specified
1054 * declared method of the class or interface represented by this
1055 * {@code Class} object. The {@code name} parameter is a
1056 * {@code String} that specifies the simple name of the desired
1057 * method, and the {@code parameterTypes} parameter is an array of
1058 * {@code Class} objects that identify the method's formal parameter
1059 * types, in declared order. If more than one method with the same
1060 * parameter types is declared in a class, and one of these methods has a
1061 * return type that is more specific than any of the others, that method is
1062 * returned; otherwise one of the methods is chosen arbitrarily. If the
1063 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
1066 * @param name the name of the method
1067 * @param parameterTypes the parameter array
1068 * @return the {@code Method} object for the method of this class
1069 * matching the specified name and parameters
1070 * @exception NoSuchMethodException if a matching method is not found.
1071 * @exception NullPointerException if {@code name} is {@code null}
1072 * @exception SecurityException
1073 * If a security manager, <i>s</i>, is present and any of the
1074 * following conditions is met:
1078 * <li> invocation of
1079 * {@link SecurityManager#checkMemberAccess
1080 * s.checkMemberAccess(this, Member.DECLARED)} denies
1081 * access to the declared method
1083 * <li> the caller's class loader is not the same as or an
1084 * ancestor of the class loader for the current class and
1085 * invocation of {@link SecurityManager#checkPackageAccess
1086 * s.checkPackageAccess()} denies access to the package
1093 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1094 throws NoSuchMethodException, SecurityException {
1096 return getMethod(name, parameterTypes);
1097 } catch (NoSuchMethodException ex) {
1098 throw new SecurityException();
1103 * Returns a {@code Field} object that reflects the specified declared
1104 * field of the class or interface represented by this {@code Class}
1105 * object. The {@code name} parameter is a {@code String} that
1106 * specifies the simple name of the desired field. Note that this method
1107 * will not reflect the {@code length} field of an array class.
1109 * @param name the name of the field
1110 * @return the {@code Field} object for the specified field in this
1112 * @exception NoSuchFieldException if a field with the specified name is
1114 * @exception NullPointerException if {@code name} is {@code null}
1115 * @exception SecurityException
1116 * If a security manager, <i>s</i>, is present and any of the
1117 * following conditions is met:
1121 * <li> invocation of
1122 * {@link SecurityManager#checkMemberAccess
1123 * s.checkMemberAccess(this, Member.DECLARED)} denies
1124 * access to the declared field
1126 * <li> the caller's class loader is not the same as or an
1127 * ancestor of the class loader for the current class and
1128 * invocation of {@link SecurityManager#checkPackageAccess
1129 * s.checkPackageAccess()} denies access to the package
1136 public Field getDeclaredField(String name)
1137 throws SecurityException {
1138 throw new SecurityException();
1142 * Returns an array containing {@code Constructor} objects reflecting
1143 * all the public constructors of the class represented by this
1144 * {@code Class} object. An array of length 0 is returned if the
1145 * class has no public constructors, or if the class is an array class, or
1146 * if the class reflects a primitive type or void.
1148 * Note that while this method returns an array of {@code
1149 * Constructor<T>} objects (that is an array of constructors from
1150 * this class), the return type of this method is {@code
1151 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1152 * might be expected. This less informative return type is
1153 * necessary since after being returned from this method, the
1154 * array could be modified to hold {@code Constructor} objects for
1155 * different classes, which would violate the type guarantees of
1156 * {@code Constructor<T>[]}.
1158 * @return the array of {@code Constructor} objects representing the
1159 * public constructors of this class
1160 * @exception SecurityException
1161 * If a security manager, <i>s</i>, is present and any of the
1162 * following conditions is met:
1166 * <li> invocation of
1167 * {@link SecurityManager#checkMemberAccess
1168 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1169 * access to the constructors within this class
1171 * <li> the caller's class loader is not the same as or an
1172 * ancestor of the class loader for the current class and
1173 * invocation of {@link SecurityManager#checkPackageAccess
1174 * s.checkPackageAccess()} denies access to the package
1181 public Constructor<?>[] getConstructors() throws SecurityException {
1182 return MethodImpl.findConstructors(this, 0x01);
1186 * Returns a {@code Constructor} object that reflects the specified
1187 * public constructor of the class represented by this {@code Class}
1188 * object. The {@code parameterTypes} parameter is an array of
1189 * {@code Class} objects that identify the constructor's formal
1190 * parameter types, in declared order.
1192 * If this {@code Class} object represents an inner class
1193 * declared in a non-static context, the formal parameter types
1194 * include the explicit enclosing instance as the first parameter.
1196 * <p> The constructor to reflect is the public constructor of the class
1197 * represented by this {@code Class} object whose formal parameter
1198 * types match those specified by {@code parameterTypes}.
1200 * @param parameterTypes the parameter array
1201 * @return the {@code Constructor} object of the public constructor that
1202 * matches the specified {@code parameterTypes}
1203 * @exception NoSuchMethodException if a matching method is not found.
1204 * @exception SecurityException
1205 * If a security manager, <i>s</i>, is present and any of the
1206 * following conditions is met:
1210 * <li> invocation of
1211 * {@link SecurityManager#checkMemberAccess
1212 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1213 * access to the constructor
1215 * <li> the caller's class loader is not the same as or an
1216 * ancestor of the class loader for the current class and
1217 * invocation of {@link SecurityManager#checkPackageAccess
1218 * s.checkPackageAccess()} denies access to the package
1225 public Constructor<T> getConstructor(Class<?>... parameterTypes)
1226 throws NoSuchMethodException, SecurityException {
1227 Constructor c = MethodImpl.findConstructor(this, parameterTypes);
1229 StringBuilder sb = new StringBuilder();
1230 sb.append(getName()).append('(');
1232 for (int i = 0; i < parameterTypes.length; i++) {
1233 sb.append(sep).append(parameterTypes[i].getName());
1237 throw new NoSuchMethodException(sb.toString());
1243 * Returns an array of {@code Constructor} objects reflecting all the
1244 * constructors declared by the class represented by this
1245 * {@code Class} object. These are public, protected, default
1246 * (package) access, and private constructors. The elements in the array
1247 * returned are not sorted and are not in any particular order. If the
1248 * class has a default constructor, it is included in the returned array.
1249 * This method returns an array of length 0 if this {@code Class}
1250 * object represents an interface, a primitive type, an array class, or
1253 * <p> See <em>The Java Language Specification</em>, section 8.2.
1255 * @return the array of {@code Constructor} objects representing all the
1256 * declared constructors of this class
1257 * @exception SecurityException
1258 * If a security manager, <i>s</i>, is present and any of the
1259 * following conditions is met:
1263 * <li> invocation of
1264 * {@link SecurityManager#checkMemberAccess
1265 * s.checkMemberAccess(this, Member.DECLARED)} denies
1266 * access to the declared constructors within this class
1268 * <li> the caller's class loader is not the same as or an
1269 * ancestor of the class loader for the current class and
1270 * invocation of {@link SecurityManager#checkPackageAccess
1271 * s.checkPackageAccess()} denies access to the package
1278 public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
1279 throw new SecurityException();
1282 * Returns a {@code Constructor} object that reflects the specified
1283 * constructor of the class or interface represented by this
1284 * {@code Class} object. The {@code parameterTypes} parameter is
1285 * an array of {@code Class} objects that identify the constructor's
1286 * formal parameter types, in declared order.
1288 * If this {@code Class} object represents an inner class
1289 * declared in a non-static context, the formal parameter types
1290 * include the explicit enclosing instance as the first parameter.
1292 * @param parameterTypes the parameter array
1293 * @return The {@code Constructor} object for the constructor with the
1294 * specified parameter list
1295 * @exception NoSuchMethodException if a matching method is not found.
1296 * @exception SecurityException
1297 * If a security manager, <i>s</i>, is present and any of the
1298 * following conditions is met:
1302 * <li> invocation of
1303 * {@link SecurityManager#checkMemberAccess
1304 * s.checkMemberAccess(this, Member.DECLARED)} denies
1305 * access to the declared constructor
1307 * <li> the caller's class loader is not the same as or an
1308 * ancestor of the class loader for the current class and
1309 * invocation of {@link SecurityManager#checkPackageAccess
1310 * s.checkPackageAccess()} denies access to the package
1317 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
1318 throws NoSuchMethodException, SecurityException {
1319 return getConstructor(parameterTypes);
1324 * Character.isDigit answers {@code true} to some non-ascii
1325 * digits. This one does not.
1327 private static boolean isAsciiDigit(char c) {
1328 return '0' <= c && c <= '9';
1332 * Returns the canonical name of the underlying class as
1333 * defined by the Java Language Specification. Returns null if
1334 * the underlying class does not have a canonical name (i.e., if
1335 * it is a local or anonymous class or an array whose component
1336 * type does not have a canonical name).
1337 * @return the canonical name of the underlying class if it exists, and
1338 * {@code null} otherwise.
1341 public String getCanonicalName() {
1343 String canonicalName = getComponentType().getCanonicalName();
1344 if (canonicalName != null)
1345 return canonicalName + "[]";
1349 // if (isLocalOrAnonymousClass())
1351 // Class<?> enclosingClass = getEnclosingClass();
1352 Class<?> enclosingClass = null;
1353 if (enclosingClass == null) { // top level class
1356 String enclosingName = enclosingClass.getCanonicalName();
1357 if (enclosingName == null)
1359 return enclosingName + "." + getSimpleName();
1364 * Finds a resource with a given name. The rules for searching resources
1365 * associated with a given class are implemented by the defining
1366 * {@linkplain ClassLoader class loader} of the class. This method
1367 * delegates to this object's class loader. If this object was loaded by
1368 * the bootstrap class loader, the method delegates to {@link
1369 * ClassLoader#getSystemResourceAsStream}.
1371 * <p> Before delegation, an absolute resource name is constructed from the
1372 * given resource name using this algorithm:
1376 * <li> If the {@code name} begins with a {@code '/'}
1377 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1378 * portion of the {@code name} following the {@code '/'}.
1380 * <li> Otherwise, the absolute name is of the following form:
1383 * {@code modified_package_name/name}
1386 * <p> Where the {@code modified_package_name} is the package name of this
1387 * object with {@code '/'} substituted for {@code '.'}
1388 * (<tt>'\u002e'</tt>).
1392 * @param name name of the desired resource
1393 * @return A {@link java.io.InputStream} object or {@code null} if
1394 * no resource with this name is found
1395 * @throws NullPointerException If {@code name} is {@code null}
1398 public InputStream getResourceAsStream(String name) {
1399 name = resolveName(name);
1400 byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
1401 return arr == null ? null : new ByteArrayInputStream(arr);
1405 * Finds a resource with a given name. The rules for searching resources
1406 * associated with a given class are implemented by the defining
1407 * {@linkplain ClassLoader class loader} of the class. This method
1408 * delegates to this object's class loader. If this object was loaded by
1409 * the bootstrap class loader, the method delegates to {@link
1410 * ClassLoader#getSystemResource}.
1412 * <p> Before delegation, an absolute resource name is constructed from the
1413 * given resource name using this algorithm:
1417 * <li> If the {@code name} begins with a {@code '/'}
1418 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1419 * portion of the {@code name} following the {@code '/'}.
1421 * <li> Otherwise, the absolute name is of the following form:
1424 * {@code modified_package_name/name}
1427 * <p> Where the {@code modified_package_name} is the package name of this
1428 * object with {@code '/'} substituted for {@code '.'}
1429 * (<tt>'\u002e'</tt>).
1433 * @param name name of the desired resource
1434 * @return A {@link java.net.URL} object or {@code null} if no
1435 * resource with this name is found
1438 public java.net.URL getResource(String name) {
1439 name = resolveName(name);
1440 byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
1441 return arr == null ? null : newResourceURL(name, arr);
1444 static URL newResourceURL(String name, byte[] arr) {
1445 return newResourceURL0(URL.class, "res:/" + name, arr);
1448 @JavaScriptBody(args = { "url", "spec", "arr" }, body =
1449 "var u = url.cnstr(true);\n"
1450 + "u.constructor.cons__VLjava_lang_String_2_3B.call(u, spec, arr);\n"
1453 private static native URL newResourceURL0(Class<URL> url, String spec, byte[] arr);
1456 * Add a package name prefix if the name is not absolute Remove leading "/"
1457 * if name is absolute
1459 private String resolveName(String name) {
1463 if (!name.startsWith("/")) {
1465 while (c.isArray()) {
1466 c = c.getComponentType();
1468 String baseName = c.getName();
1469 int index = baseName.lastIndexOf('.');
1471 name = baseName.substring(0, index).replace('.', '/')
1475 name = name.substring(1);
1481 * Returns the class loader for the class. Some implementations may use
1482 * null to represent the bootstrap class loader. This method will return
1483 * null in such implementations if this class was loaded by the bootstrap
1486 * <p> If a security manager is present, and the caller's class loader is
1487 * not null and the caller's class loader is not the same as or an ancestor of
1488 * the class loader for the class whose class loader is requested, then
1489 * this method calls the security manager's {@code checkPermission}
1490 * method with a {@code RuntimePermission("getClassLoader")}
1491 * permission to ensure it's ok to access the class loader for the class.
1494 * represents a primitive type or void, null is returned.
1496 * @return the class loader that loaded the class or interface
1497 * represented by this object.
1498 * @throws SecurityException
1499 * if a security manager exists and its
1500 * {@code checkPermission} method denies
1501 * access to the class loader for the class.
1502 * @see java.lang.ClassLoader
1503 * @see SecurityManager#checkPermission
1504 * @see java.lang.RuntimePermission
1506 public ClassLoader getClassLoader() {
1507 return ClassLoader.getSystemClassLoader();
1511 * Determines the interfaces implemented by the class or interface
1512 * represented by this object.
1514 * <p> If this object represents a class, the return value is an array
1515 * containing objects representing all interfaces implemented by the
1516 * class. The order of the interface objects in the array corresponds to
1517 * the order of the interface names in the {@code implements} clause
1518 * of the declaration of the class represented by this object. For
1519 * example, given the declaration:
1521 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1523 * suppose the value of {@code s} is an instance of
1524 * {@code Shimmer}; the value of the expression:
1526 * {@code s.getClass().getInterfaces()[0]}
1528 * is the {@code Class} object that represents interface
1529 * {@code FloorWax}; and the value of:
1531 * {@code s.getClass().getInterfaces()[1]}
1533 * is the {@code Class} object that represents interface
1534 * {@code DessertTopping}.
1536 * <p> If this object represents an interface, the array contains objects
1537 * representing all interfaces extended by the interface. The order of the
1538 * interface objects in the array corresponds to the order of the interface
1539 * names in the {@code extends} clause of the declaration of the
1540 * interface represented by this object.
1542 * <p> If this object represents a class or interface that implements no
1543 * interfaces, the method returns an array of length 0.
1545 * <p> If this object represents a primitive type or void, the method
1546 * returns an array of length 0.
1548 * @return an array of interfaces implemented by this class.
1550 @JavaScriptBody(args = { }, body = "return this.interfaces();")
1551 public native Class<?>[] getInterfaces();
1554 * Returns the {@code Class} representing the component type of an
1555 * array. If this class does not represent an array class this method
1558 * @return the {@code Class} representing the component type of this
1559 * class if this class is an array
1560 * @see java.lang.reflect.Array
1563 public Class<?> getComponentType() {
1566 Class<?> c = getComponentTypeByFnc();
1567 return c != null ? c : getComponentType0();
1568 } catch (ClassNotFoundException cnfe) {
1569 throw new IllegalStateException(cnfe);
1575 @JavaScriptBody(args = { }, body =
1577 + " var c = this.fnc(false).constructor.$class;\n"
1578 // + " java.lang.System.out.println('will call: ' + (!!this.fnc) + ' res: ' + c.jvmName);\n"
1579 + " if (c) return c;\n"
1583 private native Class<?> getComponentTypeByFnc();
1585 private Class<?> getComponentType0() throws ClassNotFoundException {
1586 String n = getName().substring(1);
1587 switch (n.charAt(0)) {
1589 n = n.substring(1, n.length() - 1);
1590 return Class.forName(n);
1592 return Integer.TYPE;
1602 return Boolean.TYPE;
1608 return Character.TYPE;
1610 return defineArray(n, null);
1612 throw new ClassNotFoundException("Unknown component type of " + getName());
1616 @JavaScriptBody(args = { "sig", "fnc" }, body =
1617 "if (!sig) sig = '[Ljava/lang/Object;';\n" +
1618 "var c = Array[sig];\n" +
1620 " c = vm.java_lang_Class(true);\n" +
1621 " Object.defineProperty(c, 'jvmName', { 'configurable': true, 'writable': true, 'value': sig });\n" +
1622 " Object.defineProperty(c, 'superclass', { 'configurable': true, 'writable': true, 'value' : vm.java_lang_Object(false).$class });\n" +
1623 " Object.defineProperty(c, 'array', { 'configurable': true, 'writable': true, 'value': true });\n" +
1624 " Array[sig] = c;\n" +
1626 "if (!c.fnc) Object.defineProperty(c, 'fnc', { 'configurable': true, 'writable': true, 'value' : fnc });\n" +
1629 private static native Class<?> defineArray(String sig, Object fnc);
1632 * Returns true if and only if this class was declared as an enum in the
1635 * @return true if and only if this class was declared as an enum in the
1639 public boolean isEnum() {
1640 // An enum must both directly extend java.lang.Enum and have
1641 // the ENUM bit set; classes for specialized enum constants
1642 // don't do the former.
1643 return (this.getModifiers() & ENUM) != 0 &&
1644 this.getSuperclass() == java.lang.Enum.class;
1648 * Casts an object to the class or interface represented
1649 * by this {@code Class} object.
1651 * @param obj the object to be cast
1652 * @return the object after casting, or null if obj is null
1654 * @throws ClassCastException if the object is not
1655 * null and is not assignable to the type T.
1659 public T cast(Object obj) {
1660 if (obj != null && !isInstance(obj))
1661 throw new ClassCastException(cannotCastMsg(obj));
1665 private String cannotCastMsg(Object obj) {
1666 return "Cannot cast " + obj.getClass().getName() + " to " + getName();
1670 * Casts this {@code Class} object to represent a subclass of the class
1671 * represented by the specified class object. Checks that that the cast
1672 * is valid, and throws a {@code ClassCastException} if it is not. If
1673 * this method succeeds, it always returns a reference to this class object.
1675 * <p>This method is useful when a client needs to "narrow" the type of
1676 * a {@code Class} object to pass it to an API that restricts the
1677 * {@code Class} objects that it is willing to accept. A cast would
1678 * generate a compile-time warning, as the correctness of the cast
1679 * could not be checked at runtime (because generic types are implemented
1682 * @return this {@code Class} object, cast to represent a subclass of
1683 * the specified class object.
1684 * @throws ClassCastException if this {@code Class} object does not
1685 * represent a subclass of the specified class (here "subclass" includes
1686 * the class itself).
1689 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
1690 if (clazz.isAssignableFrom(this))
1691 return (Class<? extends U>) this;
1693 throw new ClassCastException(this.toString());
1696 @JavaScriptBody(args = { "ac" },
1698 "if (this.anno) {\n"
1699 + " var r = this.anno['L' + ac.jvmName + ';'];\n"
1700 + " if (typeof r === 'undefined') r = null;\n"
1702 + "} else return null;\n"
1704 private Object getAnnotationData(Class<?> annotationClass) {
1705 throw new UnsupportedOperationException();
1708 * @throws NullPointerException {@inheritDoc}
1711 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
1712 Object data = getAnnotationData(annotationClass);
1713 return data == null ? null : AnnotationImpl.create(annotationClass, data);
1717 * @throws NullPointerException {@inheritDoc}
1720 @JavaScriptBody(args = { "ac" },
1721 body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
1722 + "else return false;"
1724 public boolean isAnnotationPresent(
1725 Class<? extends Annotation> annotationClass) {
1726 if (annotationClass == null)
1727 throw new NullPointerException();
1729 return getAnnotation(annotationClass) != null;
1732 @JavaScriptBody(args = {}, body = "return this.anno;")
1733 private Object getAnnotationData() {
1734 throw new UnsupportedOperationException();
1740 public Annotation[] getAnnotations() {
1741 Object data = getAnnotationData();
1742 return data == null ? new Annotation[0] : AnnotationImpl.create(data);
1748 public Annotation[] getDeclaredAnnotations() {
1749 throw new UnsupportedOperationException();
1752 @JavaScriptBody(args = "type", body = ""
1753 + "var c = vm.java_lang_Class(true);"
1754 + "c.jvmName = type;"
1755 + "c.primitive = true;"
1758 native static Class getPrimitiveClass(String type);
1760 @JavaScriptBody(args = {}, body =
1761 "return this['desiredAssertionStatus'] ? this['desiredAssertionStatus'] : false;"
1763 public native boolean desiredAssertionStatus();
1765 public boolean equals(Object obj) {
1766 if (isPrimitive() && obj instanceof Class) {
1767 Class c = ((Class)obj);
1768 return c.isPrimitive() && getName().equals(c.getName());
1770 return super.equals(obj);
1773 static void registerNatives() {
1774 boolean assertsOn = false;
1775 // assert assertsOn = true;
1779 } catch (Throwable ex) {
1785 @JavaScriptBody(args = {}, body = "var p = vm.java_lang_Object(false);"
1786 + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
1788 static native void registerToString();
1790 @JavaScriptBody(args = {"self"}, body
1791 = "var c = self.constructor.$class;\n"
1792 + "return c ? c : null;\n"
1794 static native Class<?> classFor(Object self);
1797 @JavaScriptBody(args = { "self" }, body
1798 = "if (self['$hashCode']) return self['$hashCode'];\n"
1799 + "var h = self['computeHashCode__I'] ? self['computeHashCode__I']() : Math.random() * Math.pow(2, 31);\n"
1800 + "return self['$hashCode'] = h & h;"
1802 static native int defaultHashCode(Object self);
1804 @JavaScriptBody(args = "self", body
1805 = "\nif (!self['$instOf_java_lang_Cloneable']) {"
1808 + "\n var clone = self.constructor(true);"
1809 + "\n var props = Object.getOwnPropertyNames(self);"
1810 + "\n for (var i = 0; i < props.length; i++) {"
1811 + "\n var p = props[i];"
1812 + "\n clone[p] = self[p];"
1814 + "\n return clone;"
1817 static native Object clone(Object self) throws CloneNotSupportedException;
1819 @JavaScriptOnly(name = "toJS", value = "function(v) {\n"
1820 + " if (v === null) return null;\n"
1821 + " if (Object.prototype.toString.call(v) === '[object Array]') {\n"
1822 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).convArray__Ljava_lang_Object_2Ljava_lang_Object_2(v);\n"
1824 + " return v.valueOf();\n"
1827 static native int toJS();
1830 @JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_apidesign_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
1831 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
1834 static native int activate();
1837 @JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_netbeans_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
1838 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
1841 static native int activateNew();
1843 private static Object bck2BrwsrCnvrt(Object o) {
1844 if (o instanceof Throwable) {
1847 final String msg = msg(o);
1848 if (msg == null || msg.startsWith("TypeError")) {
1849 return new NullPointerException(msg);
1851 return new Throwable(msg);
1854 @JavaScriptBody(args = {"o"}, body = "return o ? o.toString() : null;")
1855 private static native String msg(Object o);
1858 @JavaScriptOnly(name = "bck2BrwsrThrwrbl", value = "c.bck2BrwsrCnvrt__Ljava_lang_Object_2Ljava_lang_Object_2")
1859 private static void bck2BrwsrCnvrtVM() {
1863 @JavaScriptOnly(name = "castEx", value = "function() { throw vm.java_lang_ClassCastException(true); }")
1864 private static void castEx() {