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@555: import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl; jaroslav@122: import java.io.InputStream; jaroslav@56: import java.lang.annotation.Annotation; 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@225: import org.apidesign.bck2brwsr.core.JavaScriptBody; 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: * 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: *
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
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@452: Class> arrType = defineArray(className); 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@322: Class> c = loadCls(className, className.replace('.', '_')); jaroslav@321: if (c == null) { jaroslav@400: throw new ClassNotFoundException(className); jaroslav@321: } jaroslav@321: return c; 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@658: "if (!vm[c]) {\n" jaroslav@658: + " if (vm.loadClass) {\n" jaroslav@658: + " vm.loadClass(n);\n" jaroslav@658: + " }\n" jaroslav@658: + " if (!vm[c]) return null;\n" jaroslav@322: + "}\n" jaroslav@658: + "vm[c](false);" jaroslav@658: + "return vm[c].$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: *
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: * 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: *
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: * 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 TypeVariablejaroslav@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: *
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@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: *
The class initialization method {@code 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: * 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: * 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: * If the {@code name} is "{@code 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: * 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: * Before delegation, an absolute resource name is constructed from the
jaroslav@56: * given resource name using this algorithm:
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: * Before delegation, an absolute resource name is constructed from the
jaroslav@56: * given resource name using this algorithm:
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: * 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@122: throw new SecurityException();
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: * 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@451: return getComponentType0();
jaroslav@451: } catch (ClassNotFoundException cnfe) {
jaroslav@451: throw new IllegalStateException(cnfe);
jaroslav@451: }
jaroslav@451: }
jaroslav@228: return null;
jaroslav@228: }
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@451: return defineArray(n);
jaroslav@451: default:
jaroslav@451: throw new ClassNotFoundException("Unknown component type of " + getName());
jaroslav@451: }
jaroslav@451: }
jaroslav@451:
jaroslav@450: @JavaScriptBody(args = { "sig" }, body =
jaroslav@450: "var c = Array[sig];\n" +
jaroslav@450: "if (c) return c;\n" +
jaroslav@450: "c = vm.java_lang_Class(true);\n" +
jaroslav@450: "c.jvmName = sig;\n" +
jaroslav@450: "c.superclass = vm.java_lang_Object(false).$class;\n" +
jaroslav@450: "c.array = true;\n" +
jaroslav@450: "Array[sig] = c;\n" +
jaroslav@450: "return c;"
jaroslav@450: )
jaroslav@450: private static native Class> defineArray(String sig);
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@56: this.getSuperclass() == java.lang.Enum.class;
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 extends U> asSubclass(Class clazz) {
jaroslav@56: if (clazz.isAssignableFrom(this))
jaroslav@56: return (Class extends U>) this;
jaroslav@56: else
jaroslav@56: throw new ClassCastException(this.toString());
jaroslav@56: }
jaroslav@56:
jaroslav@443: @JavaScriptBody(args = { "ac" },
jaroslav@235: body =
jaroslav@443: "if (this.anno) {"
jaroslav@443: + " return this.anno['L' + ac.jvmName + ';'];"
jaroslav@266: + "} else return null;"
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 extends Annotation> 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@517: "return vm.desiredAssertionStatus ? vm.desiredAssertionStatus : false;"
jaroslav@517: )
jaroslav@517: public native boolean desiredAssertionStatus();
jaroslav@56: }
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: *
jaroslav@261: *
jaroslav@261: *
jaroslav@261: *
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: *
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: *
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
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@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: *
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@424: byte[] arr = getResourceAsStream0(name);
jaroslav@424: return arr == null ? null : new ByteArrayInputStream(arr);
jaroslav@424: }
jaroslav@424:
jaroslav@424: @JavaScriptBody(args = "name", body =
jaroslav@424: "return (vm.loadBytes) ? vm.loadBytes(name) : null;"
jaroslav@424: )
jaroslav@424: private static native byte[] getResourceAsStream0(String name);
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#getSystemResource}.
jaroslav@56: *
jaroslav@56: *
jaroslav@56: * {@code modified_package_name/name}
jaroslav@56: *
jaroslav@56: *
jaroslav@56: *
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@576: InputStream is = getResourceAsStream(name);
jaroslav@576: return is == null ? null : newResourceURL(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@576: private static native URL newResourceURL(Class
jaroslav@56: * {@code modified_package_name/name}
jaroslav@56: *
jaroslav@56: *
jaroslav@56: *
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: *