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