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