rt/emul/mini/src/main/java/java/lang/Class.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 747 emul/mini/src/main/java/java/lang/Class.java@ae352b763959
child 886 88540bb74300
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     1 /*
     2  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang;
    27 
    28 import java.io.ByteArrayInputStream;
    29 import org.apidesign.bck2brwsr.emul.reflect.AnnotationImpl;
    30 import java.io.InputStream;
    31 import java.lang.annotation.Annotation;
    32 import java.lang.reflect.Field;
    33 import java.lang.reflect.Method;
    34 import java.lang.reflect.TypeVariable;
    35 import java.net.URL;
    36 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    37 import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
    38 
    39 /**
    40  * Instances of the class {@code Class} represent classes and
    41  * interfaces in a running Java application.  An enum is a kind of
    42  * class and an annotation is a kind of interface.  Every array also
    43  * belongs to a class that is reflected as a {@code Class} object
    44  * that is shared by all arrays with the same element type and number
    45  * of dimensions.  The primitive Java types ({@code boolean},
    46  * {@code byte}, {@code char}, {@code short},
    47  * {@code int}, {@code long}, {@code float}, and
    48  * {@code double}), and the keyword {@code void} are also
    49  * represented as {@code Class} objects.
    50  *
    51  * <p> {@code Class} has no public constructor. Instead {@code Class}
    52  * objects are constructed automatically by the Java Virtual Machine as classes
    53  * are loaded and by calls to the {@code defineClass} method in the class
    54  * loader.
    55  *
    56  * <p> The following example uses a {@code Class} object to print the
    57  * class name of an object:
    58  *
    59  * <p> <blockquote><pre>
    60  *     void printClassName(Object obj) {
    61  *         System.out.println("The class of " + obj +
    62  *                            " is " + obj.getClass().getName());
    63  *     }
    64  * </pre></blockquote>
    65  *
    66  * <p> It is also possible to get the {@code Class} object for a named
    67  * type (or for void) using a class literal.  See Section 15.8.2 of
    68  * <cite>The Java&trade; Language Specification</cite>.
    69  * For example:
    70  *
    71  * <p> <blockquote>
    72  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
    73  * </blockquote>
    74  *
    75  * @param <T> the type of the class modeled by this {@code Class}
    76  * object.  For example, the type of {@code String.class} is {@code
    77  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
    78  * unknown.
    79  *
    80  * @author  unascribed
    81  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
    82  * @since   JDK1.0
    83  */
    84 public final
    85     class Class<T> implements java.io.Serializable,
    86                               java.lang.reflect.GenericDeclaration,
    87                               java.lang.reflect.Type,
    88                               java.lang.reflect.AnnotatedElement {
    89     private static final int ANNOTATION= 0x00002000;
    90     private static final int ENUM      = 0x00004000;
    91     private static final int SYNTHETIC = 0x00001000;
    92 
    93     /*
    94      * Constructor. Only the Java Virtual Machine creates Class
    95      * objects.
    96      */
    97     private Class() {}
    98 
    99 
   100     /**
   101      * Converts the object to a string. The string representation is the
   102      * string "class" or "interface", followed by a space, and then by the
   103      * fully qualified name of the class in the format returned by
   104      * {@code getName}.  If this {@code Class} object represents a
   105      * primitive type, this method returns the name of the primitive type.  If
   106      * this {@code Class} object represents void this method returns
   107      * "void".
   108      *
   109      * @return a string representation of this class object.
   110      */
   111     public String toString() {
   112         return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
   113             + getName();
   114     }
   115 
   116 
   117     /**
   118      * Returns the {@code Class} object associated with the class or
   119      * interface with the given string name.  Invoking this method is
   120      * equivalent to:
   121      *
   122      * <blockquote>
   123      *  {@code Class.forName(className, true, currentLoader)}
   124      * </blockquote>
   125      *
   126      * where {@code currentLoader} denotes the defining class loader of
   127      * the current class.
   128      *
   129      * <p> For example, the following code fragment returns the
   130      * runtime {@code Class} descriptor for the class named
   131      * {@code java.lang.Thread}:
   132      *
   133      * <blockquote>
   134      *   {@code Class t = Class.forName("java.lang.Thread")}
   135      * </blockquote>
   136      * <p>
   137      * A call to {@code forName("X")} causes the class named
   138      * {@code X} to be initialized.
   139      *
   140      * @param      className   the fully qualified name of the desired class.
   141      * @return     the {@code Class} object for the class with the
   142      *             specified name.
   143      * @exception LinkageError if the linkage fails
   144      * @exception ExceptionInInitializerError if the initialization provoked
   145      *            by this method fails
   146      * @exception ClassNotFoundException if the class cannot be located
   147      */
   148     public static Class<?> forName(String className)
   149     throws ClassNotFoundException {
   150         if (className.startsWith("[")) {
   151             Class<?> arrType = defineArray(className);
   152             Class<?> c = arrType;
   153             while (c != null && c.isArray()) {
   154                 c = c.getComponentType0(); // verify component type is sane
   155             }
   156             return arrType;
   157         }
   158         Class<?> c = loadCls(className, className.replace('.', '_'));
   159         if (c == null) {
   160             throw new ClassNotFoundException(className);
   161         }
   162         return c;
   163     }
   164 
   165 
   166     /**
   167      * Returns the {@code Class} object associated with the class or
   168      * interface with the given string name, using the given class loader.
   169      * Given the fully qualified name for a class or interface (in the same
   170      * format returned by {@code getName}) this method attempts to
   171      * locate, load, and link the class or interface.  The specified class
   172      * loader is used to load the class or interface.  If the parameter
   173      * {@code loader} is null, the class is loaded through the bootstrap
   174      * class loader.  The class is initialized only if the
   175      * {@code initialize} parameter is {@code true} and if it has
   176      * not been initialized earlier.
   177      *
   178      * <p> If {@code name} denotes a primitive type or void, an attempt
   179      * will be made to locate a user-defined class in the unnamed package whose
   180      * name is {@code name}. Therefore, this method cannot be used to
   181      * obtain any of the {@code Class} objects representing primitive
   182      * types or void.
   183      *
   184      * <p> If {@code name} denotes an array class, the component type of
   185      * the array class is loaded but not initialized.
   186      *
   187      * <p> For example, in an instance method the expression:
   188      *
   189      * <blockquote>
   190      *  {@code Class.forName("Foo")}
   191      * </blockquote>
   192      *
   193      * is equivalent to:
   194      *
   195      * <blockquote>
   196      *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
   197      * </blockquote>
   198      *
   199      * Note that this method throws errors related to loading, linking or
   200      * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
   201      * Java Language Specification</em>.
   202      * Note that this method does not check whether the requested class
   203      * is accessible to its caller.
   204      *
   205      * <p> If the {@code loader} is {@code null}, and a security
   206      * manager is present, and the caller's class loader is not null, then this
   207      * method calls the security manager's {@code checkPermission} method
   208      * with a {@code RuntimePermission("getClassLoader")} permission to
   209      * ensure it's ok to access the bootstrap class loader.
   210      *
   211      * @param name       fully qualified name of the desired class
   212      * @param initialize whether the class must be initialized
   213      * @param loader     class loader from which the class must be loaded
   214      * @return           class object representing the desired class
   215      *
   216      * @exception LinkageError if the linkage fails
   217      * @exception ExceptionInInitializerError if the initialization provoked
   218      *            by this method fails
   219      * @exception ClassNotFoundException if the class cannot be located by
   220      *            the specified class loader
   221      *
   222      * @see       java.lang.Class#forName(String)
   223      * @see       java.lang.ClassLoader
   224      * @since     1.2
   225      */
   226     public static Class<?> forName(String name, boolean initialize,
   227                                    ClassLoader loader)
   228         throws ClassNotFoundException
   229     {
   230         return forName(name);
   231     }
   232     
   233     @JavaScriptBody(args = {"n", "c" }, body =
   234         "if (!vm[c]) {\n"
   235       + "  if (vm.loadClass) {\n"
   236       + "    vm.loadClass(n);\n"
   237       + "  }\n"
   238       + "  if (!vm[c]) return null;\n"
   239       + "}\n"
   240       + "vm[c](false);"
   241       + "return vm[c].$class;"
   242     )
   243     private static native Class<?> loadCls(String n, String c);
   244 
   245 
   246     /**
   247      * Creates a new instance of the class represented by this {@code Class}
   248      * object.  The class is instantiated as if by a {@code new}
   249      * expression with an empty argument list.  The class is initialized if it
   250      * has not already been initialized.
   251      *
   252      * <p>Note that this method propagates any exception thrown by the
   253      * nullary constructor, including a checked exception.  Use of
   254      * this method effectively bypasses the compile-time exception
   255      * checking that would otherwise be performed by the compiler.
   256      * The {@link
   257      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
   258      * Constructor.newInstance} method avoids this problem by wrapping
   259      * any exception thrown by the constructor in a (checked) {@link
   260      * java.lang.reflect.InvocationTargetException}.
   261      *
   262      * @return     a newly allocated instance of the class represented by this
   263      *             object.
   264      * @exception  IllegalAccessException  if the class or its nullary
   265      *               constructor is not accessible.
   266      * @exception  InstantiationException
   267      *               if this {@code Class} represents an abstract class,
   268      *               an interface, an array class, a primitive type, or void;
   269      *               or if the class has no nullary constructor;
   270      *               or if the instantiation fails for some other reason.
   271      * @exception  ExceptionInInitializerError if the initialization
   272      *               provoked by this method fails.
   273      * @exception  SecurityException
   274      *             If a security manager, <i>s</i>, is present and any of the
   275      *             following conditions is met:
   276      *
   277      *             <ul>
   278      *
   279      *             <li> invocation of
   280      *             {@link SecurityManager#checkMemberAccess
   281      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   282      *             creation of new instances of this class
   283      *
   284      *             <li> the caller's class loader is not the same as or an
   285      *             ancestor of the class loader for the current class and
   286      *             invocation of {@link SecurityManager#checkPackageAccess
   287      *             s.checkPackageAccess()} denies access to the package
   288      *             of this class
   289      *
   290      *             </ul>
   291      *
   292      */
   293     @JavaScriptBody(args = { "self", "illegal" }, body =
   294           "\nvar c = self.cnstr;"
   295         + "\nif (c['cons__V']) {"
   296         + "\n  if ((c.cons__V.access & 0x1) != 0) {"
   297         + "\n    var inst = c();"
   298         + "\n    c.cons__V.call(inst);"
   299         + "\n    return inst;"
   300         + "\n  }"
   301         + "\n  return illegal;"
   302         + "\n}"
   303         + "\nreturn null;"
   304     )
   305     private static native Object newInstance0(Class<?> self, Object illegal);
   306     
   307     public T newInstance()
   308         throws InstantiationException, IllegalAccessException
   309     {
   310         Object illegal = new Object();
   311         Object inst = newInstance0(this, illegal);
   312         if (inst == null) {
   313             throw new InstantiationException(getName());
   314         }
   315         if (inst == illegal) {
   316             throw new IllegalAccessException();
   317         }
   318         return (T)inst;
   319     }
   320 
   321     /**
   322      * Determines if the specified {@code Object} is assignment-compatible
   323      * with the object represented by this {@code Class}.  This method is
   324      * the dynamic equivalent of the Java language {@code instanceof}
   325      * operator. The method returns {@code true} if the specified
   326      * {@code Object} argument is non-null and can be cast to the
   327      * reference type represented by this {@code Class} object without
   328      * raising a {@code ClassCastException.} It returns {@code false}
   329      * otherwise.
   330      *
   331      * <p> Specifically, if this {@code Class} object represents a
   332      * declared class, this method returns {@code true} if the specified
   333      * {@code Object} argument is an instance of the represented class (or
   334      * of any of its subclasses); it returns {@code false} otherwise. If
   335      * this {@code Class} object represents an array class, this method
   336      * returns {@code true} if the specified {@code Object} argument
   337      * can be converted to an object of the array class by an identity
   338      * conversion or by a widening reference conversion; it returns
   339      * {@code false} otherwise. If this {@code Class} object
   340      * represents an interface, this method returns {@code true} if the
   341      * class or any superclass of the specified {@code Object} argument
   342      * implements this interface; it returns {@code false} otherwise. If
   343      * this {@code Class} object represents a primitive type, this method
   344      * returns {@code false}.
   345      *
   346      * @param   obj the object to check
   347      * @return  true if {@code obj} is an instance of this class
   348      *
   349      * @since JDK1.1
   350      */
   351     public boolean isInstance(Object obj) {
   352         if (obj == null) {
   353             return false;
   354         }
   355         if (isArray()) {
   356             return isAssignableFrom(obj.getClass());
   357         }
   358         
   359         String prop = "$instOf_" + getName().replace('.', '_');
   360         return hasProperty(obj, prop);
   361     }
   362     
   363     @JavaScriptBody(args = { "who", "prop" }, body = 
   364         "if (who[prop]) return true; else return false;"
   365     )
   366     private static native boolean hasProperty(Object who, String prop);
   367 
   368 
   369     /**
   370      * Determines if the class or interface represented by this
   371      * {@code Class} object is either the same as, or is a superclass or
   372      * superinterface of, the class or interface represented by the specified
   373      * {@code Class} parameter. It returns {@code true} if so;
   374      * otherwise it returns {@code false}. If this {@code Class}
   375      * object represents a primitive type, this method returns
   376      * {@code true} if the specified {@code Class} parameter is
   377      * exactly this {@code Class} object; otherwise it returns
   378      * {@code false}.
   379      *
   380      * <p> Specifically, this method tests whether the type represented by the
   381      * specified {@code Class} parameter can be converted to the type
   382      * represented by this {@code Class} object via an identity conversion
   383      * or via a widening reference conversion. See <em>The Java Language
   384      * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
   385      *
   386      * @param cls the {@code Class} object to be checked
   387      * @return the {@code boolean} value indicating whether objects of the
   388      * type {@code cls} can be assigned to objects of this class
   389      * @exception NullPointerException if the specified Class parameter is
   390      *            null.
   391      * @since JDK1.1
   392      */
   393     public boolean isAssignableFrom(Class<?> cls) {
   394         if (this == cls) {
   395             return true;
   396         }
   397         
   398         if (isArray()) {
   399             final Class<?> cmpType = cls.getComponentType();
   400             if (isPrimitive()) {
   401                 return this == cmpType;
   402             }
   403             return cmpType != null && getComponentType().isAssignableFrom(cmpType);
   404         }
   405         String prop = "$instOf_" + getName().replace('.', '_');
   406         return hasCnstrProperty(cls, prop);
   407     }
   408 
   409     @JavaScriptBody(args = { "who", "prop" }, body = 
   410         "if (who.cnstr.prototype[prop]) return true; else return false;"
   411     )
   412     private static native boolean hasCnstrProperty(Object who, String prop);
   413     
   414     
   415     /**
   416      * Determines if the specified {@code Class} object represents an
   417      * interface type.
   418      *
   419      * @return  {@code true} if this object represents an interface;
   420      *          {@code false} otherwise.
   421      */
   422     public boolean isInterface() {
   423         return (getAccess() & 0x200) != 0;
   424     }
   425     
   426     @JavaScriptBody(args = {}, body = "return this.access;")
   427     private native int getAccess();
   428 
   429 
   430     /**
   431      * Determines if this {@code Class} object represents an array class.
   432      *
   433      * @return  {@code true} if this object represents an array class;
   434      *          {@code false} otherwise.
   435      * @since   JDK1.1
   436      */
   437     public boolean isArray() {
   438         return hasProperty(this, "array"); // NOI18N
   439     }
   440 
   441 
   442     /**
   443      * Determines if the specified {@code Class} object represents a
   444      * primitive type.
   445      *
   446      * <p> There are nine predefined {@code Class} objects to represent
   447      * the eight primitive types and void.  These are created by the Java
   448      * Virtual Machine, and have the same names as the primitive types that
   449      * they represent, namely {@code boolean}, {@code byte},
   450      * {@code char}, {@code short}, {@code int},
   451      * {@code long}, {@code float}, and {@code double}.
   452      *
   453      * <p> These objects may only be accessed via the following public static
   454      * final variables, and are the only {@code Class} objects for which
   455      * this method returns {@code true}.
   456      *
   457      * @return true if and only if this class represents a primitive type
   458      *
   459      * @see     java.lang.Boolean#TYPE
   460      * @see     java.lang.Character#TYPE
   461      * @see     java.lang.Byte#TYPE
   462      * @see     java.lang.Short#TYPE
   463      * @see     java.lang.Integer#TYPE
   464      * @see     java.lang.Long#TYPE
   465      * @see     java.lang.Float#TYPE
   466      * @see     java.lang.Double#TYPE
   467      * @see     java.lang.Void#TYPE
   468      * @since JDK1.1
   469      */
   470     @JavaScriptBody(args = {}, body = 
   471            "if (this.primitive) return true;"
   472         + "else return false;"
   473     )
   474     public native boolean isPrimitive();
   475 
   476     /**
   477      * Returns true if this {@code Class} object represents an annotation
   478      * type.  Note that if this method returns true, {@link #isInterface()}
   479      * would also return true, as all annotation types are also interfaces.
   480      *
   481      * @return {@code true} if this class object represents an annotation
   482      *      type; {@code false} otherwise
   483      * @since 1.5
   484      */
   485     public boolean isAnnotation() {
   486         return (getModifiers() & ANNOTATION) != 0;
   487     }
   488 
   489     /**
   490      * Returns {@code true} if this class is a synthetic class;
   491      * returns {@code false} otherwise.
   492      * @return {@code true} if and only if this class is a synthetic class as
   493      *         defined by the Java Language Specification.
   494      * @since 1.5
   495      */
   496     public boolean isSynthetic() {
   497         return (getModifiers() & SYNTHETIC) != 0;
   498     }
   499 
   500     /**
   501      * Returns the  name of the entity (class, interface, array class,
   502      * primitive type, or void) represented by this {@code Class} object,
   503      * as a {@code String}.
   504      *
   505      * <p> If this class object represents a reference type that is not an
   506      * array type then the binary name of the class is returned, as specified
   507      * by
   508      * <cite>The Java&trade; Language Specification</cite>.
   509      *
   510      * <p> If this class object represents a primitive type or void, then the
   511      * name returned is a {@code String} equal to the Java language
   512      * keyword corresponding to the primitive type or void.
   513      *
   514      * <p> If this class object represents a class of arrays, then the internal
   515      * form of the name consists of the name of the element type preceded by
   516      * one or more '{@code [}' characters representing the depth of the array
   517      * nesting.  The encoding of element type names is as follows:
   518      *
   519      * <blockquote><table summary="Element types and encodings">
   520      * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
   521      * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
   522      * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
   523      * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
   524      * <tr><td> class or interface
   525      *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
   526      * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
   527      * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
   528      * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
   529      * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
   530      * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
   531      * </table></blockquote>
   532      *
   533      * <p> The class or interface name <i>classname</i> is the binary name of
   534      * the class specified above.
   535      *
   536      * <p> Examples:
   537      * <blockquote><pre>
   538      * String.class.getName()
   539      *     returns "java.lang.String"
   540      * byte.class.getName()
   541      *     returns "byte"
   542      * (new Object[3]).getClass().getName()
   543      *     returns "[Ljava.lang.Object;"
   544      * (new int[3][4][5][6][7][8][9]).getClass().getName()
   545      *     returns "[[[[[[[I"
   546      * </pre></blockquote>
   547      *
   548      * @return  the name of the class or interface
   549      *          represented by this object.
   550      */
   551     public String getName() {
   552         return jvmName().replace('/', '.');
   553     }
   554 
   555     @JavaScriptBody(args = {}, body = "return this.jvmName;")
   556     private native String jvmName();
   557 
   558     
   559     /**
   560      * Returns an array of {@code TypeVariable} objects that represent the
   561      * type variables declared by the generic declaration represented by this
   562      * {@code GenericDeclaration} object, in declaration order.  Returns an
   563      * array of length 0 if the underlying generic declaration declares no type
   564      * variables.
   565      *
   566      * @return an array of {@code TypeVariable} objects that represent
   567      *     the type variables declared by this generic declaration
   568      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
   569      *     signature of this generic declaration does not conform to
   570      *     the format specified in
   571      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   572      * @since 1.5
   573      */
   574     public TypeVariable<Class<T>>[] getTypeParameters() {
   575         throw new UnsupportedOperationException();
   576     }
   577  
   578     /**
   579      * Returns the {@code Class} representing the superclass of the entity
   580      * (class, interface, primitive type or void) represented by this
   581      * {@code Class}.  If this {@code Class} represents either the
   582      * {@code Object} class, an interface, a primitive type, or void, then
   583      * null is returned.  If this object represents an array class then the
   584      * {@code Class} object representing the {@code Object} class is
   585      * returned.
   586      *
   587      * @return the superclass of the class represented by this object.
   588      */
   589     @JavaScriptBody(args = {}, body = "return this.superclass;")
   590     public native Class<? super T> getSuperclass();
   591 
   592     /**
   593      * Returns the Java language modifiers for this class or interface, encoded
   594      * in an integer. The modifiers consist of the Java Virtual Machine's
   595      * constants for {@code public}, {@code protected},
   596      * {@code private}, {@code final}, {@code static},
   597      * {@code abstract} and {@code interface}; they should be decoded
   598      * using the methods of class {@code Modifier}.
   599      *
   600      * <p> If the underlying class is an array class, then its
   601      * {@code public}, {@code private} and {@code protected}
   602      * modifiers are the same as those of its component type.  If this
   603      * {@code Class} represents a primitive type or void, its
   604      * {@code public} modifier is always {@code true}, and its
   605      * {@code protected} and {@code private} modifiers are always
   606      * {@code false}. If this object represents an array class, a
   607      * primitive type or void, then its {@code final} modifier is always
   608      * {@code true} and its interface modifier is always
   609      * {@code false}. The values of its other modifiers are not determined
   610      * by this specification.
   611      *
   612      * <p> The modifier encodings are defined in <em>The Java Virtual Machine
   613      * Specification</em>, table 4.1.
   614      *
   615      * @return the {@code int} representing the modifiers for this class
   616      * @see     java.lang.reflect.Modifier
   617      * @since JDK1.1
   618      */
   619     public int getModifiers() {
   620         return getAccess();
   621     }
   622 
   623 
   624     /**
   625      * Returns the simple name of the underlying class as given in the
   626      * source code. Returns an empty string if the underlying class is
   627      * anonymous.
   628      *
   629      * <p>The simple name of an array is the simple name of the
   630      * component type with "[]" appended.  In particular the simple
   631      * name of an array whose component type is anonymous is "[]".
   632      *
   633      * @return the simple name of the underlying class
   634      * @since 1.5
   635      */
   636     public String getSimpleName() {
   637         if (isArray())
   638             return getComponentType().getSimpleName()+"[]";
   639 
   640         String simpleName = getSimpleBinaryName();
   641         if (simpleName == null) { // top level class
   642             simpleName = getName();
   643             return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
   644         }
   645         // According to JLS3 "Binary Compatibility" (13.1) the binary
   646         // name of non-package classes (not top level) is the binary
   647         // name of the immediately enclosing class followed by a '$' followed by:
   648         // (for nested and inner classes): the simple name.
   649         // (for local classes): 1 or more digits followed by the simple name.
   650         // (for anonymous classes): 1 or more digits.
   651 
   652         // Since getSimpleBinaryName() will strip the binary name of
   653         // the immediatly enclosing class, we are now looking at a
   654         // string that matches the regular expression "\$[0-9]*"
   655         // followed by a simple name (considering the simple of an
   656         // anonymous class to be the empty string).
   657 
   658         // Remove leading "\$[0-9]*" from the name
   659         int length = simpleName.length();
   660         if (length < 1 || simpleName.charAt(0) != '$')
   661             throw new IllegalStateException("Malformed class name");
   662         int index = 1;
   663         while (index < length && isAsciiDigit(simpleName.charAt(index)))
   664             index++;
   665         // Eventually, this is the empty string iff this is an anonymous class
   666         return simpleName.substring(index);
   667     }
   668 
   669     /**
   670      * Returns the "simple binary name" of the underlying class, i.e.,
   671      * the binary name without the leading enclosing class name.
   672      * Returns {@code null} if the underlying class is a top level
   673      * class.
   674      */
   675     private String getSimpleBinaryName() {
   676         Class<?> enclosingClass = null; // XXX getEnclosingClass();
   677         if (enclosingClass == null) // top level class
   678             return null;
   679         // Otherwise, strip the enclosing class' name
   680         try {
   681             return getName().substring(enclosingClass.getName().length());
   682         } catch (IndexOutOfBoundsException ex) {
   683             throw new IllegalStateException("Malformed class name");
   684         }
   685     }
   686 
   687     /**
   688      * Returns an array containing {@code Field} objects reflecting all
   689      * the accessible public fields of the class or interface represented by
   690      * this {@code Class} object.  The elements in the array returned are
   691      * not sorted and are not in any particular order.  This method returns an
   692      * array of length 0 if the class or interface has no accessible public
   693      * fields, or if it represents an array class, a primitive type, or void.
   694      *
   695      * <p> Specifically, if this {@code Class} object represents a class,
   696      * this method returns the public fields of this class and of all its
   697      * superclasses.  If this {@code Class} object represents an
   698      * interface, this method returns the fields of this interface and of all
   699      * its superinterfaces.
   700      *
   701      * <p> The implicit length field for array class is not reflected by this
   702      * method. User code should use the methods of class {@code Array} to
   703      * manipulate arrays.
   704      *
   705      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
   706      *
   707      * @return the array of {@code Field} objects representing the
   708      * public fields
   709      * @exception  SecurityException
   710      *             If a security manager, <i>s</i>, is present and any of the
   711      *             following conditions is met:
   712      *
   713      *             <ul>
   714      *
   715      *             <li> invocation of
   716      *             {@link SecurityManager#checkMemberAccess
   717      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   718      *             access to the fields within this class
   719      *
   720      *             <li> the caller's class loader is not the same as or an
   721      *             ancestor of the class loader for the current class and
   722      *             invocation of {@link SecurityManager#checkPackageAccess
   723      *             s.checkPackageAccess()} denies access to the package
   724      *             of this class
   725      *
   726      *             </ul>
   727      *
   728      * @since JDK1.1
   729      */
   730     public Field[] getFields() throws SecurityException {
   731         throw new SecurityException();
   732     }
   733 
   734     /**
   735      * Returns an array containing {@code Method} objects reflecting all
   736      * the public <em>member</em> methods of the class or interface represented
   737      * by this {@code Class} object, including those declared by the class
   738      * or interface and those inherited from superclasses and
   739      * superinterfaces.  Array classes return all the (public) member methods
   740      * inherited from the {@code Object} class.  The elements in the array
   741      * returned are not sorted and are not in any particular order.  This
   742      * method returns an array of length 0 if this {@code Class} object
   743      * represents a class or interface that has no public member methods, or if
   744      * this {@code Class} object represents a primitive type or void.
   745      *
   746      * <p> The class initialization method {@code <clinit>} is not
   747      * included in the returned array. If the class declares multiple public
   748      * member methods with the same parameter types, they are all included in
   749      * the returned array.
   750      *
   751      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
   752      *
   753      * @return the array of {@code Method} objects representing the
   754      * public methods of this class
   755      * @exception  SecurityException
   756      *             If a security manager, <i>s</i>, is present and any of the
   757      *             following conditions is met:
   758      *
   759      *             <ul>
   760      *
   761      *             <li> invocation of
   762      *             {@link SecurityManager#checkMemberAccess
   763      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   764      *             access to the methods within this class
   765      *
   766      *             <li> the caller's class loader is not the same as or an
   767      *             ancestor of the class loader for the current class and
   768      *             invocation of {@link SecurityManager#checkPackageAccess
   769      *             s.checkPackageAccess()} denies access to the package
   770      *             of this class
   771      *
   772      *             </ul>
   773      *
   774      * @since JDK1.1
   775      */
   776     public Method[] getMethods() throws SecurityException {
   777         return MethodImpl.findMethods(this, 0x01);
   778     }
   779 
   780     /**
   781      * Returns a {@code Field} object that reflects the specified public
   782      * member field of the class or interface represented by this
   783      * {@code Class} object. The {@code name} parameter is a
   784      * {@code String} specifying the simple name of the desired field.
   785      *
   786      * <p> The field to be reflected is determined by the algorithm that
   787      * follows.  Let C be the class represented by this object:
   788      * <OL>
   789      * <LI> If C declares a public field with the name specified, that is the
   790      *      field to be reflected.</LI>
   791      * <LI> If no field was found in step 1 above, this algorithm is applied
   792      *      recursively to each direct superinterface of C. The direct
   793      *      superinterfaces are searched in the order they were declared.</LI>
   794      * <LI> If no field was found in steps 1 and 2 above, and C has a
   795      *      superclass S, then this algorithm is invoked recursively upon S.
   796      *      If C has no superclass, then a {@code NoSuchFieldException}
   797      *      is thrown.</LI>
   798      * </OL>
   799      *
   800      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
   801      *
   802      * @param name the field name
   803      * @return  the {@code Field} object of this class specified by
   804      * {@code name}
   805      * @exception NoSuchFieldException if a field with the specified name is
   806      *              not found.
   807      * @exception NullPointerException if {@code name} is {@code null}
   808      * @exception  SecurityException
   809      *             If a security manager, <i>s</i>, is present and any of the
   810      *             following conditions is met:
   811      *
   812      *             <ul>
   813      *
   814      *             <li> invocation of
   815      *             {@link SecurityManager#checkMemberAccess
   816      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   817      *             access to the field
   818      *
   819      *             <li> the caller's class loader is not the same as or an
   820      *             ancestor of the class loader for the current class and
   821      *             invocation of {@link SecurityManager#checkPackageAccess
   822      *             s.checkPackageAccess()} denies access to the package
   823      *             of this class
   824      *
   825      *             </ul>
   826      *
   827      * @since JDK1.1
   828      */
   829     public Field getField(String name)
   830         throws SecurityException {
   831         throw new SecurityException();
   832     }
   833     
   834     
   835     /**
   836      * Returns a {@code Method} object that reflects the specified public
   837      * member method of the class or interface represented by this
   838      * {@code Class} object. The {@code name} parameter is a
   839      * {@code String} specifying the simple name of the desired method. The
   840      * {@code parameterTypes} parameter is an array of {@code Class}
   841      * objects that identify the method's formal parameter types, in declared
   842      * order. If {@code parameterTypes} is {@code null}, it is
   843      * treated as if it were an empty array.
   844      *
   845      * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
   846      * {@code NoSuchMethodException} is raised. Otherwise, the method to
   847      * be reflected is determined by the algorithm that follows.  Let C be the
   848      * class represented by this object:
   849      * <OL>
   850      * <LI> C is searched for any <I>matching methods</I>. If no matching
   851      *      method is found, the algorithm of step 1 is invoked recursively on
   852      *      the superclass of C.</LI>
   853      * <LI> If no method was found in step 1 above, the superinterfaces of C
   854      *      are searched for a matching method. If any such method is found, it
   855      *      is reflected.</LI>
   856      * </OL>
   857      *
   858      * To find a matching method in a class C:&nbsp; If C declares exactly one
   859      * public method with the specified name and exactly the same formal
   860      * parameter types, that is the method reflected. If more than one such
   861      * method is found in C, and one of these methods has a return type that is
   862      * more specific than any of the others, that method is reflected;
   863      * otherwise one of the methods is chosen arbitrarily.
   864      *
   865      * <p>Note that there may be more than one matching method in a
   866      * class because while the Java language forbids a class to
   867      * declare multiple methods with the same signature but different
   868      * return types, the Java virtual machine does not.  This
   869      * increased flexibility in the virtual machine can be used to
   870      * implement various language features.  For example, covariant
   871      * returns can be implemented with {@linkplain
   872      * java.lang.reflect.Method#isBridge bridge methods}; the bridge
   873      * method and the method being overridden would have the same
   874      * signature but different return types.
   875      *
   876      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
   877      *
   878      * @param name the name of the method
   879      * @param parameterTypes the list of parameters
   880      * @return the {@code Method} object that matches the specified
   881      * {@code name} and {@code parameterTypes}
   882      * @exception NoSuchMethodException if a matching method is not found
   883      *            or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
   884      * @exception NullPointerException if {@code name} is {@code null}
   885      * @exception  SecurityException
   886      *             If a security manager, <i>s</i>, is present and any of the
   887      *             following conditions is met:
   888      *
   889      *             <ul>
   890      *
   891      *             <li> invocation of
   892      *             {@link SecurityManager#checkMemberAccess
   893      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   894      *             access to the method
   895      *
   896      *             <li> the caller's class loader is not the same as or an
   897      *             ancestor of the class loader for the current class and
   898      *             invocation of {@link SecurityManager#checkPackageAccess
   899      *             s.checkPackageAccess()} denies access to the package
   900      *             of this class
   901      *
   902      *             </ul>
   903      *
   904      * @since JDK1.1
   905      */
   906     public Method getMethod(String name, Class<?>... parameterTypes)
   907         throws SecurityException, NoSuchMethodException {
   908         Method m = MethodImpl.findMethod(this, name, parameterTypes);
   909         if (m == null) {
   910             StringBuilder sb = new StringBuilder();
   911             sb.append(getName()).append('.').append(name).append('(');
   912             String sep = "";
   913             for (int i = 0; i < parameterTypes.length; i++) {
   914                 sb.append(sep).append(parameterTypes[i].getName());
   915                 sep = ", ";
   916             }
   917             sb.append(')');
   918             throw new NoSuchMethodException(sb.toString());
   919         }
   920         return m;
   921     }
   922     
   923     /**
   924      * Returns an array of {@code Method} objects reflecting all the
   925      * methods declared by the class or interface represented by this
   926      * {@code Class} object. This includes public, protected, default
   927      * (package) access, and private methods, but excludes inherited methods.
   928      * The elements in the array returned are not sorted and are not in any
   929      * particular order.  This method returns an array of length 0 if the class
   930      * or interface declares no methods, or if this {@code Class} object
   931      * represents a primitive type, an array class, or void.  The class
   932      * initialization method {@code <clinit>} is not included in the
   933      * returned array. If the class declares multiple public member methods
   934      * with the same parameter types, they are all included in the returned
   935      * array.
   936      *
   937      * <p> See <em>The Java Language Specification</em>, section 8.2.
   938      *
   939      * @return    the array of {@code Method} objects representing all the
   940      * declared methods of this class
   941      * @exception  SecurityException
   942      *             If a security manager, <i>s</i>, is present and any of the
   943      *             following conditions is met:
   944      *
   945      *             <ul>
   946      *
   947      *             <li> invocation of
   948      *             {@link SecurityManager#checkMemberAccess
   949      *             s.checkMemberAccess(this, Member.DECLARED)} denies
   950      *             access to the declared methods within this class
   951      *
   952      *             <li> the caller's class loader is not the same as or an
   953      *             ancestor of the class loader for the current class and
   954      *             invocation of {@link SecurityManager#checkPackageAccess
   955      *             s.checkPackageAccess()} denies access to the package
   956      *             of this class
   957      *
   958      *             </ul>
   959      *
   960      * @since JDK1.1
   961      */
   962     public Method[] getDeclaredMethods() throws SecurityException {
   963         throw new SecurityException();
   964     }
   965     
   966     /**
   967      * Character.isDigit answers {@code true} to some non-ascii
   968      * digits.  This one does not.
   969      */
   970     private static boolean isAsciiDigit(char c) {
   971         return '0' <= c && c <= '9';
   972     }
   973 
   974     /**
   975      * Returns the canonical name of the underlying class as
   976      * defined by the Java Language Specification.  Returns null if
   977      * the underlying class does not have a canonical name (i.e., if
   978      * it is a local or anonymous class or an array whose component
   979      * type does not have a canonical name).
   980      * @return the canonical name of the underlying class if it exists, and
   981      * {@code null} otherwise.
   982      * @since 1.5
   983      */
   984     public String getCanonicalName() {
   985         if (isArray()) {
   986             String canonicalName = getComponentType().getCanonicalName();
   987             if (canonicalName != null)
   988                 return canonicalName + "[]";
   989             else
   990                 return null;
   991         }
   992 //        if (isLocalOrAnonymousClass())
   993 //            return null;
   994 //        Class<?> enclosingClass = getEnclosingClass();
   995         Class<?> enclosingClass = null;
   996         if (enclosingClass == null) { // top level class
   997             return getName();
   998         } else {
   999             String enclosingName = enclosingClass.getCanonicalName();
  1000             if (enclosingName == null)
  1001                 return null;
  1002             return enclosingName + "." + getSimpleName();
  1003         }
  1004     }
  1005 
  1006     /**
  1007      * Finds a resource with a given name.  The rules for searching resources
  1008      * associated with a given class are implemented by the defining
  1009      * {@linkplain ClassLoader class loader} of the class.  This method
  1010      * delegates to this object's class loader.  If this object was loaded by
  1011      * the bootstrap class loader, the method delegates to {@link
  1012      * ClassLoader#getSystemResourceAsStream}.
  1013      *
  1014      * <p> Before delegation, an absolute resource name is constructed from the
  1015      * given resource name using this algorithm:
  1016      *
  1017      * <ul>
  1018      *
  1019      * <li> If the {@code name} begins with a {@code '/'}
  1020      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
  1021      * portion of the {@code name} following the {@code '/'}.
  1022      *
  1023      * <li> Otherwise, the absolute name is of the following form:
  1024      *
  1025      * <blockquote>
  1026      *   {@code modified_package_name/name}
  1027      * </blockquote>
  1028      *
  1029      * <p> Where the {@code modified_package_name} is the package name of this
  1030      * object with {@code '/'} substituted for {@code '.'}
  1031      * (<tt>'&#92;u002e'</tt>).
  1032      *
  1033      * </ul>
  1034      *
  1035      * @param  name name of the desired resource
  1036      * @return      A {@link java.io.InputStream} object or {@code null} if
  1037      *              no resource with this name is found
  1038      * @throws  NullPointerException If {@code name} is {@code null}
  1039      * @since  JDK1.1
  1040      */
  1041      public InputStream getResourceAsStream(String name) {
  1042         name = resolveName(name);
  1043         byte[] arr = getResourceAsStream0(name);
  1044         return arr == null ? null : new ByteArrayInputStream(arr);
  1045      }
  1046      
  1047      @JavaScriptBody(args = "name", body = 
  1048          "return (vm.loadBytes) ? vm.loadBytes(name) : null;"
  1049      )
  1050      private static native byte[] getResourceAsStream0(String name);
  1051 
  1052     /**
  1053      * Finds a resource with a given name.  The rules for searching resources
  1054      * associated with a given class are implemented by the defining
  1055      * {@linkplain ClassLoader class loader} of the class.  This method
  1056      * delegates to this object's class loader.  If this object was loaded by
  1057      * the bootstrap class loader, the method delegates to {@link
  1058      * ClassLoader#getSystemResource}.
  1059      *
  1060      * <p> Before delegation, an absolute resource name is constructed from the
  1061      * given resource name using this algorithm:
  1062      *
  1063      * <ul>
  1064      *
  1065      * <li> If the {@code name} begins with a {@code '/'}
  1066      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
  1067      * portion of the {@code name} following the {@code '/'}.
  1068      *
  1069      * <li> Otherwise, the absolute name is of the following form:
  1070      *
  1071      * <blockquote>
  1072      *   {@code modified_package_name/name}
  1073      * </blockquote>
  1074      *
  1075      * <p> Where the {@code modified_package_name} is the package name of this
  1076      * object with {@code '/'} substituted for {@code '.'}
  1077      * (<tt>'&#92;u002e'</tt>).
  1078      *
  1079      * </ul>
  1080      *
  1081      * @param  name name of the desired resource
  1082      * @return      A  {@link java.net.URL} object or {@code null} if no
  1083      *              resource with this name is found
  1084      * @since  JDK1.1
  1085      */
  1086     public java.net.URL getResource(String name) {
  1087         InputStream is = getResourceAsStream(name);
  1088         return is == null ? null : newResourceURL(URL.class, "res:/" + name, is);
  1089     }
  1090     
  1091     @JavaScriptBody(args = { "url", "spec", "is" }, body = 
  1092         "var u = url.cnstr(true);\n"
  1093       + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n"
  1094       + "return u;"
  1095     )
  1096     private static native URL newResourceURL(Class<URL> url, String spec, InputStream is);
  1097 
  1098    /**
  1099      * Add a package name prefix if the name is not absolute Remove leading "/"
  1100      * if name is absolute
  1101      */
  1102     private String resolveName(String name) {
  1103         if (name == null) {
  1104             return name;
  1105         }
  1106         if (!name.startsWith("/")) {
  1107             Class<?> c = this;
  1108             while (c.isArray()) {
  1109                 c = c.getComponentType();
  1110             }
  1111             String baseName = c.getName();
  1112             int index = baseName.lastIndexOf('.');
  1113             if (index != -1) {
  1114                 name = baseName.substring(0, index).replace('.', '/')
  1115                     +"/"+name;
  1116             }
  1117         } else {
  1118             name = name.substring(1);
  1119         }
  1120         return name;
  1121     }
  1122     
  1123     /**
  1124      * Returns the class loader for the class.  Some implementations may use
  1125      * null to represent the bootstrap class loader. This method will return
  1126      * null in such implementations if this class was loaded by the bootstrap
  1127      * class loader.
  1128      *
  1129      * <p> If a security manager is present, and the caller's class loader is
  1130      * not null and the caller's class loader is not the same as or an ancestor of
  1131      * the class loader for the class whose class loader is requested, then
  1132      * this method calls the security manager's {@code checkPermission}
  1133      * method with a {@code RuntimePermission("getClassLoader")}
  1134      * permission to ensure it's ok to access the class loader for the class.
  1135      *
  1136      * <p>If this object
  1137      * represents a primitive type or void, null is returned.
  1138      *
  1139      * @return  the class loader that loaded the class or interface
  1140      *          represented by this object.
  1141      * @throws SecurityException
  1142      *    if a security manager exists and its
  1143      *    {@code checkPermission} method denies
  1144      *    access to the class loader for the class.
  1145      * @see java.lang.ClassLoader
  1146      * @see SecurityManager#checkPermission
  1147      * @see java.lang.RuntimePermission
  1148      */
  1149     public ClassLoader getClassLoader() {
  1150         throw new SecurityException();
  1151     }
  1152 
  1153     /**
  1154      * Determines the interfaces implemented by the class or interface
  1155      * represented by this object.
  1156      *
  1157      * <p> If this object represents a class, the return value is an array
  1158      * containing objects representing all interfaces implemented by the
  1159      * class. The order of the interface objects in the array corresponds to
  1160      * the order of the interface names in the {@code implements} clause
  1161      * of the declaration of the class represented by this object. For
  1162      * example, given the declaration:
  1163      * <blockquote>
  1164      * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
  1165      * </blockquote>
  1166      * suppose the value of {@code s} is an instance of
  1167      * {@code Shimmer}; the value of the expression:
  1168      * <blockquote>
  1169      * {@code s.getClass().getInterfaces()[0]}
  1170      * </blockquote>
  1171      * is the {@code Class} object that represents interface
  1172      * {@code FloorWax}; and the value of:
  1173      * <blockquote>
  1174      * {@code s.getClass().getInterfaces()[1]}
  1175      * </blockquote>
  1176      * is the {@code Class} object that represents interface
  1177      * {@code DessertTopping}.
  1178      *
  1179      * <p> If this object represents an interface, the array contains objects
  1180      * representing all interfaces extended by the interface. The order of the
  1181      * interface objects in the array corresponds to the order of the interface
  1182      * names in the {@code extends} clause of the declaration of the
  1183      * interface represented by this object.
  1184      *
  1185      * <p> If this object represents a class or interface that implements no
  1186      * interfaces, the method returns an array of length 0.
  1187      *
  1188      * <p> If this object represents a primitive type or void, the method
  1189      * returns an array of length 0.
  1190      *
  1191      * @return an array of interfaces implemented by this class.
  1192      */
  1193     public native Class<?>[] getInterfaces();
  1194     
  1195     /**
  1196      * Returns the {@code Class} representing the component type of an
  1197      * array.  If this class does not represent an array class this method
  1198      * returns null.
  1199      *
  1200      * @return the {@code Class} representing the component type of this
  1201      * class if this class is an array
  1202      * @see     java.lang.reflect.Array
  1203      * @since JDK1.1
  1204      */
  1205     public Class<?> getComponentType() {
  1206         if (isArray()) {
  1207             try {
  1208                 return getComponentType0();
  1209             } catch (ClassNotFoundException cnfe) {
  1210                 throw new IllegalStateException(cnfe);
  1211             }
  1212         }
  1213         return null;
  1214     }
  1215 
  1216     private Class<?> getComponentType0() throws ClassNotFoundException {
  1217         String n = getName().substring(1);
  1218         switch (n.charAt(0)) {
  1219             case 'L': 
  1220                 n = n.substring(1, n.length() - 1);
  1221                 return Class.forName(n);
  1222             case 'I':
  1223                 return Integer.TYPE;
  1224             case 'J':
  1225                 return Long.TYPE;
  1226             case 'D':
  1227                 return Double.TYPE;
  1228             case 'F':
  1229                 return Float.TYPE;
  1230             case 'B':
  1231                 return Byte.TYPE;
  1232             case 'Z':
  1233                 return Boolean.TYPE;
  1234             case 'S':
  1235                 return Short.TYPE;
  1236             case 'V':
  1237                 return Void.TYPE;
  1238             case 'C':
  1239                 return Character.TYPE;
  1240             case '[':
  1241                 return defineArray(n);
  1242             default:
  1243                 throw new ClassNotFoundException("Unknown component type of " + getName());
  1244         }
  1245     }
  1246     
  1247     @JavaScriptBody(args = { "sig" }, body = 
  1248         "var c = Array[sig];\n" +
  1249         "if (c) return c;\n" +
  1250         "c = vm.java_lang_Class(true);\n" +
  1251         "c.jvmName = sig;\n" +
  1252         "c.superclass = vm.java_lang_Object(false).$class;\n" +
  1253         "c.array = true;\n" +
  1254         "Array[sig] = c;\n" +
  1255         "return c;"
  1256     )
  1257     private static native Class<?> defineArray(String sig);
  1258     
  1259     /**
  1260      * Returns true if and only if this class was declared as an enum in the
  1261      * source code.
  1262      *
  1263      * @return true if and only if this class was declared as an enum in the
  1264      *     source code
  1265      * @since 1.5
  1266      */
  1267     public boolean isEnum() {
  1268         // An enum must both directly extend java.lang.Enum and have
  1269         // the ENUM bit set; classes for specialized enum constants
  1270         // don't do the former.
  1271         return (this.getModifiers() & ENUM) != 0 &&
  1272         this.getSuperclass() == java.lang.Enum.class;
  1273     }
  1274 
  1275     /**
  1276      * Casts an object to the class or interface represented
  1277      * by this {@code Class} object.
  1278      *
  1279      * @param obj the object to be cast
  1280      * @return the object after casting, or null if obj is null
  1281      *
  1282      * @throws ClassCastException if the object is not
  1283      * null and is not assignable to the type T.
  1284      *
  1285      * @since 1.5
  1286      */
  1287     public T cast(Object obj) {
  1288         if (obj != null && !isInstance(obj))
  1289             throw new ClassCastException(cannotCastMsg(obj));
  1290         return (T) obj;
  1291     }
  1292 
  1293     private String cannotCastMsg(Object obj) {
  1294         return "Cannot cast " + obj.getClass().getName() + " to " + getName();
  1295     }
  1296 
  1297     /**
  1298      * Casts this {@code Class} object to represent a subclass of the class
  1299      * represented by the specified class object.  Checks that that the cast
  1300      * is valid, and throws a {@code ClassCastException} if it is not.  If
  1301      * this method succeeds, it always returns a reference to this class object.
  1302      *
  1303      * <p>This method is useful when a client needs to "narrow" the type of
  1304      * a {@code Class} object to pass it to an API that restricts the
  1305      * {@code Class} objects that it is willing to accept.  A cast would
  1306      * generate a compile-time warning, as the correctness of the cast
  1307      * could not be checked at runtime (because generic types are implemented
  1308      * by erasure).
  1309      *
  1310      * @return this {@code Class} object, cast to represent a subclass of
  1311      *    the specified class object.
  1312      * @throws ClassCastException if this {@code Class} object does not
  1313      *    represent a subclass of the specified class (here "subclass" includes
  1314      *    the class itself).
  1315      * @since 1.5
  1316      */
  1317     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
  1318         if (clazz.isAssignableFrom(this))
  1319             return (Class<? extends U>) this;
  1320         else
  1321             throw new ClassCastException(this.toString());
  1322     }
  1323 
  1324     @JavaScriptBody(args = { "ac" }, 
  1325         body = 
  1326           "if (this.anno) {"
  1327         + "  return this.anno['L' + ac.jvmName + ';'];"
  1328         + "} else return null;"
  1329     )
  1330     private Object getAnnotationData(Class<?> annotationClass) {
  1331         throw new UnsupportedOperationException();
  1332     }
  1333     /**
  1334      * @throws NullPointerException {@inheritDoc}
  1335      * @since 1.5
  1336      */
  1337     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
  1338         Object data = getAnnotationData(annotationClass);
  1339         return data == null ? null : AnnotationImpl.create(annotationClass, data);
  1340     }
  1341 
  1342     /**
  1343      * @throws NullPointerException {@inheritDoc}
  1344      * @since 1.5
  1345      */
  1346     @JavaScriptBody(args = { "ac" }, 
  1347         body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
  1348         + "else return false;"
  1349     )
  1350     public boolean isAnnotationPresent(
  1351         Class<? extends Annotation> annotationClass) {
  1352         if (annotationClass == null)
  1353             throw new NullPointerException();
  1354 
  1355         return getAnnotation(annotationClass) != null;
  1356     }
  1357 
  1358     @JavaScriptBody(args = {}, body = "return this.anno;")
  1359     private Object getAnnotationData() {
  1360         throw new UnsupportedOperationException();
  1361     }
  1362 
  1363     /**
  1364      * @since 1.5
  1365      */
  1366     public Annotation[] getAnnotations() {
  1367         Object data = getAnnotationData();
  1368         return data == null ? new Annotation[0] : AnnotationImpl.create(data);
  1369     }
  1370 
  1371     /**
  1372      * @since 1.5
  1373      */
  1374     public Annotation[] getDeclaredAnnotations()  {
  1375         throw new UnsupportedOperationException();
  1376     }
  1377 
  1378     @JavaScriptBody(args = "type", body = ""
  1379         + "var c = vm.java_lang_Class(true);"
  1380         + "c.jvmName = type;"
  1381         + "c.primitive = true;"
  1382         + "return c;"
  1383     )
  1384     native static Class getPrimitiveClass(String type);
  1385 
  1386     @JavaScriptBody(args = {}, body = 
  1387         "return vm.desiredAssertionStatus ? vm.desiredAssertionStatus : false;"
  1388     )
  1389     public native boolean desiredAssertionStatus();
  1390 }