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