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