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