1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/mini/src/main/java/java/lang/Class.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,1390 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +import java.io.ByteArrayInputStream;
1.32 +import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
1.33 +import java.io.InputStream;
1.34 +import java.lang.annotation.Annotation;
1.35 +import java.lang.reflect.Field;
1.36 +import java.lang.reflect.Method;
1.37 +import java.lang.reflect.TypeVariable;
1.38 +import java.net.URL;
1.39 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.40 +import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
1.41 +
1.42 +/**
1.43 + * Instances of the class {@code Class} represent classes and
1.44 + * interfaces in a running Java application. An enum is a kind of
1.45 + * class and an annotation is a kind of interface. Every array also
1.46 + * belongs to a class that is reflected as a {@code Class} object
1.47 + * that is shared by all arrays with the same element type and number
1.48 + * of dimensions. The primitive Java types ({@code boolean},
1.49 + * {@code byte}, {@code char}, {@code short},
1.50 + * {@code int}, {@code long}, {@code float}, and
1.51 + * {@code double}), and the keyword {@code void} are also
1.52 + * represented as {@code Class} objects.
1.53 + *
1.54 + * <p> {@code Class} has no public constructor. Instead {@code Class}
1.55 + * objects are constructed automatically by the Java Virtual Machine as classes
1.56 + * are loaded and by calls to the {@code defineClass} method in the class
1.57 + * loader.
1.58 + *
1.59 + * <p> The following example uses a {@code Class} object to print the
1.60 + * class name of an object:
1.61 + *
1.62 + * <p> <blockquote><pre>
1.63 + * void printClassName(Object obj) {
1.64 + * System.out.println("The class of " + obj +
1.65 + * " is " + obj.getClass().getName());
1.66 + * }
1.67 + * </pre></blockquote>
1.68 + *
1.69 + * <p> It is also possible to get the {@code Class} object for a named
1.70 + * type (or for void) using a class literal. See Section 15.8.2 of
1.71 + * <cite>The Java™ Language Specification</cite>.
1.72 + * For example:
1.73 + *
1.74 + * <p> <blockquote>
1.75 + * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
1.76 + * </blockquote>
1.77 + *
1.78 + * @param <T> the type of the class modeled by this {@code Class}
1.79 + * object. For example, the type of {@code String.class} is {@code
1.80 + * Class<String>}. Use {@code Class<?>} if the class being modeled is
1.81 + * unknown.
1.82 + *
1.83 + * @author unascribed
1.84 + * @see java.lang.ClassLoader#defineClass(byte[], int, int)
1.85 + * @since JDK1.0
1.86 + */
1.87 +public final
1.88 + class Class<T> implements java.io.Serializable,
1.89 + java.lang.reflect.GenericDeclaration,
1.90 + java.lang.reflect.Type,
1.91 + java.lang.reflect.AnnotatedElement {
1.92 + private static final int ANNOTATION= 0x00002000;
1.93 + private static final int ENUM = 0x00004000;
1.94 + private static final int SYNTHETIC = 0x00001000;
1.95 +
1.96 + /*
1.97 + * Constructor. Only the Java Virtual Machine creates Class
1.98 + * objects.
1.99 + */
1.100 + private Class() {}
1.101 +
1.102 +
1.103 + /**
1.104 + * Converts the object to a string. The string representation is the
1.105 + * string "class" or "interface", followed by a space, and then by the
1.106 + * fully qualified name of the class in the format returned by
1.107 + * {@code getName}. If this {@code Class} object represents a
1.108 + * primitive type, this method returns the name of the primitive type. If
1.109 + * this {@code Class} object represents void this method returns
1.110 + * "void".
1.111 + *
1.112 + * @return a string representation of this class object.
1.113 + */
1.114 + public String toString() {
1.115 + return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
1.116 + + getName();
1.117 + }
1.118 +
1.119 +
1.120 + /**
1.121 + * Returns the {@code Class} object associated with the class or
1.122 + * interface with the given string name. Invoking this method is
1.123 + * equivalent to:
1.124 + *
1.125 + * <blockquote>
1.126 + * {@code Class.forName(className, true, currentLoader)}
1.127 + * </blockquote>
1.128 + *
1.129 + * where {@code currentLoader} denotes the defining class loader of
1.130 + * the current class.
1.131 + *
1.132 + * <p> For example, the following code fragment returns the
1.133 + * runtime {@code Class} descriptor for the class named
1.134 + * {@code java.lang.Thread}:
1.135 + *
1.136 + * <blockquote>
1.137 + * {@code Class t = Class.forName("java.lang.Thread")}
1.138 + * </blockquote>
1.139 + * <p>
1.140 + * A call to {@code forName("X")} causes the class named
1.141 + * {@code X} to be initialized.
1.142 + *
1.143 + * @param className the fully qualified name of the desired class.
1.144 + * @return the {@code Class} object for the class with the
1.145 + * specified name.
1.146 + * @exception LinkageError if the linkage fails
1.147 + * @exception ExceptionInInitializerError if the initialization provoked
1.148 + * by this method fails
1.149 + * @exception ClassNotFoundException if the class cannot be located
1.150 + */
1.151 + public static Class<?> forName(String className)
1.152 + throws ClassNotFoundException {
1.153 + if (className.startsWith("[")) {
1.154 + Class<?> arrType = defineArray(className);
1.155 + Class<?> c = arrType;
1.156 + while (c != null && c.isArray()) {
1.157 + c = c.getComponentType0(); // verify component type is sane
1.158 + }
1.159 + return arrType;
1.160 + }
1.161 + Class<?> c = loadCls(className, className.replace('.', '_'));
1.162 + if (c == null) {
1.163 + throw new ClassNotFoundException(className);
1.164 + }
1.165 + return c;
1.166 + }
1.167 +
1.168 +
1.169 + /**
1.170 + * Returns the {@code Class} object associated with the class or
1.171 + * interface with the given string name, using the given class loader.
1.172 + * Given the fully qualified name for a class or interface (in the same
1.173 + * format returned by {@code getName}) this method attempts to
1.174 + * locate, load, and link the class or interface. The specified class
1.175 + * loader is used to load the class or interface. If the parameter
1.176 + * {@code loader} is null, the class is loaded through the bootstrap
1.177 + * class loader. The class is initialized only if the
1.178 + * {@code initialize} parameter is {@code true} and if it has
1.179 + * not been initialized earlier.
1.180 + *
1.181 + * <p> If {@code name} denotes a primitive type or void, an attempt
1.182 + * will be made to locate a user-defined class in the unnamed package whose
1.183 + * name is {@code name}. Therefore, this method cannot be used to
1.184 + * obtain any of the {@code Class} objects representing primitive
1.185 + * types or void.
1.186 + *
1.187 + * <p> If {@code name} denotes an array class, the component type of
1.188 + * the array class is loaded but not initialized.
1.189 + *
1.190 + * <p> For example, in an instance method the expression:
1.191 + *
1.192 + * <blockquote>
1.193 + * {@code Class.forName("Foo")}
1.194 + * </blockquote>
1.195 + *
1.196 + * is equivalent to:
1.197 + *
1.198 + * <blockquote>
1.199 + * {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
1.200 + * </blockquote>
1.201 + *
1.202 + * Note that this method throws errors related to loading, linking or
1.203 + * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
1.204 + * Java Language Specification</em>.
1.205 + * Note that this method does not check whether the requested class
1.206 + * is accessible to its caller.
1.207 + *
1.208 + * <p> If the {@code loader} is {@code null}, and a security
1.209 + * manager is present, and the caller's class loader is not null, then this
1.210 + * method calls the security manager's {@code checkPermission} method
1.211 + * with a {@code RuntimePermission("getClassLoader")} permission to
1.212 + * ensure it's ok to access the bootstrap class loader.
1.213 + *
1.214 + * @param name fully qualified name of the desired class
1.215 + * @param initialize whether the class must be initialized
1.216 + * @param loader class loader from which the class must be loaded
1.217 + * @return class object representing the desired class
1.218 + *
1.219 + * @exception LinkageError if the linkage fails
1.220 + * @exception ExceptionInInitializerError if the initialization provoked
1.221 + * by this method fails
1.222 + * @exception ClassNotFoundException if the class cannot be located by
1.223 + * the specified class loader
1.224 + *
1.225 + * @see java.lang.Class#forName(String)
1.226 + * @see java.lang.ClassLoader
1.227 + * @since 1.2
1.228 + */
1.229 + public static Class<?> forName(String name, boolean initialize,
1.230 + ClassLoader loader)
1.231 + throws ClassNotFoundException
1.232 + {
1.233 + return forName(name);
1.234 + }
1.235 +
1.236 + @JavaScriptBody(args = {"n", "c" }, body =
1.237 + "if (!vm[c]) {\n"
1.238 + + " if (vm.loadClass) {\n"
1.239 + + " vm.loadClass(n);\n"
1.240 + + " }\n"
1.241 + + " if (!vm[c]) return null;\n"
1.242 + + "}\n"
1.243 + + "vm[c](false);"
1.244 + + "return vm[c].$class;"
1.245 + )
1.246 + private static native Class<?> loadCls(String n, String c);
1.247 +
1.248 +
1.249 + /**
1.250 + * Creates a new instance of the class represented by this {@code Class}
1.251 + * object. The class is instantiated as if by a {@code new}
1.252 + * expression with an empty argument list. The class is initialized if it
1.253 + * has not already been initialized.
1.254 + *
1.255 + * <p>Note that this method propagates any exception thrown by the
1.256 + * nullary constructor, including a checked exception. Use of
1.257 + * this method effectively bypasses the compile-time exception
1.258 + * checking that would otherwise be performed by the compiler.
1.259 + * The {@link
1.260 + * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
1.261 + * Constructor.newInstance} method avoids this problem by wrapping
1.262 + * any exception thrown by the constructor in a (checked) {@link
1.263 + * java.lang.reflect.InvocationTargetException}.
1.264 + *
1.265 + * @return a newly allocated instance of the class represented by this
1.266 + * object.
1.267 + * @exception IllegalAccessException if the class or its nullary
1.268 + * constructor is not accessible.
1.269 + * @exception InstantiationException
1.270 + * if this {@code Class} represents an abstract class,
1.271 + * an interface, an array class, a primitive type, or void;
1.272 + * or if the class has no nullary constructor;
1.273 + * or if the instantiation fails for some other reason.
1.274 + * @exception ExceptionInInitializerError if the initialization
1.275 + * provoked by this method fails.
1.276 + * @exception SecurityException
1.277 + * If a security manager, <i>s</i>, is present and any of the
1.278 + * following conditions is met:
1.279 + *
1.280 + * <ul>
1.281 + *
1.282 + * <li> invocation of
1.283 + * {@link SecurityManager#checkMemberAccess
1.284 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
1.285 + * creation of new instances of this class
1.286 + *
1.287 + * <li> the caller's class loader is not the same as or an
1.288 + * ancestor of the class loader for the current class and
1.289 + * invocation of {@link SecurityManager#checkPackageAccess
1.290 + * s.checkPackageAccess()} denies access to the package
1.291 + * of this class
1.292 + *
1.293 + * </ul>
1.294 + *
1.295 + */
1.296 + @JavaScriptBody(args = { "self", "illegal" }, body =
1.297 + "\nvar c = self.cnstr;"
1.298 + + "\nif (c['cons__V']) {"
1.299 + + "\n if ((c.cons__V.access & 0x1) != 0) {"
1.300 + + "\n var inst = c();"
1.301 + + "\n c.cons__V.call(inst);"
1.302 + + "\n return inst;"
1.303 + + "\n }"
1.304 + + "\n return illegal;"
1.305 + + "\n}"
1.306 + + "\nreturn null;"
1.307 + )
1.308 + private static native Object newInstance0(Class<?> self, Object illegal);
1.309 +
1.310 + public T newInstance()
1.311 + throws InstantiationException, IllegalAccessException
1.312 + {
1.313 + Object illegal = new Object();
1.314 + Object inst = newInstance0(this, illegal);
1.315 + if (inst == null) {
1.316 + throw new InstantiationException(getName());
1.317 + }
1.318 + if (inst == illegal) {
1.319 + throw new IllegalAccessException();
1.320 + }
1.321 + return (T)inst;
1.322 + }
1.323 +
1.324 + /**
1.325 + * Determines if the specified {@code Object} is assignment-compatible
1.326 + * with the object represented by this {@code Class}. This method is
1.327 + * the dynamic equivalent of the Java language {@code instanceof}
1.328 + * operator. The method returns {@code true} if the specified
1.329 + * {@code Object} argument is non-null and can be cast to the
1.330 + * reference type represented by this {@code Class} object without
1.331 + * raising a {@code ClassCastException.} It returns {@code false}
1.332 + * otherwise.
1.333 + *
1.334 + * <p> Specifically, if this {@code Class} object represents a
1.335 + * declared class, this method returns {@code true} if the specified
1.336 + * {@code Object} argument is an instance of the represented class (or
1.337 + * of any of its subclasses); it returns {@code false} otherwise. If
1.338 + * this {@code Class} object represents an array class, this method
1.339 + * returns {@code true} if the specified {@code Object} argument
1.340 + * can be converted to an object of the array class by an identity
1.341 + * conversion or by a widening reference conversion; it returns
1.342 + * {@code false} otherwise. If this {@code Class} object
1.343 + * represents an interface, this method returns {@code true} if the
1.344 + * class or any superclass of the specified {@code Object} argument
1.345 + * implements this interface; it returns {@code false} otherwise. If
1.346 + * this {@code Class} object represents a primitive type, this method
1.347 + * returns {@code false}.
1.348 + *
1.349 + * @param obj the object to check
1.350 + * @return true if {@code obj} is an instance of this class
1.351 + *
1.352 + * @since JDK1.1
1.353 + */
1.354 + public boolean isInstance(Object obj) {
1.355 + if (obj == null) {
1.356 + return false;
1.357 + }
1.358 + if (isArray()) {
1.359 + return isAssignableFrom(obj.getClass());
1.360 + }
1.361 +
1.362 + String prop = "$instOf_" + getName().replace('.', '_');
1.363 + return hasProperty(obj, prop);
1.364 + }
1.365 +
1.366 + @JavaScriptBody(args = { "who", "prop" }, body =
1.367 + "if (who[prop]) return true; else return false;"
1.368 + )
1.369 + private static native boolean hasProperty(Object who, String prop);
1.370 +
1.371 +
1.372 + /**
1.373 + * Determines if the class or interface represented by this
1.374 + * {@code Class} object is either the same as, or is a superclass or
1.375 + * superinterface of, the class or interface represented by the specified
1.376 + * {@code Class} parameter. It returns {@code true} if so;
1.377 + * otherwise it returns {@code false}. If this {@code Class}
1.378 + * object represents a primitive type, this method returns
1.379 + * {@code true} if the specified {@code Class} parameter is
1.380 + * exactly this {@code Class} object; otherwise it returns
1.381 + * {@code false}.
1.382 + *
1.383 + * <p> Specifically, this method tests whether the type represented by the
1.384 + * specified {@code Class} parameter can be converted to the type
1.385 + * represented by this {@code Class} object via an identity conversion
1.386 + * or via a widening reference conversion. See <em>The Java Language
1.387 + * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
1.388 + *
1.389 + * @param cls the {@code Class} object to be checked
1.390 + * @return the {@code boolean} value indicating whether objects of the
1.391 + * type {@code cls} can be assigned to objects of this class
1.392 + * @exception NullPointerException if the specified Class parameter is
1.393 + * null.
1.394 + * @since JDK1.1
1.395 + */
1.396 + public boolean isAssignableFrom(Class<?> cls) {
1.397 + if (this == cls) {
1.398 + return true;
1.399 + }
1.400 +
1.401 + if (isArray()) {
1.402 + final Class<?> cmpType = cls.getComponentType();
1.403 + if (isPrimitive()) {
1.404 + return this == cmpType;
1.405 + }
1.406 + return cmpType != null && getComponentType().isAssignableFrom(cmpType);
1.407 + }
1.408 + String prop = "$instOf_" + getName().replace('.', '_');
1.409 + return hasCnstrProperty(cls, prop);
1.410 + }
1.411 +
1.412 + @JavaScriptBody(args = { "who", "prop" }, body =
1.413 + "if (who.cnstr.prototype[prop]) return true; else return false;"
1.414 + )
1.415 + private static native boolean hasCnstrProperty(Object who, String prop);
1.416 +
1.417 +
1.418 + /**
1.419 + * Determines if the specified {@code Class} object represents an
1.420 + * interface type.
1.421 + *
1.422 + * @return {@code true} if this object represents an interface;
1.423 + * {@code false} otherwise.
1.424 + */
1.425 + public boolean isInterface() {
1.426 + return (getAccess() & 0x200) != 0;
1.427 + }
1.428 +
1.429 + @JavaScriptBody(args = {}, body = "return this.access;")
1.430 + private native int getAccess();
1.431 +
1.432 +
1.433 + /**
1.434 + * Determines if this {@code Class} object represents an array class.
1.435 + *
1.436 + * @return {@code true} if this object represents an array class;
1.437 + * {@code false} otherwise.
1.438 + * @since JDK1.1
1.439 + */
1.440 + public boolean isArray() {
1.441 + return hasProperty(this, "array"); // NOI18N
1.442 + }
1.443 +
1.444 +
1.445 + /**
1.446 + * Determines if the specified {@code Class} object represents a
1.447 + * primitive type.
1.448 + *
1.449 + * <p> There are nine predefined {@code Class} objects to represent
1.450 + * the eight primitive types and void. These are created by the Java
1.451 + * Virtual Machine, and have the same names as the primitive types that
1.452 + * they represent, namely {@code boolean}, {@code byte},
1.453 + * {@code char}, {@code short}, {@code int},
1.454 + * {@code long}, {@code float}, and {@code double}.
1.455 + *
1.456 + * <p> These objects may only be accessed via the following public static
1.457 + * final variables, and are the only {@code Class} objects for which
1.458 + * this method returns {@code true}.
1.459 + *
1.460 + * @return true if and only if this class represents a primitive type
1.461 + *
1.462 + * @see java.lang.Boolean#TYPE
1.463 + * @see java.lang.Character#TYPE
1.464 + * @see java.lang.Byte#TYPE
1.465 + * @see java.lang.Short#TYPE
1.466 + * @see java.lang.Integer#TYPE
1.467 + * @see java.lang.Long#TYPE
1.468 + * @see java.lang.Float#TYPE
1.469 + * @see java.lang.Double#TYPE
1.470 + * @see java.lang.Void#TYPE
1.471 + * @since JDK1.1
1.472 + */
1.473 + @JavaScriptBody(args = {}, body =
1.474 + "if (this.primitive) return true;"
1.475 + + "else return false;"
1.476 + )
1.477 + public native boolean isPrimitive();
1.478 +
1.479 + /**
1.480 + * Returns true if this {@code Class} object represents an annotation
1.481 + * type. Note that if this method returns true, {@link #isInterface()}
1.482 + * would also return true, as all annotation types are also interfaces.
1.483 + *
1.484 + * @return {@code true} if this class object represents an annotation
1.485 + * type; {@code false} otherwise
1.486 + * @since 1.5
1.487 + */
1.488 + public boolean isAnnotation() {
1.489 + return (getModifiers() & ANNOTATION) != 0;
1.490 + }
1.491 +
1.492 + /**
1.493 + * Returns {@code true} if this class is a synthetic class;
1.494 + * returns {@code false} otherwise.
1.495 + * @return {@code true} if and only if this class is a synthetic class as
1.496 + * defined by the Java Language Specification.
1.497 + * @since 1.5
1.498 + */
1.499 + public boolean isSynthetic() {
1.500 + return (getModifiers() & SYNTHETIC) != 0;
1.501 + }
1.502 +
1.503 + /**
1.504 + * Returns the name of the entity (class, interface, array class,
1.505 + * primitive type, or void) represented by this {@code Class} object,
1.506 + * as a {@code String}.
1.507 + *
1.508 + * <p> If this class object represents a reference type that is not an
1.509 + * array type then the binary name of the class is returned, as specified
1.510 + * by
1.511 + * <cite>The Java™ Language Specification</cite>.
1.512 + *
1.513 + * <p> If this class object represents a primitive type or void, then the
1.514 + * name returned is a {@code String} equal to the Java language
1.515 + * keyword corresponding to the primitive type or void.
1.516 + *
1.517 + * <p> If this class object represents a class of arrays, then the internal
1.518 + * form of the name consists of the name of the element type preceded by
1.519 + * one or more '{@code [}' characters representing the depth of the array
1.520 + * nesting. The encoding of element type names is as follows:
1.521 + *
1.522 + * <blockquote><table summary="Element types and encodings">
1.523 + * <tr><th> Element Type <th> <th> Encoding
1.524 + * <tr><td> boolean <td> <td align=center> Z
1.525 + * <tr><td> byte <td> <td align=center> B
1.526 + * <tr><td> char <td> <td align=center> C
1.527 + * <tr><td> class or interface
1.528 + * <td> <td align=center> L<i>classname</i>;
1.529 + * <tr><td> double <td> <td align=center> D
1.530 + * <tr><td> float <td> <td align=center> F
1.531 + * <tr><td> int <td> <td align=center> I
1.532 + * <tr><td> long <td> <td align=center> J
1.533 + * <tr><td> short <td> <td align=center> S
1.534 + * </table></blockquote>
1.535 + *
1.536 + * <p> The class or interface name <i>classname</i> is the binary name of
1.537 + * the class specified above.
1.538 + *
1.539 + * <p> Examples:
1.540 + * <blockquote><pre>
1.541 + * String.class.getName()
1.542 + * returns "java.lang.String"
1.543 + * byte.class.getName()
1.544 + * returns "byte"
1.545 + * (new Object[3]).getClass().getName()
1.546 + * returns "[Ljava.lang.Object;"
1.547 + * (new int[3][4][5][6][7][8][9]).getClass().getName()
1.548 + * returns "[[[[[[[I"
1.549 + * </pre></blockquote>
1.550 + *
1.551 + * @return the name of the class or interface
1.552 + * represented by this object.
1.553 + */
1.554 + public String getName() {
1.555 + return jvmName().replace('/', '.');
1.556 + }
1.557 +
1.558 + @JavaScriptBody(args = {}, body = "return this.jvmName;")
1.559 + private native String jvmName();
1.560 +
1.561 +
1.562 + /**
1.563 + * Returns an array of {@code TypeVariable} objects that represent the
1.564 + * type variables declared by the generic declaration represented by this
1.565 + * {@code GenericDeclaration} object, in declaration order. Returns an
1.566 + * array of length 0 if the underlying generic declaration declares no type
1.567 + * variables.
1.568 + *
1.569 + * @return an array of {@code TypeVariable} objects that represent
1.570 + * the type variables declared by this generic declaration
1.571 + * @throws java.lang.reflect.GenericSignatureFormatError if the generic
1.572 + * signature of this generic declaration does not conform to
1.573 + * the format specified in
1.574 + * <cite>The Java™ Virtual Machine Specification</cite>
1.575 + * @since 1.5
1.576 + */
1.577 + public TypeVariable<Class<T>>[] getTypeParameters() {
1.578 + throw new UnsupportedOperationException();
1.579 + }
1.580 +
1.581 + /**
1.582 + * Returns the {@code Class} representing the superclass of the entity
1.583 + * (class, interface, primitive type or void) represented by this
1.584 + * {@code Class}. If this {@code Class} represents either the
1.585 + * {@code Object} class, an interface, a primitive type, or void, then
1.586 + * null is returned. If this object represents an array class then the
1.587 + * {@code Class} object representing the {@code Object} class is
1.588 + * returned.
1.589 + *
1.590 + * @return the superclass of the class represented by this object.
1.591 + */
1.592 + @JavaScriptBody(args = {}, body = "return this.superclass;")
1.593 + public native Class<? super T> getSuperclass();
1.594 +
1.595 + /**
1.596 + * Returns the Java language modifiers for this class or interface, encoded
1.597 + * in an integer. The modifiers consist of the Java Virtual Machine's
1.598 + * constants for {@code public}, {@code protected},
1.599 + * {@code private}, {@code final}, {@code static},
1.600 + * {@code abstract} and {@code interface}; they should be decoded
1.601 + * using the methods of class {@code Modifier}.
1.602 + *
1.603 + * <p> If the underlying class is an array class, then its
1.604 + * {@code public}, {@code private} and {@code protected}
1.605 + * modifiers are the same as those of its component type. If this
1.606 + * {@code Class} represents a primitive type or void, its
1.607 + * {@code public} modifier is always {@code true}, and its
1.608 + * {@code protected} and {@code private} modifiers are always
1.609 + * {@code false}. If this object represents an array class, a
1.610 + * primitive type or void, then its {@code final} modifier is always
1.611 + * {@code true} and its interface modifier is always
1.612 + * {@code false}. The values of its other modifiers are not determined
1.613 + * by this specification.
1.614 + *
1.615 + * <p> The modifier encodings are defined in <em>The Java Virtual Machine
1.616 + * Specification</em>, table 4.1.
1.617 + *
1.618 + * @return the {@code int} representing the modifiers for this class
1.619 + * @see java.lang.reflect.Modifier
1.620 + * @since JDK1.1
1.621 + */
1.622 + public int getModifiers() {
1.623 + return getAccess();
1.624 + }
1.625 +
1.626 +
1.627 + /**
1.628 + * Returns the simple name of the underlying class as given in the
1.629 + * source code. Returns an empty string if the underlying class is
1.630 + * anonymous.
1.631 + *
1.632 + * <p>The simple name of an array is the simple name of the
1.633 + * component type with "[]" appended. In particular the simple
1.634 + * name of an array whose component type is anonymous is "[]".
1.635 + *
1.636 + * @return the simple name of the underlying class
1.637 + * @since 1.5
1.638 + */
1.639 + public String getSimpleName() {
1.640 + if (isArray())
1.641 + return getComponentType().getSimpleName()+"[]";
1.642 +
1.643 + String simpleName = getSimpleBinaryName();
1.644 + if (simpleName == null) { // top level class
1.645 + simpleName = getName();
1.646 + return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
1.647 + }
1.648 + // According to JLS3 "Binary Compatibility" (13.1) the binary
1.649 + // name of non-package classes (not top level) is the binary
1.650 + // name of the immediately enclosing class followed by a '$' followed by:
1.651 + // (for nested and inner classes): the simple name.
1.652 + // (for local classes): 1 or more digits followed by the simple name.
1.653 + // (for anonymous classes): 1 or more digits.
1.654 +
1.655 + // Since getSimpleBinaryName() will strip the binary name of
1.656 + // the immediatly enclosing class, we are now looking at a
1.657 + // string that matches the regular expression "\$[0-9]*"
1.658 + // followed by a simple name (considering the simple of an
1.659 + // anonymous class to be the empty string).
1.660 +
1.661 + // Remove leading "\$[0-9]*" from the name
1.662 + int length = simpleName.length();
1.663 + if (length < 1 || simpleName.charAt(0) != '$')
1.664 + throw new IllegalStateException("Malformed class name");
1.665 + int index = 1;
1.666 + while (index < length && isAsciiDigit(simpleName.charAt(index)))
1.667 + index++;
1.668 + // Eventually, this is the empty string iff this is an anonymous class
1.669 + return simpleName.substring(index);
1.670 + }
1.671 +
1.672 + /**
1.673 + * Returns the "simple binary name" of the underlying class, i.e.,
1.674 + * the binary name without the leading enclosing class name.
1.675 + * Returns {@code null} if the underlying class is a top level
1.676 + * class.
1.677 + */
1.678 + private String getSimpleBinaryName() {
1.679 + Class<?> enclosingClass = null; // XXX getEnclosingClass();
1.680 + if (enclosingClass == null) // top level class
1.681 + return null;
1.682 + // Otherwise, strip the enclosing class' name
1.683 + try {
1.684 + return getName().substring(enclosingClass.getName().length());
1.685 + } catch (IndexOutOfBoundsException ex) {
1.686 + throw new IllegalStateException("Malformed class name");
1.687 + }
1.688 + }
1.689 +
1.690 + /**
1.691 + * Returns an array containing {@code Field} objects reflecting all
1.692 + * the accessible public fields of the class or interface represented by
1.693 + * this {@code Class} object. The elements in the array returned are
1.694 + * not sorted and are not in any particular order. This method returns an
1.695 + * array of length 0 if the class or interface has no accessible public
1.696 + * fields, or if it represents an array class, a primitive type, or void.
1.697 + *
1.698 + * <p> Specifically, if this {@code Class} object represents a class,
1.699 + * this method returns the public fields of this class and of all its
1.700 + * superclasses. If this {@code Class} object represents an
1.701 + * interface, this method returns the fields of this interface and of all
1.702 + * its superinterfaces.
1.703 + *
1.704 + * <p> The implicit length field for array class is not reflected by this
1.705 + * method. User code should use the methods of class {@code Array} to
1.706 + * manipulate arrays.
1.707 + *
1.708 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1.709 + *
1.710 + * @return the array of {@code Field} objects representing the
1.711 + * public fields
1.712 + * @exception SecurityException
1.713 + * If a security manager, <i>s</i>, is present and any of the
1.714 + * following conditions is met:
1.715 + *
1.716 + * <ul>
1.717 + *
1.718 + * <li> invocation of
1.719 + * {@link SecurityManager#checkMemberAccess
1.720 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
1.721 + * access to the fields within this class
1.722 + *
1.723 + * <li> the caller's class loader is not the same as or an
1.724 + * ancestor of the class loader for the current class and
1.725 + * invocation of {@link SecurityManager#checkPackageAccess
1.726 + * s.checkPackageAccess()} denies access to the package
1.727 + * of this class
1.728 + *
1.729 + * </ul>
1.730 + *
1.731 + * @since JDK1.1
1.732 + */
1.733 + public Field[] getFields() throws SecurityException {
1.734 + throw new SecurityException();
1.735 + }
1.736 +
1.737 + /**
1.738 + * Returns an array containing {@code Method} objects reflecting all
1.739 + * the public <em>member</em> methods of the class or interface represented
1.740 + * by this {@code Class} object, including those declared by the class
1.741 + * or interface and those inherited from superclasses and
1.742 + * superinterfaces. Array classes return all the (public) member methods
1.743 + * inherited from the {@code Object} class. The elements in the array
1.744 + * returned are not sorted and are not in any particular order. This
1.745 + * method returns an array of length 0 if this {@code Class} object
1.746 + * represents a class or interface that has no public member methods, or if
1.747 + * this {@code Class} object represents a primitive type or void.
1.748 + *
1.749 + * <p> The class initialization method {@code <clinit>} is not
1.750 + * included in the returned array. If the class declares multiple public
1.751 + * member methods with the same parameter types, they are all included in
1.752 + * the returned array.
1.753 + *
1.754 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
1.755 + *
1.756 + * @return the array of {@code Method} objects representing the
1.757 + * public methods of this class
1.758 + * @exception SecurityException
1.759 + * If a security manager, <i>s</i>, is present and any of the
1.760 + * following conditions is met:
1.761 + *
1.762 + * <ul>
1.763 + *
1.764 + * <li> invocation of
1.765 + * {@link SecurityManager#checkMemberAccess
1.766 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
1.767 + * access to the methods within this class
1.768 + *
1.769 + * <li> the caller's class loader is not the same as or an
1.770 + * ancestor of the class loader for the current class and
1.771 + * invocation of {@link SecurityManager#checkPackageAccess
1.772 + * s.checkPackageAccess()} denies access to the package
1.773 + * of this class
1.774 + *
1.775 + * </ul>
1.776 + *
1.777 + * @since JDK1.1
1.778 + */
1.779 + public Method[] getMethods() throws SecurityException {
1.780 + return MethodImpl.findMethods(this, 0x01);
1.781 + }
1.782 +
1.783 + /**
1.784 + * Returns a {@code Field} object that reflects the specified public
1.785 + * member field of the class or interface represented by this
1.786 + * {@code Class} object. The {@code name} parameter is a
1.787 + * {@code String} specifying the simple name of the desired field.
1.788 + *
1.789 + * <p> The field to be reflected is determined by the algorithm that
1.790 + * follows. Let C be the class represented by this object:
1.791 + * <OL>
1.792 + * <LI> If C declares a public field with the name specified, that is the
1.793 + * field to be reflected.</LI>
1.794 + * <LI> If no field was found in step 1 above, this algorithm is applied
1.795 + * recursively to each direct superinterface of C. The direct
1.796 + * superinterfaces are searched in the order they were declared.</LI>
1.797 + * <LI> If no field was found in steps 1 and 2 above, and C has a
1.798 + * superclass S, then this algorithm is invoked recursively upon S.
1.799 + * If C has no superclass, then a {@code NoSuchFieldException}
1.800 + * is thrown.</LI>
1.801 + * </OL>
1.802 + *
1.803 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
1.804 + *
1.805 + * @param name the field name
1.806 + * @return the {@code Field} object of this class specified by
1.807 + * {@code name}
1.808 + * @exception NoSuchFieldException if a field with the specified name is
1.809 + * not found.
1.810 + * @exception NullPointerException if {@code name} is {@code null}
1.811 + * @exception SecurityException
1.812 + * If a security manager, <i>s</i>, is present and any of the
1.813 + * following conditions is met:
1.814 + *
1.815 + * <ul>
1.816 + *
1.817 + * <li> invocation of
1.818 + * {@link SecurityManager#checkMemberAccess
1.819 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
1.820 + * access to the field
1.821 + *
1.822 + * <li> the caller's class loader is not the same as or an
1.823 + * ancestor of the class loader for the current class and
1.824 + * invocation of {@link SecurityManager#checkPackageAccess
1.825 + * s.checkPackageAccess()} denies access to the package
1.826 + * of this class
1.827 + *
1.828 + * </ul>
1.829 + *
1.830 + * @since JDK1.1
1.831 + */
1.832 + public Field getField(String name)
1.833 + throws SecurityException {
1.834 + throw new SecurityException();
1.835 + }
1.836 +
1.837 +
1.838 + /**
1.839 + * Returns a {@code Method} object that reflects the specified public
1.840 + * member method of the class or interface represented by this
1.841 + * {@code Class} object. The {@code name} parameter is a
1.842 + * {@code String} specifying the simple name of the desired method. The
1.843 + * {@code parameterTypes} parameter is an array of {@code Class}
1.844 + * objects that identify the method's formal parameter types, in declared
1.845 + * order. If {@code parameterTypes} is {@code null}, it is
1.846 + * treated as if it were an empty array.
1.847 + *
1.848 + * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
1.849 + * {@code NoSuchMethodException} is raised. Otherwise, the method to
1.850 + * be reflected is determined by the algorithm that follows. Let C be the
1.851 + * class represented by this object:
1.852 + * <OL>
1.853 + * <LI> C is searched for any <I>matching methods</I>. If no matching
1.854 + * method is found, the algorithm of step 1 is invoked recursively on
1.855 + * the superclass of C.</LI>
1.856 + * <LI> If no method was found in step 1 above, the superinterfaces of C
1.857 + * are searched for a matching method. If any such method is found, it
1.858 + * is reflected.</LI>
1.859 + * </OL>
1.860 + *
1.861 + * To find a matching method in a class C: If C declares exactly one
1.862 + * public method with the specified name and exactly the same formal
1.863 + * parameter types, that is the method reflected. If more than one such
1.864 + * method is found in C, and one of these methods has a return type that is
1.865 + * more specific than any of the others, that method is reflected;
1.866 + * otherwise one of the methods is chosen arbitrarily.
1.867 + *
1.868 + * <p>Note that there may be more than one matching method in a
1.869 + * class because while the Java language forbids a class to
1.870 + * declare multiple methods with the same signature but different
1.871 + * return types, the Java virtual machine does not. This
1.872 + * increased flexibility in the virtual machine can be used to
1.873 + * implement various language features. For example, covariant
1.874 + * returns can be implemented with {@linkplain
1.875 + * java.lang.reflect.Method#isBridge bridge methods}; the bridge
1.876 + * method and the method being overridden would have the same
1.877 + * signature but different return types.
1.878 + *
1.879 + * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
1.880 + *
1.881 + * @param name the name of the method
1.882 + * @param parameterTypes the list of parameters
1.883 + * @return the {@code Method} object that matches the specified
1.884 + * {@code name} and {@code parameterTypes}
1.885 + * @exception NoSuchMethodException if a matching method is not found
1.886 + * or if the name is "<init>"or "<clinit>".
1.887 + * @exception NullPointerException if {@code name} is {@code null}
1.888 + * @exception SecurityException
1.889 + * If a security manager, <i>s</i>, is present and any of the
1.890 + * following conditions is met:
1.891 + *
1.892 + * <ul>
1.893 + *
1.894 + * <li> invocation of
1.895 + * {@link SecurityManager#checkMemberAccess
1.896 + * s.checkMemberAccess(this, Member.PUBLIC)} denies
1.897 + * access to the method
1.898 + *
1.899 + * <li> the caller's class loader is not the same as or an
1.900 + * ancestor of the class loader for the current class and
1.901 + * invocation of {@link SecurityManager#checkPackageAccess
1.902 + * s.checkPackageAccess()} denies access to the package
1.903 + * of this class
1.904 + *
1.905 + * </ul>
1.906 + *
1.907 + * @since JDK1.1
1.908 + */
1.909 + public Method getMethod(String name, Class<?>... parameterTypes)
1.910 + throws SecurityException, NoSuchMethodException {
1.911 + Method m = MethodImpl.findMethod(this, name, parameterTypes);
1.912 + if (m == null) {
1.913 + StringBuilder sb = new StringBuilder();
1.914 + sb.append(getName()).append('.').append(name).append('(');
1.915 + String sep = "";
1.916 + for (int i = 0; i < parameterTypes.length; i++) {
1.917 + sb.append(sep).append(parameterTypes[i].getName());
1.918 + sep = ", ";
1.919 + }
1.920 + sb.append(')');
1.921 + throw new NoSuchMethodException(sb.toString());
1.922 + }
1.923 + return m;
1.924 + }
1.925 +
1.926 + /**
1.927 + * Returns an array of {@code Method} objects reflecting all the
1.928 + * methods declared by the class or interface represented by this
1.929 + * {@code Class} object. This includes public, protected, default
1.930 + * (package) access, and private methods, but excludes inherited methods.
1.931 + * The elements in the array returned are not sorted and are not in any
1.932 + * particular order. This method returns an array of length 0 if the class
1.933 + * or interface declares no methods, or if this {@code Class} object
1.934 + * represents a primitive type, an array class, or void. The class
1.935 + * initialization method {@code <clinit>} is not included in the
1.936 + * returned array. If the class declares multiple public member methods
1.937 + * with the same parameter types, they are all included in the returned
1.938 + * array.
1.939 + *
1.940 + * <p> See <em>The Java Language Specification</em>, section 8.2.
1.941 + *
1.942 + * @return the array of {@code Method} objects representing all the
1.943 + * declared methods of this class
1.944 + * @exception SecurityException
1.945 + * If a security manager, <i>s</i>, is present and any of the
1.946 + * following conditions is met:
1.947 + *
1.948 + * <ul>
1.949 + *
1.950 + * <li> invocation of
1.951 + * {@link SecurityManager#checkMemberAccess
1.952 + * s.checkMemberAccess(this, Member.DECLARED)} denies
1.953 + * access to the declared methods within this class
1.954 + *
1.955 + * <li> the caller's class loader is not the same as or an
1.956 + * ancestor of the class loader for the current class and
1.957 + * invocation of {@link SecurityManager#checkPackageAccess
1.958 + * s.checkPackageAccess()} denies access to the package
1.959 + * of this class
1.960 + *
1.961 + * </ul>
1.962 + *
1.963 + * @since JDK1.1
1.964 + */
1.965 + public Method[] getDeclaredMethods() throws SecurityException {
1.966 + throw new SecurityException();
1.967 + }
1.968 +
1.969 + /**
1.970 + * Character.isDigit answers {@code true} to some non-ascii
1.971 + * digits. This one does not.
1.972 + */
1.973 + private static boolean isAsciiDigit(char c) {
1.974 + return '0' <= c && c <= '9';
1.975 + }
1.976 +
1.977 + /**
1.978 + * Returns the canonical name of the underlying class as
1.979 + * defined by the Java Language Specification. Returns null if
1.980 + * the underlying class does not have a canonical name (i.e., if
1.981 + * it is a local or anonymous class or an array whose component
1.982 + * type does not have a canonical name).
1.983 + * @return the canonical name of the underlying class if it exists, and
1.984 + * {@code null} otherwise.
1.985 + * @since 1.5
1.986 + */
1.987 + public String getCanonicalName() {
1.988 + if (isArray()) {
1.989 + String canonicalName = getComponentType().getCanonicalName();
1.990 + if (canonicalName != null)
1.991 + return canonicalName + "[]";
1.992 + else
1.993 + return null;
1.994 + }
1.995 +// if (isLocalOrAnonymousClass())
1.996 +// return null;
1.997 +// Class<?> enclosingClass = getEnclosingClass();
1.998 + Class<?> enclosingClass = null;
1.999 + if (enclosingClass == null) { // top level class
1.1000 + return getName();
1.1001 + } else {
1.1002 + String enclosingName = enclosingClass.getCanonicalName();
1.1003 + if (enclosingName == null)
1.1004 + return null;
1.1005 + return enclosingName + "." + getSimpleName();
1.1006 + }
1.1007 + }
1.1008 +
1.1009 + /**
1.1010 + * Finds a resource with a given name. The rules for searching resources
1.1011 + * associated with a given class are implemented by the defining
1.1012 + * {@linkplain ClassLoader class loader} of the class. This method
1.1013 + * delegates to this object's class loader. If this object was loaded by
1.1014 + * the bootstrap class loader, the method delegates to {@link
1.1015 + * ClassLoader#getSystemResourceAsStream}.
1.1016 + *
1.1017 + * <p> Before delegation, an absolute resource name is constructed from the
1.1018 + * given resource name using this algorithm:
1.1019 + *
1.1020 + * <ul>
1.1021 + *
1.1022 + * <li> If the {@code name} begins with a {@code '/'}
1.1023 + * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1.1024 + * portion of the {@code name} following the {@code '/'}.
1.1025 + *
1.1026 + * <li> Otherwise, the absolute name is of the following form:
1.1027 + *
1.1028 + * <blockquote>
1.1029 + * {@code modified_package_name/name}
1.1030 + * </blockquote>
1.1031 + *
1.1032 + * <p> Where the {@code modified_package_name} is the package name of this
1.1033 + * object with {@code '/'} substituted for {@code '.'}
1.1034 + * (<tt>'\u002e'</tt>).
1.1035 + *
1.1036 + * </ul>
1.1037 + *
1.1038 + * @param name name of the desired resource
1.1039 + * @return A {@link java.io.InputStream} object or {@code null} if
1.1040 + * no resource with this name is found
1.1041 + * @throws NullPointerException If {@code name} is {@code null}
1.1042 + * @since JDK1.1
1.1043 + */
1.1044 + public InputStream getResourceAsStream(String name) {
1.1045 + name = resolveName(name);
1.1046 + byte[] arr = getResourceAsStream0(name);
1.1047 + return arr == null ? null : new ByteArrayInputStream(arr);
1.1048 + }
1.1049 +
1.1050 + @JavaScriptBody(args = "name", body =
1.1051 + "return (vm.loadBytes) ? vm.loadBytes(name) : null;"
1.1052 + )
1.1053 + private static native byte[] getResourceAsStream0(String name);
1.1054 +
1.1055 + /**
1.1056 + * Finds a resource with a given name. The rules for searching resources
1.1057 + * associated with a given class are implemented by the defining
1.1058 + * {@linkplain ClassLoader class loader} of the class. This method
1.1059 + * delegates to this object's class loader. If this object was loaded by
1.1060 + * the bootstrap class loader, the method delegates to {@link
1.1061 + * ClassLoader#getSystemResource}.
1.1062 + *
1.1063 + * <p> Before delegation, an absolute resource name is constructed from the
1.1064 + * given resource name using this algorithm:
1.1065 + *
1.1066 + * <ul>
1.1067 + *
1.1068 + * <li> If the {@code name} begins with a {@code '/'}
1.1069 + * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
1.1070 + * portion of the {@code name} following the {@code '/'}.
1.1071 + *
1.1072 + * <li> Otherwise, the absolute name is of the following form:
1.1073 + *
1.1074 + * <blockquote>
1.1075 + * {@code modified_package_name/name}
1.1076 + * </blockquote>
1.1077 + *
1.1078 + * <p> Where the {@code modified_package_name} is the package name of this
1.1079 + * object with {@code '/'} substituted for {@code '.'}
1.1080 + * (<tt>'\u002e'</tt>).
1.1081 + *
1.1082 + * </ul>
1.1083 + *
1.1084 + * @param name name of the desired resource
1.1085 + * @return A {@link java.net.URL} object or {@code null} if no
1.1086 + * resource with this name is found
1.1087 + * @since JDK1.1
1.1088 + */
1.1089 + public java.net.URL getResource(String name) {
1.1090 + InputStream is = getResourceAsStream(name);
1.1091 + return is == null ? null : newResourceURL(URL.class, "res:/" + name, is);
1.1092 + }
1.1093 +
1.1094 + @JavaScriptBody(args = { "url", "spec", "is" }, body =
1.1095 + "var u = url.cnstr(true);\n"
1.1096 + + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n"
1.1097 + + "return u;"
1.1098 + )
1.1099 + private static native URL newResourceURL(Class<URL> url, String spec, InputStream is);
1.1100 +
1.1101 + /**
1.1102 + * Add a package name prefix if the name is not absolute Remove leading "/"
1.1103 + * if name is absolute
1.1104 + */
1.1105 + private String resolveName(String name) {
1.1106 + if (name == null) {
1.1107 + return name;
1.1108 + }
1.1109 + if (!name.startsWith("/")) {
1.1110 + Class<?> c = this;
1.1111 + while (c.isArray()) {
1.1112 + c = c.getComponentType();
1.1113 + }
1.1114 + String baseName = c.getName();
1.1115 + int index = baseName.lastIndexOf('.');
1.1116 + if (index != -1) {
1.1117 + name = baseName.substring(0, index).replace('.', '/')
1.1118 + +"/"+name;
1.1119 + }
1.1120 + } else {
1.1121 + name = name.substring(1);
1.1122 + }
1.1123 + return name;
1.1124 + }
1.1125 +
1.1126 + /**
1.1127 + * Returns the class loader for the class. Some implementations may use
1.1128 + * null to represent the bootstrap class loader. This method will return
1.1129 + * null in such implementations if this class was loaded by the bootstrap
1.1130 + * class loader.
1.1131 + *
1.1132 + * <p> If a security manager is present, and the caller's class loader is
1.1133 + * not null and the caller's class loader is not the same as or an ancestor of
1.1134 + * the class loader for the class whose class loader is requested, then
1.1135 + * this method calls the security manager's {@code checkPermission}
1.1136 + * method with a {@code RuntimePermission("getClassLoader")}
1.1137 + * permission to ensure it's ok to access the class loader for the class.
1.1138 + *
1.1139 + * <p>If this object
1.1140 + * represents a primitive type or void, null is returned.
1.1141 + *
1.1142 + * @return the class loader that loaded the class or interface
1.1143 + * represented by this object.
1.1144 + * @throws SecurityException
1.1145 + * if a security manager exists and its
1.1146 + * {@code checkPermission} method denies
1.1147 + * access to the class loader for the class.
1.1148 + * @see java.lang.ClassLoader
1.1149 + * @see SecurityManager#checkPermission
1.1150 + * @see java.lang.RuntimePermission
1.1151 + */
1.1152 + public ClassLoader getClassLoader() {
1.1153 + throw new SecurityException();
1.1154 + }
1.1155 +
1.1156 + /**
1.1157 + * Determines the interfaces implemented by the class or interface
1.1158 + * represented by this object.
1.1159 + *
1.1160 + * <p> If this object represents a class, the return value is an array
1.1161 + * containing objects representing all interfaces implemented by the
1.1162 + * class. The order of the interface objects in the array corresponds to
1.1163 + * the order of the interface names in the {@code implements} clause
1.1164 + * of the declaration of the class represented by this object. For
1.1165 + * example, given the declaration:
1.1166 + * <blockquote>
1.1167 + * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
1.1168 + * </blockquote>
1.1169 + * suppose the value of {@code s} is an instance of
1.1170 + * {@code Shimmer}; the value of the expression:
1.1171 + * <blockquote>
1.1172 + * {@code s.getClass().getInterfaces()[0]}
1.1173 + * </blockquote>
1.1174 + * is the {@code Class} object that represents interface
1.1175 + * {@code FloorWax}; and the value of:
1.1176 + * <blockquote>
1.1177 + * {@code s.getClass().getInterfaces()[1]}
1.1178 + * </blockquote>
1.1179 + * is the {@code Class} object that represents interface
1.1180 + * {@code DessertTopping}.
1.1181 + *
1.1182 + * <p> If this object represents an interface, the array contains objects
1.1183 + * representing all interfaces extended by the interface. The order of the
1.1184 + * interface objects in the array corresponds to the order of the interface
1.1185 + * names in the {@code extends} clause of the declaration of the
1.1186 + * interface represented by this object.
1.1187 + *
1.1188 + * <p> If this object represents a class or interface that implements no
1.1189 + * interfaces, the method returns an array of length 0.
1.1190 + *
1.1191 + * <p> If this object represents a primitive type or void, the method
1.1192 + * returns an array of length 0.
1.1193 + *
1.1194 + * @return an array of interfaces implemented by this class.
1.1195 + */
1.1196 + public native Class<?>[] getInterfaces();
1.1197 +
1.1198 + /**
1.1199 + * Returns the {@code Class} representing the component type of an
1.1200 + * array. If this class does not represent an array class this method
1.1201 + * returns null.
1.1202 + *
1.1203 + * @return the {@code Class} representing the component type of this
1.1204 + * class if this class is an array
1.1205 + * @see java.lang.reflect.Array
1.1206 + * @since JDK1.1
1.1207 + */
1.1208 + public Class<?> getComponentType() {
1.1209 + if (isArray()) {
1.1210 + try {
1.1211 + return getComponentType0();
1.1212 + } catch (ClassNotFoundException cnfe) {
1.1213 + throw new IllegalStateException(cnfe);
1.1214 + }
1.1215 + }
1.1216 + return null;
1.1217 + }
1.1218 +
1.1219 + private Class<?> getComponentType0() throws ClassNotFoundException {
1.1220 + String n = getName().substring(1);
1.1221 + switch (n.charAt(0)) {
1.1222 + case 'L':
1.1223 + n = n.substring(1, n.length() - 1);
1.1224 + return Class.forName(n);
1.1225 + case 'I':
1.1226 + return Integer.TYPE;
1.1227 + case 'J':
1.1228 + return Long.TYPE;
1.1229 + case 'D':
1.1230 + return Double.TYPE;
1.1231 + case 'F':
1.1232 + return Float.TYPE;
1.1233 + case 'B':
1.1234 + return Byte.TYPE;
1.1235 + case 'Z':
1.1236 + return Boolean.TYPE;
1.1237 + case 'S':
1.1238 + return Short.TYPE;
1.1239 + case 'V':
1.1240 + return Void.TYPE;
1.1241 + case 'C':
1.1242 + return Character.TYPE;
1.1243 + case '[':
1.1244 + return defineArray(n);
1.1245 + default:
1.1246 + throw new ClassNotFoundException("Unknown component type of " + getName());
1.1247 + }
1.1248 + }
1.1249 +
1.1250 + @JavaScriptBody(args = { "sig" }, body =
1.1251 + "var c = Array[sig];\n" +
1.1252 + "if (c) return c;\n" +
1.1253 + "c = vm.java_lang_Class(true);\n" +
1.1254 + "c.jvmName = sig;\n" +
1.1255 + "c.superclass = vm.java_lang_Object(false).$class;\n" +
1.1256 + "c.array = true;\n" +
1.1257 + "Array[sig] = c;\n" +
1.1258 + "return c;"
1.1259 + )
1.1260 + private static native Class<?> defineArray(String sig);
1.1261 +
1.1262 + /**
1.1263 + * Returns true if and only if this class was declared as an enum in the
1.1264 + * source code.
1.1265 + *
1.1266 + * @return true if and only if this class was declared as an enum in the
1.1267 + * source code
1.1268 + * @since 1.5
1.1269 + */
1.1270 + public boolean isEnum() {
1.1271 + // An enum must both directly extend java.lang.Enum and have
1.1272 + // the ENUM bit set; classes for specialized enum constants
1.1273 + // don't do the former.
1.1274 + return (this.getModifiers() & ENUM) != 0 &&
1.1275 + this.getSuperclass() == java.lang.Enum.class;
1.1276 + }
1.1277 +
1.1278 + /**
1.1279 + * Casts an object to the class or interface represented
1.1280 + * by this {@code Class} object.
1.1281 + *
1.1282 + * @param obj the object to be cast
1.1283 + * @return the object after casting, or null if obj is null
1.1284 + *
1.1285 + * @throws ClassCastException if the object is not
1.1286 + * null and is not assignable to the type T.
1.1287 + *
1.1288 + * @since 1.5
1.1289 + */
1.1290 + public T cast(Object obj) {
1.1291 + if (obj != null && !isInstance(obj))
1.1292 + throw new ClassCastException(cannotCastMsg(obj));
1.1293 + return (T) obj;
1.1294 + }
1.1295 +
1.1296 + private String cannotCastMsg(Object obj) {
1.1297 + return "Cannot cast " + obj.getClass().getName() + " to " + getName();
1.1298 + }
1.1299 +
1.1300 + /**
1.1301 + * Casts this {@code Class} object to represent a subclass of the class
1.1302 + * represented by the specified class object. Checks that that the cast
1.1303 + * is valid, and throws a {@code ClassCastException} if it is not. If
1.1304 + * this method succeeds, it always returns a reference to this class object.
1.1305 + *
1.1306 + * <p>This method is useful when a client needs to "narrow" the type of
1.1307 + * a {@code Class} object to pass it to an API that restricts the
1.1308 + * {@code Class} objects that it is willing to accept. A cast would
1.1309 + * generate a compile-time warning, as the correctness of the cast
1.1310 + * could not be checked at runtime (because generic types are implemented
1.1311 + * by erasure).
1.1312 + *
1.1313 + * @return this {@code Class} object, cast to represent a subclass of
1.1314 + * the specified class object.
1.1315 + * @throws ClassCastException if this {@code Class} object does not
1.1316 + * represent a subclass of the specified class (here "subclass" includes
1.1317 + * the class itself).
1.1318 + * @since 1.5
1.1319 + */
1.1320 + public <U> Class<? extends U> asSubclass(Class<U> clazz) {
1.1321 + if (clazz.isAssignableFrom(this))
1.1322 + return (Class<? extends U>) this;
1.1323 + else
1.1324 + throw new ClassCastException(this.toString());
1.1325 + }
1.1326 +
1.1327 + @JavaScriptBody(args = { "ac" },
1.1328 + body =
1.1329 + "if (this.anno) {"
1.1330 + + " return this.anno['L' + ac.jvmName + ';'];"
1.1331 + + "} else return null;"
1.1332 + )
1.1333 + private Object getAnnotationData(Class<?> annotationClass) {
1.1334 + throw new UnsupportedOperationException();
1.1335 + }
1.1336 + /**
1.1337 + * @throws NullPointerException {@inheritDoc}
1.1338 + * @since 1.5
1.1339 + */
1.1340 + public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
1.1341 + Object data = getAnnotationData(annotationClass);
1.1342 + return data == null ? null : AnnotationImpl.create(annotationClass, data);
1.1343 + }
1.1344 +
1.1345 + /**
1.1346 + * @throws NullPointerException {@inheritDoc}
1.1347 + * @since 1.5
1.1348 + */
1.1349 + @JavaScriptBody(args = { "ac" },
1.1350 + body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
1.1351 + + "else return false;"
1.1352 + )
1.1353 + public boolean isAnnotationPresent(
1.1354 + Class<? extends Annotation> annotationClass) {
1.1355 + if (annotationClass == null)
1.1356 + throw new NullPointerException();
1.1357 +
1.1358 + return getAnnotation(annotationClass) != null;
1.1359 + }
1.1360 +
1.1361 + @JavaScriptBody(args = {}, body = "return this.anno;")
1.1362 + private Object getAnnotationData() {
1.1363 + throw new UnsupportedOperationException();
1.1364 + }
1.1365 +
1.1366 + /**
1.1367 + * @since 1.5
1.1368 + */
1.1369 + public Annotation[] getAnnotations() {
1.1370 + Object data = getAnnotationData();
1.1371 + return data == null ? new Annotation[0] : AnnotationImpl.create(data);
1.1372 + }
1.1373 +
1.1374 + /**
1.1375 + * @since 1.5
1.1376 + */
1.1377 + public Annotation[] getDeclaredAnnotations() {
1.1378 + throw new UnsupportedOperationException();
1.1379 + }
1.1380 +
1.1381 + @JavaScriptBody(args = "type", body = ""
1.1382 + + "var c = vm.java_lang_Class(true);"
1.1383 + + "c.jvmName = type;"
1.1384 + + "c.primitive = true;"
1.1385 + + "return c;"
1.1386 + )
1.1387 + native static Class getPrimitiveClass(String type);
1.1388 +
1.1389 + @JavaScriptBody(args = {}, body =
1.1390 + "return vm.desiredAssertionStatus ? vm.desiredAssertionStatus : false;"
1.1391 + )
1.1392 + public native boolean desiredAssertionStatus();
1.1393 +}