emul/src/main/java/java/lang/Class.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 15 Dec 2012 08:17:45 +0100
changeset 322 3884815c0629
parent 321 1848c77df886
child 353 fd38bdad7fb5
permissions -rw-r--r--
Class.forName can load new classes
     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 org.apidesign.bck2brwsr.emul.AnnotationImpl;
    29 import java.io.InputStream;
    30 import java.lang.annotation.Annotation;
    31 import java.lang.reflect.Field;
    32 import java.lang.reflect.Method;
    33 import java.lang.reflect.TypeVariable;
    34 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    35 
    36 /**
    37  * Instances of the class {@code Class} represent classes and
    38  * interfaces in a running Java application.  An enum is a kind of
    39  * class and an annotation is a kind of interface.  Every array also
    40  * belongs to a class that is reflected as a {@code Class} object
    41  * that is shared by all arrays with the same element type and number
    42  * of dimensions.  The primitive Java types ({@code boolean},
    43  * {@code byte}, {@code char}, {@code short},
    44  * {@code int}, {@code long}, {@code float}, and
    45  * {@code double}), and the keyword {@code void} are also
    46  * represented as {@code Class} objects.
    47  *
    48  * <p> {@code Class} has no public constructor. Instead {@code Class}
    49  * objects are constructed automatically by the Java Virtual Machine as classes
    50  * are loaded and by calls to the {@code defineClass} method in the class
    51  * loader.
    52  *
    53  * <p> The following example uses a {@code Class} object to print the
    54  * class name of an object:
    55  *
    56  * <p> <blockquote><pre>
    57  *     void printClassName(Object obj) {
    58  *         System.out.println("The class of " + obj +
    59  *                            " is " + obj.getClass().getName());
    60  *     }
    61  * </pre></blockquote>
    62  *
    63  * <p> It is also possible to get the {@code Class} object for a named
    64  * type (or for void) using a class literal.  See Section 15.8.2 of
    65  * <cite>The Java&trade; Language Specification</cite>.
    66  * For example:
    67  *
    68  * <p> <blockquote>
    69  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
    70  * </blockquote>
    71  *
    72  * @param <T> the type of the class modeled by this {@code Class}
    73  * object.  For example, the type of {@code String.class} is {@code
    74  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
    75  * unknown.
    76  *
    77  * @author  unascribed
    78  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
    79  * @since   JDK1.0
    80  */
    81 public final
    82     class Class<T> implements java.io.Serializable,
    83                               java.lang.reflect.GenericDeclaration,
    84                               java.lang.reflect.Type,
    85                               java.lang.reflect.AnnotatedElement {
    86     private static final int ANNOTATION= 0x00002000;
    87     private static final int ENUM      = 0x00004000;
    88     private static final int SYNTHETIC = 0x00001000;
    89 
    90     /*
    91      * Constructor. Only the Java Virtual Machine creates Class
    92      * objects.
    93      */
    94     private Class() {}
    95 
    96 
    97     /**
    98      * Converts the object to a string. The string representation is the
    99      * string "class" or "interface", followed by a space, and then by the
   100      * fully qualified name of the class in the format returned by
   101      * {@code getName}.  If this {@code Class} object represents a
   102      * primitive type, this method returns the name of the primitive type.  If
   103      * this {@code Class} object represents void this method returns
   104      * "void".
   105      *
   106      * @return a string representation of this class object.
   107      */
   108     public String toString() {
   109         return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
   110             + getName();
   111     }
   112 
   113 
   114     /**
   115      * Returns the {@code Class} object associated with the class or
   116      * interface with the given string name.  Invoking this method is
   117      * equivalent to:
   118      *
   119      * <blockquote>
   120      *  {@code Class.forName(className, true, currentLoader)}
   121      * </blockquote>
   122      *
   123      * where {@code currentLoader} denotes the defining class loader of
   124      * the current class.
   125      *
   126      * <p> For example, the following code fragment returns the
   127      * runtime {@code Class} descriptor for the class named
   128      * {@code java.lang.Thread}:
   129      *
   130      * <blockquote>
   131      *   {@code Class t = Class.forName("java.lang.Thread")}
   132      * </blockquote>
   133      * <p>
   134      * A call to {@code forName("X")} causes the class named
   135      * {@code X} to be initialized.
   136      *
   137      * @param      className   the fully qualified name of the desired class.
   138      * @return     the {@code Class} object for the class with the
   139      *             specified name.
   140      * @exception LinkageError if the linkage fails
   141      * @exception ExceptionInInitializerError if the initialization provoked
   142      *            by this method fails
   143      * @exception ClassNotFoundException if the class cannot be located
   144      */
   145     public static Class<?> forName(String className)
   146                 throws ClassNotFoundException {
   147         Class<?> c = loadCls(className, className.replace('.', '_'));
   148         if (c == null) {
   149             throw new ClassNotFoundException();
   150         }
   151         return c;
   152     }
   153     
   154     @JavaScriptBody(args = {"n", "c" }, body =
   155         "if (vm[c]) return vm[c].$class;\n"
   156       + "if (vm.loadClass) {\n"
   157       + "  vm.loadClass(n);\n"
   158       + "  if (vm[c]) return vm[c].$class;\n"
   159       + "}\n"
   160       + "return null;"
   161     )
   162     private static native Class<?> loadCls(String n, String c);
   163 
   164 
   165     /**
   166      * Creates a new instance of the class represented by this {@code Class}
   167      * object.  The class is instantiated as if by a {@code new}
   168      * expression with an empty argument list.  The class is initialized if it
   169      * has not already been initialized.
   170      *
   171      * <p>Note that this method propagates any exception thrown by the
   172      * nullary constructor, including a checked exception.  Use of
   173      * this method effectively bypasses the compile-time exception
   174      * checking that would otherwise be performed by the compiler.
   175      * The {@link
   176      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
   177      * Constructor.newInstance} method avoids this problem by wrapping
   178      * any exception thrown by the constructor in a (checked) {@link
   179      * java.lang.reflect.InvocationTargetException}.
   180      *
   181      * @return     a newly allocated instance of the class represented by this
   182      *             object.
   183      * @exception  IllegalAccessException  if the class or its nullary
   184      *               constructor is not accessible.
   185      * @exception  InstantiationException
   186      *               if this {@code Class} represents an abstract class,
   187      *               an interface, an array class, a primitive type, or void;
   188      *               or if the class has no nullary constructor;
   189      *               or if the instantiation fails for some other reason.
   190      * @exception  ExceptionInInitializerError if the initialization
   191      *               provoked by this method fails.
   192      * @exception  SecurityException
   193      *             If a security manager, <i>s</i>, is present and any of the
   194      *             following conditions is met:
   195      *
   196      *             <ul>
   197      *
   198      *             <li> invocation of
   199      *             {@link SecurityManager#checkMemberAccess
   200      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   201      *             creation of new instances of this class
   202      *
   203      *             <li> the caller's class loader is not the same as or an
   204      *             ancestor of the class loader for the current class and
   205      *             invocation of {@link SecurityManager#checkPackageAccess
   206      *             s.checkPackageAccess()} denies access to the package
   207      *             of this class
   208      *
   209      *             </ul>
   210      *
   211      */
   212     @JavaScriptBody(args = "self", body =
   213           "var inst = self.cnstr();"
   214         + "inst.cons__V(inst);"
   215         + "return inst;"
   216     )
   217     public T newInstance()
   218         throws InstantiationException, IllegalAccessException
   219     {
   220         throw new UnsupportedOperationException();
   221     }
   222 
   223     /**
   224      * Determines if the specified {@code Object} is assignment-compatible
   225      * with the object represented by this {@code Class}.  This method is
   226      * the dynamic equivalent of the Java language {@code instanceof}
   227      * operator. The method returns {@code true} if the specified
   228      * {@code Object} argument is non-null and can be cast to the
   229      * reference type represented by this {@code Class} object without
   230      * raising a {@code ClassCastException.} It returns {@code false}
   231      * otherwise.
   232      *
   233      * <p> Specifically, if this {@code Class} object represents a
   234      * declared class, this method returns {@code true} if the specified
   235      * {@code Object} argument is an instance of the represented class (or
   236      * of any of its subclasses); it returns {@code false} otherwise. If
   237      * this {@code Class} object represents an array class, this method
   238      * returns {@code true} if the specified {@code Object} argument
   239      * can be converted to an object of the array class by an identity
   240      * conversion or by a widening reference conversion; it returns
   241      * {@code false} otherwise. If this {@code Class} object
   242      * represents an interface, this method returns {@code true} if the
   243      * class or any superclass of the specified {@code Object} argument
   244      * implements this interface; it returns {@code false} otherwise. If
   245      * this {@code Class} object represents a primitive type, this method
   246      * returns {@code false}.
   247      *
   248      * @param   obj the object to check
   249      * @return  true if {@code obj} is an instance of this class
   250      *
   251      * @since JDK1.1
   252      */
   253     public native boolean isInstance(Object obj);
   254 
   255 
   256     /**
   257      * Determines if the class or interface represented by this
   258      * {@code Class} object is either the same as, or is a superclass or
   259      * superinterface of, the class or interface represented by the specified
   260      * {@code Class} parameter. It returns {@code true} if so;
   261      * otherwise it returns {@code false}. If this {@code Class}
   262      * object represents a primitive type, this method returns
   263      * {@code true} if the specified {@code Class} parameter is
   264      * exactly this {@code Class} object; otherwise it returns
   265      * {@code false}.
   266      *
   267      * <p> Specifically, this method tests whether the type represented by the
   268      * specified {@code Class} parameter can be converted to the type
   269      * represented by this {@code Class} object via an identity conversion
   270      * or via a widening reference conversion. See <em>The Java Language
   271      * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
   272      *
   273      * @param cls the {@code Class} object to be checked
   274      * @return the {@code boolean} value indicating whether objects of the
   275      * type {@code cls} can be assigned to objects of this class
   276      * @exception NullPointerException if the specified Class parameter is
   277      *            null.
   278      * @since JDK1.1
   279      */
   280     public native boolean isAssignableFrom(Class<?> cls);
   281 
   282 
   283     /**
   284      * Determines if the specified {@code Class} object represents an
   285      * interface type.
   286      *
   287      * @return  {@code true} if this object represents an interface;
   288      *          {@code false} otherwise.
   289      */
   290     public native boolean isInterface();
   291 
   292 
   293     /**
   294      * Determines if this {@code Class} object represents an array class.
   295      *
   296      * @return  {@code true} if this object represents an array class;
   297      *          {@code false} otherwise.
   298      * @since   JDK1.1
   299      */
   300     public boolean isArray() {
   301         return false;
   302     }
   303 
   304 
   305     /**
   306      * Determines if the specified {@code Class} object represents a
   307      * primitive type.
   308      *
   309      * <p> There are nine predefined {@code Class} objects to represent
   310      * the eight primitive types and void.  These are created by the Java
   311      * Virtual Machine, and have the same names as the primitive types that
   312      * they represent, namely {@code boolean}, {@code byte},
   313      * {@code char}, {@code short}, {@code int},
   314      * {@code long}, {@code float}, and {@code double}.
   315      *
   316      * <p> These objects may only be accessed via the following public static
   317      * final variables, and are the only {@code Class} objects for which
   318      * this method returns {@code true}.
   319      *
   320      * @return true if and only if this class represents a primitive type
   321      *
   322      * @see     java.lang.Boolean#TYPE
   323      * @see     java.lang.Character#TYPE
   324      * @see     java.lang.Byte#TYPE
   325      * @see     java.lang.Short#TYPE
   326      * @see     java.lang.Integer#TYPE
   327      * @see     java.lang.Long#TYPE
   328      * @see     java.lang.Float#TYPE
   329      * @see     java.lang.Double#TYPE
   330      * @see     java.lang.Void#TYPE
   331      * @since JDK1.1
   332      */
   333     public native boolean isPrimitive();
   334 
   335     /**
   336      * Returns true if this {@code Class} object represents an annotation
   337      * type.  Note that if this method returns true, {@link #isInterface()}
   338      * would also return true, as all annotation types are also interfaces.
   339      *
   340      * @return {@code true} if this class object represents an annotation
   341      *      type; {@code false} otherwise
   342      * @since 1.5
   343      */
   344     public boolean isAnnotation() {
   345         return (getModifiers() & ANNOTATION) != 0;
   346     }
   347 
   348     /**
   349      * Returns {@code true} if this class is a synthetic class;
   350      * returns {@code false} otherwise.
   351      * @return {@code true} if and only if this class is a synthetic class as
   352      *         defined by the Java Language Specification.
   353      * @since 1.5
   354      */
   355     public boolean isSynthetic() {
   356         return (getModifiers() & SYNTHETIC) != 0;
   357     }
   358 
   359     /**
   360      * Returns the  name of the entity (class, interface, array class,
   361      * primitive type, or void) represented by this {@code Class} object,
   362      * as a {@code String}.
   363      *
   364      * <p> If this class object represents a reference type that is not an
   365      * array type then the binary name of the class is returned, as specified
   366      * by
   367      * <cite>The Java&trade; Language Specification</cite>.
   368      *
   369      * <p> If this class object represents a primitive type or void, then the
   370      * name returned is a {@code String} equal to the Java language
   371      * keyword corresponding to the primitive type or void.
   372      *
   373      * <p> If this class object represents a class of arrays, then the internal
   374      * form of the name consists of the name of the element type preceded by
   375      * one or more '{@code [}' characters representing the depth of the array
   376      * nesting.  The encoding of element type names is as follows:
   377      *
   378      * <blockquote><table summary="Element types and encodings">
   379      * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
   380      * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
   381      * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
   382      * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
   383      * <tr><td> class or interface
   384      *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
   385      * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
   386      * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
   387      * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
   388      * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
   389      * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
   390      * </table></blockquote>
   391      *
   392      * <p> The class or interface name <i>classname</i> is the binary name of
   393      * the class specified above.
   394      *
   395      * <p> Examples:
   396      * <blockquote><pre>
   397      * String.class.getName()
   398      *     returns "java.lang.String"
   399      * byte.class.getName()
   400      *     returns "byte"
   401      * (new Object[3]).getClass().getName()
   402      *     returns "[Ljava.lang.Object;"
   403      * (new int[3][4][5][6][7][8][9]).getClass().getName()
   404      *     returns "[[[[[[[I"
   405      * </pre></blockquote>
   406      *
   407      * @return  the name of the class or interface
   408      *          represented by this object.
   409      */
   410     public String getName() {
   411         return jvmName().replace('/', '.');
   412     }
   413 
   414     @JavaScriptBody(args = "self", body = "return self.jvmName;")
   415     private native String jvmName();
   416 
   417     
   418     /**
   419      * Returns an array of {@code TypeVariable} objects that represent the
   420      * type variables declared by the generic declaration represented by this
   421      * {@code GenericDeclaration} object, in declaration order.  Returns an
   422      * array of length 0 if the underlying generic declaration declares no type
   423      * variables.
   424      *
   425      * @return an array of {@code TypeVariable} objects that represent
   426      *     the type variables declared by this generic declaration
   427      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
   428      *     signature of this generic declaration does not conform to
   429      *     the format specified in
   430      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   431      * @since 1.5
   432      */
   433     public TypeVariable<Class<T>>[] getTypeParameters() {
   434         throw new UnsupportedOperationException();
   435     }
   436  
   437     /**
   438      * Returns the {@code Class} representing the superclass of the entity
   439      * (class, interface, primitive type or void) represented by this
   440      * {@code Class}.  If this {@code Class} represents either the
   441      * {@code Object} class, an interface, a primitive type, or void, then
   442      * null is returned.  If this object represents an array class then the
   443      * {@code Class} object representing the {@code Object} class is
   444      * returned.
   445      *
   446      * @return the superclass of the class represented by this object.
   447      */
   448     @JavaScriptBody(args = "self", body = "return self.superclass;")
   449     public native Class<? super T> getSuperclass();
   450 
   451     /**
   452      * Returns the Java language modifiers for this class or interface, encoded
   453      * in an integer. The modifiers consist of the Java Virtual Machine's
   454      * constants for {@code public}, {@code protected},
   455      * {@code private}, {@code final}, {@code static},
   456      * {@code abstract} and {@code interface}; they should be decoded
   457      * using the methods of class {@code Modifier}.
   458      *
   459      * <p> If the underlying class is an array class, then its
   460      * {@code public}, {@code private} and {@code protected}
   461      * modifiers are the same as those of its component type.  If this
   462      * {@code Class} represents a primitive type or void, its
   463      * {@code public} modifier is always {@code true}, and its
   464      * {@code protected} and {@code private} modifiers are always
   465      * {@code false}. If this object represents an array class, a
   466      * primitive type or void, then its {@code final} modifier is always
   467      * {@code true} and its interface modifier is always
   468      * {@code false}. The values of its other modifiers are not determined
   469      * by this specification.
   470      *
   471      * <p> The modifier encodings are defined in <em>The Java Virtual Machine
   472      * Specification</em>, table 4.1.
   473      *
   474      * @return the {@code int} representing the modifiers for this class
   475      * @see     java.lang.reflect.Modifier
   476      * @since JDK1.1
   477      */
   478     public native int getModifiers();
   479 
   480 
   481     /**
   482      * Returns the simple name of the underlying class as given in the
   483      * source code. Returns an empty string if the underlying class is
   484      * anonymous.
   485      *
   486      * <p>The simple name of an array is the simple name of the
   487      * component type with "[]" appended.  In particular the simple
   488      * name of an array whose component type is anonymous is "[]".
   489      *
   490      * @return the simple name of the underlying class
   491      * @since 1.5
   492      */
   493     public String getSimpleName() {
   494         if (isArray())
   495             return getComponentType().getSimpleName()+"[]";
   496 
   497         String simpleName = getSimpleBinaryName();
   498         if (simpleName == null) { // top level class
   499             simpleName = getName();
   500             return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
   501         }
   502         // According to JLS3 "Binary Compatibility" (13.1) the binary
   503         // name of non-package classes (not top level) is the binary
   504         // name of the immediately enclosing class followed by a '$' followed by:
   505         // (for nested and inner classes): the simple name.
   506         // (for local classes): 1 or more digits followed by the simple name.
   507         // (for anonymous classes): 1 or more digits.
   508 
   509         // Since getSimpleBinaryName() will strip the binary name of
   510         // the immediatly enclosing class, we are now looking at a
   511         // string that matches the regular expression "\$[0-9]*"
   512         // followed by a simple name (considering the simple of an
   513         // anonymous class to be the empty string).
   514 
   515         // Remove leading "\$[0-9]*" from the name
   516         int length = simpleName.length();
   517         if (length < 1 || simpleName.charAt(0) != '$')
   518             throw new IllegalStateException("Malformed class name");
   519         int index = 1;
   520         while (index < length && isAsciiDigit(simpleName.charAt(index)))
   521             index++;
   522         // Eventually, this is the empty string iff this is an anonymous class
   523         return simpleName.substring(index);
   524     }
   525 
   526     /**
   527      * Returns the "simple binary name" of the underlying class, i.e.,
   528      * the binary name without the leading enclosing class name.
   529      * Returns {@code null} if the underlying class is a top level
   530      * class.
   531      */
   532     private String getSimpleBinaryName() {
   533         Class<?> enclosingClass = null; // XXX getEnclosingClass();
   534         if (enclosingClass == null) // top level class
   535             return null;
   536         // Otherwise, strip the enclosing class' name
   537         try {
   538             return getName().substring(enclosingClass.getName().length());
   539         } catch (IndexOutOfBoundsException ex) {
   540             throw new IllegalStateException("Malformed class name");
   541         }
   542     }
   543 
   544     /**
   545      * Returns an array containing {@code Field} objects reflecting all
   546      * the accessible public fields of the class or interface represented by
   547      * this {@code Class} object.  The elements in the array returned are
   548      * not sorted and are not in any particular order.  This method returns an
   549      * array of length 0 if the class or interface has no accessible public
   550      * fields, or if it represents an array class, a primitive type, or void.
   551      *
   552      * <p> Specifically, if this {@code Class} object represents a class,
   553      * this method returns the public fields of this class and of all its
   554      * superclasses.  If this {@code Class} object represents an
   555      * interface, this method returns the fields of this interface and of all
   556      * its superinterfaces.
   557      *
   558      * <p> The implicit length field for array class is not reflected by this
   559      * method. User code should use the methods of class {@code Array} to
   560      * manipulate arrays.
   561      *
   562      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
   563      *
   564      * @return the array of {@code Field} objects representing the
   565      * public fields
   566      * @exception  SecurityException
   567      *             If a security manager, <i>s</i>, is present and any of the
   568      *             following conditions is met:
   569      *
   570      *             <ul>
   571      *
   572      *             <li> invocation of
   573      *             {@link SecurityManager#checkMemberAccess
   574      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   575      *             access to the fields within this class
   576      *
   577      *             <li> the caller's class loader is not the same as or an
   578      *             ancestor of the class loader for the current class and
   579      *             invocation of {@link SecurityManager#checkPackageAccess
   580      *             s.checkPackageAccess()} denies access to the package
   581      *             of this class
   582      *
   583      *             </ul>
   584      *
   585      * @since JDK1.1
   586      */
   587     public Field[] getFields() throws SecurityException {
   588         throw new SecurityException();
   589     }
   590 
   591     /**
   592      * Returns an array containing {@code Method} objects reflecting all
   593      * the public <em>member</em> methods of the class or interface represented
   594      * by this {@code Class} object, including those declared by the class
   595      * or interface and those inherited from superclasses and
   596      * superinterfaces.  Array classes return all the (public) member methods
   597      * inherited from the {@code Object} class.  The elements in the array
   598      * returned are not sorted and are not in any particular order.  This
   599      * method returns an array of length 0 if this {@code Class} object
   600      * represents a class or interface that has no public member methods, or if
   601      * this {@code Class} object represents a primitive type or void.
   602      *
   603      * <p> The class initialization method {@code <clinit>} is not
   604      * included in the returned array. If the class declares multiple public
   605      * member methods with the same parameter types, they are all included in
   606      * the returned array.
   607      *
   608      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
   609      *
   610      * @return the array of {@code Method} objects representing the
   611      * public methods of this class
   612      * @exception  SecurityException
   613      *             If a security manager, <i>s</i>, is present and any of the
   614      *             following conditions is met:
   615      *
   616      *             <ul>
   617      *
   618      *             <li> invocation of
   619      *             {@link SecurityManager#checkMemberAccess
   620      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   621      *             access to the methods within this class
   622      *
   623      *             <li> the caller's class loader is not the same as or an
   624      *             ancestor of the class loader for the current class and
   625      *             invocation of {@link SecurityManager#checkPackageAccess
   626      *             s.checkPackageAccess()} denies access to the package
   627      *             of this class
   628      *
   629      *             </ul>
   630      *
   631      * @since JDK1.1
   632      */
   633     public Method[] getMethods() throws SecurityException {
   634         return Method.findMethods(this);
   635     }
   636 
   637     /**
   638      * Returns a {@code Field} object that reflects the specified public
   639      * member field of the class or interface represented by this
   640      * {@code Class} object. The {@code name} parameter is a
   641      * {@code String} specifying the simple name of the desired field.
   642      *
   643      * <p> The field to be reflected is determined by the algorithm that
   644      * follows.  Let C be the class represented by this object:
   645      * <OL>
   646      * <LI> If C declares a public field with the name specified, that is the
   647      *      field to be reflected.</LI>
   648      * <LI> If no field was found in step 1 above, this algorithm is applied
   649      *      recursively to each direct superinterface of C. The direct
   650      *      superinterfaces are searched in the order they were declared.</LI>
   651      * <LI> If no field was found in steps 1 and 2 above, and C has a
   652      *      superclass S, then this algorithm is invoked recursively upon S.
   653      *      If C has no superclass, then a {@code NoSuchFieldException}
   654      *      is thrown.</LI>
   655      * </OL>
   656      *
   657      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
   658      *
   659      * @param name the field name
   660      * @return  the {@code Field} object of this class specified by
   661      * {@code name}
   662      * @exception NoSuchFieldException if a field with the specified name is
   663      *              not found.
   664      * @exception NullPointerException if {@code name} is {@code null}
   665      * @exception  SecurityException
   666      *             If a security manager, <i>s</i>, is present and any of the
   667      *             following conditions is met:
   668      *
   669      *             <ul>
   670      *
   671      *             <li> invocation of
   672      *             {@link SecurityManager#checkMemberAccess
   673      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   674      *             access to the field
   675      *
   676      *             <li> the caller's class loader is not the same as or an
   677      *             ancestor of the class loader for the current class and
   678      *             invocation of {@link SecurityManager#checkPackageAccess
   679      *             s.checkPackageAccess()} denies access to the package
   680      *             of this class
   681      *
   682      *             </ul>
   683      *
   684      * @since JDK1.1
   685      */
   686     public Field getField(String name)
   687         throws SecurityException {
   688         throw new SecurityException();
   689     }
   690     
   691     
   692     /**
   693      * Returns a {@code Method} object that reflects the specified public
   694      * member method of the class or interface represented by this
   695      * {@code Class} object. The {@code name} parameter is a
   696      * {@code String} specifying the simple name of the desired method. The
   697      * {@code parameterTypes} parameter is an array of {@code Class}
   698      * objects that identify the method's formal parameter types, in declared
   699      * order. If {@code parameterTypes} is {@code null}, it is
   700      * treated as if it were an empty array.
   701      *
   702      * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
   703      * {@code NoSuchMethodException} is raised. Otherwise, the method to
   704      * be reflected is determined by the algorithm that follows.  Let C be the
   705      * class represented by this object:
   706      * <OL>
   707      * <LI> C is searched for any <I>matching methods</I>. If no matching
   708      *      method is found, the algorithm of step 1 is invoked recursively on
   709      *      the superclass of C.</LI>
   710      * <LI> If no method was found in step 1 above, the superinterfaces of C
   711      *      are searched for a matching method. If any such method is found, it
   712      *      is reflected.</LI>
   713      * </OL>
   714      *
   715      * To find a matching method in a class C:&nbsp; If C declares exactly one
   716      * public method with the specified name and exactly the same formal
   717      * parameter types, that is the method reflected. If more than one such
   718      * method is found in C, and one of these methods has a return type that is
   719      * more specific than any of the others, that method is reflected;
   720      * otherwise one of the methods is chosen arbitrarily.
   721      *
   722      * <p>Note that there may be more than one matching method in a
   723      * class because while the Java language forbids a class to
   724      * declare multiple methods with the same signature but different
   725      * return types, the Java virtual machine does not.  This
   726      * increased flexibility in the virtual machine can be used to
   727      * implement various language features.  For example, covariant
   728      * returns can be implemented with {@linkplain
   729      * java.lang.reflect.Method#isBridge bridge methods}; the bridge
   730      * method and the method being overridden would have the same
   731      * signature but different return types.
   732      *
   733      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
   734      *
   735      * @param name the name of the method
   736      * @param parameterTypes the list of parameters
   737      * @return the {@code Method} object that matches the specified
   738      * {@code name} and {@code parameterTypes}
   739      * @exception NoSuchMethodException if a matching method is not found
   740      *            or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
   741      * @exception NullPointerException if {@code name} is {@code null}
   742      * @exception  SecurityException
   743      *             If a security manager, <i>s</i>, is present and any of the
   744      *             following conditions is met:
   745      *
   746      *             <ul>
   747      *
   748      *             <li> invocation of
   749      *             {@link SecurityManager#checkMemberAccess
   750      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   751      *             access to the method
   752      *
   753      *             <li> the caller's class loader is not the same as or an
   754      *             ancestor of the class loader for the current class and
   755      *             invocation of {@link SecurityManager#checkPackageAccess
   756      *             s.checkPackageAccess()} denies access to the package
   757      *             of this class
   758      *
   759      *             </ul>
   760      *
   761      * @since JDK1.1
   762      */
   763     public Method getMethod(String name, Class<?>... parameterTypes)
   764         throws SecurityException {
   765         Method m = Method.findMethod(this, name, parameterTypes);
   766         if (m == null) {
   767             throw new SecurityException(); // XXX: NoSuchMethodException
   768         }
   769         return m;
   770     }
   771 
   772     /**
   773      * Character.isDigit answers {@code true} to some non-ascii
   774      * digits.  This one does not.
   775      */
   776     private static boolean isAsciiDigit(char c) {
   777         return '0' <= c && c <= '9';
   778     }
   779 
   780     /**
   781      * Returns the canonical name of the underlying class as
   782      * defined by the Java Language Specification.  Returns null if
   783      * the underlying class does not have a canonical name (i.e., if
   784      * it is a local or anonymous class or an array whose component
   785      * type does not have a canonical name).
   786      * @return the canonical name of the underlying class if it exists, and
   787      * {@code null} otherwise.
   788      * @since 1.5
   789      */
   790     public String getCanonicalName() {
   791         if (isArray()) {
   792             String canonicalName = getComponentType().getCanonicalName();
   793             if (canonicalName != null)
   794                 return canonicalName + "[]";
   795             else
   796                 return null;
   797         }
   798 //        if (isLocalOrAnonymousClass())
   799 //            return null;
   800 //        Class<?> enclosingClass = getEnclosingClass();
   801         Class<?> enclosingClass = null;
   802         if (enclosingClass == null) { // top level class
   803             return getName();
   804         } else {
   805             String enclosingName = enclosingClass.getCanonicalName();
   806             if (enclosingName == null)
   807                 return null;
   808             return enclosingName + "." + getSimpleName();
   809         }
   810     }
   811 
   812     /**
   813      * Finds a resource with a given name.  The rules for searching resources
   814      * associated with a given class are implemented by the defining
   815      * {@linkplain ClassLoader class loader} of the class.  This method
   816      * delegates to this object's class loader.  If this object was loaded by
   817      * the bootstrap class loader, the method delegates to {@link
   818      * ClassLoader#getSystemResourceAsStream}.
   819      *
   820      * <p> Before delegation, an absolute resource name is constructed from the
   821      * given resource name using this algorithm:
   822      *
   823      * <ul>
   824      *
   825      * <li> If the {@code name} begins with a {@code '/'}
   826      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
   827      * portion of the {@code name} following the {@code '/'}.
   828      *
   829      * <li> Otherwise, the absolute name is of the following form:
   830      *
   831      * <blockquote>
   832      *   {@code modified_package_name/name}
   833      * </blockquote>
   834      *
   835      * <p> Where the {@code modified_package_name} is the package name of this
   836      * object with {@code '/'} substituted for {@code '.'}
   837      * (<tt>'&#92;u002e'</tt>).
   838      *
   839      * </ul>
   840      *
   841      * @param  name name of the desired resource
   842      * @return      A {@link java.io.InputStream} object or {@code null} if
   843      *              no resource with this name is found
   844      * @throws  NullPointerException If {@code name} is {@code null}
   845      * @since  JDK1.1
   846      */
   847      public InputStream getResourceAsStream(String name) {
   848         name = resolveName(name);
   849         ClassLoader cl = getClassLoader0();
   850         if (cl==null) {
   851             // A system class.
   852             return ClassLoader.getSystemResourceAsStream(name);
   853         }
   854         return cl.getResourceAsStream(name);
   855     }
   856 
   857     /**
   858      * Finds a resource with a given name.  The rules for searching resources
   859      * associated with a given class are implemented by the defining
   860      * {@linkplain ClassLoader class loader} of the class.  This method
   861      * delegates to this object's class loader.  If this object was loaded by
   862      * the bootstrap class loader, the method delegates to {@link
   863      * ClassLoader#getSystemResource}.
   864      *
   865      * <p> Before delegation, an absolute resource name is constructed from the
   866      * given resource name using this algorithm:
   867      *
   868      * <ul>
   869      *
   870      * <li> If the {@code name} begins with a {@code '/'}
   871      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
   872      * portion of the {@code name} following the {@code '/'}.
   873      *
   874      * <li> Otherwise, the absolute name is of the following form:
   875      *
   876      * <blockquote>
   877      *   {@code modified_package_name/name}
   878      * </blockquote>
   879      *
   880      * <p> Where the {@code modified_package_name} is the package name of this
   881      * object with {@code '/'} substituted for {@code '.'}
   882      * (<tt>'&#92;u002e'</tt>).
   883      *
   884      * </ul>
   885      *
   886      * @param  name name of the desired resource
   887      * @return      A  {@link java.net.URL} object or {@code null} if no
   888      *              resource with this name is found
   889      * @since  JDK1.1
   890      */
   891     public java.net.URL getResource(String name) {
   892         name = resolveName(name);
   893         ClassLoader cl = getClassLoader0();
   894         if (cl==null) {
   895             // A system class.
   896             return ClassLoader.getSystemResource(name);
   897         }
   898         return cl.getResource(name);
   899     }
   900 
   901 
   902    /**
   903      * Add a package name prefix if the name is not absolute Remove leading "/"
   904      * if name is absolute
   905      */
   906     private String resolveName(String name) {
   907         if (name == null) {
   908             return name;
   909         }
   910         if (!name.startsWith("/")) {
   911             Class<?> c = this;
   912             while (c.isArray()) {
   913                 c = c.getComponentType();
   914             }
   915             String baseName = c.getName();
   916             int index = baseName.lastIndexOf('.');
   917             if (index != -1) {
   918                 name = baseName.substring(0, index).replace('.', '/')
   919                     +"/"+name;
   920             }
   921         } else {
   922             name = name.substring(1);
   923         }
   924         return name;
   925     }
   926     
   927     /**
   928      * Returns the class loader for the class.  Some implementations may use
   929      * null to represent the bootstrap class loader. This method will return
   930      * null in such implementations if this class was loaded by the bootstrap
   931      * class loader.
   932      *
   933      * <p> If a security manager is present, and the caller's class loader is
   934      * not null and the caller's class loader is not the same as or an ancestor of
   935      * the class loader for the class whose class loader is requested, then
   936      * this method calls the security manager's {@code checkPermission}
   937      * method with a {@code RuntimePermission("getClassLoader")}
   938      * permission to ensure it's ok to access the class loader for the class.
   939      *
   940      * <p>If this object
   941      * represents a primitive type or void, null is returned.
   942      *
   943      * @return  the class loader that loaded the class or interface
   944      *          represented by this object.
   945      * @throws SecurityException
   946      *    if a security manager exists and its
   947      *    {@code checkPermission} method denies
   948      *    access to the class loader for the class.
   949      * @see java.lang.ClassLoader
   950      * @see SecurityManager#checkPermission
   951      * @see java.lang.RuntimePermission
   952      */
   953     public ClassLoader getClassLoader() {
   954         throw new SecurityException();
   955     }
   956     
   957     // Package-private to allow ClassLoader access
   958     native ClassLoader getClassLoader0();    
   959 
   960     /**
   961      * Returns the {@code Class} representing the component type of an
   962      * array.  If this class does not represent an array class this method
   963      * returns null.
   964      *
   965      * @return the {@code Class} representing the component type of this
   966      * class if this class is an array
   967      * @see     java.lang.reflect.Array
   968      * @since JDK1.1
   969      */
   970     public Class<?> getComponentType() {
   971         return null;
   972     }
   973 
   974     /**
   975      * Returns true if and only if this class was declared as an enum in the
   976      * source code.
   977      *
   978      * @return true if and only if this class was declared as an enum in the
   979      *     source code
   980      * @since 1.5
   981      */
   982     public boolean isEnum() {
   983         // An enum must both directly extend java.lang.Enum and have
   984         // the ENUM bit set; classes for specialized enum constants
   985         // don't do the former.
   986         return (this.getModifiers() & ENUM) != 0 &&
   987         this.getSuperclass() == java.lang.Enum.class;
   988     }
   989 
   990     /**
   991      * Casts an object to the class or interface represented
   992      * by this {@code Class} object.
   993      *
   994      * @param obj the object to be cast
   995      * @return the object after casting, or null if obj is null
   996      *
   997      * @throws ClassCastException if the object is not
   998      * null and is not assignable to the type T.
   999      *
  1000      * @since 1.5
  1001      */
  1002     public T cast(Object obj) {
  1003         if (obj != null && !isInstance(obj))
  1004             throw new ClassCastException(cannotCastMsg(obj));
  1005         return (T) obj;
  1006     }
  1007 
  1008     private String cannotCastMsg(Object obj) {
  1009         return "Cannot cast " + obj.getClass().getName() + " to " + getName();
  1010     }
  1011 
  1012     /**
  1013      * Casts this {@code Class} object to represent a subclass of the class
  1014      * represented by the specified class object.  Checks that that the cast
  1015      * is valid, and throws a {@code ClassCastException} if it is not.  If
  1016      * this method succeeds, it always returns a reference to this class object.
  1017      *
  1018      * <p>This method is useful when a client needs to "narrow" the type of
  1019      * a {@code Class} object to pass it to an API that restricts the
  1020      * {@code Class} objects that it is willing to accept.  A cast would
  1021      * generate a compile-time warning, as the correctness of the cast
  1022      * could not be checked at runtime (because generic types are implemented
  1023      * by erasure).
  1024      *
  1025      * @return this {@code Class} object, cast to represent a subclass of
  1026      *    the specified class object.
  1027      * @throws ClassCastException if this {@code Class} object does not
  1028      *    represent a subclass of the specified class (here "subclass" includes
  1029      *    the class itself).
  1030      * @since 1.5
  1031      */
  1032     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
  1033         if (clazz.isAssignableFrom(this))
  1034             return (Class<? extends U>) this;
  1035         else
  1036             throw new ClassCastException(this.toString());
  1037     }
  1038 
  1039     @JavaScriptBody(args = { "self", "ac" }, 
  1040         body = 
  1041           "if (self.anno) {"
  1042         + "  return self.anno['L' + ac.jvmName + ';'];"
  1043         + "} else return null;"
  1044     )
  1045     private Object getAnnotationData(Class<?> annotationClass) {
  1046         throw new UnsupportedOperationException();
  1047     }
  1048     /**
  1049      * @throws NullPointerException {@inheritDoc}
  1050      * @since 1.5
  1051      */
  1052     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
  1053         Object data = getAnnotationData(annotationClass);
  1054         return data == null ? null : AnnotationImpl.create(annotationClass, data);
  1055     }
  1056 
  1057     /**
  1058      * @throws NullPointerException {@inheritDoc}
  1059      * @since 1.5
  1060      */
  1061     @JavaScriptBody(args = { "self", "ac" }, 
  1062         body = "if (self.anno && self.anno['L' + ac.jvmName + ';']) { return true; }"
  1063         + "else return false;"
  1064     )
  1065     public boolean isAnnotationPresent(
  1066         Class<? extends Annotation> annotationClass) {
  1067         if (annotationClass == null)
  1068             throw new NullPointerException();
  1069 
  1070         return getAnnotation(annotationClass) != null;
  1071     }
  1072 
  1073     @JavaScriptBody(args = "self", body = "return self.anno;")
  1074     private Object getAnnotationData() {
  1075         throw new UnsupportedOperationException();
  1076     }
  1077 
  1078     /**
  1079      * @since 1.5
  1080      */
  1081     public Annotation[] getAnnotations() {
  1082         Object data = getAnnotationData();
  1083         return data == null ? new Annotation[0] : AnnotationImpl.create(data);
  1084     }
  1085 
  1086     /**
  1087      * @since 1.5
  1088      */
  1089     public Annotation[] getDeclaredAnnotations()  {
  1090         throw new UnsupportedOperationException();
  1091     }
  1092 
  1093     static Class getPrimitiveClass(String type) {
  1094         // XXX
  1095         return Object.class;
  1096     }
  1097 
  1098     public boolean desiredAssertionStatus() {
  1099         return false;
  1100     }
  1101 }