emul/src/main/java/java/lang/Class.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Dec 2012 16:43:37 +0100
branchlauncher
changeset 354 002b7c3d5157
parent 353 fd38bdad7fb5
child 355 eea0065bcc1a
permissions -rw-r--r--
Reflective calls on methods that return int
     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     @JavaScriptBody(args = "self", body = 
   334            "if (self.primitive) return true;"
   335         + "else return false;"
   336     )
   337     public native boolean isPrimitive();
   338 
   339     /**
   340      * Returns true if this {@code Class} object represents an annotation
   341      * type.  Note that if this method returns true, {@link #isInterface()}
   342      * would also return true, as all annotation types are also interfaces.
   343      *
   344      * @return {@code true} if this class object represents an annotation
   345      *      type; {@code false} otherwise
   346      * @since 1.5
   347      */
   348     public boolean isAnnotation() {
   349         return (getModifiers() & ANNOTATION) != 0;
   350     }
   351 
   352     /**
   353      * Returns {@code true} if this class is a synthetic class;
   354      * returns {@code false} otherwise.
   355      * @return {@code true} if and only if this class is a synthetic class as
   356      *         defined by the Java Language Specification.
   357      * @since 1.5
   358      */
   359     public boolean isSynthetic() {
   360         return (getModifiers() & SYNTHETIC) != 0;
   361     }
   362 
   363     /**
   364      * Returns the  name of the entity (class, interface, array class,
   365      * primitive type, or void) represented by this {@code Class} object,
   366      * as a {@code String}.
   367      *
   368      * <p> If this class object represents a reference type that is not an
   369      * array type then the binary name of the class is returned, as specified
   370      * by
   371      * <cite>The Java&trade; Language Specification</cite>.
   372      *
   373      * <p> If this class object represents a primitive type or void, then the
   374      * name returned is a {@code String} equal to the Java language
   375      * keyword corresponding to the primitive type or void.
   376      *
   377      * <p> If this class object represents a class of arrays, then the internal
   378      * form of the name consists of the name of the element type preceded by
   379      * one or more '{@code [}' characters representing the depth of the array
   380      * nesting.  The encoding of element type names is as follows:
   381      *
   382      * <blockquote><table summary="Element types and encodings">
   383      * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
   384      * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
   385      * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
   386      * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
   387      * <tr><td> class or interface
   388      *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
   389      * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
   390      * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
   391      * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
   392      * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
   393      * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
   394      * </table></blockquote>
   395      *
   396      * <p> The class or interface name <i>classname</i> is the binary name of
   397      * the class specified above.
   398      *
   399      * <p> Examples:
   400      * <blockquote><pre>
   401      * String.class.getName()
   402      *     returns "java.lang.String"
   403      * byte.class.getName()
   404      *     returns "byte"
   405      * (new Object[3]).getClass().getName()
   406      *     returns "[Ljava.lang.Object;"
   407      * (new int[3][4][5][6][7][8][9]).getClass().getName()
   408      *     returns "[[[[[[[I"
   409      * </pre></blockquote>
   410      *
   411      * @return  the name of the class or interface
   412      *          represented by this object.
   413      */
   414     public String getName() {
   415         return jvmName().replace('/', '.');
   416     }
   417 
   418     @JavaScriptBody(args = "self", body = "return self.jvmName;")
   419     private native String jvmName();
   420 
   421     
   422     /**
   423      * Returns an array of {@code TypeVariable} objects that represent the
   424      * type variables declared by the generic declaration represented by this
   425      * {@code GenericDeclaration} object, in declaration order.  Returns an
   426      * array of length 0 if the underlying generic declaration declares no type
   427      * variables.
   428      *
   429      * @return an array of {@code TypeVariable} objects that represent
   430      *     the type variables declared by this generic declaration
   431      * @throws java.lang.reflect.GenericSignatureFormatError if the generic
   432      *     signature of this generic declaration does not conform to
   433      *     the format specified in
   434      *     <cite>The Java&trade; Virtual Machine Specification</cite>
   435      * @since 1.5
   436      */
   437     public TypeVariable<Class<T>>[] getTypeParameters() {
   438         throw new UnsupportedOperationException();
   439     }
   440  
   441     /**
   442      * Returns the {@code Class} representing the superclass of the entity
   443      * (class, interface, primitive type or void) represented by this
   444      * {@code Class}.  If this {@code Class} represents either the
   445      * {@code Object} class, an interface, a primitive type, or void, then
   446      * null is returned.  If this object represents an array class then the
   447      * {@code Class} object representing the {@code Object} class is
   448      * returned.
   449      *
   450      * @return the superclass of the class represented by this object.
   451      */
   452     @JavaScriptBody(args = "self", body = "return self.superclass;")
   453     public native Class<? super T> getSuperclass();
   454 
   455     /**
   456      * Returns the Java language modifiers for this class or interface, encoded
   457      * in an integer. The modifiers consist of the Java Virtual Machine's
   458      * constants for {@code public}, {@code protected},
   459      * {@code private}, {@code final}, {@code static},
   460      * {@code abstract} and {@code interface}; they should be decoded
   461      * using the methods of class {@code Modifier}.
   462      *
   463      * <p> If the underlying class is an array class, then its
   464      * {@code public}, {@code private} and {@code protected}
   465      * modifiers are the same as those of its component type.  If this
   466      * {@code Class} represents a primitive type or void, its
   467      * {@code public} modifier is always {@code true}, and its
   468      * {@code protected} and {@code private} modifiers are always
   469      * {@code false}. If this object represents an array class, a
   470      * primitive type or void, then its {@code final} modifier is always
   471      * {@code true} and its interface modifier is always
   472      * {@code false}. The values of its other modifiers are not determined
   473      * by this specification.
   474      *
   475      * <p> The modifier encodings are defined in <em>The Java Virtual Machine
   476      * Specification</em>, table 4.1.
   477      *
   478      * @return the {@code int} representing the modifiers for this class
   479      * @see     java.lang.reflect.Modifier
   480      * @since JDK1.1
   481      */
   482     public native int getModifiers();
   483 
   484 
   485     /**
   486      * Returns the simple name of the underlying class as given in the
   487      * source code. Returns an empty string if the underlying class is
   488      * anonymous.
   489      *
   490      * <p>The simple name of an array is the simple name of the
   491      * component type with "[]" appended.  In particular the simple
   492      * name of an array whose component type is anonymous is "[]".
   493      *
   494      * @return the simple name of the underlying class
   495      * @since 1.5
   496      */
   497     public String getSimpleName() {
   498         if (isArray())
   499             return getComponentType().getSimpleName()+"[]";
   500 
   501         String simpleName = getSimpleBinaryName();
   502         if (simpleName == null) { // top level class
   503             simpleName = getName();
   504             return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
   505         }
   506         // According to JLS3 "Binary Compatibility" (13.1) the binary
   507         // name of non-package classes (not top level) is the binary
   508         // name of the immediately enclosing class followed by a '$' followed by:
   509         // (for nested and inner classes): the simple name.
   510         // (for local classes): 1 or more digits followed by the simple name.
   511         // (for anonymous classes): 1 or more digits.
   512 
   513         // Since getSimpleBinaryName() will strip the binary name of
   514         // the immediatly enclosing class, we are now looking at a
   515         // string that matches the regular expression "\$[0-9]*"
   516         // followed by a simple name (considering the simple of an
   517         // anonymous class to be the empty string).
   518 
   519         // Remove leading "\$[0-9]*" from the name
   520         int length = simpleName.length();
   521         if (length < 1 || simpleName.charAt(0) != '$')
   522             throw new IllegalStateException("Malformed class name");
   523         int index = 1;
   524         while (index < length && isAsciiDigit(simpleName.charAt(index)))
   525             index++;
   526         // Eventually, this is the empty string iff this is an anonymous class
   527         return simpleName.substring(index);
   528     }
   529 
   530     /**
   531      * Returns the "simple binary name" of the underlying class, i.e.,
   532      * the binary name without the leading enclosing class name.
   533      * Returns {@code null} if the underlying class is a top level
   534      * class.
   535      */
   536     private String getSimpleBinaryName() {
   537         Class<?> enclosingClass = null; // XXX getEnclosingClass();
   538         if (enclosingClass == null) // top level class
   539             return null;
   540         // Otherwise, strip the enclosing class' name
   541         try {
   542             return getName().substring(enclosingClass.getName().length());
   543         } catch (IndexOutOfBoundsException ex) {
   544             throw new IllegalStateException("Malformed class name");
   545         }
   546     }
   547 
   548     /**
   549      * Returns an array containing {@code Field} objects reflecting all
   550      * the accessible public fields of the class or interface represented by
   551      * this {@code Class} object.  The elements in the array returned are
   552      * not sorted and are not in any particular order.  This method returns an
   553      * array of length 0 if the class or interface has no accessible public
   554      * fields, or if it represents an array class, a primitive type, or void.
   555      *
   556      * <p> Specifically, if this {@code Class} object represents a class,
   557      * this method returns the public fields of this class and of all its
   558      * superclasses.  If this {@code Class} object represents an
   559      * interface, this method returns the fields of this interface and of all
   560      * its superinterfaces.
   561      *
   562      * <p> The implicit length field for array class is not reflected by this
   563      * method. User code should use the methods of class {@code Array} to
   564      * manipulate arrays.
   565      *
   566      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
   567      *
   568      * @return the array of {@code Field} objects representing the
   569      * public fields
   570      * @exception  SecurityException
   571      *             If a security manager, <i>s</i>, is present and any of the
   572      *             following conditions is met:
   573      *
   574      *             <ul>
   575      *
   576      *             <li> invocation of
   577      *             {@link SecurityManager#checkMemberAccess
   578      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   579      *             access to the fields within this class
   580      *
   581      *             <li> the caller's class loader is not the same as or an
   582      *             ancestor of the class loader for the current class and
   583      *             invocation of {@link SecurityManager#checkPackageAccess
   584      *             s.checkPackageAccess()} denies access to the package
   585      *             of this class
   586      *
   587      *             </ul>
   588      *
   589      * @since JDK1.1
   590      */
   591     public Field[] getFields() throws SecurityException {
   592         throw new SecurityException();
   593     }
   594 
   595     /**
   596      * Returns an array containing {@code Method} objects reflecting all
   597      * the public <em>member</em> methods of the class or interface represented
   598      * by this {@code Class} object, including those declared by the class
   599      * or interface and those inherited from superclasses and
   600      * superinterfaces.  Array classes return all the (public) member methods
   601      * inherited from the {@code Object} class.  The elements in the array
   602      * returned are not sorted and are not in any particular order.  This
   603      * method returns an array of length 0 if this {@code Class} object
   604      * represents a class or interface that has no public member methods, or if
   605      * this {@code Class} object represents a primitive type or void.
   606      *
   607      * <p> The class initialization method {@code <clinit>} is not
   608      * included in the returned array. If the class declares multiple public
   609      * member methods with the same parameter types, they are all included in
   610      * the returned array.
   611      *
   612      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
   613      *
   614      * @return the array of {@code Method} objects representing the
   615      * public methods of this class
   616      * @exception  SecurityException
   617      *             If a security manager, <i>s</i>, is present and any of the
   618      *             following conditions is met:
   619      *
   620      *             <ul>
   621      *
   622      *             <li> invocation of
   623      *             {@link SecurityManager#checkMemberAccess
   624      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   625      *             access to the methods within this class
   626      *
   627      *             <li> the caller's class loader is not the same as or an
   628      *             ancestor of the class loader for the current class and
   629      *             invocation of {@link SecurityManager#checkPackageAccess
   630      *             s.checkPackageAccess()} denies access to the package
   631      *             of this class
   632      *
   633      *             </ul>
   634      *
   635      * @since JDK1.1
   636      */
   637     public Method[] getMethods() throws SecurityException {
   638         return Method.findMethods(this);
   639     }
   640 
   641     /**
   642      * Returns a {@code Field} object that reflects the specified public
   643      * member field of the class or interface represented by this
   644      * {@code Class} object. The {@code name} parameter is a
   645      * {@code String} specifying the simple name of the desired field.
   646      *
   647      * <p> The field to be reflected is determined by the algorithm that
   648      * follows.  Let C be the class represented by this object:
   649      * <OL>
   650      * <LI> If C declares a public field with the name specified, that is the
   651      *      field to be reflected.</LI>
   652      * <LI> If no field was found in step 1 above, this algorithm is applied
   653      *      recursively to each direct superinterface of C. The direct
   654      *      superinterfaces are searched in the order they were declared.</LI>
   655      * <LI> If no field was found in steps 1 and 2 above, and C has a
   656      *      superclass S, then this algorithm is invoked recursively upon S.
   657      *      If C has no superclass, then a {@code NoSuchFieldException}
   658      *      is thrown.</LI>
   659      * </OL>
   660      *
   661      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
   662      *
   663      * @param name the field name
   664      * @return  the {@code Field} object of this class specified by
   665      * {@code name}
   666      * @exception NoSuchFieldException if a field with the specified name is
   667      *              not found.
   668      * @exception NullPointerException if {@code name} is {@code null}
   669      * @exception  SecurityException
   670      *             If a security manager, <i>s</i>, is present and any of the
   671      *             following conditions is met:
   672      *
   673      *             <ul>
   674      *
   675      *             <li> invocation of
   676      *             {@link SecurityManager#checkMemberAccess
   677      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   678      *             access to the field
   679      *
   680      *             <li> the caller's class loader is not the same as or an
   681      *             ancestor of the class loader for the current class and
   682      *             invocation of {@link SecurityManager#checkPackageAccess
   683      *             s.checkPackageAccess()} denies access to the package
   684      *             of this class
   685      *
   686      *             </ul>
   687      *
   688      * @since JDK1.1
   689      */
   690     public Field getField(String name)
   691         throws SecurityException {
   692         throw new SecurityException();
   693     }
   694     
   695     
   696     /**
   697      * Returns a {@code Method} object that reflects the specified public
   698      * member method of the class or interface represented by this
   699      * {@code Class} object. The {@code name} parameter is a
   700      * {@code String} specifying the simple name of the desired method. The
   701      * {@code parameterTypes} parameter is an array of {@code Class}
   702      * objects that identify the method's formal parameter types, in declared
   703      * order. If {@code parameterTypes} is {@code null}, it is
   704      * treated as if it were an empty array.
   705      *
   706      * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
   707      * {@code NoSuchMethodException} is raised. Otherwise, the method to
   708      * be reflected is determined by the algorithm that follows.  Let C be the
   709      * class represented by this object:
   710      * <OL>
   711      * <LI> C is searched for any <I>matching methods</I>. If no matching
   712      *      method is found, the algorithm of step 1 is invoked recursively on
   713      *      the superclass of C.</LI>
   714      * <LI> If no method was found in step 1 above, the superinterfaces of C
   715      *      are searched for a matching method. If any such method is found, it
   716      *      is reflected.</LI>
   717      * </OL>
   718      *
   719      * To find a matching method in a class C:&nbsp; If C declares exactly one
   720      * public method with the specified name and exactly the same formal
   721      * parameter types, that is the method reflected. If more than one such
   722      * method is found in C, and one of these methods has a return type that is
   723      * more specific than any of the others, that method is reflected;
   724      * otherwise one of the methods is chosen arbitrarily.
   725      *
   726      * <p>Note that there may be more than one matching method in a
   727      * class because while the Java language forbids a class to
   728      * declare multiple methods with the same signature but different
   729      * return types, the Java virtual machine does not.  This
   730      * increased flexibility in the virtual machine can be used to
   731      * implement various language features.  For example, covariant
   732      * returns can be implemented with {@linkplain
   733      * java.lang.reflect.Method#isBridge bridge methods}; the bridge
   734      * method and the method being overridden would have the same
   735      * signature but different return types.
   736      *
   737      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
   738      *
   739      * @param name the name of the method
   740      * @param parameterTypes the list of parameters
   741      * @return the {@code Method} object that matches the specified
   742      * {@code name} and {@code parameterTypes}
   743      * @exception NoSuchMethodException if a matching method is not found
   744      *            or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
   745      * @exception NullPointerException if {@code name} is {@code null}
   746      * @exception  SecurityException
   747      *             If a security manager, <i>s</i>, is present and any of the
   748      *             following conditions is met:
   749      *
   750      *             <ul>
   751      *
   752      *             <li> invocation of
   753      *             {@link SecurityManager#checkMemberAccess
   754      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   755      *             access to the method
   756      *
   757      *             <li> the caller's class loader is not the same as or an
   758      *             ancestor of the class loader for the current class and
   759      *             invocation of {@link SecurityManager#checkPackageAccess
   760      *             s.checkPackageAccess()} denies access to the package
   761      *             of this class
   762      *
   763      *             </ul>
   764      *
   765      * @since JDK1.1
   766      */
   767     public Method getMethod(String name, Class<?>... parameterTypes)
   768         throws SecurityException {
   769         Method m = Method.findMethod(this, name, parameterTypes);
   770         if (m == null) {
   771             throw new SecurityException(); // XXX: NoSuchMethodException
   772         }
   773         return m;
   774     }
   775 
   776     /**
   777      * Character.isDigit answers {@code true} to some non-ascii
   778      * digits.  This one does not.
   779      */
   780     private static boolean isAsciiDigit(char c) {
   781         return '0' <= c && c <= '9';
   782     }
   783 
   784     /**
   785      * Returns the canonical name of the underlying class as
   786      * defined by the Java Language Specification.  Returns null if
   787      * the underlying class does not have a canonical name (i.e., if
   788      * it is a local or anonymous class or an array whose component
   789      * type does not have a canonical name).
   790      * @return the canonical name of the underlying class if it exists, and
   791      * {@code null} otherwise.
   792      * @since 1.5
   793      */
   794     public String getCanonicalName() {
   795         if (isArray()) {
   796             String canonicalName = getComponentType().getCanonicalName();
   797             if (canonicalName != null)
   798                 return canonicalName + "[]";
   799             else
   800                 return null;
   801         }
   802 //        if (isLocalOrAnonymousClass())
   803 //            return null;
   804 //        Class<?> enclosingClass = getEnclosingClass();
   805         Class<?> enclosingClass = null;
   806         if (enclosingClass == null) { // top level class
   807             return getName();
   808         } else {
   809             String enclosingName = enclosingClass.getCanonicalName();
   810             if (enclosingName == null)
   811                 return null;
   812             return enclosingName + "." + getSimpleName();
   813         }
   814     }
   815 
   816     /**
   817      * Finds a resource with a given name.  The rules for searching resources
   818      * associated with a given class are implemented by the defining
   819      * {@linkplain ClassLoader class loader} of the class.  This method
   820      * delegates to this object's class loader.  If this object was loaded by
   821      * the bootstrap class loader, the method delegates to {@link
   822      * ClassLoader#getSystemResourceAsStream}.
   823      *
   824      * <p> Before delegation, an absolute resource name is constructed from the
   825      * given resource name using this algorithm:
   826      *
   827      * <ul>
   828      *
   829      * <li> If the {@code name} begins with a {@code '/'}
   830      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
   831      * portion of the {@code name} following the {@code '/'}.
   832      *
   833      * <li> Otherwise, the absolute name is of the following form:
   834      *
   835      * <blockquote>
   836      *   {@code modified_package_name/name}
   837      * </blockquote>
   838      *
   839      * <p> Where the {@code modified_package_name} is the package name of this
   840      * object with {@code '/'} substituted for {@code '.'}
   841      * (<tt>'&#92;u002e'</tt>).
   842      *
   843      * </ul>
   844      *
   845      * @param  name name of the desired resource
   846      * @return      A {@link java.io.InputStream} object or {@code null} if
   847      *              no resource with this name is found
   848      * @throws  NullPointerException If {@code name} is {@code null}
   849      * @since  JDK1.1
   850      */
   851      public InputStream getResourceAsStream(String name) {
   852         name = resolveName(name);
   853         ClassLoader cl = getClassLoader0();
   854         if (cl==null) {
   855             // A system class.
   856             return ClassLoader.getSystemResourceAsStream(name);
   857         }
   858         return cl.getResourceAsStream(name);
   859     }
   860 
   861     /**
   862      * Finds a resource with a given name.  The rules for searching resources
   863      * associated with a given class are implemented by the defining
   864      * {@linkplain ClassLoader class loader} of the class.  This method
   865      * delegates to this object's class loader.  If this object was loaded by
   866      * the bootstrap class loader, the method delegates to {@link
   867      * ClassLoader#getSystemResource}.
   868      *
   869      * <p> Before delegation, an absolute resource name is constructed from the
   870      * given resource name using this algorithm:
   871      *
   872      * <ul>
   873      *
   874      * <li> If the {@code name} begins with a {@code '/'}
   875      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
   876      * portion of the {@code name} following the {@code '/'}.
   877      *
   878      * <li> Otherwise, the absolute name is of the following form:
   879      *
   880      * <blockquote>
   881      *   {@code modified_package_name/name}
   882      * </blockquote>
   883      *
   884      * <p> Where the {@code modified_package_name} is the package name of this
   885      * object with {@code '/'} substituted for {@code '.'}
   886      * (<tt>'&#92;u002e'</tt>).
   887      *
   888      * </ul>
   889      *
   890      * @param  name name of the desired resource
   891      * @return      A  {@link java.net.URL} object or {@code null} if no
   892      *              resource with this name is found
   893      * @since  JDK1.1
   894      */
   895     public java.net.URL getResource(String name) {
   896         name = resolveName(name);
   897         ClassLoader cl = getClassLoader0();
   898         if (cl==null) {
   899             // A system class.
   900             return ClassLoader.getSystemResource(name);
   901         }
   902         return cl.getResource(name);
   903     }
   904 
   905 
   906    /**
   907      * Add a package name prefix if the name is not absolute Remove leading "/"
   908      * if name is absolute
   909      */
   910     private String resolveName(String name) {
   911         if (name == null) {
   912             return name;
   913         }
   914         if (!name.startsWith("/")) {
   915             Class<?> c = this;
   916             while (c.isArray()) {
   917                 c = c.getComponentType();
   918             }
   919             String baseName = c.getName();
   920             int index = baseName.lastIndexOf('.');
   921             if (index != -1) {
   922                 name = baseName.substring(0, index).replace('.', '/')
   923                     +"/"+name;
   924             }
   925         } else {
   926             name = name.substring(1);
   927         }
   928         return name;
   929     }
   930     
   931     /**
   932      * Returns the class loader for the class.  Some implementations may use
   933      * null to represent the bootstrap class loader. This method will return
   934      * null in such implementations if this class was loaded by the bootstrap
   935      * class loader.
   936      *
   937      * <p> If a security manager is present, and the caller's class loader is
   938      * not null and the caller's class loader is not the same as or an ancestor of
   939      * the class loader for the class whose class loader is requested, then
   940      * this method calls the security manager's {@code checkPermission}
   941      * method with a {@code RuntimePermission("getClassLoader")}
   942      * permission to ensure it's ok to access the class loader for the class.
   943      *
   944      * <p>If this object
   945      * represents a primitive type or void, null is returned.
   946      *
   947      * @return  the class loader that loaded the class or interface
   948      *          represented by this object.
   949      * @throws SecurityException
   950      *    if a security manager exists and its
   951      *    {@code checkPermission} method denies
   952      *    access to the class loader for the class.
   953      * @see java.lang.ClassLoader
   954      * @see SecurityManager#checkPermission
   955      * @see java.lang.RuntimePermission
   956      */
   957     public ClassLoader getClassLoader() {
   958         throw new SecurityException();
   959     }
   960     
   961     // Package-private to allow ClassLoader access
   962     native ClassLoader getClassLoader0();    
   963 
   964     /**
   965      * Returns the {@code Class} representing the component type of an
   966      * array.  If this class does not represent an array class this method
   967      * returns null.
   968      *
   969      * @return the {@code Class} representing the component type of this
   970      * class if this class is an array
   971      * @see     java.lang.reflect.Array
   972      * @since JDK1.1
   973      */
   974     public Class<?> getComponentType() {
   975         return null;
   976     }
   977 
   978     /**
   979      * Returns true if and only if this class was declared as an enum in the
   980      * source code.
   981      *
   982      * @return true if and only if this class was declared as an enum in the
   983      *     source code
   984      * @since 1.5
   985      */
   986     public boolean isEnum() {
   987         // An enum must both directly extend java.lang.Enum and have
   988         // the ENUM bit set; classes for specialized enum constants
   989         // don't do the former.
   990         return (this.getModifiers() & ENUM) != 0 &&
   991         this.getSuperclass() == java.lang.Enum.class;
   992     }
   993 
   994     /**
   995      * Casts an object to the class or interface represented
   996      * by this {@code Class} object.
   997      *
   998      * @param obj the object to be cast
   999      * @return the object after casting, or null if obj is null
  1000      *
  1001      * @throws ClassCastException if the object is not
  1002      * null and is not assignable to the type T.
  1003      *
  1004      * @since 1.5
  1005      */
  1006     public T cast(Object obj) {
  1007         if (obj != null && !isInstance(obj))
  1008             throw new ClassCastException(cannotCastMsg(obj));
  1009         return (T) obj;
  1010     }
  1011 
  1012     private String cannotCastMsg(Object obj) {
  1013         return "Cannot cast " + obj.getClass().getName() + " to " + getName();
  1014     }
  1015 
  1016     /**
  1017      * Casts this {@code Class} object to represent a subclass of the class
  1018      * represented by the specified class object.  Checks that that the cast
  1019      * is valid, and throws a {@code ClassCastException} if it is not.  If
  1020      * this method succeeds, it always returns a reference to this class object.
  1021      *
  1022      * <p>This method is useful when a client needs to "narrow" the type of
  1023      * a {@code Class} object to pass it to an API that restricts the
  1024      * {@code Class} objects that it is willing to accept.  A cast would
  1025      * generate a compile-time warning, as the correctness of the cast
  1026      * could not be checked at runtime (because generic types are implemented
  1027      * by erasure).
  1028      *
  1029      * @return this {@code Class} object, cast to represent a subclass of
  1030      *    the specified class object.
  1031      * @throws ClassCastException if this {@code Class} object does not
  1032      *    represent a subclass of the specified class (here "subclass" includes
  1033      *    the class itself).
  1034      * @since 1.5
  1035      */
  1036     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
  1037         if (clazz.isAssignableFrom(this))
  1038             return (Class<? extends U>) this;
  1039         else
  1040             throw new ClassCastException(this.toString());
  1041     }
  1042 
  1043     @JavaScriptBody(args = { "self", "ac" }, 
  1044         body = 
  1045           "if (self.anno) {"
  1046         + "  return self.anno['L' + ac.jvmName + ';'];"
  1047         + "} else return null;"
  1048     )
  1049     private Object getAnnotationData(Class<?> annotationClass) {
  1050         throw new UnsupportedOperationException();
  1051     }
  1052     /**
  1053      * @throws NullPointerException {@inheritDoc}
  1054      * @since 1.5
  1055      */
  1056     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
  1057         Object data = getAnnotationData(annotationClass);
  1058         return data == null ? null : AnnotationImpl.create(annotationClass, data);
  1059     }
  1060 
  1061     /**
  1062      * @throws NullPointerException {@inheritDoc}
  1063      * @since 1.5
  1064      */
  1065     @JavaScriptBody(args = { "self", "ac" }, 
  1066         body = "if (self.anno && self.anno['L' + ac.jvmName + ';']) { return true; }"
  1067         + "else return false;"
  1068     )
  1069     public boolean isAnnotationPresent(
  1070         Class<? extends Annotation> annotationClass) {
  1071         if (annotationClass == null)
  1072             throw new NullPointerException();
  1073 
  1074         return getAnnotation(annotationClass) != null;
  1075     }
  1076 
  1077     @JavaScriptBody(args = "self", body = "return self.anno;")
  1078     private Object getAnnotationData() {
  1079         throw new UnsupportedOperationException();
  1080     }
  1081 
  1082     /**
  1083      * @since 1.5
  1084      */
  1085     public Annotation[] getAnnotations() {
  1086         Object data = getAnnotationData();
  1087         return data == null ? new Annotation[0] : AnnotationImpl.create(data);
  1088     }
  1089 
  1090     /**
  1091      * @since 1.5
  1092      */
  1093     public Annotation[] getDeclaredAnnotations()  {
  1094         throw new UnsupportedOperationException();
  1095     }
  1096 
  1097     @JavaScriptBody(args = "type", body = ""
  1098         + "var c = vm.java_lang_Class(true);"
  1099         + "c.jvmName = type;"
  1100         + "c.primitive = true;"
  1101         + "return c;"
  1102     )
  1103     native static Class getPrimitiveClass(String type);
  1104 
  1105     public boolean desiredAssertionStatus() {
  1106         return false;
  1107     }
  1108 }