jaroslav@56: /* jaroslav@56: * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@56: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@56: * jaroslav@56: * This code is free software; you can redistribute it and/or modify it jaroslav@56: * under the terms of the GNU General Public License version 2 only, as jaroslav@56: * published by the Free Software Foundation. Oracle designates this jaroslav@56: * particular file as subject to the "Classpath" exception as provided jaroslav@56: * by Oracle in the LICENSE file that accompanied this code. jaroslav@56: * jaroslav@56: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@56: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@56: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@56: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@56: * accompanied this code). jaroslav@56: * jaroslav@56: * You should have received a copy of the GNU General Public License version jaroslav@56: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@56: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@56: * jaroslav@56: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@56: * or visit www.oracle.com if you need additional information or have any jaroslav@56: * questions. jaroslav@56: */ jaroslav@56: jaroslav@56: package java.lang; jaroslav@56: jaroslav@424: import java.io.ByteArrayInputStream; jaroslav@122: import java.io.InputStream; jaroslav@56: import java.lang.annotation.Annotation; jtulach@1483: import java.lang.reflect.Array; jaroslav@1321: import java.lang.reflect.Constructor; jaroslav@261: import java.lang.reflect.Field; jaroslav@261: import java.lang.reflect.Method; jaroslav@260: import java.lang.reflect.TypeVariable; jaroslav@576: import java.net.URL; jaroslav@1586: import org.apidesign.bck2brwsr.core.Exported; jaroslav@225: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@1515: import org.apidesign.bck2brwsr.core.JavaScriptOnly; jtulach@1483: import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl; jaroslav@555: import org.apidesign.bck2brwsr.emul.reflect.MethodImpl; jaroslav@56: jaroslav@56: /** jaroslav@56: * Instances of the class {@code Class} represent classes and jaroslav@56: * interfaces in a running Java application. An enum is a kind of jaroslav@56: * class and an annotation is a kind of interface. Every array also jaroslav@56: * belongs to a class that is reflected as a {@code Class} object jaroslav@56: * that is shared by all arrays with the same element type and number jaroslav@56: * of dimensions. The primitive Java types ({@code boolean}, jaroslav@56: * {@code byte}, {@code char}, {@code short}, jaroslav@56: * {@code int}, {@code long}, {@code float}, and jaroslav@56: * {@code double}), and the keyword {@code void} are also jaroslav@56: * represented as {@code Class} objects. jaroslav@56: * jaroslav@56: *

{@code Class} has no public constructor. Instead {@code Class} jaroslav@56: * objects are constructed automatically by the Java Virtual Machine as classes jaroslav@56: * are loaded and by calls to the {@code defineClass} method in the class jaroslav@56: * loader. jaroslav@56: * jaroslav@56: *

The following example uses a {@code Class} object to print the jaroslav@56: * class name of an object: jaroslav@56: * jaroslav@56: *

jaroslav@56:  *     void printClassName(Object obj) {
jaroslav@56:  *         System.out.println("The class of " + obj +
jaroslav@56:  *                            " is " + obj.getClass().getName());
jaroslav@56:  *     }
jaroslav@56:  * 
jaroslav@56: * jaroslav@56: *

It is also possible to get the {@code Class} object for a named jaroslav@56: * type (or for void) using a class literal. See Section 15.8.2 of jaroslav@56: * The Java™ Language Specification. jaroslav@56: * For example: jaroslav@56: * jaroslav@56: *

jaroslav@56: * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} jaroslav@56: *
jaroslav@56: * jaroslav@56: * @param the type of the class modeled by this {@code Class} jaroslav@56: * object. For example, the type of {@code String.class} is {@code jaroslav@56: * Class}. Use {@code Class} if the class being modeled is jaroslav@56: * unknown. jaroslav@56: * jaroslav@56: * @author unascribed jaroslav@56: * @see java.lang.ClassLoader#defineClass(byte[], int, int) jaroslav@56: * @since JDK1.0 jaroslav@56: */ jaroslav@56: public final jaroslav@260: class Class implements java.io.Serializable, jaroslav@260: java.lang.reflect.GenericDeclaration, jaroslav@260: java.lang.reflect.Type, jaroslav@260: java.lang.reflect.AnnotatedElement { jaroslav@56: private static final int ANNOTATION= 0x00002000; jaroslav@56: private static final int ENUM = 0x00004000; jaroslav@56: private static final int SYNTHETIC = 0x00001000; jaroslav@56: jaroslav@56: /* jaroslav@56: * Constructor. Only the Java Virtual Machine creates Class jaroslav@56: * objects. jaroslav@56: */ jaroslav@56: private Class() {} jaroslav@56: jaroslav@56: jaroslav@56: /** jaroslav@56: * Converts the object to a string. The string representation is the jaroslav@56: * string "class" or "interface", followed by a space, and then by the jaroslav@56: * fully qualified name of the class in the format returned by jaroslav@56: * {@code getName}. If this {@code Class} object represents a jaroslav@56: * primitive type, this method returns the name of the primitive type. If jaroslav@56: * this {@code Class} object represents void this method returns jaroslav@56: * "void". jaroslav@56: * jaroslav@56: * @return a string representation of this class object. jaroslav@56: */ jaroslav@56: public String toString() { jaroslav@56: return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) jaroslav@56: + getName(); jaroslav@56: } jaroslav@56: jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the {@code Class} object associated with the class or jaroslav@56: * interface with the given string name. Invoking this method is jaroslav@56: * equivalent to: jaroslav@56: * jaroslav@56: *
jaroslav@56: * {@code Class.forName(className, true, currentLoader)} jaroslav@56: *
jaroslav@56: * jaroslav@56: * where {@code currentLoader} denotes the defining class loader of jaroslav@56: * the current class. jaroslav@56: * jaroslav@56: *

For example, the following code fragment returns the jaroslav@56: * runtime {@code Class} descriptor for the class named jaroslav@56: * {@code java.lang.Thread}: jaroslav@56: * jaroslav@56: *

jaroslav@56: * {@code Class t = Class.forName("java.lang.Thread")} jaroslav@56: *
jaroslav@56: *

jaroslav@56: * A call to {@code forName("X")} causes the class named jaroslav@56: * {@code X} to be initialized. jaroslav@56: * jaroslav@56: * @param className the fully qualified name of the desired class. jaroslav@56: * @return the {@code Class} object for the class with the jaroslav@56: * specified name. jaroslav@56: * @exception LinkageError if the linkage fails jaroslav@56: * @exception ExceptionInInitializerError if the initialization provoked jaroslav@56: * by this method fails jaroslav@56: * @exception ClassNotFoundException if the class cannot be located jaroslav@56: */ jaroslav@56: public static Class forName(String className) jaroslav@450: throws ClassNotFoundException { jaroslav@450: if (className.startsWith("[")) { jaroslav@1532: Class arrType = defineArray(className, null); jaroslav@452: Class c = arrType; jaroslav@452: while (c != null && c.isArray()) { jaroslav@452: c = c.getComponentType0(); // verify component type is sane jaroslav@452: } jaroslav@452: return arrType; jaroslav@450: } jaroslav@1252: try { jaroslav@1252: Class c = loadCls(className, className.replace('.', '_')); jaroslav@1252: if (c == null) { jaroslav@1252: throw new ClassNotFoundException(className); jaroslav@1252: } jaroslav@1252: return c; jaroslav@1252: } catch (Throwable ex) { jaroslav@1252: throw new ClassNotFoundException(className, ex); jaroslav@321: } jaroslav@56: } jaroslav@562: jaroslav@562: jaroslav@562: /** jaroslav@562: * Returns the {@code Class} object associated with the class or jaroslav@562: * interface with the given string name, using the given class loader. jaroslav@562: * Given the fully qualified name for a class or interface (in the same jaroslav@562: * format returned by {@code getName}) this method attempts to jaroslav@562: * locate, load, and link the class or interface. The specified class jaroslav@562: * loader is used to load the class or interface. If the parameter jaroslav@562: * {@code loader} is null, the class is loaded through the bootstrap jaroslav@562: * class loader. The class is initialized only if the jaroslav@562: * {@code initialize} parameter is {@code true} and if it has jaroslav@562: * not been initialized earlier. jaroslav@562: * jaroslav@562: *

If {@code name} denotes a primitive type or void, an attempt jaroslav@562: * will be made to locate a user-defined class in the unnamed package whose jaroslav@562: * name is {@code name}. Therefore, this method cannot be used to jaroslav@562: * obtain any of the {@code Class} objects representing primitive jaroslav@562: * types or void. jaroslav@562: * jaroslav@562: *

If {@code name} denotes an array class, the component type of jaroslav@562: * the array class is loaded but not initialized. jaroslav@562: * jaroslav@562: *

For example, in an instance method the expression: jaroslav@562: * jaroslav@562: *

jaroslav@562: * {@code Class.forName("Foo")} jaroslav@562: *
jaroslav@562: * jaroslav@562: * is equivalent to: jaroslav@562: * jaroslav@562: *
jaroslav@562: * {@code Class.forName("Foo", true, this.getClass().getClassLoader())} jaroslav@562: *
jaroslav@562: * jaroslav@562: * Note that this method throws errors related to loading, linking or jaroslav@562: * initializing as specified in Sections 12.2, 12.3 and 12.4 of The jaroslav@562: * Java Language Specification. jaroslav@562: * Note that this method does not check whether the requested class jaroslav@562: * is accessible to its caller. jaroslav@562: * jaroslav@562: *

If the {@code loader} is {@code null}, and a security jaroslav@562: * manager is present, and the caller's class loader is not null, then this jaroslav@562: * method calls the security manager's {@code checkPermission} method jaroslav@562: * with a {@code RuntimePermission("getClassLoader")} permission to jaroslav@562: * ensure it's ok to access the bootstrap class loader. jaroslav@562: * jaroslav@562: * @param name fully qualified name of the desired class jaroslav@562: * @param initialize whether the class must be initialized jaroslav@562: * @param loader class loader from which the class must be loaded jaroslav@562: * @return class object representing the desired class jaroslav@562: * jaroslav@562: * @exception LinkageError if the linkage fails jaroslav@562: * @exception ExceptionInInitializerError if the initialization provoked jaroslav@562: * by this method fails jaroslav@562: * @exception ClassNotFoundException if the class cannot be located by jaroslav@562: * the specified class loader jaroslav@562: * jaroslav@562: * @see java.lang.Class#forName(String) jaroslav@562: * @see java.lang.ClassLoader jaroslav@562: * @since 1.2 jaroslav@562: */ jaroslav@562: public static Class forName(String name, boolean initialize, jaroslav@562: ClassLoader loader) jaroslav@562: throws ClassNotFoundException jaroslav@562: { jaroslav@562: return forName(name); jaroslav@562: } jaroslav@321: jaroslav@322: @JavaScriptBody(args = {"n", "c" }, body = jaroslav@1585: "var m = vm[c];\n" jaroslav@1585: + "if (!m) {\n" jaroslav@1585: + " var l = vm.loadClass ? vm.loadClass : exports ? exports.loadClass : null;\n" jaroslav@1585: + " if (l) {\n" jaroslav@1585: + " l(n);\n" jaroslav@658: + " }\n" jaroslav@1585: + " if (vm[c]) m = vm[c];\n" jaroslav@1585: + " else if (exports[c]) m = exports[c];\n" jaroslav@322: + "}\n" jaroslav@1585: + "if (!m) return null;" jaroslav@1585: + "m(false);" jaroslav@1585: + "return m.$class;" jaroslav@321: ) jaroslav@322: private static native Class loadCls(String n, String c); jaroslav@56: jaroslav@56: jaroslav@56: /** jaroslav@56: * Creates a new instance of the class represented by this {@code Class} jaroslav@56: * object. The class is instantiated as if by a {@code new} jaroslav@56: * expression with an empty argument list. The class is initialized if it jaroslav@56: * has not already been initialized. jaroslav@56: * jaroslav@56: *

Note that this method propagates any exception thrown by the jaroslav@56: * nullary constructor, including a checked exception. Use of jaroslav@56: * this method effectively bypasses the compile-time exception jaroslav@56: * checking that would otherwise be performed by the compiler. jaroslav@56: * The {@link jaroslav@56: * java.lang.reflect.Constructor#newInstance(java.lang.Object...) jaroslav@56: * Constructor.newInstance} method avoids this problem by wrapping jaroslav@56: * any exception thrown by the constructor in a (checked) {@link jaroslav@56: * java.lang.reflect.InvocationTargetException}. jaroslav@56: * jaroslav@56: * @return a newly allocated instance of the class represented by this jaroslav@56: * object. jaroslav@56: * @exception IllegalAccessException if the class or its nullary jaroslav@56: * constructor is not accessible. jaroslav@56: * @exception InstantiationException jaroslav@56: * if this {@code Class} represents an abstract class, jaroslav@56: * an interface, an array class, a primitive type, or void; jaroslav@56: * or if the class has no nullary constructor; jaroslav@56: * or if the instantiation fails for some other reason. jaroslav@56: * @exception ExceptionInInitializerError if the initialization jaroslav@56: * provoked by this method fails. jaroslav@56: * @exception SecurityException jaroslav@56: * If a security manager, s, is present and any of the jaroslav@56: * following conditions is met: jaroslav@56: * jaroslav@56: *

    jaroslav@56: * jaroslav@56: *
  • invocation of jaroslav@56: * {@link SecurityManager#checkMemberAccess jaroslav@56: * s.checkMemberAccess(this, Member.PUBLIC)} denies jaroslav@56: * creation of new instances of this class jaroslav@56: * jaroslav@56: *
  • the caller's class loader is not the same as or an jaroslav@56: * ancestor of the class loader for the current class and jaroslav@56: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@56: * s.checkPackageAccess()} denies access to the package jaroslav@56: * of this class jaroslav@56: * jaroslav@56: *
jaroslav@56: * jaroslav@56: */ jaroslav@397: @JavaScriptBody(args = { "self", "illegal" }, body = jaroslav@397: "\nvar c = self.cnstr;" jaroslav@397: + "\nif (c['cons__V']) {" jaroslav@1612: + "\n if ((c['cons__V'].access & 0x1) != 0) {" jaroslav@397: + "\n var inst = c();" jaroslav@1612: + "\n c['cons__V'].call(inst);" jaroslav@397: + "\n return inst;" jaroslav@397: + "\n }" jaroslav@397: + "\n return illegal;" jaroslav@397: + "\n}" jaroslav@397: + "\nreturn null;" jaroslav@231: ) jaroslav@397: private static native Object newInstance0(Class self, Object illegal); jaroslav@397: jaroslav@56: public T newInstance() jaroslav@56: throws InstantiationException, IllegalAccessException jaroslav@56: { jaroslav@397: Object illegal = new Object(); jaroslav@397: Object inst = newInstance0(this, illegal); jaroslav@397: if (inst == null) { jaroslav@397: throw new InstantiationException(getName()); jaroslav@397: } jaroslav@397: if (inst == illegal) { jaroslav@397: throw new IllegalAccessException(); jaroslav@397: } jaroslav@397: return (T)inst; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Determines if the specified {@code Object} is assignment-compatible jaroslav@56: * with the object represented by this {@code Class}. This method is jaroslav@56: * the dynamic equivalent of the Java language {@code instanceof} jaroslav@56: * operator. The method returns {@code true} if the specified jaroslav@56: * {@code Object} argument is non-null and can be cast to the jaroslav@56: * reference type represented by this {@code Class} object without jaroslav@56: * raising a {@code ClassCastException.} It returns {@code false} jaroslav@56: * otherwise. jaroslav@56: * jaroslav@56: *

Specifically, if this {@code Class} object represents a jaroslav@56: * declared class, this method returns {@code true} if the specified jaroslav@56: * {@code Object} argument is an instance of the represented class (or jaroslav@56: * of any of its subclasses); it returns {@code false} otherwise. If jaroslav@56: * this {@code Class} object represents an array class, this method jaroslav@56: * returns {@code true} if the specified {@code Object} argument jaroslav@56: * can be converted to an object of the array class by an identity jaroslav@56: * conversion or by a widening reference conversion; it returns jaroslav@56: * {@code false} otherwise. If this {@code Class} object jaroslav@56: * represents an interface, this method returns {@code true} if the jaroslav@56: * class or any superclass of the specified {@code Object} argument jaroslav@56: * implements this interface; it returns {@code false} otherwise. If jaroslav@56: * this {@code Class} object represents a primitive type, this method jaroslav@56: * returns {@code false}. jaroslav@56: * jaroslav@56: * @param obj the object to check jaroslav@56: * @return true if {@code obj} is an instance of this class jaroslav@56: * jaroslav@56: * @since JDK1.1 jaroslav@56: */ jaroslav@434: public boolean isInstance(Object obj) { jaroslav@643: if (obj == null) { jaroslav@643: return false; jaroslav@643: } jaroslav@571: if (isArray()) { jaroslav@571: return isAssignableFrom(obj.getClass()); jaroslav@571: } jaroslav@571: jaroslav@434: String prop = "$instOf_" + getName().replace('.', '_'); jaroslav@434: return hasProperty(obj, prop); jaroslav@434: } jaroslav@434: jaroslav@434: @JavaScriptBody(args = { "who", "prop" }, body = jaroslav@434: "if (who[prop]) return true; else return false;" jaroslav@434: ) jaroslav@434: private static native boolean hasProperty(Object who, String prop); jaroslav@56: jaroslav@56: jaroslav@56: /** jaroslav@56: * Determines if the class or interface represented by this jaroslav@56: * {@code Class} object is either the same as, or is a superclass or jaroslav@56: * superinterface of, the class or interface represented by the specified jaroslav@56: * {@code Class} parameter. It returns {@code true} if so; jaroslav@56: * otherwise it returns {@code false}. If this {@code Class} jaroslav@56: * object represents a primitive type, this method returns jaroslav@56: * {@code true} if the specified {@code Class} parameter is jaroslav@56: * exactly this {@code Class} object; otherwise it returns jaroslav@56: * {@code false}. jaroslav@56: * jaroslav@56: *

Specifically, this method tests whether the type represented by the jaroslav@56: * specified {@code Class} parameter can be converted to the type jaroslav@56: * represented by this {@code Class} object via an identity conversion jaroslav@56: * or via a widening reference conversion. See The Java Language jaroslav@56: * Specification, sections 5.1.1 and 5.1.4 , for details. jaroslav@56: * jaroslav@56: * @param cls the {@code Class} object to be checked jaroslav@56: * @return the {@code boolean} value indicating whether objects of the jaroslav@56: * type {@code cls} can be assigned to objects of this class jaroslav@56: * @exception NullPointerException if the specified Class parameter is jaroslav@56: * null. jaroslav@56: * @since JDK1.1 jaroslav@56: */ jaroslav@571: public boolean isAssignableFrom(Class cls) { jaroslav@571: if (this == cls) { jaroslav@571: return true; jaroslav@571: } jaroslav@571: jaroslav@571: if (isArray()) { jaroslav@571: final Class cmpType = cls.getComponentType(); jaroslav@571: if (isPrimitive()) { jaroslav@571: return this == cmpType; jaroslav@571: } jaroslav@571: return cmpType != null && getComponentType().isAssignableFrom(cmpType); jaroslav@571: } jaroslav@886: if (isPrimitive()) { jaroslav@886: return false; jaroslav@886: } else { jaroslav@886: if (cls.isPrimitive()) { jaroslav@886: return false; jaroslav@886: } jaroslav@886: String prop = "$instOf_" + getName().replace('.', '_'); jaroslav@886: return hasCnstrProperty(cls, prop); jaroslav@886: } jaroslav@571: } jaroslav@56: jaroslav@733: @JavaScriptBody(args = { "who", "prop" }, body = jaroslav@733: "if (who.cnstr.prototype[prop]) return true; else return false;" jaroslav@733: ) jaroslav@733: private static native boolean hasCnstrProperty(Object who, String prop); jaroslav@733: jaroslav@733: jaroslav@56: /** jaroslav@56: * Determines if the specified {@code Class} object represents an jaroslav@56: * interface type. jaroslav@56: * jaroslav@56: * @return {@code true} if this object represents an interface; jaroslav@56: * {@code false} otherwise. jaroslav@56: */ jaroslav@355: public boolean isInterface() { jaroslav@355: return (getAccess() & 0x200) != 0; jaroslav@355: } jaroslav@355: jaroslav@443: @JavaScriptBody(args = {}, body = "return this.access;") jaroslav@355: private native int getAccess(); jaroslav@56: jaroslav@56: jaroslav@56: /** jaroslav@56: * Determines if this {@code Class} object represents an array class. jaroslav@56: * jaroslav@56: * @return {@code true} if this object represents an array class; jaroslav@56: * {@code false} otherwise. jaroslav@56: * @since JDK1.1 jaroslav@56: */ jaroslav@228: public boolean isArray() { jaroslav@448: return hasProperty(this, "array"); // NOI18N jaroslav@228: } jaroslav@56: jaroslav@56: jaroslav@56: /** jaroslav@56: * Determines if the specified {@code Class} object represents a jaroslav@56: * primitive type. jaroslav@56: * jaroslav@56: *

There are nine predefined {@code Class} objects to represent jaroslav@56: * the eight primitive types and void. These are created by the Java jaroslav@56: * Virtual Machine, and have the same names as the primitive types that jaroslav@56: * they represent, namely {@code boolean}, {@code byte}, jaroslav@56: * {@code char}, {@code short}, {@code int}, jaroslav@56: * {@code long}, {@code float}, and {@code double}. jaroslav@56: * jaroslav@56: *

These objects may only be accessed via the following public static jaroslav@56: * final variables, and are the only {@code Class} objects for which jaroslav@56: * this method returns {@code true}. jaroslav@56: * jaroslav@56: * @return true if and only if this class represents a primitive type jaroslav@56: * jaroslav@56: * @see java.lang.Boolean#TYPE jaroslav@56: * @see java.lang.Character#TYPE jaroslav@56: * @see java.lang.Byte#TYPE jaroslav@56: * @see java.lang.Short#TYPE jaroslav@56: * @see java.lang.Integer#TYPE jaroslav@56: * @see java.lang.Long#TYPE jaroslav@56: * @see java.lang.Float#TYPE jaroslav@56: * @see java.lang.Double#TYPE jaroslav@56: * @see java.lang.Void#TYPE jaroslav@56: * @since JDK1.1 jaroslav@56: */ jaroslav@443: @JavaScriptBody(args = {}, body = jaroslav@443: "if (this.primitive) return true;" jaroslav@354: + "else return false;" jaroslav@354: ) jaroslav@56: public native boolean isPrimitive(); jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns true if this {@code Class} object represents an annotation jaroslav@56: * type. Note that if this method returns true, {@link #isInterface()} jaroslav@56: * would also return true, as all annotation types are also interfaces. jaroslav@56: * jaroslav@56: * @return {@code true} if this class object represents an annotation jaroslav@56: * type; {@code false} otherwise jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public boolean isAnnotation() { jaroslav@56: return (getModifiers() & ANNOTATION) != 0; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns {@code true} if this class is a synthetic class; jaroslav@56: * returns {@code false} otherwise. jaroslav@56: * @return {@code true} if and only if this class is a synthetic class as jaroslav@56: * defined by the Java Language Specification. jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public boolean isSynthetic() { jaroslav@56: return (getModifiers() & SYNTHETIC) != 0; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the name of the entity (class, interface, array class, jaroslav@56: * primitive type, or void) represented by this {@code Class} object, jaroslav@56: * as a {@code String}. jaroslav@56: * jaroslav@56: *

If this class object represents a reference type that is not an jaroslav@56: * array type then the binary name of the class is returned, as specified jaroslav@56: * by jaroslav@56: * The Java™ Language Specification. jaroslav@56: * jaroslav@56: *

If this class object represents a primitive type or void, then the jaroslav@56: * name returned is a {@code String} equal to the Java language jaroslav@56: * keyword corresponding to the primitive type or void. jaroslav@56: * jaroslav@56: *

If this class object represents a class of arrays, then the internal jaroslav@56: * form of the name consists of the name of the element type preceded by jaroslav@56: * one or more '{@code [}' characters representing the depth of the array jaroslav@56: * nesting. The encoding of element type names is as follows: jaroslav@56: * jaroslav@56: *

jaroslav@56: *
Element Type     Encoding jaroslav@56: *
boolean     Z jaroslav@56: *
byte     B jaroslav@56: *
char     C jaroslav@56: *
class or interface jaroslav@56: *     Lclassname; jaroslav@56: *
double     D jaroslav@56: *
float     F jaroslav@56: *
int     I jaroslav@56: *
long     J jaroslav@56: *
short     S jaroslav@56: *
jaroslav@56: * jaroslav@56: *

The class or interface name classname is the binary name of jaroslav@56: * the class specified above. jaroslav@56: * jaroslav@56: *

Examples: jaroslav@56: *

jaroslav@56:      * String.class.getName()
jaroslav@56:      *     returns "java.lang.String"
jaroslav@56:      * byte.class.getName()
jaroslav@56:      *     returns "byte"
jaroslav@56:      * (new Object[3]).getClass().getName()
jaroslav@56:      *     returns "[Ljava.lang.Object;"
jaroslav@56:      * (new int[3][4][5][6][7][8][9]).getClass().getName()
jaroslav@56:      *     returns "[[[[[[[I"
jaroslav@56:      * 
jaroslav@56: * jaroslav@56: * @return the name of the class or interface jaroslav@56: * represented by this object. jaroslav@56: */ jaroslav@56: public String getName() { jaroslav@225: return jvmName().replace('/', '.'); jaroslav@56: } jaroslav@56: jaroslav@443: @JavaScriptBody(args = {}, body = "return this.jvmName;") jaroslav@225: private native String jvmName(); jaroslav@225: jaroslav@260: jaroslav@260: /** jaroslav@260: * Returns an array of {@code TypeVariable} objects that represent the jaroslav@260: * type variables declared by the generic declaration represented by this jaroslav@260: * {@code GenericDeclaration} object, in declaration order. Returns an jaroslav@260: * array of length 0 if the underlying generic declaration declares no type jaroslav@260: * variables. jaroslav@260: * jaroslav@260: * @return an array of {@code TypeVariable} objects that represent jaroslav@260: * the type variables declared by this generic declaration jaroslav@260: * @throws java.lang.reflect.GenericSignatureFormatError if the generic jaroslav@260: * signature of this generic declaration does not conform to jaroslav@260: * the format specified in jaroslav@260: * The Java™ Virtual Machine Specification jaroslav@260: * @since 1.5 jaroslav@260: */ jaroslav@260: public TypeVariable>[] getTypeParameters() { jaroslav@260: throw new UnsupportedOperationException(); jaroslav@260: } jaroslav@260: jaroslav@56: /** jaroslav@56: * Returns the {@code Class} representing the superclass of the entity jaroslav@56: * (class, interface, primitive type or void) represented by this jaroslav@56: * {@code Class}. If this {@code Class} represents either the jaroslav@56: * {@code Object} class, an interface, a primitive type, or void, then jaroslav@56: * null is returned. If this object represents an array class then the jaroslav@56: * {@code Class} object representing the {@code Object} class is jaroslav@56: * returned. jaroslav@56: * jaroslav@56: * @return the superclass of the class represented by this object. jaroslav@56: */ jaroslav@443: @JavaScriptBody(args = {}, body = "return this.superclass;") jaroslav@56: public native Class getSuperclass(); jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the Java language modifiers for this class or interface, encoded jaroslav@56: * in an integer. The modifiers consist of the Java Virtual Machine's jaroslav@56: * constants for {@code public}, {@code protected}, jaroslav@56: * {@code private}, {@code final}, {@code static}, jaroslav@56: * {@code abstract} and {@code interface}; they should be decoded jaroslav@56: * using the methods of class {@code Modifier}. jaroslav@56: * jaroslav@56: *

If the underlying class is an array class, then its jaroslav@56: * {@code public}, {@code private} and {@code protected} jaroslav@56: * modifiers are the same as those of its component type. If this jaroslav@56: * {@code Class} represents a primitive type or void, its jaroslav@56: * {@code public} modifier is always {@code true}, and its jaroslav@56: * {@code protected} and {@code private} modifiers are always jaroslav@56: * {@code false}. If this object represents an array class, a jaroslav@56: * primitive type or void, then its {@code final} modifier is always jaroslav@56: * {@code true} and its interface modifier is always jaroslav@56: * {@code false}. The values of its other modifiers are not determined jaroslav@56: * by this specification. jaroslav@56: * jaroslav@56: *

The modifier encodings are defined in The Java Virtual Machine jaroslav@56: * Specification, table 4.1. jaroslav@56: * jaroslav@56: * @return the {@code int} representing the modifiers for this class jaroslav@56: * @see java.lang.reflect.Modifier jaroslav@56: * @since JDK1.1 jaroslav@56: */ jaroslav@586: public int getModifiers() { jaroslav@586: return getAccess(); jaroslav@586: } jaroslav@56: jaroslav@1419: /** jaroslav@1419: * If the class or interface represented by this {@code Class} object jaroslav@1419: * is a member of another class, returns the {@code Class} object jaroslav@1419: * representing the class in which it was declared. This method returns jaroslav@1419: * null if this class or interface is not a member of any other class. If jaroslav@1419: * this {@code Class} object represents an array class, a primitive jaroslav@1419: * type, or void,then this method returns null. jaroslav@1419: * jaroslav@1419: * @return the declaring class for this class jaroslav@1419: * @since JDK1.1 jaroslav@1419: */ jaroslav@1419: public Class getDeclaringClass() { jaroslav@1419: throw new SecurityException(); jaroslav@1419: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the simple name of the underlying class as given in the jaroslav@56: * source code. Returns an empty string if the underlying class is jaroslav@56: * anonymous. jaroslav@56: * jaroslav@56: *

The simple name of an array is the simple name of the jaroslav@56: * component type with "[]" appended. In particular the simple jaroslav@56: * name of an array whose component type is anonymous is "[]". jaroslav@56: * jaroslav@56: * @return the simple name of the underlying class jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public String getSimpleName() { jaroslav@229: if (isArray()) jaroslav@229: return getComponentType().getSimpleName()+"[]"; jaroslav@229: jaroslav@229: String simpleName = getSimpleBinaryName(); jaroslav@229: if (simpleName == null) { // top level class jaroslav@229: simpleName = getName(); jaroslav@229: return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name jaroslav@229: } jaroslav@229: // According to JLS3 "Binary Compatibility" (13.1) the binary jaroslav@229: // name of non-package classes (not top level) is the binary jaroslav@229: // name of the immediately enclosing class followed by a '$' followed by: jaroslav@229: // (for nested and inner classes): the simple name. jaroslav@229: // (for local classes): 1 or more digits followed by the simple name. jaroslav@229: // (for anonymous classes): 1 or more digits. jaroslav@229: jaroslav@229: // Since getSimpleBinaryName() will strip the binary name of jaroslav@229: // the immediatly enclosing class, we are now looking at a jaroslav@229: // string that matches the regular expression "\$[0-9]*" jaroslav@229: // followed by a simple name (considering the simple of an jaroslav@229: // anonymous class to be the empty string). jaroslav@229: jaroslav@229: // Remove leading "\$[0-9]*" from the name jaroslav@229: int length = simpleName.length(); jaroslav@229: if (length < 1 || simpleName.charAt(0) != '$') jaroslav@229: throw new IllegalStateException("Malformed class name"); jaroslav@229: int index = 1; jaroslav@229: while (index < length && isAsciiDigit(simpleName.charAt(index))) jaroslav@229: index++; jaroslav@229: // Eventually, this is the empty string iff this is an anonymous class jaroslav@229: return simpleName.substring(index); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@229: * Returns the "simple binary name" of the underlying class, i.e., jaroslav@229: * the binary name without the leading enclosing class name. jaroslav@229: * Returns {@code null} if the underlying class is a top level jaroslav@229: * class. jaroslav@229: */ jaroslav@229: private String getSimpleBinaryName() { jaroslav@229: Class enclosingClass = null; // XXX getEnclosingClass(); jaroslav@229: if (enclosingClass == null) // top level class jaroslav@229: return null; jaroslav@229: // Otherwise, strip the enclosing class' name jaroslav@229: try { jaroslav@229: return getName().substring(enclosingClass.getName().length()); jaroslav@229: } catch (IndexOutOfBoundsException ex) { jaroslav@229: throw new IllegalStateException("Malformed class name"); jaroslav@229: } jaroslav@229: } jaroslav@261: jaroslav@261: /** jaroslav@261: * Returns an array containing {@code Field} objects reflecting all jaroslav@261: * the accessible public fields of the class or interface represented by jaroslav@261: * this {@code Class} object. The elements in the array returned are jaroslav@261: * not sorted and are not in any particular order. This method returns an jaroslav@261: * array of length 0 if the class or interface has no accessible public jaroslav@261: * fields, or if it represents an array class, a primitive type, or void. jaroslav@261: * jaroslav@261: *

Specifically, if this {@code Class} object represents a class, jaroslav@261: * this method returns the public fields of this class and of all its jaroslav@261: * superclasses. If this {@code Class} object represents an jaroslav@261: * interface, this method returns the fields of this interface and of all jaroslav@261: * its superinterfaces. jaroslav@261: * jaroslav@261: *

The implicit length field for array class is not reflected by this jaroslav@261: * method. User code should use the methods of class {@code Array} to jaroslav@261: * manipulate arrays. jaroslav@261: * jaroslav@261: *

See The Java Language Specification, sections 8.2 and 8.3. jaroslav@261: * jaroslav@261: * @return the array of {@code Field} objects representing the jaroslav@261: * public fields jaroslav@261: * @exception SecurityException jaroslav@261: * If a security manager, s, is present and any of the jaroslav@261: * following conditions is met: jaroslav@261: * jaroslav@261: *

    jaroslav@261: * jaroslav@261: *
  • invocation of jaroslav@261: * {@link SecurityManager#checkMemberAccess jaroslav@261: * s.checkMemberAccess(this, Member.PUBLIC)} denies jaroslav@261: * access to the fields within this class jaroslav@261: * jaroslav@261: *
  • the caller's class loader is not the same as or an jaroslav@261: * ancestor of the class loader for the current class and jaroslav@261: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@261: * s.checkPackageAccess()} denies access to the package jaroslav@261: * of this class jaroslav@261: * jaroslav@261: *
jaroslav@261: * jaroslav@261: * @since JDK1.1 jaroslav@261: */ jaroslav@261: public Field[] getFields() throws SecurityException { jaroslav@261: throw new SecurityException(); jaroslav@261: } jaroslav@261: jaroslav@261: /** jaroslav@261: * Returns an array containing {@code Method} objects reflecting all jaroslav@261: * the public member methods of the class or interface represented jaroslav@261: * by this {@code Class} object, including those declared by the class jaroslav@261: * or interface and those inherited from superclasses and jaroslav@261: * superinterfaces. Array classes return all the (public) member methods jaroslav@261: * inherited from the {@code Object} class. The elements in the array jaroslav@261: * returned are not sorted and are not in any particular order. This jaroslav@261: * method returns an array of length 0 if this {@code Class} object jaroslav@261: * represents a class or interface that has no public member methods, or if jaroslav@261: * this {@code Class} object represents a primitive type or void. jaroslav@261: * jaroslav@261: *

The class initialization method {@code } is not jaroslav@261: * included in the returned array. If the class declares multiple public jaroslav@261: * member methods with the same parameter types, they are all included in jaroslav@261: * the returned array. jaroslav@261: * jaroslav@261: *

See The Java Language Specification, sections 8.2 and 8.4. jaroslav@261: * jaroslav@261: * @return the array of {@code Method} objects representing the jaroslav@261: * public methods of this class jaroslav@261: * @exception SecurityException jaroslav@261: * If a security manager, s, is present and any of the jaroslav@261: * following conditions is met: jaroslav@261: * jaroslav@261: *

    jaroslav@261: * jaroslav@261: *
  • invocation of jaroslav@261: * {@link SecurityManager#checkMemberAccess jaroslav@261: * s.checkMemberAccess(this, Member.PUBLIC)} denies jaroslav@261: * access to the methods within this class jaroslav@261: * jaroslav@261: *
  • the caller's class loader is not the same as or an jaroslav@261: * ancestor of the class loader for the current class and jaroslav@261: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@261: * s.checkPackageAccess()} denies access to the package jaroslav@261: * of this class jaroslav@261: * jaroslav@261: *
jaroslav@261: * jaroslav@261: * @since JDK1.1 jaroslav@261: */ jaroslav@261: public Method[] getMethods() throws SecurityException { jaroslav@392: return MethodImpl.findMethods(this, 0x01); jaroslav@261: } jaroslav@261: jaroslav@261: /** jaroslav@261: * Returns a {@code Field} object that reflects the specified public jaroslav@261: * member field of the class or interface represented by this jaroslav@261: * {@code Class} object. The {@code name} parameter is a jaroslav@261: * {@code String} specifying the simple name of the desired field. jaroslav@261: * jaroslav@261: *

The field to be reflected is determined by the algorithm that jaroslav@261: * follows. Let C be the class represented by this object: jaroslav@261: *

    jaroslav@261: *
  1. If C declares a public field with the name specified, that is the jaroslav@261: * field to be reflected.
  2. jaroslav@261: *
  3. If no field was found in step 1 above, this algorithm is applied jaroslav@261: * recursively to each direct superinterface of C. The direct jaroslav@261: * superinterfaces are searched in the order they were declared.
  4. jaroslav@261: *
  5. If no field was found in steps 1 and 2 above, and C has a jaroslav@261: * superclass S, then this algorithm is invoked recursively upon S. jaroslav@261: * If C has no superclass, then a {@code NoSuchFieldException} jaroslav@261: * is thrown.
  6. jaroslav@261: *
jaroslav@261: * jaroslav@261: *

See The Java Language Specification, sections 8.2 and 8.3. jaroslav@261: * jaroslav@261: * @param name the field name jaroslav@261: * @return the {@code Field} object of this class specified by jaroslav@261: * {@code name} jaroslav@261: * @exception NoSuchFieldException if a field with the specified name is jaroslav@261: * not found. jaroslav@261: * @exception NullPointerException if {@code name} is {@code null} jaroslav@261: * @exception SecurityException jaroslav@261: * If a security manager, s, is present and any of the jaroslav@261: * following conditions is met: jaroslav@261: * jaroslav@261: *

    jaroslav@261: * jaroslav@261: *
  • invocation of jaroslav@261: * {@link SecurityManager#checkMemberAccess jaroslav@261: * s.checkMemberAccess(this, Member.PUBLIC)} denies jaroslav@261: * access to the field jaroslav@261: * jaroslav@261: *
  • the caller's class loader is not the same as or an jaroslav@261: * ancestor of the class loader for the current class and jaroslav@261: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@261: * s.checkPackageAccess()} denies access to the package jaroslav@261: * of this class jaroslav@261: * jaroslav@261: *
jaroslav@261: * jaroslav@261: * @since JDK1.1 jaroslav@261: */ jaroslav@261: public Field getField(String name) jaroslav@261: throws SecurityException { jaroslav@261: throw new SecurityException(); jaroslav@261: } jaroslav@229: jaroslav@261: jaroslav@261: /** jaroslav@261: * Returns a {@code Method} object that reflects the specified public jaroslav@261: * member method of the class or interface represented by this jaroslav@261: * {@code Class} object. The {@code name} parameter is a jaroslav@261: * {@code String} specifying the simple name of the desired method. The jaroslav@261: * {@code parameterTypes} parameter is an array of {@code Class} jaroslav@261: * objects that identify the method's formal parameter types, in declared jaroslav@261: * order. If {@code parameterTypes} is {@code null}, it is jaroslav@261: * treated as if it were an empty array. jaroslav@261: * jaroslav@261: *

If the {@code name} is "{@code };"or "{@code }" a jaroslav@261: * {@code NoSuchMethodException} is raised. Otherwise, the method to jaroslav@261: * be reflected is determined by the algorithm that follows. Let C be the jaroslav@261: * class represented by this object: jaroslav@261: *

    jaroslav@261: *
  1. C is searched for any matching methods. If no matching jaroslav@261: * method is found, the algorithm of step 1 is invoked recursively on jaroslav@261: * the superclass of C.
  2. jaroslav@261: *
  3. If no method was found in step 1 above, the superinterfaces of C jaroslav@261: * are searched for a matching method. If any such method is found, it jaroslav@261: * is reflected.
  4. jaroslav@261: *
jaroslav@261: * jaroslav@261: * To find a matching method in a class C:  If C declares exactly one jaroslav@261: * public method with the specified name and exactly the same formal jaroslav@261: * parameter types, that is the method reflected. If more than one such jaroslav@261: * method is found in C, and one of these methods has a return type that is jaroslav@261: * more specific than any of the others, that method is reflected; jaroslav@261: * otherwise one of the methods is chosen arbitrarily. jaroslav@261: * jaroslav@261: *

Note that there may be more than one matching method in a jaroslav@261: * class because while the Java language forbids a class to jaroslav@261: * declare multiple methods with the same signature but different jaroslav@261: * return types, the Java virtual machine does not. This jaroslav@261: * increased flexibility in the virtual machine can be used to jaroslav@261: * implement various language features. For example, covariant jaroslav@261: * returns can be implemented with {@linkplain jaroslav@261: * java.lang.reflect.Method#isBridge bridge methods}; the bridge jaroslav@261: * method and the method being overridden would have the same jaroslav@261: * signature but different return types. jaroslav@261: * jaroslav@261: *

See The Java Language Specification, sections 8.2 and 8.4. jaroslav@261: * jaroslav@261: * @param name the name of the method jaroslav@261: * @param parameterTypes the list of parameters jaroslav@261: * @return the {@code Method} object that matches the specified jaroslav@261: * {@code name} and {@code parameterTypes} jaroslav@261: * @exception NoSuchMethodException if a matching method is not found jaroslav@261: * or if the name is "<init>"or "<clinit>". jaroslav@261: * @exception NullPointerException if {@code name} is {@code null} jaroslav@261: * @exception SecurityException jaroslav@261: * If a security manager, s, is present and any of the jaroslav@261: * following conditions is met: jaroslav@261: * jaroslav@261: *

    jaroslav@261: * jaroslav@261: *
  • invocation of jaroslav@261: * {@link SecurityManager#checkMemberAccess jaroslav@261: * s.checkMemberAccess(this, Member.PUBLIC)} denies jaroslav@261: * access to the method jaroslav@261: * jaroslav@261: *
  • the caller's class loader is not the same as or an jaroslav@261: * ancestor of the class loader for the current class and jaroslav@261: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@261: * s.checkPackageAccess()} denies access to the package jaroslav@261: * of this class jaroslav@261: * jaroslav@261: *
jaroslav@261: * jaroslav@261: * @since JDK1.1 jaroslav@261: */ jaroslav@261: public Method getMethod(String name, Class... parameterTypes) jaroslav@420: throws SecurityException, NoSuchMethodException { jaroslav@391: Method m = MethodImpl.findMethod(this, name, parameterTypes); jaroslav@262: if (m == null) { jaroslav@420: StringBuilder sb = new StringBuilder(); jaroslav@420: sb.append(getName()).append('.').append(name).append('('); jaroslav@420: String sep = ""; jaroslav@420: for (int i = 0; i < parameterTypes.length; i++) { jaroslav@420: sb.append(sep).append(parameterTypes[i].getName()); jaroslav@420: sep = ", "; jaroslav@420: } jaroslav@420: sb.append(')'); jaroslav@420: throw new NoSuchMethodException(sb.toString()); jaroslav@262: } jaroslav@262: return m; jaroslav@261: } jaroslav@604: jaroslav@604: /** jaroslav@604: * Returns an array of {@code Method} objects reflecting all the jaroslav@604: * methods declared by the class or interface represented by this jaroslav@604: * {@code Class} object. This includes public, protected, default jaroslav@604: * (package) access, and private methods, but excludes inherited methods. jaroslav@604: * The elements in the array returned are not sorted and are not in any jaroslav@604: * particular order. This method returns an array of length 0 if the class jaroslav@604: * or interface declares no methods, or if this {@code Class} object jaroslav@604: * represents a primitive type, an array class, or void. The class jaroslav@604: * initialization method {@code } is not included in the jaroslav@604: * returned array. If the class declares multiple public member methods jaroslav@604: * with the same parameter types, they are all included in the returned jaroslav@604: * array. jaroslav@604: * jaroslav@604: *

See The Java Language Specification, section 8.2. jaroslav@604: * jaroslav@604: * @return the array of {@code Method} objects representing all the jaroslav@604: * declared methods of this class jaroslav@604: * @exception SecurityException jaroslav@604: * If a security manager, s, is present and any of the jaroslav@604: * following conditions is met: jaroslav@604: * jaroslav@604: *

    jaroslav@604: * jaroslav@604: *
  • invocation of jaroslav@604: * {@link SecurityManager#checkMemberAccess jaroslav@604: * s.checkMemberAccess(this, Member.DECLARED)} denies jaroslav@604: * access to the declared methods within this class jaroslav@604: * jaroslav@604: *
  • the caller's class loader is not the same as or an jaroslav@604: * ancestor of the class loader for the current class and jaroslav@604: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@604: * s.checkPackageAccess()} denies access to the package jaroslav@604: * of this class jaroslav@604: * jaroslav@604: *
jaroslav@604: * jaroslav@604: * @since JDK1.1 jaroslav@604: */ jaroslav@604: public Method[] getDeclaredMethods() throws SecurityException { jaroslav@604: throw new SecurityException(); jaroslav@604: } jaroslav@604: jaroslav@229: /** jaroslav@1312: * Returns an array of {@code Field} objects reflecting all the fields jaroslav@1312: * declared by the class or interface represented by this jaroslav@1312: * {@code Class} object. This includes public, protected, default jaroslav@1312: * (package) access, and private fields, but excludes inherited fields. jaroslav@1312: * The elements in the array returned are not sorted and are not in any jaroslav@1312: * particular order. This method returns an array of length 0 if the class jaroslav@1312: * or interface declares no fields, or if this {@code Class} object jaroslav@1312: * represents a primitive type, an array class, or void. jaroslav@1312: * jaroslav@1312: *

See The Java Language Specification, sections 8.2 and 8.3. jaroslav@1312: * jaroslav@1312: * @return the array of {@code Field} objects representing all the jaroslav@1312: * declared fields of this class jaroslav@1312: * @exception SecurityException jaroslav@1312: * If a security manager, s, is present and any of the jaroslav@1312: * following conditions is met: jaroslav@1312: * jaroslav@1312: *

    jaroslav@1312: * jaroslav@1312: *
  • invocation of jaroslav@1312: * {@link SecurityManager#checkMemberAccess jaroslav@1312: * s.checkMemberAccess(this, Member.DECLARED)} denies jaroslav@1312: * access to the declared fields within this class jaroslav@1312: * jaroslav@1312: *
  • the caller's class loader is not the same as or an jaroslav@1312: * ancestor of the class loader for the current class and jaroslav@1312: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@1312: * s.checkPackageAccess()} denies access to the package jaroslav@1312: * of this class jaroslav@1312: * jaroslav@1312: *
jaroslav@1312: * jaroslav@1312: * @since JDK1.1 jaroslav@1312: */ jaroslav@1312: public Field[] getDeclaredFields() throws SecurityException { jaroslav@1312: throw new SecurityException(); jaroslav@1312: } jaroslav@1312: jaroslav@1312: /** jaroslav@1312: * Bck2Brwsr emulation can only seek public methods, otherwise it jaroslav@1312: * throws a {@code SecurityException}. jaroslav@1312: *

jaroslav@1312: * Returns a {@code Method} object that reflects the specified jaroslav@1312: * declared method of the class or interface represented by this jaroslav@1312: * {@code Class} object. The {@code name} parameter is a jaroslav@1312: * {@code String} that specifies the simple name of the desired jaroslav@1312: * method, and the {@code parameterTypes} parameter is an array of jaroslav@1312: * {@code Class} objects that identify the method's formal parameter jaroslav@1312: * types, in declared order. If more than one method with the same jaroslav@1312: * parameter types is declared in a class, and one of these methods has a jaroslav@1312: * return type that is more specific than any of the others, that method is jaroslav@1312: * returned; otherwise one of the methods is chosen arbitrarily. If the jaroslav@1312: * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} jaroslav@1312: * is raised. jaroslav@1312: * jaroslav@1312: * @param name the name of the method jaroslav@1312: * @param parameterTypes the parameter array jaroslav@1312: * @return the {@code Method} object for the method of this class jaroslav@1312: * matching the specified name and parameters jaroslav@1312: * @exception NoSuchMethodException if a matching method is not found. jaroslav@1312: * @exception NullPointerException if {@code name} is {@code null} jaroslav@1312: * @exception SecurityException jaroslav@1312: * If a security manager, s, is present and any of the jaroslav@1312: * following conditions is met: jaroslav@1312: * jaroslav@1312: *

    jaroslav@1312: * jaroslav@1312: *
  • invocation of jaroslav@1312: * {@link SecurityManager#checkMemberAccess jaroslav@1312: * s.checkMemberAccess(this, Member.DECLARED)} denies jaroslav@1312: * access to the declared method jaroslav@1312: * jaroslav@1312: *
  • the caller's class loader is not the same as or an jaroslav@1312: * ancestor of the class loader for the current class and jaroslav@1312: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@1312: * s.checkPackageAccess()} denies access to the package jaroslav@1312: * of this class jaroslav@1312: * jaroslav@1312: *
jaroslav@1312: * jaroslav@1312: * @since JDK1.1 jaroslav@1312: */ jaroslav@1312: public Method getDeclaredMethod(String name, Class... parameterTypes) jaroslav@1312: throws NoSuchMethodException, SecurityException { jaroslav@1312: try { jaroslav@1312: return getMethod(name, parameterTypes); jaroslav@1312: } catch (NoSuchMethodException ex) { jaroslav@1312: throw new SecurityException(); jaroslav@1312: } jaroslav@1312: } jaroslav@1312: jaroslav@1312: /** jaroslav@1312: * Returns a {@code Field} object that reflects the specified declared jaroslav@1312: * field of the class or interface represented by this {@code Class} jaroslav@1312: * object. The {@code name} parameter is a {@code String} that jaroslav@1312: * specifies the simple name of the desired field. Note that this method jaroslav@1312: * will not reflect the {@code length} field of an array class. jaroslav@1312: * jaroslav@1312: * @param name the name of the field jaroslav@1312: * @return the {@code Field} object for the specified field in this jaroslav@1312: * class jaroslav@1312: * @exception NoSuchFieldException if a field with the specified name is jaroslav@1312: * not found. jaroslav@1312: * @exception NullPointerException if {@code name} is {@code null} jaroslav@1312: * @exception SecurityException jaroslav@1312: * If a security manager, s, is present and any of the jaroslav@1312: * following conditions is met: jaroslav@1312: * jaroslav@1312: *
    jaroslav@1312: * jaroslav@1312: *
  • invocation of jaroslav@1312: * {@link SecurityManager#checkMemberAccess jaroslav@1312: * s.checkMemberAccess(this, Member.DECLARED)} denies jaroslav@1312: * access to the declared field jaroslav@1312: * jaroslav@1312: *
  • the caller's class loader is not the same as or an jaroslav@1312: * ancestor of the class loader for the current class and jaroslav@1312: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@1312: * s.checkPackageAccess()} denies access to the package jaroslav@1312: * of this class jaroslav@1312: * jaroslav@1312: *
jaroslav@1312: * jaroslav@1312: * @since JDK1.1 jaroslav@1312: */ jaroslav@1312: public Field getDeclaredField(String name) jaroslav@1312: throws SecurityException { jaroslav@1312: throw new SecurityException(); jaroslav@1312: } jaroslav@1312: jaroslav@1312: /** jaroslav@1321: * Returns an array containing {@code Constructor} objects reflecting jaroslav@1321: * all the public constructors of the class represented by this jaroslav@1321: * {@code Class} object. An array of length 0 is returned if the jaroslav@1321: * class has no public constructors, or if the class is an array class, or jaroslav@1321: * if the class reflects a primitive type or void. jaroslav@1321: * jaroslav@1321: * Note that while this method returns an array of {@code jaroslav@1321: * Constructor} objects (that is an array of constructors from jaroslav@1321: * this class), the return type of this method is {@code jaroslav@1321: * Constructor[]} and not {@code Constructor[]} as jaroslav@1321: * might be expected. This less informative return type is jaroslav@1321: * necessary since after being returned from this method, the jaroslav@1321: * array could be modified to hold {@code Constructor} objects for jaroslav@1321: * different classes, which would violate the type guarantees of jaroslav@1321: * {@code Constructor[]}. jaroslav@1321: * jaroslav@1321: * @return the array of {@code Constructor} objects representing the jaroslav@1321: * public constructors of this class jaroslav@1321: * @exception SecurityException jaroslav@1321: * If a security manager, s, is present and any of the jaroslav@1321: * following conditions is met: jaroslav@1321: * jaroslav@1321: *
    jaroslav@1321: * jaroslav@1321: *
  • invocation of jaroslav@1321: * {@link SecurityManager#checkMemberAccess jaroslav@1321: * s.checkMemberAccess(this, Member.PUBLIC)} denies jaroslav@1321: * access to the constructors within this class jaroslav@1321: * jaroslav@1321: *
  • the caller's class loader is not the same as or an jaroslav@1321: * ancestor of the class loader for the current class and jaroslav@1321: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@1321: * s.checkPackageAccess()} denies access to the package jaroslav@1321: * of this class jaroslav@1321: * jaroslav@1321: *
jaroslav@1321: * jaroslav@1321: * @since JDK1.1 jaroslav@1321: */ jaroslav@1321: public Constructor[] getConstructors() throws SecurityException { jaroslav@1321: return MethodImpl.findConstructors(this, 0x01); jaroslav@1321: } jaroslav@1321: jaroslav@1321: /** jaroslav@1321: * Returns a {@code Constructor} object that reflects the specified jaroslav@1321: * public constructor of the class represented by this {@code Class} jaroslav@1321: * object. The {@code parameterTypes} parameter is an array of jaroslav@1321: * {@code Class} objects that identify the constructor's formal jaroslav@1321: * parameter types, in declared order. jaroslav@1321: * jaroslav@1321: * If this {@code Class} object represents an inner class jaroslav@1321: * declared in a non-static context, the formal parameter types jaroslav@1321: * include the explicit enclosing instance as the first parameter. jaroslav@1321: * jaroslav@1321: *

The constructor to reflect is the public constructor of the class jaroslav@1321: * represented by this {@code Class} object whose formal parameter jaroslav@1321: * types match those specified by {@code parameterTypes}. jaroslav@1321: * jaroslav@1321: * @param parameterTypes the parameter array jaroslav@1321: * @return the {@code Constructor} object of the public constructor that jaroslav@1321: * matches the specified {@code parameterTypes} jaroslav@1321: * @exception NoSuchMethodException if a matching method is not found. jaroslav@1321: * @exception SecurityException jaroslav@1321: * If a security manager, s, is present and any of the jaroslav@1321: * following conditions is met: jaroslav@1321: * jaroslav@1321: *

    jaroslav@1321: * jaroslav@1321: *
  • invocation of jaroslav@1321: * {@link SecurityManager#checkMemberAccess jaroslav@1321: * s.checkMemberAccess(this, Member.PUBLIC)} denies jaroslav@1321: * access to the constructor jaroslav@1321: * jaroslav@1321: *
  • the caller's class loader is not the same as or an jaroslav@1321: * ancestor of the class loader for the current class and jaroslav@1321: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@1321: * s.checkPackageAccess()} denies access to the package jaroslav@1321: * of this class jaroslav@1321: * jaroslav@1321: *
jaroslav@1321: * jaroslav@1321: * @since JDK1.1 jaroslav@1321: */ jaroslav@1321: public Constructor getConstructor(Class... parameterTypes) jaroslav@1321: throws NoSuchMethodException, SecurityException { jaroslav@1321: Constructor c = MethodImpl.findConstructor(this, parameterTypes); jaroslav@1321: if (c == null) { jaroslav@1321: StringBuilder sb = new StringBuilder(); jaroslav@1321: sb.append(getName()).append('('); jaroslav@1321: String sep = ""; jaroslav@1321: for (int i = 0; i < parameterTypes.length; i++) { jaroslav@1321: sb.append(sep).append(parameterTypes[i].getName()); jaroslav@1321: sep = ", "; jaroslav@1321: } jaroslav@1321: sb.append(')'); jaroslav@1321: throw new NoSuchMethodException(sb.toString()); jaroslav@1321: } jaroslav@1321: return c; jaroslav@1321: } jaroslav@1321: jaroslav@1321: /** jaroslav@1321: * Returns an array of {@code Constructor} objects reflecting all the jaroslav@1321: * constructors declared by the class represented by this jaroslav@1321: * {@code Class} object. These are public, protected, default jaroslav@1321: * (package) access, and private constructors. The elements in the array jaroslav@1321: * returned are not sorted and are not in any particular order. If the jaroslav@1321: * class has a default constructor, it is included in the returned array. jaroslav@1321: * This method returns an array of length 0 if this {@code Class} jaroslav@1321: * object represents an interface, a primitive type, an array class, or jaroslav@1321: * void. jaroslav@1321: * jaroslav@1321: *

See The Java Language Specification, section 8.2. jaroslav@1321: * jaroslav@1321: * @return the array of {@code Constructor} objects representing all the jaroslav@1321: * declared constructors of this class jaroslav@1321: * @exception SecurityException jaroslav@1321: * If a security manager, s, is present and any of the jaroslav@1321: * following conditions is met: jaroslav@1321: * jaroslav@1321: *

    jaroslav@1321: * jaroslav@1321: *
  • invocation of jaroslav@1321: * {@link SecurityManager#checkMemberAccess jaroslav@1321: * s.checkMemberAccess(this, Member.DECLARED)} denies jaroslav@1321: * access to the declared constructors within this class jaroslav@1321: * jaroslav@1321: *
  • the caller's class loader is not the same as or an jaroslav@1321: * ancestor of the class loader for the current class and jaroslav@1321: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@1321: * s.checkPackageAccess()} denies access to the package jaroslav@1321: * of this class jaroslav@1321: * jaroslav@1321: *
jaroslav@1321: * jaroslav@1321: * @since JDK1.1 jaroslav@1321: */ jaroslav@1321: public Constructor[] getDeclaredConstructors() throws SecurityException { jaroslav@1321: throw new SecurityException(); jaroslav@1321: } jaroslav@1321: /** jaroslav@1321: * Returns a {@code Constructor} object that reflects the specified jaroslav@1321: * constructor of the class or interface represented by this jaroslav@1321: * {@code Class} object. The {@code parameterTypes} parameter is jaroslav@1321: * an array of {@code Class} objects that identify the constructor's jaroslav@1321: * formal parameter types, in declared order. jaroslav@1321: * jaroslav@1321: * If this {@code Class} object represents an inner class jaroslav@1321: * declared in a non-static context, the formal parameter types jaroslav@1321: * include the explicit enclosing instance as the first parameter. jaroslav@1321: * jaroslav@1321: * @param parameterTypes the parameter array jaroslav@1321: * @return The {@code Constructor} object for the constructor with the jaroslav@1321: * specified parameter list jaroslav@1321: * @exception NoSuchMethodException if a matching method is not found. jaroslav@1321: * @exception SecurityException jaroslav@1321: * If a security manager, s, is present and any of the jaroslav@1321: * following conditions is met: jaroslav@1321: * jaroslav@1321: *
    jaroslav@1321: * jaroslav@1321: *
  • invocation of jaroslav@1321: * {@link SecurityManager#checkMemberAccess jaroslav@1321: * s.checkMemberAccess(this, Member.DECLARED)} denies jaroslav@1321: * access to the declared constructor jaroslav@1321: * jaroslav@1321: *
  • the caller's class loader is not the same as or an jaroslav@1321: * ancestor of the class loader for the current class and jaroslav@1321: * invocation of {@link SecurityManager#checkPackageAccess jaroslav@1321: * s.checkPackageAccess()} denies access to the package jaroslav@1321: * of this class jaroslav@1321: * jaroslav@1321: *
jaroslav@1321: * jaroslav@1321: * @since JDK1.1 jaroslav@1321: */ jaroslav@1321: public Constructor getDeclaredConstructor(Class... parameterTypes) jaroslav@1321: throws NoSuchMethodException, SecurityException { jaroslav@1321: return getConstructor(parameterTypes); jaroslav@1321: } jaroslav@1321: jaroslav@1321: jaroslav@1321: /** jaroslav@56: * Character.isDigit answers {@code true} to some non-ascii jaroslav@56: * digits. This one does not. jaroslav@56: */ jaroslav@56: private static boolean isAsciiDigit(char c) { jaroslav@56: return '0' <= c && c <= '9'; jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Returns the canonical name of the underlying class as jaroslav@56: * defined by the Java Language Specification. Returns null if jaroslav@56: * the underlying class does not have a canonical name (i.e., if jaroslav@56: * it is a local or anonymous class or an array whose component jaroslav@56: * type does not have a canonical name). jaroslav@56: * @return the canonical name of the underlying class if it exists, and jaroslav@56: * {@code null} otherwise. jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public String getCanonicalName() { jaroslav@228: if (isArray()) { jaroslav@228: String canonicalName = getComponentType().getCanonicalName(); jaroslav@228: if (canonicalName != null) jaroslav@228: return canonicalName + "[]"; jaroslav@228: else jaroslav@228: return null; jaroslav@228: } jaroslav@65: // if (isLocalOrAnonymousClass()) jaroslav@65: // return null; jaroslav@65: // Class enclosingClass = getEnclosingClass(); jaroslav@228: Class enclosingClass = null; jaroslav@228: if (enclosingClass == null) { // top level class jaroslav@228: return getName(); jaroslav@228: } else { jaroslav@228: String enclosingName = enclosingClass.getCanonicalName(); jaroslav@228: if (enclosingName == null) jaroslav@228: return null; jaroslav@228: return enclosingName + "." + getSimpleName(); jaroslav@228: } jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Finds a resource with a given name. The rules for searching resources jaroslav@56: * associated with a given class are implemented by the defining jaroslav@56: * {@linkplain ClassLoader class loader} of the class. This method jaroslav@56: * delegates to this object's class loader. If this object was loaded by jaroslav@56: * the bootstrap class loader, the method delegates to {@link jaroslav@56: * ClassLoader#getSystemResourceAsStream}. jaroslav@56: * jaroslav@56: *

Before delegation, an absolute resource name is constructed from the jaroslav@56: * given resource name using this algorithm: jaroslav@56: * jaroslav@56: *

    jaroslav@56: * jaroslav@56: *
  • If the {@code name} begins with a {@code '/'} jaroslav@56: * ('\u002f'), then the absolute name of the resource is the jaroslav@56: * portion of the {@code name} following the {@code '/'}. jaroslav@56: * jaroslav@56: *
  • Otherwise, the absolute name is of the following form: jaroslav@56: * jaroslav@56: *
    jaroslav@56: * {@code modified_package_name/name} jaroslav@56: *
    jaroslav@56: * jaroslav@56: *

    Where the {@code modified_package_name} is the package name of this jaroslav@56: * object with {@code '/'} substituted for {@code '.'} jaroslav@56: * ('\u002e'). jaroslav@56: * jaroslav@56: *

jaroslav@56: * jaroslav@56: * @param name name of the desired resource jaroslav@56: * @return A {@link java.io.InputStream} object or {@code null} if jaroslav@56: * no resource with this name is found jaroslav@56: * @throws NullPointerException If {@code name} is {@code null} jaroslav@56: * @since JDK1.1 jaroslav@56: */ jaroslav@122: public InputStream getResourceAsStream(String name) { jaroslav@122: name = resolveName(name); jaroslav@1375: byte[] arr = ClassLoader.getResourceAsStream0(name, 0); jaroslav@424: return arr == null ? null : new ByteArrayInputStream(arr); jaroslav@424: } jaroslav@1375: jaroslav@56: /** jaroslav@56: * Finds a resource with a given name. The rules for searching resources jaroslav@56: * associated with a given class are implemented by the defining jaroslav@56: * {@linkplain ClassLoader class loader} of the class. This method jaroslav@56: * delegates to this object's class loader. If this object was loaded by jaroslav@56: * the bootstrap class loader, the method delegates to {@link jaroslav@56: * ClassLoader#getSystemResource}. jaroslav@56: * jaroslav@56: *

Before delegation, an absolute resource name is constructed from the jaroslav@56: * given resource name using this algorithm: jaroslav@56: * jaroslav@56: *

    jaroslav@56: * jaroslav@56: *
  • If the {@code name} begins with a {@code '/'} jaroslav@56: * ('\u002f'), then the absolute name of the resource is the jaroslav@56: * portion of the {@code name} following the {@code '/'}. jaroslav@56: * jaroslav@56: *
  • Otherwise, the absolute name is of the following form: jaroslav@56: * jaroslav@56: *
    jaroslav@56: * {@code modified_package_name/name} jaroslav@56: *
    jaroslav@56: * jaroslav@56: *

    Where the {@code modified_package_name} is the package name of this jaroslav@56: * object with {@code '/'} substituted for {@code '.'} jaroslav@56: * ('\u002e'). jaroslav@56: * jaroslav@56: *

jaroslav@56: * jaroslav@56: * @param name name of the desired resource jaroslav@56: * @return A {@link java.net.URL} object or {@code null} if no jaroslav@56: * resource with this name is found jaroslav@56: * @since JDK1.1 jaroslav@56: */ jaroslav@122: public java.net.URL getResource(String name) { jaroslav@1375: return newResourceURL(name, getResourceAsStream(name)); jaroslav@1375: } jaroslav@1375: jaroslav@1375: static URL newResourceURL(String name, InputStream is) { jaroslav@1375: return is == null ? null : newResourceURL0(URL.class, "res:/" + name, is); jaroslav@122: } jaroslav@576: jaroslav@576: @JavaScriptBody(args = { "url", "spec", "is" }, body = jaroslav@576: "var u = url.cnstr(true);\n" jaroslav@576: + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n" jaroslav@576: + "return u;" jaroslav@576: ) jaroslav@1375: private static native URL newResourceURL0(Class url, String spec, InputStream is); jaroslav@56: jaroslav@122: /** jaroslav@122: * Add a package name prefix if the name is not absolute Remove leading "/" jaroslav@122: * if name is absolute jaroslav@122: */ jaroslav@122: private String resolveName(String name) { jaroslav@122: if (name == null) { jaroslav@122: return name; jaroslav@122: } jaroslav@122: if (!name.startsWith("/")) { jaroslav@122: Class c = this; jaroslav@122: while (c.isArray()) { jaroslav@122: c = c.getComponentType(); jaroslav@122: } jaroslav@122: String baseName = c.getName(); jaroslav@122: int index = baseName.lastIndexOf('.'); jaroslav@122: if (index != -1) { jaroslav@122: name = baseName.substring(0, index).replace('.', '/') jaroslav@122: +"/"+name; jaroslav@122: } jaroslav@122: } else { jaroslav@122: name = name.substring(1); jaroslav@122: } jaroslav@122: return name; jaroslav@122: } jaroslav@122: jaroslav@122: /** jaroslav@122: * Returns the class loader for the class. Some implementations may use jaroslav@122: * null to represent the bootstrap class loader. This method will return jaroslav@122: * null in such implementations if this class was loaded by the bootstrap jaroslav@122: * class loader. jaroslav@122: * jaroslav@122: *

If a security manager is present, and the caller's class loader is jaroslav@122: * not null and the caller's class loader is not the same as or an ancestor of jaroslav@122: * the class loader for the class whose class loader is requested, then jaroslav@122: * this method calls the security manager's {@code checkPermission} jaroslav@122: * method with a {@code RuntimePermission("getClassLoader")} jaroslav@122: * permission to ensure it's ok to access the class loader for the class. jaroslav@122: * jaroslav@122: *

If this object jaroslav@122: * represents a primitive type or void, null is returned. jaroslav@122: * jaroslav@122: * @return the class loader that loaded the class or interface jaroslav@122: * represented by this object. jaroslav@122: * @throws SecurityException jaroslav@122: * if a security manager exists and its jaroslav@122: * {@code checkPermission} method denies jaroslav@122: * access to the class loader for the class. jaroslav@122: * @see java.lang.ClassLoader jaroslav@122: * @see SecurityManager#checkPermission jaroslav@122: * @see java.lang.RuntimePermission jaroslav@122: */ jaroslav@122: public ClassLoader getClassLoader() { jaroslav@1333: return ClassLoader.getSystemClassLoader(); jaroslav@122: } jaroslav@604: jaroslav@604: /** jaroslav@604: * Determines the interfaces implemented by the class or interface jaroslav@604: * represented by this object. jaroslav@604: * jaroslav@604: *

If this object represents a class, the return value is an array jaroslav@604: * containing objects representing all interfaces implemented by the jaroslav@604: * class. The order of the interface objects in the array corresponds to jaroslav@604: * the order of the interface names in the {@code implements} clause jaroslav@604: * of the declaration of the class represented by this object. For jaroslav@604: * example, given the declaration: jaroslav@604: *

jaroslav@604: * {@code class Shimmer implements FloorWax, DessertTopping { ... }} jaroslav@604: *
jaroslav@604: * suppose the value of {@code s} is an instance of jaroslav@604: * {@code Shimmer}; the value of the expression: jaroslav@604: *
jaroslav@604: * {@code s.getClass().getInterfaces()[0]} jaroslav@604: *
jaroslav@604: * is the {@code Class} object that represents interface jaroslav@604: * {@code FloorWax}; and the value of: jaroslav@604: *
jaroslav@604: * {@code s.getClass().getInterfaces()[1]} jaroslav@604: *
jaroslav@604: * is the {@code Class} object that represents interface jaroslav@604: * {@code DessertTopping}. jaroslav@604: * jaroslav@604: *

If this object represents an interface, the array contains objects jaroslav@604: * representing all interfaces extended by the interface. The order of the jaroslav@604: * interface objects in the array corresponds to the order of the interface jaroslav@604: * names in the {@code extends} clause of the declaration of the jaroslav@604: * interface represented by this object. jaroslav@604: * jaroslav@604: *

If this object represents a class or interface that implements no jaroslav@604: * interfaces, the method returns an array of length 0. jaroslav@604: * jaroslav@604: *

If this object represents a primitive type or void, the method jaroslav@604: * returns an array of length 0. jaroslav@604: * jaroslav@604: * @return an array of interfaces implemented by this class. jaroslav@604: */ jaroslav@604: public native Class[] getInterfaces(); jaroslav@122: jaroslav@122: /** jaroslav@122: * Returns the {@code Class} representing the component type of an jaroslav@122: * array. If this class does not represent an array class this method jaroslav@122: * returns null. jaroslav@122: * jaroslav@122: * @return the {@code Class} representing the component type of this jaroslav@122: * class if this class is an array jaroslav@122: * @see java.lang.reflect.Array jaroslav@122: * @since JDK1.1 jaroslav@122: */ jaroslav@228: public Class getComponentType() { jaroslav@451: if (isArray()) { jaroslav@451: try { jaroslav@1532: Class c = getComponentTypeByFnc(); jaroslav@1532: return c != null ? c : getComponentType0(); jaroslav@451: } catch (ClassNotFoundException cnfe) { jaroslav@451: throw new IllegalStateException(cnfe); jaroslav@451: } jaroslav@451: } jaroslav@228: return null; jaroslav@228: } jaroslav@1532: jaroslav@1532: @JavaScriptBody(args = { }, body = jaroslav@1532: "if (this.fnc) {\n" jaroslav@1532: + " var c = this.fnc(false).constructor.$class;\n" jaroslav@1532: // + " java.lang.System.out.println('will call: ' + (!!this.fnc) + ' res: ' + c.jvmName);\n" jaroslav@1532: + " if (c) return c;\n" jaroslav@1532: + "}\n" jaroslav@1532: + "return null;" jaroslav@1532: ) jaroslav@1532: private native Class getComponentTypeByFnc(); jaroslav@56: jaroslav@451: private Class getComponentType0() throws ClassNotFoundException { jaroslav@451: String n = getName().substring(1); jaroslav@451: switch (n.charAt(0)) { jaroslav@451: case 'L': jaroslav@451: n = n.substring(1, n.length() - 1); jaroslav@451: return Class.forName(n); jaroslav@451: case 'I': jaroslav@451: return Integer.TYPE; jaroslav@451: case 'J': jaroslav@451: return Long.TYPE; jaroslav@451: case 'D': jaroslav@451: return Double.TYPE; jaroslav@451: case 'F': jaroslav@451: return Float.TYPE; jaroslav@451: case 'B': jaroslav@451: return Byte.TYPE; jaroslav@451: case 'Z': jaroslav@451: return Boolean.TYPE; jaroslav@451: case 'S': jaroslav@451: return Short.TYPE; jaroslav@451: case 'V': jaroslav@451: return Void.TYPE; jaroslav@451: case 'C': jaroslav@451: return Character.TYPE; jaroslav@451: case '[': jaroslav@1532: return defineArray(n, null); jaroslav@451: default: jaroslav@451: throw new ClassNotFoundException("Unknown component type of " + getName()); jaroslav@451: } jaroslav@451: } jaroslav@451: jaroslav@1532: @JavaScriptBody(args = { "sig", "fnc" }, body = jaroslav@933: "if (!sig) sig = '[Ljava/lang/Object;';\n" + jaroslav@450: "var c = Array[sig];\n" + jaroslav@1532: "if (!c) {\n" + jaroslav@1532: " c = vm.java_lang_Class(true);\n" + jaroslav@1532: " c.jvmName = sig;\n" + jaroslav@1532: " c.superclass = vm.java_lang_Object(false).$class;\n" + jaroslav@1532: " c.array = true;\n" + jaroslav@1532: " Array[sig] = c;\n" + jaroslav@1532: "}\n" + jaroslav@1532: "if (!c.fnc) c.fnc = fnc;\n" + jaroslav@450: "return c;" jaroslav@450: ) jaroslav@1532: private static native Class defineArray(String sig, Object fnc); jaroslav@450: jaroslav@56: /** jaroslav@56: * Returns true if and only if this class was declared as an enum in the jaroslav@56: * source code. jaroslav@56: * jaroslav@56: * @return true if and only if this class was declared as an enum in the jaroslav@56: * source code jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public boolean isEnum() { jaroslav@56: // An enum must both directly extend java.lang.Enum and have jaroslav@56: // the ENUM bit set; classes for specialized enum constants jaroslav@56: // don't do the former. jaroslav@56: return (this.getModifiers() & ENUM) != 0 && jaroslav@1621: this.getSuperclass().getName().equals("java.lang.Enum"); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Casts an object to the class or interface represented jaroslav@56: * by this {@code Class} object. jaroslav@56: * jaroslav@56: * @param obj the object to be cast jaroslav@56: * @return the object after casting, or null if obj is null jaroslav@56: * jaroslav@56: * @throws ClassCastException if the object is not jaroslav@56: * null and is not assignable to the type T. jaroslav@56: * jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public T cast(Object obj) { jaroslav@56: if (obj != null && !isInstance(obj)) jaroslav@56: throw new ClassCastException(cannotCastMsg(obj)); jaroslav@56: return (T) obj; jaroslav@56: } jaroslav@56: jaroslav@56: private String cannotCastMsg(Object obj) { jaroslav@56: return "Cannot cast " + obj.getClass().getName() + " to " + getName(); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * Casts this {@code Class} object to represent a subclass of the class jaroslav@56: * represented by the specified class object. Checks that that the cast jaroslav@56: * is valid, and throws a {@code ClassCastException} if it is not. If jaroslav@56: * this method succeeds, it always returns a reference to this class object. jaroslav@56: * jaroslav@56: *

This method is useful when a client needs to "narrow" the type of jaroslav@56: * a {@code Class} object to pass it to an API that restricts the jaroslav@56: * {@code Class} objects that it is willing to accept. A cast would jaroslav@56: * generate a compile-time warning, as the correctness of the cast jaroslav@56: * could not be checked at runtime (because generic types are implemented jaroslav@56: * by erasure). jaroslav@56: * jaroslav@56: * @return this {@code Class} object, cast to represent a subclass of jaroslav@56: * the specified class object. jaroslav@56: * @throws ClassCastException if this {@code Class} object does not jaroslav@56: * represent a subclass of the specified class (here "subclass" includes jaroslav@56: * the class itself). jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public Class asSubclass(Class clazz) { jaroslav@56: if (clazz.isAssignableFrom(this)) jaroslav@56: return (Class) this; jaroslav@56: else jaroslav@56: throw new ClassCastException(this.toString()); jaroslav@56: } jaroslav@56: jaroslav@443: @JavaScriptBody(args = { "ac" }, jaroslav@235: body = jaroslav@1370: "if (this.anno) {\n" jaroslav@1370: + " var r = this.anno['L' + ac.jvmName + ';'];\n" jaroslav@1370: + " if (typeof r === 'undefined') r = null;\n" jaroslav@1370: + " return r;\n" jaroslav@1370: + "} else return null;\n" jaroslav@235: ) jaroslav@235: private Object getAnnotationData(Class annotationClass) { jaroslav@235: throw new UnsupportedOperationException(); jaroslav@235: } jaroslav@237: /** jaroslav@237: * @throws NullPointerException {@inheritDoc} jaroslav@237: * @since 1.5 jaroslav@237: */ jaroslav@56: public A getAnnotation(Class annotationClass) { jaroslav@235: Object data = getAnnotationData(annotationClass); jaroslav@235: return data == null ? null : AnnotationImpl.create(annotationClass, data); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * @throws NullPointerException {@inheritDoc} jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@443: @JavaScriptBody(args = { "ac" }, jaroslav@443: body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }" jaroslav@235: + "else return false;" jaroslav@235: ) jaroslav@56: public boolean isAnnotationPresent( jaroslav@56: Class annotationClass) { jaroslav@56: if (annotationClass == null) jaroslav@56: throw new NullPointerException(); jaroslav@56: jaroslav@56: return getAnnotation(annotationClass) != null; jaroslav@56: } jaroslav@56: jaroslav@443: @JavaScriptBody(args = {}, body = "return this.anno;") jaroslav@238: private Object getAnnotationData() { jaroslav@238: throw new UnsupportedOperationException(); jaroslav@238: } jaroslav@56: jaroslav@56: /** jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public Annotation[] getAnnotations() { jaroslav@238: Object data = getAnnotationData(); jaroslav@238: return data == null ? new Annotation[0] : AnnotationImpl.create(data); jaroslav@56: } jaroslav@56: jaroslav@56: /** jaroslav@56: * @since 1.5 jaroslav@56: */ jaroslav@56: public Annotation[] getDeclaredAnnotations() { jaroslav@65: throw new UnsupportedOperationException(); jaroslav@56: } jaroslav@56: jaroslav@353: @JavaScriptBody(args = "type", body = "" jaroslav@353: + "var c = vm.java_lang_Class(true);" jaroslav@353: + "c.jvmName = type;" jaroslav@354: + "c.primitive = true;" jaroslav@353: + "return c;" jaroslav@353: ) jaroslav@353: native static Class getPrimitiveClass(String type); jaroslav@88: jaroslav@517: @JavaScriptBody(args = {}, body = jaroslav@1586: "return this['desiredAssertionStatus'] ? this['desiredAssertionStatus'] : false;" jaroslav@517: ) jaroslav@517: public native boolean desiredAssertionStatus(); jaroslav@1607: jaroslav@1607: public boolean equals(Object obj) { jaroslav@1607: if (isPrimitive() && obj instanceof Class) { jaroslav@1607: Class c = ((Class)obj); jaroslav@1607: return c.isPrimitive() && getName().equals(c.getName()); jaroslav@1607: } jaroslav@1607: return super.equals(obj); jaroslav@1607: } jtulach@1483: jtulach@1483: static void registerNatives() { jtulach@1483: boolean assertsOn = false; jtulach@1484: // assert assertsOn = true; jtulach@1483: if (assertsOn) { jtulach@1483: try { jtulach@1483: Array.get(null, 0); jtulach@1483: } catch (Throwable ex) { jtulach@1483: // ignore jtulach@1483: } jtulach@1483: } jtulach@1483: } jtulach@1483: jtulach@1483: @JavaScriptBody(args = {}, body = "var p = vm.java_lang_Object(false);" jtulach@1483: + "p.toString = function() { return this.toString__Ljava_lang_String_2(); };" jtulach@1483: ) jtulach@1483: static native void registerToString(); jtulach@1483: jtulach@1483: @JavaScriptBody(args = {"self"}, body jtulach@1483: = "var c = self.constructor.$class;\n" jtulach@1483: + "return c ? c : null;\n" jtulach@1483: ) jtulach@1483: static native Class classFor(Object self); jtulach@1483: jaroslav@1586: @Exported jtulach@1483: @JavaScriptBody(args = { "self" }, body jaroslav@1586: = "if (self['$hashCode']) return self['$hashCode'];\n" jaroslav@1586: + "var h = self['computeHashCode__I'] ? self['computeHashCode__I']() : Math.random() * Math.pow(2, 31);\n" jaroslav@1586: + "return self['$hashCode'] = h & h;" jtulach@1483: ) jtulach@1483: static native int defaultHashCode(Object self); jtulach@1483: jtulach@1483: @JavaScriptBody(args = "self", body jaroslav@1513: = "\nif (!self['$instOf_java_lang_Cloneable']) {" jtulach@1483: + "\n return null;" jtulach@1483: + "\n} else {" jtulach@1483: + "\n var clone = self.constructor(true);" jtulach@1483: + "\n var props = Object.getOwnPropertyNames(self);" jtulach@1483: + "\n for (var i = 0; i < props.length; i++) {" jtulach@1483: + "\n var p = props[i];" jtulach@1483: + "\n clone[p] = self[p];" jtulach@1483: + "\n };" jtulach@1483: + "\n return clone;" jtulach@1483: + "\n}" jtulach@1483: ) jtulach@1483: static native Object clone(Object self) throws CloneNotSupportedException; jaroslav@1515: jaroslav@1515: @JavaScriptOnly(name = "toJS", value = "function(v) {\n" jaroslav@1515: + " if (v === null) return null;\n" jaroslav@1515: + " if (Object.prototype.toString.call(v) === '[object Array]') {\n" jaroslav@1515: + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).convArray__Ljava_lang_Object_2Ljava_lang_Object_2(v);\n" jaroslav@1515: + " }\n" jaroslav@1515: + " return v.valueOf();\n" jaroslav@1515: + "}\n" jaroslav@1515: ) jaroslav@1515: static native int toJS(); jaroslav@1515: jaroslav@1586: @Exported jaroslav@1515: @JavaScriptOnly(name = "activate__Ljava_io_Closeable_2Lorg_apidesign_html_boot_spi_Fn$Presenter_2", value = "function() {\n" jaroslav@1515: + " return vm.org_apidesign_bck2brwsr_emul_lang_System(false).activate__Ljava_io_Closeable_2();" jaroslav@1515: + "}\n" jaroslav@1515: ) jaroslav@1515: static native int activate(); jaroslav@1522: jaroslav@1522: private static Object bck2BrwsrCnvrt(Object o) { jaroslav@1522: if (o instanceof Throwable) { jaroslav@1522: return o; jaroslav@1522: } jaroslav@1522: final String msg = msg(o); jaroslav@1522: if (msg == null || msg.startsWith("TypeError")) { jaroslav@1522: return new NullPointerException(msg); jaroslav@1522: } jaroslav@1522: return new Throwable(msg); jaroslav@1522: } jaroslav@1522: jaroslav@1522: @JavaScriptBody(args = {"o"}, body = "return o ? o.toString() : null;") jaroslav@1522: private static native String msg(Object o); jaroslav@1522: jaroslav@1586: @Exported jaroslav@1522: @JavaScriptOnly(name = "bck2BrwsrThrwrbl", value = "c.bck2BrwsrCnvrt__Ljava_lang_Object_2Ljava_lang_Object_2") jaroslav@1522: private static void bck2BrwsrCnvrtVM() { jaroslav@1522: } jaroslav@1522: jaroslav@56: }