emul/mini/src/main/java/java/lang/Class.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 05 Feb 2013 08:27:42 +0100
branchemul
changeset 666 8338ab1991e6
parent 643 a2c0afa35c09
parent 658 578cea5b4359
child 747 ae352b763959
permissions -rw-r--r--
Merging improved (inner) annotation support to allow its usage in emul branch
     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 hasProperty(cls, prop);
   407     }
   408 
   409 
   410     /**
   411      * Determines if the specified {@code Class} object represents an
   412      * interface type.
   413      *
   414      * @return  {@code true} if this object represents an interface;
   415      *          {@code false} otherwise.
   416      */
   417     public boolean isInterface() {
   418         return (getAccess() & 0x200) != 0;
   419     }
   420     
   421     @JavaScriptBody(args = {}, body = "return this.access;")
   422     private native int getAccess();
   423 
   424 
   425     /**
   426      * Determines if this {@code Class} object represents an array class.
   427      *
   428      * @return  {@code true} if this object represents an array class;
   429      *          {@code false} otherwise.
   430      * @since   JDK1.1
   431      */
   432     public boolean isArray() {
   433         return hasProperty(this, "array"); // NOI18N
   434     }
   435 
   436 
   437     /**
   438      * Determines if the specified {@code Class} object represents a
   439      * primitive type.
   440      *
   441      * <p> There are nine predefined {@code Class} objects to represent
   442      * the eight primitive types and void.  These are created by the Java
   443      * Virtual Machine, and have the same names as the primitive types that
   444      * they represent, namely {@code boolean}, {@code byte},
   445      * {@code char}, {@code short}, {@code int},
   446      * {@code long}, {@code float}, and {@code double}.
   447      *
   448      * <p> These objects may only be accessed via the following public static
   449      * final variables, and are the only {@code Class} objects for which
   450      * this method returns {@code true}.
   451      *
   452      * @return true if and only if this class represents a primitive type
   453      *
   454      * @see     java.lang.Boolean#TYPE
   455      * @see     java.lang.Character#TYPE
   456      * @see     java.lang.Byte#TYPE
   457      * @see     java.lang.Short#TYPE
   458      * @see     java.lang.Integer#TYPE
   459      * @see     java.lang.Long#TYPE
   460      * @see     java.lang.Float#TYPE
   461      * @see     java.lang.Double#TYPE
   462      * @see     java.lang.Void#TYPE
   463      * @since JDK1.1
   464      */
   465     @JavaScriptBody(args = {}, body = 
   466            "if (this.primitive) return true;"
   467         + "else return false;"
   468     )
   469     public native boolean isPrimitive();
   470 
   471     /**
   472      * Returns true if this {@code Class} object represents an annotation
   473      * type.  Note that if this method returns true, {@link #isInterface()}
   474      * would also return true, as all annotation types are also interfaces.
   475      *
   476      * @return {@code true} if this class object represents an annotation
   477      *      type; {@code false} otherwise
   478      * @since 1.5
   479      */
   480     public boolean isAnnotation() {
   481         return (getModifiers() & ANNOTATION) != 0;
   482     }
   483 
   484     /**
   485      * Returns {@code true} if this class is a synthetic class;
   486      * returns {@code false} otherwise.
   487      * @return {@code true} if and only if this class is a synthetic class as
   488      *         defined by the Java Language Specification.
   489      * @since 1.5
   490      */
   491     public boolean isSynthetic() {
   492         return (getModifiers() & SYNTHETIC) != 0;
   493     }
   494 
   495     /**
   496      * Returns the  name of the entity (class, interface, array class,
   497      * primitive type, or void) represented by this {@code Class} object,
   498      * as a {@code String}.
   499      *
   500      * <p> If this class object represents a reference type that is not an
   501      * array type then the binary name of the class is returned, as specified
   502      * by
   503      * <cite>The Java&trade; Language Specification</cite>.
   504      *
   505      * <p> If this class object represents a primitive type or void, then the
   506      * name returned is a {@code String} equal to the Java language
   507      * keyword corresponding to the primitive type or void.
   508      *
   509      * <p> If this class object represents a class of arrays, then the internal
   510      * form of the name consists of the name of the element type preceded by
   511      * one or more '{@code [}' characters representing the depth of the array
   512      * nesting.  The encoding of element type names is as follows:
   513      *
   514      * <blockquote><table summary="Element types and encodings">
   515      * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
   516      * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
   517      * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
   518      * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
   519      * <tr><td> class or interface
   520      *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
   521      * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
   522      * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
   523      * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
   524      * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
   525      * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
   526      * </table></blockquote>
   527      *
   528      * <p> The class or interface name <i>classname</i> is the binary name of
   529      * the class specified above.
   530      *
   531      * <p> Examples:
   532      * <blockquote><pre>
   533      * String.class.getName()
   534      *     returns "java.lang.String"
   535      * byte.class.getName()
   536      *     returns "byte"
   537      * (new Object[3]).getClass().getName()
   538      *     returns "[Ljava.lang.Object;"
   539      * (new int[3][4][5][6][7][8][9]).getClass().getName()
   540      *     returns "[[[[[[[I"
   541      * </pre></blockquote>
   542      *
   543      * @return  the name of the class or interface
   544      *          represented by this object.
   545      */
   546     public String getName() {
   547         return jvmName().replace('/', '.');
   548     }
   549 
   550     @JavaScriptBody(args = {}, body = "return this.jvmName;")
   551     private native String jvmName();
   552 
   553     
   554     /**
   555      * Returns an array of {@code TypeVariable} objects that represent the
   556      * type variables declared by the generic declaration represented by this
   557      * {@code GenericDeclaration} object, in declaration order.  Returns an
   558      * array of length 0 if the underlying generic declaration declares no type
   559      * variables.
   560      *
   561      * @return an array of {@code TypeVariable} objects that represent
   562      *     the type variables declared by this generic declaration
   563      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
   564      *     signature of this generic declaration does not conform to
   565      *     the format specified in
   566      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   567      * @since 1.5
   568      */
   569     public TypeVariable<Class<T>>[] getTypeParameters() {
   570         throw new UnsupportedOperationException();
   571     }
   572  
   573     /**
   574      * Returns the {@code Class} representing the superclass of the entity
   575      * (class, interface, primitive type or void) represented by this
   576      * {@code Class}.  If this {@code Class} represents either the
   577      * {@code Object} class, an interface, a primitive type, or void, then
   578      * null is returned.  If this object represents an array class then the
   579      * {@code Class} object representing the {@code Object} class is
   580      * returned.
   581      *
   582      * @return the superclass of the class represented by this object.
   583      */
   584     @JavaScriptBody(args = {}, body = "return this.superclass;")
   585     public native Class<? super T> getSuperclass();
   586 
   587     /**
   588      * Returns the Java language modifiers for this class or interface, encoded
   589      * in an integer. The modifiers consist of the Java Virtual Machine's
   590      * constants for {@code public}, {@code protected},
   591      * {@code private}, {@code final}, {@code static},
   592      * {@code abstract} and {@code interface}; they should be decoded
   593      * using the methods of class {@code Modifier}.
   594      *
   595      * <p> If the underlying class is an array class, then its
   596      * {@code public}, {@code private} and {@code protected}
   597      * modifiers are the same as those of its component type.  If this
   598      * {@code Class} represents a primitive type or void, its
   599      * {@code public} modifier is always {@code true}, and its
   600      * {@code protected} and {@code private} modifiers are always
   601      * {@code false}. If this object represents an array class, a
   602      * primitive type or void, then its {@code final} modifier is always
   603      * {@code true} and its interface modifier is always
   604      * {@code false}. The values of its other modifiers are not determined
   605      * by this specification.
   606      *
   607      * <p> The modifier encodings are defined in <em>The Java Virtual Machine
   608      * Specification</em>, table 4.1.
   609      *
   610      * @return the {@code int} representing the modifiers for this class
   611      * @see     java.lang.reflect.Modifier
   612      * @since JDK1.1
   613      */
   614     public int getModifiers() {
   615         return getAccess();
   616     }
   617 
   618 
   619     /**
   620      * Returns the simple name of the underlying class as given in the
   621      * source code. Returns an empty string if the underlying class is
   622      * anonymous.
   623      *
   624      * <p>The simple name of an array is the simple name of the
   625      * component type with "[]" appended.  In particular the simple
   626      * name of an array whose component type is anonymous is "[]".
   627      *
   628      * @return the simple name of the underlying class
   629      * @since 1.5
   630      */
   631     public String getSimpleName() {
   632         if (isArray())
   633             return getComponentType().getSimpleName()+"[]";
   634 
   635         String simpleName = getSimpleBinaryName();
   636         if (simpleName == null) { // top level class
   637             simpleName = getName();
   638             return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
   639         }
   640         // According to JLS3 "Binary Compatibility" (13.1) the binary
   641         // name of non-package classes (not top level) is the binary
   642         // name of the immediately enclosing class followed by a '$' followed by:
   643         // (for nested and inner classes): the simple name.
   644         // (for local classes): 1 or more digits followed by the simple name.
   645         // (for anonymous classes): 1 or more digits.
   646 
   647         // Since getSimpleBinaryName() will strip the binary name of
   648         // the immediatly enclosing class, we are now looking at a
   649         // string that matches the regular expression "\$[0-9]*"
   650         // followed by a simple name (considering the simple of an
   651         // anonymous class to be the empty string).
   652 
   653         // Remove leading "\$[0-9]*" from the name
   654         int length = simpleName.length();
   655         if (length < 1 || simpleName.charAt(0) != '$')
   656             throw new IllegalStateException("Malformed class name");
   657         int index = 1;
   658         while (index < length && isAsciiDigit(simpleName.charAt(index)))
   659             index++;
   660         // Eventually, this is the empty string iff this is an anonymous class
   661         return simpleName.substring(index);
   662     }
   663 
   664     /**
   665      * Returns the "simple binary name" of the underlying class, i.e.,
   666      * the binary name without the leading enclosing class name.
   667      * Returns {@code null} if the underlying class is a top level
   668      * class.
   669      */
   670     private String getSimpleBinaryName() {
   671         Class<?> enclosingClass = null; // XXX getEnclosingClass();
   672         if (enclosingClass == null) // top level class
   673             return null;
   674         // Otherwise, strip the enclosing class' name
   675         try {
   676             return getName().substring(enclosingClass.getName().length());
   677         } catch (IndexOutOfBoundsException ex) {
   678             throw new IllegalStateException("Malformed class name");
   679         }
   680     }
   681 
   682     /**
   683      * Returns an array containing {@code Field} objects reflecting all
   684      * the accessible public fields of the class or interface represented by
   685      * this {@code Class} object.  The elements in the array returned are
   686      * not sorted and are not in any particular order.  This method returns an
   687      * array of length 0 if the class or interface has no accessible public
   688      * fields, or if it represents an array class, a primitive type, or void.
   689      *
   690      * <p> Specifically, if this {@code Class} object represents a class,
   691      * this method returns the public fields of this class and of all its
   692      * superclasses.  If this {@code Class} object represents an
   693      * interface, this method returns the fields of this interface and of all
   694      * its superinterfaces.
   695      *
   696      * <p> The implicit length field for array class is not reflected by this
   697      * method. User code should use the methods of class {@code Array} to
   698      * manipulate arrays.
   699      *
   700      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
   701      *
   702      * @return the array of {@code Field} objects representing the
   703      * public fields
   704      * @exception  SecurityException
   705      *             If a security manager, <i>s</i>, is present and any of the
   706      *             following conditions is met:
   707      *
   708      *             <ul>
   709      *
   710      *             <li> invocation of
   711      *             {@link SecurityManager#checkMemberAccess
   712      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   713      *             access to the fields within this class
   714      *
   715      *             <li> the caller's class loader is not the same as or an
   716      *             ancestor of the class loader for the current class and
   717      *             invocation of {@link SecurityManager#checkPackageAccess
   718      *             s.checkPackageAccess()} denies access to the package
   719      *             of this class
   720      *
   721      *             </ul>
   722      *
   723      * @since JDK1.1
   724      */
   725     public Field[] getFields() throws SecurityException {
   726         throw new SecurityException();
   727     }
   728 
   729     /**
   730      * Returns an array containing {@code Method} objects reflecting all
   731      * the public <em>member</em> methods of the class or interface represented
   732      * by this {@code Class} object, including those declared by the class
   733      * or interface and those inherited from superclasses and
   734      * superinterfaces.  Array classes return all the (public) member methods
   735      * inherited from the {@code Object} class.  The elements in the array
   736      * returned are not sorted and are not in any particular order.  This
   737      * method returns an array of length 0 if this {@code Class} object
   738      * represents a class or interface that has no public member methods, or if
   739      * this {@code Class} object represents a primitive type or void.
   740      *
   741      * <p> The class initialization method {@code <clinit>} is not
   742      * included in the returned array. If the class declares multiple public
   743      * member methods with the same parameter types, they are all included in
   744      * the returned array.
   745      *
   746      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
   747      *
   748      * @return the array of {@code Method} objects representing the
   749      * public methods of this class
   750      * @exception  SecurityException
   751      *             If a security manager, <i>s</i>, is present and any of the
   752      *             following conditions is met:
   753      *
   754      *             <ul>
   755      *
   756      *             <li> invocation of
   757      *             {@link SecurityManager#checkMemberAccess
   758      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   759      *             access to the methods within this class
   760      *
   761      *             <li> the caller's class loader is not the same as or an
   762      *             ancestor of the class loader for the current class and
   763      *             invocation of {@link SecurityManager#checkPackageAccess
   764      *             s.checkPackageAccess()} denies access to the package
   765      *             of this class
   766      *
   767      *             </ul>
   768      *
   769      * @since JDK1.1
   770      */
   771     public Method[] getMethods() throws SecurityException {
   772         return MethodImpl.findMethods(this, 0x01);
   773     }
   774 
   775     /**
   776      * Returns a {@code Field} object that reflects the specified public
   777      * member field of the class or interface represented by this
   778      * {@code Class} object. The {@code name} parameter is a
   779      * {@code String} specifying the simple name of the desired field.
   780      *
   781      * <p> The field to be reflected is determined by the algorithm that
   782      * follows.  Let C be the class represented by this object:
   783      * <OL>
   784      * <LI> If C declares a public field with the name specified, that is the
   785      *      field to be reflected.</LI>
   786      * <LI> If no field was found in step 1 above, this algorithm is applied
   787      *      recursively to each direct superinterface of C. The direct
   788      *      superinterfaces are searched in the order they were declared.</LI>
   789      * <LI> If no field was found in steps 1 and 2 above, and C has a
   790      *      superclass S, then this algorithm is invoked recursively upon S.
   791      *      If C has no superclass, then a {@code NoSuchFieldException}
   792      *      is thrown.</LI>
   793      * </OL>
   794      *
   795      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
   796      *
   797      * @param name the field name
   798      * @return  the {@code Field} object of this class specified by
   799      * {@code name}
   800      * @exception NoSuchFieldException if a field with the specified name is
   801      *              not found.
   802      * @exception NullPointerException if {@code name} is {@code null}
   803      * @exception  SecurityException
   804      *             If a security manager, <i>s</i>, is present and any of the
   805      *             following conditions is met:
   806      *
   807      *             <ul>
   808      *
   809      *             <li> invocation of
   810      *             {@link SecurityManager#checkMemberAccess
   811      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   812      *             access to the field
   813      *
   814      *             <li> the caller's class loader is not the same as or an
   815      *             ancestor of the class loader for the current class and
   816      *             invocation of {@link SecurityManager#checkPackageAccess
   817      *             s.checkPackageAccess()} denies access to the package
   818      *             of this class
   819      *
   820      *             </ul>
   821      *
   822      * @since JDK1.1
   823      */
   824     public Field getField(String name)
   825         throws SecurityException {
   826         throw new SecurityException();
   827     }
   828     
   829     
   830     /**
   831      * Returns a {@code Method} object that reflects the specified public
   832      * member method of the class or interface represented by this
   833      * {@code Class} object. The {@code name} parameter is a
   834      * {@code String} specifying the simple name of the desired method. The
   835      * {@code parameterTypes} parameter is an array of {@code Class}
   836      * objects that identify the method's formal parameter types, in declared
   837      * order. If {@code parameterTypes} is {@code null}, it is
   838      * treated as if it were an empty array.
   839      *
   840      * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
   841      * {@code NoSuchMethodException} is raised. Otherwise, the method to
   842      * be reflected is determined by the algorithm that follows.  Let C be the
   843      * class represented by this object:
   844      * <OL>
   845      * <LI> C is searched for any <I>matching methods</I>. If no matching
   846      *      method is found, the algorithm of step 1 is invoked recursively on
   847      *      the superclass of C.</LI>
   848      * <LI> If no method was found in step 1 above, the superinterfaces of C
   849      *      are searched for a matching method. If any such method is found, it
   850      *      is reflected.</LI>
   851      * </OL>
   852      *
   853      * To find a matching method in a class C:&nbsp; If C declares exactly one
   854      * public method with the specified name and exactly the same formal
   855      * parameter types, that is the method reflected. If more than one such
   856      * method is found in C, and one of these methods has a return type that is
   857      * more specific than any of the others, that method is reflected;
   858      * otherwise one of the methods is chosen arbitrarily.
   859      *
   860      * <p>Note that there may be more than one matching method in a
   861      * class because while the Java language forbids a class to
   862      * declare multiple methods with the same signature but different
   863      * return types, the Java virtual machine does not.  This
   864      * increased flexibility in the virtual machine can be used to
   865      * implement various language features.  For example, covariant
   866      * returns can be implemented with {@linkplain
   867      * java.lang.reflect.Method#isBridge bridge methods}; the bridge
   868      * method and the method being overridden would have the same
   869      * signature but different return types.
   870      *
   871      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
   872      *
   873      * @param name the name of the method
   874      * @param parameterTypes the list of parameters
   875      * @return the {@code Method} object that matches the specified
   876      * {@code name} and {@code parameterTypes}
   877      * @exception NoSuchMethodException if a matching method is not found
   878      *            or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
   879      * @exception NullPointerException if {@code name} is {@code null}
   880      * @exception  SecurityException
   881      *             If a security manager, <i>s</i>, is present and any of the
   882      *             following conditions is met:
   883      *
   884      *             <ul>
   885      *
   886      *             <li> invocation of
   887      *             {@link SecurityManager#checkMemberAccess
   888      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   889      *             access to the method
   890      *
   891      *             <li> the caller's class loader is not the same as or an
   892      *             ancestor of the class loader for the current class and
   893      *             invocation of {@link SecurityManager#checkPackageAccess
   894      *             s.checkPackageAccess()} denies access to the package
   895      *             of this class
   896      *
   897      *             </ul>
   898      *
   899      * @since JDK1.1
   900      */
   901     public Method getMethod(String name, Class<?>... parameterTypes)
   902         throws SecurityException, NoSuchMethodException {
   903         Method m = MethodImpl.findMethod(this, name, parameterTypes);
   904         if (m == null) {
   905             StringBuilder sb = new StringBuilder();
   906             sb.append(getName()).append('.').append(name).append('(');
   907             String sep = "";
   908             for (int i = 0; i < parameterTypes.length; i++) {
   909                 sb.append(sep).append(parameterTypes[i].getName());
   910                 sep = ", ";
   911             }
   912             sb.append(')');
   913             throw new NoSuchMethodException(sb.toString());
   914         }
   915         return m;
   916     }
   917     
   918     /**
   919      * Returns an array of {@code Method} objects reflecting all the
   920      * methods declared by the class or interface represented by this
   921      * {@code Class} object. This includes public, protected, default
   922      * (package) access, and private methods, but excludes inherited methods.
   923      * The elements in the array returned are not sorted and are not in any
   924      * particular order.  This method returns an array of length 0 if the class
   925      * or interface declares no methods, or if this {@code Class} object
   926      * represents a primitive type, an array class, or void.  The class
   927      * initialization method {@code <clinit>} is not included in the
   928      * returned array. If the class declares multiple public member methods
   929      * with the same parameter types, they are all included in the returned
   930      * array.
   931      *
   932      * <p> See <em>The Java Language Specification</em>, section 8.2.
   933      *
   934      * @return    the array of {@code Method} objects representing all the
   935      * declared methods of this class
   936      * @exception  SecurityException
   937      *             If a security manager, <i>s</i>, is present and any of the
   938      *             following conditions is met:
   939      *
   940      *             <ul>
   941      *
   942      *             <li> invocation of
   943      *             {@link SecurityManager#checkMemberAccess
   944      *             s.checkMemberAccess(this, Member.DECLARED)} denies
   945      *             access to the declared methods within this class
   946      *
   947      *             <li> the caller's class loader is not the same as or an
   948      *             ancestor of the class loader for the current class and
   949      *             invocation of {@link SecurityManager#checkPackageAccess
   950      *             s.checkPackageAccess()} denies access to the package
   951      *             of this class
   952      *
   953      *             </ul>
   954      *
   955      * @since JDK1.1
   956      */
   957     public Method[] getDeclaredMethods() throws SecurityException {
   958         throw new SecurityException();
   959     }
   960     
   961     /**
   962      * Character.isDigit answers {@code true} to some non-ascii
   963      * digits.  This one does not.
   964      */
   965     private static boolean isAsciiDigit(char c) {
   966         return '0' <= c && c <= '9';
   967     }
   968 
   969     /**
   970      * Returns the canonical name of the underlying class as
   971      * defined by the Java Language Specification.  Returns null if
   972      * the underlying class does not have a canonical name (i.e., if
   973      * it is a local or anonymous class or an array whose component
   974      * type does not have a canonical name).
   975      * @return the canonical name of the underlying class if it exists, and
   976      * {@code null} otherwise.
   977      * @since 1.5
   978      */
   979     public String getCanonicalName() {
   980         if (isArray()) {
   981             String canonicalName = getComponentType().getCanonicalName();
   982             if (canonicalName != null)
   983                 return canonicalName + "[]";
   984             else
   985                 return null;
   986         }
   987 //        if (isLocalOrAnonymousClass())
   988 //            return null;
   989 //        Class<?> enclosingClass = getEnclosingClass();
   990         Class<?> enclosingClass = null;
   991         if (enclosingClass == null) { // top level class
   992             return getName();
   993         } else {
   994             String enclosingName = enclosingClass.getCanonicalName();
   995             if (enclosingName == null)
   996                 return null;
   997             return enclosingName + "." + getSimpleName();
   998         }
   999     }
  1000 
  1001     /**
  1002      * Finds a resource with a given name.  The rules for searching resources
  1003      * associated with a given class are implemented by the defining
  1004      * {@linkplain ClassLoader class loader} of the class.  This method
  1005      * delegates to this object's class loader.  If this object was loaded by
  1006      * the bootstrap class loader, the method delegates to {@link
  1007      * ClassLoader#getSystemResourceAsStream}.
  1008      *
  1009      * <p> Before delegation, an absolute resource name is constructed from the
  1010      * given resource name using this algorithm:
  1011      *
  1012      * <ul>
  1013      *
  1014      * <li> If the {@code name} begins with a {@code '/'}
  1015      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
  1016      * portion of the {@code name} following the {@code '/'}.
  1017      *
  1018      * <li> Otherwise, the absolute name is of the following form:
  1019      *
  1020      * <blockquote>
  1021      *   {@code modified_package_name/name}
  1022      * </blockquote>
  1023      *
  1024      * <p> Where the {@code modified_package_name} is the package name of this
  1025      * object with {@code '/'} substituted for {@code '.'}
  1026      * (<tt>'&#92;u002e'</tt>).
  1027      *
  1028      * </ul>
  1029      *
  1030      * @param  name name of the desired resource
  1031      * @return      A {@link java.io.InputStream} object or {@code null} if
  1032      *              no resource with this name is found
  1033      * @throws  NullPointerException If {@code name} is {@code null}
  1034      * @since  JDK1.1
  1035      */
  1036      public InputStream getResourceAsStream(String name) {
  1037         name = resolveName(name);
  1038         byte[] arr = getResourceAsStream0(name);
  1039         return arr == null ? null : new ByteArrayInputStream(arr);
  1040      }
  1041      
  1042      @JavaScriptBody(args = "name", body = 
  1043          "return (vm.loadBytes) ? vm.loadBytes(name) : null;"
  1044      )
  1045      private static native byte[] getResourceAsStream0(String name);
  1046 
  1047     /**
  1048      * Finds a resource with a given name.  The rules for searching resources
  1049      * associated with a given class are implemented by the defining
  1050      * {@linkplain ClassLoader class loader} of the class.  This method
  1051      * delegates to this object's class loader.  If this object was loaded by
  1052      * the bootstrap class loader, the method delegates to {@link
  1053      * ClassLoader#getSystemResource}.
  1054      *
  1055      * <p> Before delegation, an absolute resource name is constructed from the
  1056      * given resource name using this algorithm:
  1057      *
  1058      * <ul>
  1059      *
  1060      * <li> If the {@code name} begins with a {@code '/'}
  1061      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
  1062      * portion of the {@code name} following the {@code '/'}.
  1063      *
  1064      * <li> Otherwise, the absolute name is of the following form:
  1065      *
  1066      * <blockquote>
  1067      *   {@code modified_package_name/name}
  1068      * </blockquote>
  1069      *
  1070      * <p> Where the {@code modified_package_name} is the package name of this
  1071      * object with {@code '/'} substituted for {@code '.'}
  1072      * (<tt>'&#92;u002e'</tt>).
  1073      *
  1074      * </ul>
  1075      *
  1076      * @param  name name of the desired resource
  1077      * @return      A  {@link java.net.URL} object or {@code null} if no
  1078      *              resource with this name is found
  1079      * @since  JDK1.1
  1080      */
  1081     public java.net.URL getResource(String name) {
  1082         InputStream is = getResourceAsStream(name);
  1083         return is == null ? null : newResourceURL(URL.class, "res:/" + name, is);
  1084     }
  1085     
  1086     @JavaScriptBody(args = { "url", "spec", "is" }, body = 
  1087         "var u = url.cnstr(true);\n"
  1088       + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n"
  1089       + "return u;"
  1090     )
  1091     private static native URL newResourceURL(Class<URL> url, String spec, InputStream is);
  1092 
  1093    /**
  1094      * Add a package name prefix if the name is not absolute Remove leading "/"
  1095      * if name is absolute
  1096      */
  1097     private String resolveName(String name) {
  1098         if (name == null) {
  1099             return name;
  1100         }
  1101         if (!name.startsWith("/")) {
  1102             Class<?> c = this;
  1103             while (c.isArray()) {
  1104                 c = c.getComponentType();
  1105             }
  1106             String baseName = c.getName();
  1107             int index = baseName.lastIndexOf('.');
  1108             if (index != -1) {
  1109                 name = baseName.substring(0, index).replace('.', '/')
  1110                     +"/"+name;
  1111             }
  1112         } else {
  1113             name = name.substring(1);
  1114         }
  1115         return name;
  1116     }
  1117     
  1118     /**
  1119      * Returns the class loader for the class.  Some implementations may use
  1120      * null to represent the bootstrap class loader. This method will return
  1121      * null in such implementations if this class was loaded by the bootstrap
  1122      * class loader.
  1123      *
  1124      * <p> If a security manager is present, and the caller's class loader is
  1125      * not null and the caller's class loader is not the same as or an ancestor of
  1126      * the class loader for the class whose class loader is requested, then
  1127      * this method calls the security manager's {@code checkPermission}
  1128      * method with a {@code RuntimePermission("getClassLoader")}
  1129      * permission to ensure it's ok to access the class loader for the class.
  1130      *
  1131      * <p>If this object
  1132      * represents a primitive type or void, null is returned.
  1133      *
  1134      * @return  the class loader that loaded the class or interface
  1135      *          represented by this object.
  1136      * @throws SecurityException
  1137      *    if a security manager exists and its
  1138      *    {@code checkPermission} method denies
  1139      *    access to the class loader for the class.
  1140      * @see java.lang.ClassLoader
  1141      * @see SecurityManager#checkPermission
  1142      * @see java.lang.RuntimePermission
  1143      */
  1144     public ClassLoader getClassLoader() {
  1145         throw new SecurityException();
  1146     }
  1147 
  1148     /**
  1149      * Determines the interfaces implemented by the class or interface
  1150      * represented by this object.
  1151      *
  1152      * <p> If this object represents a class, the return value is an array
  1153      * containing objects representing all interfaces implemented by the
  1154      * class. The order of the interface objects in the array corresponds to
  1155      * the order of the interface names in the {@code implements} clause
  1156      * of the declaration of the class represented by this object. For
  1157      * example, given the declaration:
  1158      * <blockquote>
  1159      * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
  1160      * </blockquote>
  1161      * suppose the value of {@code s} is an instance of
  1162      * {@code Shimmer}; the value of the expression:
  1163      * <blockquote>
  1164      * {@code s.getClass().getInterfaces()[0]}
  1165      * </blockquote>
  1166      * is the {@code Class} object that represents interface
  1167      * {@code FloorWax}; and the value of:
  1168      * <blockquote>
  1169      * {@code s.getClass().getInterfaces()[1]}
  1170      * </blockquote>
  1171      * is the {@code Class} object that represents interface
  1172      * {@code DessertTopping}.
  1173      *
  1174      * <p> If this object represents an interface, the array contains objects
  1175      * representing all interfaces extended by the interface. The order of the
  1176      * interface objects in the array corresponds to the order of the interface
  1177      * names in the {@code extends} clause of the declaration of the
  1178      * interface represented by this object.
  1179      *
  1180      * <p> If this object represents a class or interface that implements no
  1181      * interfaces, the method returns an array of length 0.
  1182      *
  1183      * <p> If this object represents a primitive type or void, the method
  1184      * returns an array of length 0.
  1185      *
  1186      * @return an array of interfaces implemented by this class.
  1187      */
  1188     public native Class<?>[] getInterfaces();
  1189     
  1190     /**
  1191      * Returns the {@code Class} representing the component type of an
  1192      * array.  If this class does not represent an array class this method
  1193      * returns null.
  1194      *
  1195      * @return the {@code Class} representing the component type of this
  1196      * class if this class is an array
  1197      * @see     java.lang.reflect.Array
  1198      * @since JDK1.1
  1199      */
  1200     public Class<?> getComponentType() {
  1201         if (isArray()) {
  1202             try {
  1203                 return getComponentType0();
  1204             } catch (ClassNotFoundException cnfe) {
  1205                 throw new IllegalStateException(cnfe);
  1206             }
  1207         }
  1208         return null;
  1209     }
  1210 
  1211     private Class<?> getComponentType0() throws ClassNotFoundException {
  1212         String n = getName().substring(1);
  1213         switch (n.charAt(0)) {
  1214             case 'L': 
  1215                 n = n.substring(1, n.length() - 1);
  1216                 return Class.forName(n);
  1217             case 'I':
  1218                 return Integer.TYPE;
  1219             case 'J':
  1220                 return Long.TYPE;
  1221             case 'D':
  1222                 return Double.TYPE;
  1223             case 'F':
  1224                 return Float.TYPE;
  1225             case 'B':
  1226                 return Byte.TYPE;
  1227             case 'Z':
  1228                 return Boolean.TYPE;
  1229             case 'S':
  1230                 return Short.TYPE;
  1231             case 'V':
  1232                 return Void.TYPE;
  1233             case 'C':
  1234                 return Character.TYPE;
  1235             case '[':
  1236                 return defineArray(n);
  1237             default:
  1238                 throw new ClassNotFoundException("Unknown component type of " + getName());
  1239         }
  1240     }
  1241     
  1242     @JavaScriptBody(args = { "sig" }, body = 
  1243         "var c = Array[sig];\n" +
  1244         "if (c) return c;\n" +
  1245         "c = vm.java_lang_Class(true);\n" +
  1246         "c.jvmName = sig;\n" +
  1247         "c.superclass = vm.java_lang_Object(false).$class;\n" +
  1248         "c.array = true;\n" +
  1249         "Array[sig] = c;\n" +
  1250         "return c;"
  1251     )
  1252     private static native Class<?> defineArray(String sig);
  1253     
  1254     /**
  1255      * Returns true if and only if this class was declared as an enum in the
  1256      * source code.
  1257      *
  1258      * @return true if and only if this class was declared as an enum in the
  1259      *     source code
  1260      * @since 1.5
  1261      */
  1262     public boolean isEnum() {
  1263         // An enum must both directly extend java.lang.Enum and have
  1264         // the ENUM bit set; classes for specialized enum constants
  1265         // don't do the former.
  1266         return (this.getModifiers() & ENUM) != 0 &&
  1267         this.getSuperclass() == java.lang.Enum.class;
  1268     }
  1269 
  1270     /**
  1271      * Casts an object to the class or interface represented
  1272      * by this {@code Class} object.
  1273      *
  1274      * @param obj the object to be cast
  1275      * @return the object after casting, or null if obj is null
  1276      *
  1277      * @throws ClassCastException if the object is not
  1278      * null and is not assignable to the type T.
  1279      *
  1280      * @since 1.5
  1281      */
  1282     public T cast(Object obj) {
  1283         if (obj != null && !isInstance(obj))
  1284             throw new ClassCastException(cannotCastMsg(obj));
  1285         return (T) obj;
  1286     }
  1287 
  1288     private String cannotCastMsg(Object obj) {
  1289         return "Cannot cast " + obj.getClass().getName() + " to " + getName();
  1290     }
  1291 
  1292     /**
  1293      * Casts this {@code Class} object to represent a subclass of the class
  1294      * represented by the specified class object.  Checks that that the cast
  1295      * is valid, and throws a {@code ClassCastException} if it is not.  If
  1296      * this method succeeds, it always returns a reference to this class object.
  1297      *
  1298      * <p>This method is useful when a client needs to "narrow" the type of
  1299      * a {@code Class} object to pass it to an API that restricts the
  1300      * {@code Class} objects that it is willing to accept.  A cast would
  1301      * generate a compile-time warning, as the correctness of the cast
  1302      * could not be checked at runtime (because generic types are implemented
  1303      * by erasure).
  1304      *
  1305      * @return this {@code Class} object, cast to represent a subclass of
  1306      *    the specified class object.
  1307      * @throws ClassCastException if this {@code Class} object does not
  1308      *    represent a subclass of the specified class (here "subclass" includes
  1309      *    the class itself).
  1310      * @since 1.5
  1311      */
  1312     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
  1313         if (clazz.isAssignableFrom(this))
  1314             return (Class<? extends U>) this;
  1315         else
  1316             throw new ClassCastException(this.toString());
  1317     }
  1318 
  1319     @JavaScriptBody(args = { "ac" }, 
  1320         body = 
  1321           "if (this.anno) {"
  1322         + "  return this.anno['L' + ac.jvmName + ';'];"
  1323         + "} else return null;"
  1324     )
  1325     private Object getAnnotationData(Class<?> annotationClass) {
  1326         throw new UnsupportedOperationException();
  1327     }
  1328     /**
  1329      * @throws NullPointerException {@inheritDoc}
  1330      * @since 1.5
  1331      */
  1332     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
  1333         Object data = getAnnotationData(annotationClass);
  1334         return data == null ? null : AnnotationImpl.create(annotationClass, data);
  1335     }
  1336 
  1337     /**
  1338      * @throws NullPointerException {@inheritDoc}
  1339      * @since 1.5
  1340      */
  1341     @JavaScriptBody(args = { "ac" }, 
  1342         body = "if (this.anno && this.anno['L' + ac.jvmName + ';']) { return true; }"
  1343         + "else return false;"
  1344     )
  1345     public boolean isAnnotationPresent(
  1346         Class<? extends Annotation> annotationClass) {
  1347         if (annotationClass == null)
  1348             throw new NullPointerException();
  1349 
  1350         return getAnnotation(annotationClass) != null;
  1351     }
  1352 
  1353     @JavaScriptBody(args = {}, body = "return this.anno;")
  1354     private Object getAnnotationData() {
  1355         throw new UnsupportedOperationException();
  1356     }
  1357 
  1358     /**
  1359      * @since 1.5
  1360      */
  1361     public Annotation[] getAnnotations() {
  1362         Object data = getAnnotationData();
  1363         return data == null ? new Annotation[0] : AnnotationImpl.create(data);
  1364     }
  1365 
  1366     /**
  1367      * @since 1.5
  1368      */
  1369     public Annotation[] getDeclaredAnnotations()  {
  1370         throw new UnsupportedOperationException();
  1371     }
  1372 
  1373     @JavaScriptBody(args = "type", body = ""
  1374         + "var c = vm.java_lang_Class(true);"
  1375         + "c.jvmName = type;"
  1376         + "c.primitive = true;"
  1377         + "return c;"
  1378     )
  1379     native static Class getPrimitiveClass(String type);
  1380 
  1381     @JavaScriptBody(args = {}, body = 
  1382         "return vm.desiredAssertionStatus ? vm.desiredAssertionStatus : false;"
  1383     )
  1384     public native boolean desiredAssertionStatus();
  1385 }