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