Sometimes, when running on nashorn, the Class.forName could return an undefined symbol. Detecting such case and redoing the query seems to return things to normal.
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 final String inJsName = className.replace('.', '_');
169 Class<?> c = loadCls(className, inJsName);
171 c = loadCls(className, inJsName);
174 throw new ClassNotFoundException(className);
177 } catch (Throwable ex) {
178 throw new ClassNotFoundException(className, ex);
184 * Returns the {@code Class} object associated with the class or
185 * interface with the given string name, using the given class loader.
186 * Given the fully qualified name for a class or interface (in the same
187 * format returned by {@code getName}) this method attempts to
188 * locate, load, and link the class or interface. The specified class
189 * loader is used to load the class or interface. If the parameter
190 * {@code loader} is null, the class is loaded through the bootstrap
191 * class loader. The class is initialized only if the
192 * {@code initialize} parameter is {@code true} and if it has
193 * not been initialized earlier.
195 * <p> If {@code name} denotes a primitive type or void, an attempt
196 * will be made to locate a user-defined class in the unnamed package whose
197 * name is {@code name}. Therefore, this method cannot be used to
198 * obtain any of the {@code Class} objects representing primitive
201 * <p> If {@code name} denotes an array class, the component type of
202 * the array class is loaded but not initialized.
204 * <p> For example, in an instance method the expression:
207 * {@code Class.forName("Foo")}
213 * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
216 * Note that this method throws errors related to loading, linking or
217 * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
218 * Java Language Specification</em>.
219 * Note that this method does not check whether the requested class
220 * is accessible to its caller.
222 * <p> If the {@code loader} is {@code null}, and a security
223 * manager is present, and the caller's class loader is not null, then this
224 * method calls the security manager's {@code checkPermission} method
225 * with a {@code RuntimePermission("getClassLoader")} permission to
226 * ensure it's ok to access the bootstrap class loader.
228 * @param name fully qualified name of the desired class
229 * @param initialize whether the class must be initialized
230 * @param loader class loader from which the class must be loaded
231 * @return class object representing the desired class
233 * @exception LinkageError if the linkage fails
234 * @exception ExceptionInInitializerError if the initialization provoked
235 * by this method fails
236 * @exception ClassNotFoundException if the class cannot be located by
237 * the specified class loader
239 * @see java.lang.Class#forName(String)
240 * @see java.lang.ClassLoader
243 public static Class<?> forName(String name, boolean initialize,
245 throws ClassNotFoundException
247 return forName(name);
250 @JavaScriptBody(args = {"n", "c" }, body =
253 + " var l = vm.loadClass ? vm.loadClass : exports ? exports.loadClass : null;\n"
257 + " if (vm[c]) m = vm[c];\n"
258 + " else if (exports[c]) m = exports[c];\n"
260 + "if (!m) return null;"
262 + "return m.$class ? m.$class : null;"
264 private static native Class<?> loadCls(String n, String c);
268 * Creates a new instance of the class represented by this {@code Class}
269 * object. The class is instantiated as if by a {@code new}
270 * expression with an empty argument list. The class is initialized if it
271 * has not already been initialized.
273 * <p>Note that this method propagates any exception thrown by the
274 * nullary constructor, including a checked exception. Use of
275 * this method effectively bypasses the compile-time exception
276 * checking that would otherwise be performed by the compiler.
278 * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
279 * Constructor.newInstance} method avoids this problem by wrapping
280 * any exception thrown by the constructor in a (checked) {@link
281 * java.lang.reflect.InvocationTargetException}.
283 * @return a newly allocated instance of the class represented by this
285 * @exception IllegalAccessException if the class or its nullary
286 * constructor is not accessible.
287 * @exception InstantiationException
288 * if this {@code Class} represents an abstract class,
289 * an interface, an array class, a primitive type, or void;
290 * or if the class has no nullary constructor;
291 * or if the instantiation fails for some other reason.
292 * @exception ExceptionInInitializerError if the initialization
293 * provoked by this method fails.
294 * @exception SecurityException
295 * If a security manager, <i>s</i>, is present and any of the
296 * following conditions is met:
301 * {@link SecurityManager#checkMemberAccess
302 * s.checkMemberAccess(this, Member.PUBLIC)} denies
303 * creation of new instances of this class
305 * <li> the caller's class loader is not the same as or an
306 * ancestor of the class loader for the current class and
307 * invocation of {@link SecurityManager#checkPackageAccess
308 * s.checkPackageAccess()} denies access to the package
314 @JavaScriptBody(args = { "self", "illegal" }, body =
315 "\nvar c = self.cnstr;"
316 + "\nif (c['cons__V']) {"
317 + "\n if ((c['cons__V'].access & 0x1) != 0) {"
318 + "\n var inst = c();"
319 + "\n c['cons__V'].call(inst);"
322 + "\n return illegal;"
326 private static native Object newInstance0(Class<?> self, Object illegal);
328 public T newInstance()
329 throws InstantiationException, IllegalAccessException
331 Object illegal = new Object();
332 Object inst = newInstance0(this, illegal);
334 throw new InstantiationException(getName());
336 if (inst == illegal) {
337 throw new IllegalAccessException();
343 * Determines if the specified {@code Object} is assignment-compatible
344 * with the object represented by this {@code Class}. This method is
345 * the dynamic equivalent of the Java language {@code instanceof}
346 * operator. The method returns {@code true} if the specified
347 * {@code Object} argument is non-null and can be cast to the
348 * reference type represented by this {@code Class} object without
349 * raising a {@code ClassCastException.} It returns {@code false}
352 * <p> Specifically, if this {@code Class} object represents a
353 * declared class, this method returns {@code true} if the specified
354 * {@code Object} argument is an instance of the represented class (or
355 * of any of its subclasses); it returns {@code false} otherwise. If
356 * this {@code Class} object represents an array class, this method
357 * returns {@code true} if the specified {@code Object} argument
358 * can be converted to an object of the array class by an identity
359 * conversion or by a widening reference conversion; it returns
360 * {@code false} otherwise. If this {@code Class} object
361 * represents an interface, this method returns {@code true} if the
362 * class or any superclass of the specified {@code Object} argument
363 * implements this interface; it returns {@code false} otherwise. If
364 * this {@code Class} object represents a primitive type, this method
365 * returns {@code false}.
367 * @param obj the object to check
368 * @return true if {@code obj} is an instance of this class
372 public boolean isInstance(Object obj) {
377 return isAssignableFrom(obj.getClass());
380 String prop = "$instOf_" + getName().replace('.', '_');
381 return hasProperty(obj, prop);
384 @JavaScriptBody(args = { "who", "prop" }, body =
385 "if (who[prop]) return true; else return false;"
387 private static native boolean hasProperty(Object who, String prop);
391 * Determines if the class or interface represented by this
392 * {@code Class} object is either the same as, or is a superclass or
393 * superinterface of, the class or interface represented by the specified
394 * {@code Class} parameter. It returns {@code true} if so;
395 * otherwise it returns {@code false}. If this {@code Class}
396 * object represents a primitive type, this method returns
397 * {@code true} if the specified {@code Class} parameter is
398 * exactly this {@code Class} object; otherwise it returns
401 * <p> Specifically, this method tests whether the type represented by the
402 * specified {@code Class} parameter can be converted to the type
403 * represented by this {@code Class} object via an identity conversion
404 * or via a widening reference conversion. See <em>The Java Language
405 * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
407 * @param cls the {@code Class} object to be checked
408 * @return the {@code boolean} value indicating whether objects of the
409 * type {@code cls} can be assigned to objects of this class
410 * @exception NullPointerException if the specified Class parameter is
414 public boolean isAssignableFrom(Class<?> cls) {
418 if (this == Object.class) {
423 final Class<?> cmpType = cls.getComponentType();
425 return this == cmpType;
427 return cmpType != null && getComponentType().isAssignableFrom(cmpType);
432 if (cls.isPrimitive()) {
438 String prop = "$instOf_" + getName().replace('.', '_');
439 return hasCnstrProperty(cls, prop);
443 @JavaScriptBody(args = { "who", "prop" }, body =
444 "if (who.cnstr.prototype[prop]) return true; else return false;"
446 private static native boolean hasCnstrProperty(Object who, String prop);
450 * Determines if the specified {@code Class} object represents an
453 * @return {@code true} if this object represents an interface;
454 * {@code false} otherwise.
456 public boolean isInterface() {
457 return (getAccess() & 0x200) != 0;
460 @JavaScriptBody(args = {}, body = "return this.access;")
461 private native int getAccess();
465 * Determines if this {@code Class} object represents an array class.
467 * @return {@code true} if this object represents an array class;
468 * {@code false} otherwise.
471 public boolean isArray() {
472 return hasProperty(this, "array"); // NOI18N
477 * Determines if the specified {@code Class} object represents a
480 * <p> There are nine predefined {@code Class} objects to represent
481 * the eight primitive types and void. These are created by the Java
482 * Virtual Machine, and have the same names as the primitive types that
483 * they represent, namely {@code boolean}, {@code byte},
484 * {@code char}, {@code short}, {@code int},
485 * {@code long}, {@code float}, and {@code double}.
487 * <p> These objects may only be accessed via the following public static
488 * final variables, and are the only {@code Class} objects for which
489 * this method returns {@code true}.
491 * @return true if and only if this class represents a primitive type
493 * @see java.lang.Boolean#TYPE
494 * @see java.lang.Character#TYPE
495 * @see java.lang.Byte#TYPE
496 * @see java.lang.Short#TYPE
497 * @see java.lang.Integer#TYPE
498 * @see java.lang.Long#TYPE
499 * @see java.lang.Float#TYPE
500 * @see java.lang.Double#TYPE
501 * @see java.lang.Void#TYPE
504 @JavaScriptBody(args = {}, body =
505 "if (this.primitive) return true;"
506 + "else return false;"
508 public native boolean isPrimitive();
511 * Returns true if this {@code Class} object represents an annotation
512 * type. Note that if this method returns true, {@link #isInterface()}
513 * would also return true, as all annotation types are also interfaces.
515 * @return {@code true} if this class object represents an annotation
516 * type; {@code false} otherwise
519 public boolean isAnnotation() {
520 return (getModifiers() & ANNOTATION) != 0;
524 * Returns {@code true} if this class is a synthetic class;
525 * returns {@code false} otherwise.
526 * @return {@code true} if and only if this class is a synthetic class as
527 * defined by the Java Language Specification.
530 public boolean isSynthetic() {
531 return (getModifiers() & SYNTHETIC) != 0;
535 * Returns the name of the entity (class, interface, array class,
536 * primitive type, or void) represented by this {@code Class} object,
537 * as a {@code String}.
539 * <p> If this class object represents a reference type that is not an
540 * array type then the binary name of the class is returned, as specified
542 * <cite>The Java™ Language Specification</cite>.
544 * <p> If this class object represents a primitive type or void, then the
545 * name returned is a {@code String} equal to the Java language
546 * keyword corresponding to the primitive type or void.
548 * <p> If this class object represents a class of arrays, then the internal
549 * form of the name consists of the name of the element type preceded by
550 * one or more '{@code [}' characters representing the depth of the array
551 * nesting. The encoding of element type names is as follows:
553 * <blockquote><table summary="Element types and encodings">
554 * <tr><th> Element Type <th> <th> Encoding
555 * <tr><td> boolean <td> <td align=center> Z
556 * <tr><td> byte <td> <td align=center> B
557 * <tr><td> char <td> <td align=center> C
558 * <tr><td> class or interface
559 * <td> <td align=center> L<i>classname</i>;
560 * <tr><td> double <td> <td align=center> D
561 * <tr><td> float <td> <td align=center> F
562 * <tr><td> int <td> <td align=center> I
563 * <tr><td> long <td> <td align=center> J
564 * <tr><td> short <td> <td align=center> S
565 * </table></blockquote>
567 * <p> The class or interface name <i>classname</i> is the binary name of
568 * the class specified above.
572 * String.class.getName()
573 * returns "java.lang.String"
574 * byte.class.getName()
576 * (new Object[3]).getClass().getName()
577 * returns "[Ljava.lang.Object;"
578 * (new int[3][4][5][6][7][8][9]).getClass().getName()
580 * </pre></blockquote>
582 * @return the name of the class or interface
583 * represented by this object.
585 public String getName() {
586 return jvmName().replace('/', '.');
589 @JavaScriptBody(args = {}, body = "return this.jvmName;")
590 private native String jvmName();
594 * Returns an array of {@code TypeVariable} objects that represent the
595 * type variables declared by the generic declaration represented by this
596 * {@code GenericDeclaration} object, in declaration order. Returns an
597 * array of length 0 if the underlying generic declaration declares no type
600 * @return an array of {@code TypeVariable} objects that represent
601 * the type variables declared by this generic declaration
602 * @throws java.lang.reflect.GenericSignatureFormatError if the generic
603 * signature of this generic declaration does not conform to
604 * the format specified in
605 * <cite>The Java™ Virtual Machine Specification</cite>
608 public TypeVariable<Class<T>>[] getTypeParameters() {
609 throw new UnsupportedOperationException();
613 * Returns the {@code Class} representing the superclass of the entity
614 * (class, interface, primitive type or void) represented by this
615 * {@code Class}. If this {@code Class} represents either the
616 * {@code Object} class, an interface, a primitive type, or void, then
617 * null is returned. If this object represents an array class then the
618 * {@code Class} object representing the {@code Object} class is
621 * @return the superclass of the class represented by this object.
623 @JavaScriptBody(args = {}, body = "return this.superclass;")
624 public native Class<? super T> getSuperclass();
627 * Returns the Java language modifiers for this class or interface, encoded
628 * in an integer. The modifiers consist of the Java Virtual Machine's
629 * constants for {@code public}, {@code protected},
630 * {@code private}, {@code final}, {@code static},
631 * {@code abstract} and {@code interface}; they should be decoded
632 * using the methods of class {@code Modifier}.
634 * <p> If the underlying class is an array class, then its
635 * {@code public}, {@code private} and {@code protected}
636 * modifiers are the same as those of its component type. If this
637 * {@code Class} represents a primitive type or void, its
638 * {@code public} modifier is always {@code true}, and its
639 * {@code protected} and {@code private} modifiers are always
640 * {@code false}. If this object represents an array class, a
641 * primitive type or void, then its {@code final} modifier is always
642 * {@code true} and its interface modifier is always
643 * {@code false}. The values of its other modifiers are not determined
644 * by this specification.
646 * <p> The modifier encodings are defined in <em>The Java Virtual Machine
647 * Specification</em>, table 4.1.
649 * @return the {@code int} representing the modifiers for this class
650 * @see java.lang.reflect.Modifier
653 public int getModifiers() {
658 * If the class or interface represented by this {@code Class} object
659 * is a member of another class, returns the {@code Class} object
660 * representing the class in which it was declared. This method returns
661 * null if this class or interface is not a member of any other class. If
662 * this {@code Class} object represents an array class, a primitive
663 * type, or void,then this method returns null.
665 * @return the declaring class for this class
668 public Class<?> getDeclaringClass() {
669 throw new SecurityException();
673 * Returns the simple name of the underlying class as given in the
674 * source code. Returns an empty string if the underlying class is
677 * <p>The simple name of an array is the simple name of the
678 * component type with "[]" appended. In particular the simple
679 * name of an array whose component type is anonymous is "[]".
681 * @return the simple name of the underlying class
684 public String getSimpleName() {
686 return getComponentType().getSimpleName()+"[]";
688 String simpleName = getSimpleBinaryName();
689 if (simpleName == null) { // top level class
690 simpleName = getName();
691 return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
693 // According to JLS3 "Binary Compatibility" (13.1) the binary
694 // name of non-package classes (not top level) is the binary
695 // name of the immediately enclosing class followed by a '$' followed by:
696 // (for nested and inner classes): the simple name.
697 // (for local classes): 1 or more digits followed by the simple name.
698 // (for anonymous classes): 1 or more digits.
700 // Since getSimpleBinaryName() will strip the binary name of
701 // the immediatly enclosing class, we are now looking at a
702 // string that matches the regular expression "\$[0-9]*"
703 // followed by a simple name (considering the simple of an
704 // anonymous class to be the empty string).
706 // Remove leading "\$[0-9]*" from the name
707 int length = simpleName.length();
708 if (length < 1 || simpleName.charAt(0) != '$')
709 throw new IllegalStateException("Malformed class name");
711 while (index < length && isAsciiDigit(simpleName.charAt(index)))
713 // Eventually, this is the empty string iff this is an anonymous class
714 return simpleName.substring(index);
718 * Returns the "simple binary name" of the underlying class, i.e.,
719 * the binary name without the leading enclosing class name.
720 * Returns {@code null} if the underlying class is a top level
723 private String getSimpleBinaryName() {
724 Class<?> enclosingClass = null; // XXX getEnclosingClass();
725 if (enclosingClass == null) // top level class
727 // Otherwise, strip the enclosing class' name
729 return getName().substring(enclosingClass.getName().length());
730 } catch (IndexOutOfBoundsException ex) {
731 throw new IllegalStateException("Malformed class name");
736 * Returns an array containing {@code Field} objects reflecting all
737 * the accessible public fields of the class or interface represented by
738 * this {@code Class} object. The elements in the array returned are
739 * not sorted and are not in any particular order. This method returns an
740 * array of length 0 if the class or interface has no accessible public
741 * fields, or if it represents an array class, a primitive type, or void.
743 * <p> Specifically, if this {@code Class} object represents a class,
744 * this method returns the public fields of this class and of all its
745 * superclasses. If this {@code Class} object represents an
746 * interface, this method returns the fields of this interface and of all
747 * its superinterfaces.
749 * <p> The implicit length field for array class is not reflected by this
750 * method. User code should use the methods of class {@code Array} to
753 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
755 * @return the array of {@code Field} objects representing the
757 * @exception SecurityException
758 * If a security manager, <i>s</i>, is present and any of the
759 * following conditions is met:
764 * {@link SecurityManager#checkMemberAccess
765 * s.checkMemberAccess(this, Member.PUBLIC)} denies
766 * access to the fields within this class
768 * <li> the caller's class loader is not the same as or an
769 * ancestor of the class loader for the current class and
770 * invocation of {@link SecurityManager#checkPackageAccess
771 * s.checkPackageAccess()} denies access to the package
778 public Field[] getFields() throws SecurityException {
779 throw new SecurityException();
783 * Returns an array containing {@code Method} objects reflecting all
784 * the public <em>member</em> methods of the class or interface represented
785 * by this {@code Class} object, including those declared by the class
786 * or interface and those inherited from superclasses and
787 * superinterfaces. Array classes return all the (public) member methods
788 * inherited from the {@code Object} class. The elements in the array
789 * returned are not sorted and are not in any particular order. This
790 * method returns an array of length 0 if this {@code Class} object
791 * represents a class or interface that has no public member methods, or if
792 * this {@code Class} object represents a primitive type or void.
794 * <p> The class initialization method {@code <clinit>} is not
795 * included in the returned array. If the class declares multiple public
796 * member methods with the same parameter types, they are all included in
797 * the returned array.
799 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
801 * @return the array of {@code Method} objects representing the
802 * public methods of this class
803 * @exception SecurityException
804 * If a security manager, <i>s</i>, is present and any of the
805 * following conditions is met:
810 * {@link SecurityManager#checkMemberAccess
811 * s.checkMemberAccess(this, Member.PUBLIC)} denies
812 * access to the methods within this class
814 * <li> the caller's class loader is not the same as or an
815 * ancestor of the class loader for the current class and
816 * invocation of {@link SecurityManager#checkPackageAccess
817 * s.checkPackageAccess()} denies access to the package
824 public Method[] getMethods() throws SecurityException {
825 return MethodImpl.findMethods(this, 0x01);
829 * Returns a {@code Field} object that reflects the specified public
830 * member field of the class or interface represented by this
831 * {@code Class} object. The {@code name} parameter is a
832 * {@code String} specifying the simple name of the desired field.
834 * <p> The field to be reflected is determined by the algorithm that
835 * follows. Let C be the class represented by this object:
837 * <LI> If C declares a public field with the name specified, that is the
838 * field to be reflected.</LI>
839 * <LI> If no field was found in step 1 above, this algorithm is applied
840 * recursively to each direct superinterface of C. The direct
841 * superinterfaces are searched in the order they were declared.</LI>
842 * <LI> If no field was found in steps 1 and 2 above, and C has a
843 * superclass S, then this algorithm is invoked recursively upon S.
844 * If C has no superclass, then a {@code NoSuchFieldException}
848 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
850 * @param name the field name
851 * @return the {@code Field} object of this class specified by
853 * @exception NoSuchFieldException if a field with the specified name is
855 * @exception NullPointerException if {@code name} is {@code null}
856 * @exception SecurityException
857 * If a security manager, <i>s</i>, is present and any of the
858 * following conditions is met:
863 * {@link SecurityManager#checkMemberAccess
864 * s.checkMemberAccess(this, Member.PUBLIC)} denies
865 * access to the field
867 * <li> the caller's class loader is not the same as or an
868 * ancestor of the class loader for the current class and
869 * invocation of {@link SecurityManager#checkPackageAccess
870 * s.checkPackageAccess()} denies access to the package
877 public Field getField(String name)
878 throws SecurityException {
879 throw new SecurityException();
884 * Returns a {@code Method} object that reflects the specified public
885 * member method of the class or interface represented by this
886 * {@code Class} object. The {@code name} parameter is a
887 * {@code String} specifying the simple name of the desired method. The
888 * {@code parameterTypes} parameter is an array of {@code Class}
889 * objects that identify the method's formal parameter types, in declared
890 * order. If {@code parameterTypes} is {@code null}, it is
891 * treated as if it were an empty array.
893 * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
894 * {@code NoSuchMethodException} is raised. Otherwise, the method to
895 * be reflected is determined by the algorithm that follows. Let C be the
896 * class represented by this object:
898 * <LI> C is searched for any <I>matching methods</I>. If no matching
899 * method is found, the algorithm of step 1 is invoked recursively on
900 * the superclass of C.</LI>
901 * <LI> If no method was found in step 1 above, the superinterfaces of C
902 * are searched for a matching method. If any such method is found, it
906 * To find a matching method in a class C: If C declares exactly one
907 * public method with the specified name and exactly the same formal
908 * parameter types, that is the method reflected. If more than one such
909 * method is found in C, and one of these methods has a return type that is
910 * more specific than any of the others, that method is reflected;
911 * otherwise one of the methods is chosen arbitrarily.
913 * <p>Note that there may be more than one matching method in a
914 * class because while the Java language forbids a class to
915 * declare multiple methods with the same signature but different
916 * return types, the Java virtual machine does not. This
917 * increased flexibility in the virtual machine can be used to
918 * implement various language features. For example, covariant
919 * returns can be implemented with {@linkplain
920 * java.lang.reflect.Method#isBridge bridge methods}; the bridge
921 * method and the method being overridden would have the same
922 * signature but different return types.
924 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
926 * @param name the name of the method
927 * @param parameterTypes the list of parameters
928 * @return the {@code Method} object that matches the specified
929 * {@code name} and {@code parameterTypes}
930 * @exception NoSuchMethodException if a matching method is not found
931 * or if the name is "<init>"or "<clinit>".
932 * @exception NullPointerException if {@code name} is {@code null}
933 * @exception SecurityException
934 * If a security manager, <i>s</i>, is present and any of the
935 * following conditions is met:
940 * {@link SecurityManager#checkMemberAccess
941 * s.checkMemberAccess(this, Member.PUBLIC)} denies
942 * access to the method
944 * <li> the caller's class loader is not the same as or an
945 * ancestor of the class loader for the current class and
946 * invocation of {@link SecurityManager#checkPackageAccess
947 * s.checkPackageAccess()} denies access to the package
954 public Method getMethod(String name, Class<?>... parameterTypes)
955 throws SecurityException, NoSuchMethodException {
956 Method m = MethodImpl.findMethod(this, name, parameterTypes);
958 StringBuilder sb = new StringBuilder();
959 sb.append(getName()).append('.').append(name).append('(');
961 for (int i = 0; i < parameterTypes.length; i++) {
962 sb.append(sep).append(parameterTypes[i].getName());
966 throw new NoSuchMethodException(sb.toString());
972 * Returns an array of {@code Method} objects reflecting all the
973 * methods declared by the class or interface represented by this
974 * {@code Class} object. This includes public, protected, default
975 * (package) access, and private methods, but excludes inherited methods.
976 * The elements in the array returned are not sorted and are not in any
977 * particular order. This method returns an array of length 0 if the class
978 * or interface declares no methods, or if this {@code Class} object
979 * represents a primitive type, an array class, or void. The class
980 * initialization method {@code <clinit>} is not included in the
981 * returned array. If the class declares multiple public member methods
982 * with the same parameter types, they are all included in the returned
985 * <p> See <em>The Java Language Specification</em>, section 8.2.
987 * @return the array of {@code Method} objects representing all the
988 * declared methods of this class
989 * @exception SecurityException
990 * If a security manager, <i>s</i>, is present and any of the
991 * following conditions is met:
996 * {@link SecurityManager#checkMemberAccess
997 * s.checkMemberAccess(this, Member.DECLARED)} denies
998 * access to the declared methods within this class
1000 * <li> the caller's class loader is not the same as or an
1001 * ancestor of the class loader for the current class and
1002 * invocation of {@link SecurityManager#checkPackageAccess
1003 * s.checkPackageAccess()} denies access to the package
1010 public Method[] getDeclaredMethods() throws SecurityException {
1011 throw new SecurityException();
1015 * Returns an array of {@code Field} objects reflecting all the fields
1016 * declared by the class or interface represented by this
1017 * {@code Class} object. This includes public, protected, default
1018 * (package) access, and private fields, but excludes inherited fields.
1019 * The elements in the array returned are not sorted and are not in any
1020 * particular order. This method returns an array of length 0 if the class
1021 * or interface declares no fields, or if this {@code Class} object
1022 * represents a primitive type, an array class, or void.
1024 * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1026 * @return the array of {@code Field} objects representing all the
1027 * declared fields of this class
1028 * @exception SecurityException
1029 * If a security manager, <i>s</i>, is present and any of the
1030 * following conditions is met:
1034 * <li> invocation of
1035 * {@link SecurityManager#checkMemberAccess
1036 * s.checkMemberAccess(this, Member.DECLARED)} denies
1037 * access to the declared fields within this class
1039 * <li> the caller's class loader is not the same as or an
1040 * ancestor of the class loader for the current class and
1041 * invocation of {@link SecurityManager#checkPackageAccess
1042 * s.checkPackageAccess()} denies access to the package
1049 public Field[] getDeclaredFields() throws SecurityException {
1050 throw new SecurityException();
1054 * <b>Bck2Brwsr</b> emulation can only seek public methods, otherwise it
1055 * throws a {@code SecurityException}.
1057 * Returns a {@code Method} object that reflects the specified
1058 * declared method of the class or interface represented by this
1059 * {@code Class} object. The {@code name} parameter is a
1060 * {@code String} that specifies the simple name of the desired
1061 * method, and the {@code parameterTypes} parameter is an array of
1062 * {@code Class} objects that identify the method's formal parameter
1063 * types, in declared order. If more than one method with the same
1064 * parameter types is declared in a class, and one of these methods has a
1065 * return type that is more specific than any of the others, that method is
1066 * returned; otherwise one of the methods is chosen arbitrarily. If the
1067 * name is "<init>"or "<clinit>" a {@code NoSuchMethodException}
1070 * @param name the name of the method
1071 * @param parameterTypes the parameter array
1072 * @return the {@code Method} object for the method of this class
1073 * matching the specified name and parameters
1074 * @exception NoSuchMethodException if a matching method is not found.
1075 * @exception NullPointerException if {@code name} is {@code null}
1076 * @exception SecurityException
1077 * If a security manager, <i>s</i>, is present and any of the
1078 * following conditions is met:
1082 * <li> invocation of
1083 * {@link SecurityManager#checkMemberAccess
1084 * s.checkMemberAccess(this, Member.DECLARED)} denies
1085 * access to the declared method
1087 * <li> the caller's class loader is not the same as or an
1088 * ancestor of the class loader for the current class and
1089 * invocation of {@link SecurityManager#checkPackageAccess
1090 * s.checkPackageAccess()} denies access to the package
1097 public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
1098 throws NoSuchMethodException, SecurityException {
1100 return getMethod(name, parameterTypes);
1101 } catch (NoSuchMethodException ex) {
1102 throw new SecurityException();
1107 * Returns a {@code Field} object that reflects the specified declared
1108 * field of the class or interface represented by this {@code Class}
1109 * object. The {@code name} parameter is a {@code String} that
1110 * specifies the simple name of the desired field. Note that this method
1111 * will not reflect the {@code length} field of an array class.
1113 * @param name the name of the field
1114 * @return the {@code Field} object for the specified field in this
1116 * @exception NoSuchFieldException if a field with the specified name is
1118 * @exception NullPointerException if {@code name} is {@code null}
1119 * @exception SecurityException
1120 * If a security manager, <i>s</i>, is present and any of the
1121 * following conditions is met:
1125 * <li> invocation of
1126 * {@link SecurityManager#checkMemberAccess
1127 * s.checkMemberAccess(this, Member.DECLARED)} denies
1128 * access to the declared field
1130 * <li> the caller's class loader is not the same as or an
1131 * ancestor of the class loader for the current class and
1132 * invocation of {@link SecurityManager#checkPackageAccess
1133 * s.checkPackageAccess()} denies access to the package
1140 public Field getDeclaredField(String name)
1141 throws SecurityException {
1142 throw new SecurityException();
1146 * Returns an array containing {@code Constructor} objects reflecting
1147 * all the public constructors of the class represented by this
1148 * {@code Class} object. An array of length 0 is returned if the
1149 * class has no public constructors, or if the class is an array class, or
1150 * if the class reflects a primitive type or void.
1152 * Note that while this method returns an array of {@code
1153 * Constructor<T>} objects (that is an array of constructors from
1154 * this class), the return type of this method is {@code
1155 * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
1156 * might be expected. This less informative return type is
1157 * necessary since after being returned from this method, the
1158 * array could be modified to hold {@code Constructor} objects for
1159 * different classes, which would violate the type guarantees of
1160 * {@code Constructor<T>[]}.
1162 * @return the array of {@code Constructor} objects representing the
1163 * public constructors of this class
1164 * @exception SecurityException
1165 * If a security manager, <i>s</i>, is present and any of the
1166 * following conditions is met:
1170 * <li> invocation of
1171 * {@link SecurityManager#checkMemberAccess
1172 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1173 * access to the constructors within this class
1175 * <li> the caller's class loader is not the same as or an
1176 * ancestor of the class loader for the current class and
1177 * invocation of {@link SecurityManager#checkPackageAccess
1178 * s.checkPackageAccess()} denies access to the package
1185 public Constructor<?>[] getConstructors() throws SecurityException {
1186 return MethodImpl.findConstructors(this, 0x01);
1190 * Returns a {@code Constructor} object that reflects the specified
1191 * public constructor of the class represented by this {@code Class}
1192 * object. The {@code parameterTypes} parameter is an array of
1193 * {@code Class} objects that identify the constructor's formal
1194 * parameter types, in declared order.
1196 * If this {@code Class} object represents an inner class
1197 * declared in a non-static context, the formal parameter types
1198 * include the explicit enclosing instance as the first parameter.
1200 * <p> The constructor to reflect is the public constructor of the class
1201 * represented by this {@code Class} object whose formal parameter
1202 * types match those specified by {@code parameterTypes}.
1204 * @param parameterTypes the parameter array
1205 * @return the {@code Constructor} object of the public constructor that
1206 * matches the specified {@code parameterTypes}
1207 * @exception NoSuchMethodException if a matching method is not found.
1208 * @exception SecurityException
1209 * If a security manager, <i>s</i>, is present and any of the
1210 * following conditions is met:
1214 * <li> invocation of
1215 * {@link SecurityManager#checkMemberAccess
1216 * s.checkMemberAccess(this, Member.PUBLIC)} denies
1217 * access to the constructor
1219 * <li> the caller's class loader is not the same as or an
1220 * ancestor of the class loader for the current class and
1221 * invocation of {@link SecurityManager#checkPackageAccess
1222 * s.checkPackageAccess()} denies access to the package
1229 public Constructor<T> getConstructor(Class<?>... parameterTypes)
1230 throws NoSuchMethodException, SecurityException {
1231 Constructor c = MethodImpl.findConstructor(this, parameterTypes);
1233 StringBuilder sb = new StringBuilder();
1234 sb.append(getName()).append('(');
1236 for (int i = 0; i < parameterTypes.length; i++) {
1237 sb.append(sep).append(parameterTypes[i].getName());
1241 throw new NoSuchMethodException(sb.toString());
1247 * Returns an array of {@code Constructor} objects reflecting all the
1248 * constructors declared by the class represented by this
1249 * {@code Class} object. These are public, protected, default
1250 * (package) access, and private constructors. The elements in the array
1251 * returned are not sorted and are not in any particular order. If the
1252 * class has a default constructor, it is included in the returned array.
1253 * This method returns an array of length 0 if this {@code Class}
1254 * object represents an interface, a primitive type, an array class, or
1257 * <p> See <em>The Java Language Specification</em>, section 8.2.
1259 * @return the array of {@code Constructor} objects representing all the
1260 * declared constructors of this class
1261 * @exception SecurityException
1262 * If a security manager, <i>s</i>, is present and any of the
1263 * following conditions is met:
1267 * <li> invocation of
1268 * {@link SecurityManager#checkMemberAccess
1269 * s.checkMemberAccess(this, Member.DECLARED)} denies
1270 * access to the declared constructors within this class
1272 * <li> the caller's class loader is not the same as or an
1273 * ancestor of the class loader for the current class and
1274 * invocation of {@link SecurityManager#checkPackageAccess
1275 * s.checkPackageAccess()} denies access to the package
1282 public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
1283 throw new SecurityException();
1286 * Returns a {@code Constructor} object that reflects the specified
1287 * constructor of the class or interface represented by this
1288 * {@code Class} object. The {@code parameterTypes} parameter is
1289 * an array of {@code Class} objects that identify the constructor's
1290 * formal parameter types, in declared order.
1292 * If this {@code Class} object represents an inner class
1293 * declared in a non-static context, the formal parameter types
1294 * include the explicit enclosing instance as the first parameter.
1296 * @param parameterTypes the parameter array
1297 * @return The {@code Constructor} object for the constructor with the
1298 * specified parameter list
1299 * @exception NoSuchMethodException if a matching method is not found.
1300 * @exception SecurityException
1301 * If a security manager, <i>s</i>, is present and any of the
1302 * following conditions is met:
1306 * <li> invocation of
1307 * {@link SecurityManager#checkMemberAccess
1308 * s.checkMemberAccess(this, Member.DECLARED)} denies
1309 * access to the declared constructor
1311 * <li> the caller's class loader is not the same as or an
1312 * ancestor of the class loader for the current class and
1313 * invocation of {@link SecurityManager#checkPackageAccess
1314 * s.checkPackageAccess()} denies access to the package
1321 public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
1322 throws NoSuchMethodException, SecurityException {
1323 return getConstructor(parameterTypes);
1328 * Character.isDigit answers {@code true} to some non-ascii
1329 * digits. This one does not.
1331 private static boolean isAsciiDigit(char c) {
1332 return '0' <= c && c <= '9';
1336 * Returns the canonical name of the underlying class as
1337 * defined by the Java Language Specification. Returns null if
1338 * the underlying class does not have a canonical name (i.e., if
1339 * it is a local or anonymous class or an array whose component
1340 * type does not have a canonical name).
1341 * @return the canonical name of the underlying class if it exists, and
1342 * {@code null} otherwise.
1345 public String getCanonicalName() {
1347 String canonicalName = getComponentType().getCanonicalName();
1348 if (canonicalName != null)
1349 return canonicalName + "[]";
1353 // if (isLocalOrAnonymousClass())
1355 // Class<?> enclosingClass = getEnclosingClass();
1356 Class<?> enclosingClass = null;
1357 if (enclosingClass == null) { // top level class
1360 String enclosingName = enclosingClass.getCanonicalName();
1361 if (enclosingName == null)
1363 return enclosingName + "." + getSimpleName();
1368 * Finds a resource with a given name. The rules for searching resources
1369 * associated with a given class are implemented by the defining
1370 * {@linkplain ClassLoader class loader} of the class. This method
1371 * delegates to this object's class loader. If this object was loaded by
1372 * the bootstrap class loader, the method delegates to {@link
1373 * ClassLoader#getSystemResourceAsStream}.
1375 * <p> Before delegation, an absolute resource name is constructed from the
1376 * given resource name using this algorithm:
1380 * <li> If the {@code name} begins with a {@code '/'}
1381 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1382 * portion of the {@code name} following the {@code '/'}.
1384 * <li> Otherwise, the absolute name is of the following form:
1387 * {@code modified_package_name/name}
1390 * <p> Where the {@code modified_package_name} is the package name of this
1391 * object with {@code '/'} substituted for {@code '.'}
1392 * (<tt>'\u002e'</tt>).
1396 * @param name name of the desired resource
1397 * @return A {@link java.io.InputStream} object or {@code null} if
1398 * no resource with this name is found
1399 * @throws NullPointerException If {@code name} is {@code null}
1402 public InputStream getResourceAsStream(String name) {
1403 name = resolveName(name);
1404 byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
1405 return arr == null ? null : new ByteArrayInputStream(arr);
1409 * Finds a resource with a given name. The rules for searching resources
1410 * associated with a given class are implemented by the defining
1411 * {@linkplain ClassLoader class loader} of the class. This method
1412 * delegates to this object's class loader. If this object was loaded by
1413 * the bootstrap class loader, the method delegates to {@link
1414 * ClassLoader#getSystemResource}.
1416 * <p> Before delegation, an absolute resource name is constructed from the
1417 * given resource name using this algorithm:
1421 * <li> If the {@code name} begins with a {@code '/'}
1422 * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1423 * portion of the {@code name} following the {@code '/'}.
1425 * <li> Otherwise, the absolute name is of the following form:
1428 * {@code modified_package_name/name}
1431 * <p> Where the {@code modified_package_name} is the package name of this
1432 * object with {@code '/'} substituted for {@code '.'}
1433 * (<tt>'\u002e'</tt>).
1437 * @param name name of the desired resource
1438 * @return A {@link java.net.URL} object or {@code null} if no
1439 * resource with this name is found
1442 public java.net.URL getResource(String name) {
1443 name = resolveName(name);
1444 byte[] arr = ClassLoader.getResourceAsStream0(name, 0);
1445 return arr == null ? null : newResourceURL(name, arr);
1448 static URL newResourceURL(String name, byte[] arr) {
1449 return newResourceURL0(URL.class, "res:/" + name, arr);
1452 @JavaScriptBody(args = { "url", "spec", "arr" }, body =
1453 "var u = url.cnstr(true);\n"
1454 + "u.constructor.cons__VLjava_lang_String_2_3B.call(u, spec, arr);\n"
1457 private static native URL newResourceURL0(Class<URL> url, String spec, byte[] arr);
1460 * Add a package name prefix if the name is not absolute Remove leading "/"
1461 * if name is absolute
1463 private String resolveName(String name) {
1467 if (!name.startsWith("/")) {
1469 while (c.isArray()) {
1470 c = c.getComponentType();
1472 String baseName = c.getName();
1473 int index = baseName.lastIndexOf('.');
1475 name = baseName.substring(0, index).replace('.', '/')
1479 name = name.substring(1);
1485 * Returns the class loader for the class. Some implementations may use
1486 * null to represent the bootstrap class loader. This method will return
1487 * null in such implementations if this class was loaded by the bootstrap
1490 * <p> If a security manager is present, and the caller's class loader is
1491 * not null and the caller's class loader is not the same as or an ancestor of
1492 * the class loader for the class whose class loader is requested, then
1493 * this method calls the security manager's {@code checkPermission}
1494 * method with a {@code RuntimePermission("getClassLoader")}
1495 * permission to ensure it's ok to access the class loader for the class.
1498 * represents a primitive type or void, null is returned.
1500 * @return the class loader that loaded the class or interface
1501 * represented by this object.
1502 * @throws SecurityException
1503 * if a security manager exists and its
1504 * {@code checkPermission} method denies
1505 * access to the class loader for the class.
1506 * @see java.lang.ClassLoader
1507 * @see SecurityManager#checkPermission
1508 * @see java.lang.RuntimePermission
1510 public ClassLoader getClassLoader() {
1511 return ClassLoader.getSystemClassLoader();
1515 * Determines the interfaces implemented by the class or interface
1516 * represented by this object.
1518 * <p> If this object represents a class, the return value is an array
1519 * containing objects representing all interfaces implemented by the
1520 * class. The order of the interface objects in the array corresponds to
1521 * the order of the interface names in the {@code implements} clause
1522 * of the declaration of the class represented by this object. For
1523 * example, given the declaration:
1525 * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1527 * suppose the value of {@code s} is an instance of
1528 * {@code Shimmer}; the value of the expression:
1530 * {@code s.getClass().getInterfaces()[0]}
1532 * is the {@code Class} object that represents interface
1533 * {@code FloorWax}; and the value of:
1535 * {@code s.getClass().getInterfaces()[1]}
1537 * is the {@code Class} object that represents interface
1538 * {@code DessertTopping}.
1540 * <p> If this object represents an interface, the array contains objects
1541 * representing all interfaces extended by the interface. The order of the
1542 * interface objects in the array corresponds to the order of the interface
1543 * names in the {@code extends} clause of the declaration of the
1544 * interface represented by this object.
1546 * <p> If this object represents a class or interface that implements no
1547 * interfaces, the method returns an array of length 0.
1549 * <p> If this object represents a primitive type or void, the method
1550 * returns an array of length 0.
1552 * @return an array of interfaces implemented by this class.
1554 @JavaScriptBody(args = { }, body = "return this.interfaces();")
1555 public native Class<?>[] getInterfaces();
1558 * Returns the {@code Class} representing the component type of an
1559 * array. If this class does not represent an array class this method
1562 * @return the {@code Class} representing the component type of this
1563 * class if this class is an array
1564 * @see java.lang.reflect.Array
1567 public Class<?> getComponentType() {
1570 Class<?> c = getComponentTypeByFnc();
1571 return c != null ? c : getComponentType0();
1572 } catch (ClassNotFoundException cnfe) {
1573 throw new IllegalStateException(cnfe);
1579 @JavaScriptBody(args = { }, body =
1581 + " var c = this.fnc(false).constructor.$class;\n"
1582 // + " java.lang.System.out.println('will call: ' + (!!this.fnc) + ' res: ' + c.jvmName);\n"
1583 + " if (c) return c;\n"
1587 private native Class<?> getComponentTypeByFnc();
1589 private Class<?> getComponentType0() throws ClassNotFoundException {
1590 String n = getName().substring(1);
1591 switch (n.charAt(0)) {
1593 n = n.substring(1, n.length() - 1);
1594 return Class.forName(n);
1596 return Integer.TYPE;
1606 return Boolean.TYPE;
1612 return Character.TYPE;
1614 return defineArray(n, null);
1616 throw new ClassNotFoundException("Unknown component type of " + getName());
1620 @JavaScriptBody(args = { "sig", "fnc" }, body =
1621 "if (!sig) sig = '[Ljava/lang/Object;';\n" +
1622 "var c = Array[sig];\n" +
1624 " c = vm.java_lang_Class(true);\n" +
1625 " Object.defineProperty(c, 'jvmName', { 'configurable': true, 'writable': true, 'value': sig });\n" +
1626 " Object.defineProperty(c, 'superclass', { 'configurable': true, 'writable': true, 'value' : vm.java_lang_Object(false).$class });\n" +
1627 " Object.defineProperty(c, 'array', { 'configurable': true, 'writable': true, 'value': true });\n" +
1628 " Array[sig] = c;\n" +
1630 "if (!c.fnc) Object.defineProperty(c, 'fnc', { 'configurable': true, 'writable': true, 'value' : fnc });\n" +
1633 private static native Class<?> defineArray(String sig, Object fnc);
1636 * Returns true if and only if this class was declared as an enum in the
1639 * @return true if and only if this class was declared as an enum in the
1643 public boolean isEnum() {
1644 // An enum must both directly extend java.lang.Enum and have
1645 // the ENUM bit set; classes for specialized enum constants
1646 // don't do the former.
1647 return (this.getModifiers() & ENUM) != 0 &&
1648 this.getSuperclass() == java.lang.Enum.class;
1652 * Casts an object to the class or interface represented
1653 * by this {@code Class} object.
1655 * @param obj the object to be cast
1656 * @return the object after casting, or null if obj is null
1658 * @throws ClassCastException if the object is not
1659 * null and is not assignable to the type T.
1663 public T cast(Object obj) {
1664 if (obj != null && !isInstance(obj))
1665 throw new ClassCastException(cannotCastMsg(obj));
1669 private String cannotCastMsg(Object obj) {
1670 return "Cannot cast " + obj.getClass().getName() + " to " + getName();
1674 * Casts this {@code Class} object to represent a subclass of the class
1675 * represented by the specified class object. Checks that that the cast
1676 * is valid, and throws a {@code ClassCastException} if it is not. If
1677 * this method succeeds, it always returns a reference to this class object.
1679 * <p>This method is useful when a client needs to "narrow" the type of
1680 * a {@code Class} object to pass it to an API that restricts the
1681 * {@code Class} objects that it is willing to accept. A cast would
1682 * generate a compile-time warning, as the correctness of the cast
1683 * could not be checked at runtime (because generic types are implemented
1686 * @return this {@code Class} object, cast to represent a subclass of
1687 * the specified class object.
1688 * @throws ClassCastException if this {@code Class} object does not
1689 * represent a subclass of the specified class (here "subclass" includes
1690 * the class itself).
1693 public <U> Class<? extends U> asSubclass(Class<U> clazz) {
1694 if (clazz.isAssignableFrom(this))
1695 return (Class<? extends U>) this;
1697 throw new ClassCastException(this.toString());
1700 @JavaScriptBody(args = { "ac" },
1702 "if (this.anno) {\n"
1703 + " var r = this.anno['L' + ac.jvmName + ';'];\n"
1704 + " if (typeof r === 'undefined') r = null;\n"
1706 + "} else return null;\n"
1708 private Object getAnnotationData(Class<?> annotationClass) {
1709 throw new UnsupportedOperationException();
1712 * @throws NullPointerException {@inheritDoc}
1715 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
1716 Object data = getAnnotationData(annotationClass);
1717 return data == null ? null : AnnotationImpl.create(annotationClass, data);
1721 * @throws NullPointerException {@inheritDoc}
1724 @JavaScriptBody(args = { "ac" },
1725 body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
1726 + "else return false;"
1728 public boolean isAnnotationPresent(
1729 Class<? extends Annotation> annotationClass) {
1730 if (annotationClass == null)
1731 throw new NullPointerException();
1733 return getAnnotation(annotationClass) != null;
1736 @JavaScriptBody(args = {}, body = "return this.anno;")
1737 private Object getAnnotationData() {
1738 throw new UnsupportedOperationException();
1744 public Annotation[] getAnnotations() {
1745 Object data = getAnnotationData();
1746 return data == null ? new Annotation[0] : AnnotationImpl.create(data);
1752 public Annotation[] getDeclaredAnnotations() {
1753 throw new UnsupportedOperationException();
1756 @JavaScriptBody(args = "type", body = ""
1757 + "var c = vm.java_lang_Class(true);"
1758 + "c.jvmName = type;"
1759 + "c.primitive = true;"
1762 native static Class getPrimitiveClass(String type);
1764 @JavaScriptBody(args = {}, body =
1765 "return this['desiredAssertionStatus'] ? this['desiredAssertionStatus'] : false;"
1767 public native boolean desiredAssertionStatus();
1769 public boolean equals(Object obj) {
1770 if (isPrimitive() && obj instanceof Class) {
1771 Class c = ((Class)obj);
1772 return c.isPrimitive() && getName().equals(c.getName());
1774 return super.equals(obj);
1777 static void registerNatives() {
1778 boolean assertsOn = false;
1779 // assert assertsOn = true;
1783 } catch (Throwable ex) {
1789 @JavaScriptBody(args = {}, body = "var p = vm.java_lang_Object(false);"
1790 + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };"
1792 static native void registerToString();
1794 @JavaScriptBody(args = {"self"}, body
1795 = "var c = self.constructor.$class;\n"
1796 + "return c ? c : null;\n"
1798 static native Class<?> classFor(Object self);
1801 @JavaScriptBody(args = { "self" }, body
1802 = "if (self['$hashCode']) return self['$hashCode'];\n"
1803 + "var h = self['computeHashCode__I'] ? self['computeHashCode__I']() : Math.random() * Math.pow(2, 31);\n"
1804 + "return self['$hashCode'] = h & h;"
1806 static native int defaultHashCode(Object self);
1808 @JavaScriptBody(args = "self", body
1809 = "\nif (!self['$instOf_java_lang_Cloneable']) {"
1812 + "\n var clone = self.constructor(true);"
1813 + "\n var props = Object.getOwnPropertyNames(self);"
1814 + "\n for (var i = 0; i < props.length; i++) {"
1815 + "\n var p = props[i];"
1816 + "\n clone[p] = self[p];"
1818 + "\n return clone;"
1821 static native Object clone(Object self) throws CloneNotSupportedException;
1823 @JavaScriptOnly(name = "toJS", value = "function(v) {\n"
1824 + " if (v === null) return null;\n"
1825 + " if (Object.prototype.toString.call(v) === '[object Array]') {\n"
1826 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).convArray__Ljava_lang_Object_2Ljava_lang_Object_2(v);\n"
1828 + " return v.valueOf();\n"
1831 static native int toJS();
1834 @JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_apidesign_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
1835 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
1838 static native int activate();
1841 @JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_netbeans_html_boot_spi_Fn$Presenter_2", value = "function() {\n"
1842 + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();"
1845 static native int activateNew();
1847 private static Object bck2BrwsrCnvrt(Object o) {
1848 if (o instanceof Throwable) {
1851 final String msg = msg(o);
1852 if (msg == null || msg.startsWith("TypeError")) {
1853 return new NullPointerException(msg);
1855 return new Throwable(msg);
1858 @JavaScriptBody(args = {"o"}, body = "return o ? o.toString() : null;")
1859 private static native String msg(Object o);
1862 @JavaScriptOnly(name = "bck2BrwsrThrwrbl", value = "c.bck2BrwsrCnvrt__Ljava_lang_Object_2Ljava_lang_Object_2")
1863 private static void bck2BrwsrCnvrtVM() {
1867 @JavaScriptOnly(name = "castEx", value = "function() { throw vm.java_lang_ClassCastException(true); }")
1868 private static void castEx() {