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