emul/src/main/java/java/lang/Class.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 02 Dec 2012 12:26:14 +0100
branchreflection
changeset 235 bf0a77f029c4
parent 231 dde8422fb5ae
child 237 84ffc347412d
permissions -rw-r--r--
Initial support for runtime annotations
     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.InputStream;
    29 import java.lang.annotation.Annotation;
    30 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    31 
    32 /**
    33  * Instances of the class {@code Class} represent classes and
    34  * interfaces in a running Java application.  An enum is a kind of
    35  * class and an annotation is a kind of interface.  Every array also
    36  * belongs to a class that is reflected as a {@code Class} object
    37  * that is shared by all arrays with the same element type and number
    38  * of dimensions.  The primitive Java types ({@code boolean},
    39  * {@code byte}, {@code char}, {@code short},
    40  * {@code int}, {@code long}, {@code float}, and
    41  * {@code double}), and the keyword {@code void} are also
    42  * represented as {@code Class} objects.
    43  *
    44  * <p> {@code Class} has no public constructor. Instead {@code Class}
    45  * objects are constructed automatically by the Java Virtual Machine as classes
    46  * are loaded and by calls to the {@code defineClass} method in the class
    47  * loader.
    48  *
    49  * <p> The following example uses a {@code Class} object to print the
    50  * class name of an object:
    51  *
    52  * <p> <blockquote><pre>
    53  *     void printClassName(Object obj) {
    54  *         System.out.println("The class of " + obj +
    55  *                            " is " + obj.getClass().getName());
    56  *     }
    57  * </pre></blockquote>
    58  *
    59  * <p> It is also possible to get the {@code Class} object for a named
    60  * type (or for void) using a class literal.  See Section 15.8.2 of
    61  * <cite>The Java&trade; Language Specification</cite>.
    62  * For example:
    63  *
    64  * <p> <blockquote>
    65  *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
    66  * </blockquote>
    67  *
    68  * @param <T> the type of the class modeled by this {@code Class}
    69  * object.  For example, the type of {@code String.class} is {@code
    70  * Class<String>}.  Use {@code Class<?>} if the class being modeled is
    71  * unknown.
    72  *
    73  * @author  unascribed
    74  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
    75  * @since   JDK1.0
    76  */
    77 public final
    78     class Class<T> implements java.io.Serializable {
    79 //                              java.lang.reflect.GenericDeclaration,
    80 //                              java.lang.reflect.Type,
    81 //                              java.lang.reflect.AnnotatedElement {
    82     private static final int ANNOTATION= 0x00002000;
    83     private static final int ENUM      = 0x00004000;
    84     private static final int SYNTHETIC = 0x00001000;
    85 
    86     /*
    87      * Constructor. Only the Java Virtual Machine creates Class
    88      * objects.
    89      */
    90     private Class() {}
    91 
    92 
    93     /**
    94      * Converts the object to a string. The string representation is the
    95      * string "class" or "interface", followed by a space, and then by the
    96      * fully qualified name of the class in the format returned by
    97      * {@code getName}.  If this {@code Class} object represents a
    98      * primitive type, this method returns the name of the primitive type.  If
    99      * this {@code Class} object represents void this method returns
   100      * "void".
   101      *
   102      * @return a string representation of this class object.
   103      */
   104     public String toString() {
   105         return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
   106             + getName();
   107     }
   108 
   109 
   110     /**
   111      * Returns the {@code Class} object associated with the class or
   112      * interface with the given string name.  Invoking this method is
   113      * equivalent to:
   114      *
   115      * <blockquote>
   116      *  {@code Class.forName(className, true, currentLoader)}
   117      * </blockquote>
   118      *
   119      * where {@code currentLoader} denotes the defining class loader of
   120      * the current class.
   121      *
   122      * <p> For example, the following code fragment returns the
   123      * runtime {@code Class} descriptor for the class named
   124      * {@code java.lang.Thread}:
   125      *
   126      * <blockquote>
   127      *   {@code Class t = Class.forName("java.lang.Thread")}
   128      * </blockquote>
   129      * <p>
   130      * A call to {@code forName("X")} causes the class named
   131      * {@code X} to be initialized.
   132      *
   133      * @param      className   the fully qualified name of the desired class.
   134      * @return     the {@code Class} object for the class with the
   135      *             specified name.
   136      * @exception LinkageError if the linkage fails
   137      * @exception ExceptionInInitializerError if the initialization provoked
   138      *            by this method fails
   139      * @exception ClassNotFoundException if the class cannot be located
   140      */
   141     public static Class<?> forName(String className)
   142                 throws ClassNotFoundException {
   143         throw new UnsupportedOperationException();
   144     }
   145 
   146 
   147     /**
   148      * Creates a new instance of the class represented by this {@code Class}
   149      * object.  The class is instantiated as if by a {@code new}
   150      * expression with an empty argument list.  The class is initialized if it
   151      * has not already been initialized.
   152      *
   153      * <p>Note that this method propagates any exception thrown by the
   154      * nullary constructor, including a checked exception.  Use of
   155      * this method effectively bypasses the compile-time exception
   156      * checking that would otherwise be performed by the compiler.
   157      * The {@link
   158      * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
   159      * Constructor.newInstance} method avoids this problem by wrapping
   160      * any exception thrown by the constructor in a (checked) {@link
   161      * java.lang.reflect.InvocationTargetException}.
   162      *
   163      * @return     a newly allocated instance of the class represented by this
   164      *             object.
   165      * @exception  IllegalAccessException  if the class or its nullary
   166      *               constructor is not accessible.
   167      * @exception  InstantiationException
   168      *               if this {@code Class} represents an abstract class,
   169      *               an interface, an array class, a primitive type, or void;
   170      *               or if the class has no nullary constructor;
   171      *               or if the instantiation fails for some other reason.
   172      * @exception  ExceptionInInitializerError if the initialization
   173      *               provoked by this method fails.
   174      * @exception  SecurityException
   175      *             If a security manager, <i>s</i>, is present and any of the
   176      *             following conditions is met:
   177      *
   178      *             <ul>
   179      *
   180      *             <li> invocation of
   181      *             {@link SecurityManager#checkMemberAccess
   182      *             s.checkMemberAccess(this, Member.PUBLIC)} denies
   183      *             creation of new instances of this class
   184      *
   185      *             <li> the caller's class loader is not the same as or an
   186      *             ancestor of the class loader for the current class and
   187      *             invocation of {@link SecurityManager#checkPackageAccess
   188      *             s.checkPackageAccess()} denies access to the package
   189      *             of this class
   190      *
   191      *             </ul>
   192      *
   193      */
   194     @JavaScriptBody(args = "self", body =
   195           "var inst = self.cnstr();"
   196         + "inst.consV(inst);"
   197         + "return inst;"
   198     )
   199     public T newInstance()
   200         throws InstantiationException, IllegalAccessException
   201     {
   202         throw new UnsupportedOperationException();
   203     }
   204 
   205     /**
   206      * Determines if the specified {@code Object} is assignment-compatible
   207      * with the object represented by this {@code Class}.  This method is
   208      * the dynamic equivalent of the Java language {@code instanceof}
   209      * operator. The method returns {@code true} if the specified
   210      * {@code Object} argument is non-null and can be cast to the
   211      * reference type represented by this {@code Class} object without
   212      * raising a {@code ClassCastException.} It returns {@code false}
   213      * otherwise.
   214      *
   215      * <p> Specifically, if this {@code Class} object represents a
   216      * declared class, this method returns {@code true} if the specified
   217      * {@code Object} argument is an instance of the represented class (or
   218      * of any of its subclasses); it returns {@code false} otherwise. If
   219      * this {@code Class} object represents an array class, this method
   220      * returns {@code true} if the specified {@code Object} argument
   221      * can be converted to an object of the array class by an identity
   222      * conversion or by a widening reference conversion; it returns
   223      * {@code false} otherwise. If this {@code Class} object
   224      * represents an interface, this method returns {@code true} if the
   225      * class or any superclass of the specified {@code Object} argument
   226      * implements this interface; it returns {@code false} otherwise. If
   227      * this {@code Class} object represents a primitive type, this method
   228      * returns {@code false}.
   229      *
   230      * @param   obj the object to check
   231      * @return  true if {@code obj} is an instance of this class
   232      *
   233      * @since JDK1.1
   234      */
   235     public native boolean isInstance(Object obj);
   236 
   237 
   238     /**
   239      * Determines if the class or interface represented by this
   240      * {@code Class} object is either the same as, or is a superclass or
   241      * superinterface of, the class or interface represented by the specified
   242      * {@code Class} parameter. It returns {@code true} if so;
   243      * otherwise it returns {@code false}. If this {@code Class}
   244      * object represents a primitive type, this method returns
   245      * {@code true} if the specified {@code Class} parameter is
   246      * exactly this {@code Class} object; otherwise it returns
   247      * {@code false}.
   248      *
   249      * <p> Specifically, this method tests whether the type represented by the
   250      * specified {@code Class} parameter can be converted to the type
   251      * represented by this {@code Class} object via an identity conversion
   252      * or via a widening reference conversion. See <em>The Java Language
   253      * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
   254      *
   255      * @param cls the {@code Class} object to be checked
   256      * @return the {@code boolean} value indicating whether objects of the
   257      * type {@code cls} can be assigned to objects of this class
   258      * @exception NullPointerException if the specified Class parameter is
   259      *            null.
   260      * @since JDK1.1
   261      */
   262     public native boolean isAssignableFrom(Class<?> cls);
   263 
   264 
   265     /**
   266      * Determines if the specified {@code Class} object represents an
   267      * interface type.
   268      *
   269      * @return  {@code true} if this object represents an interface;
   270      *          {@code false} otherwise.
   271      */
   272     public native boolean isInterface();
   273 
   274 
   275     /**
   276      * Determines if this {@code Class} object represents an array class.
   277      *
   278      * @return  {@code true} if this object represents an array class;
   279      *          {@code false} otherwise.
   280      * @since   JDK1.1
   281      */
   282     public boolean isArray() {
   283         return false;
   284     }
   285 
   286 
   287     /**
   288      * Determines if the specified {@code Class} object represents a
   289      * primitive type.
   290      *
   291      * <p> There are nine predefined {@code Class} objects to represent
   292      * the eight primitive types and void.  These are created by the Java
   293      * Virtual Machine, and have the same names as the primitive types that
   294      * they represent, namely {@code boolean}, {@code byte},
   295      * {@code char}, {@code short}, {@code int},
   296      * {@code long}, {@code float}, and {@code double}.
   297      *
   298      * <p> These objects may only be accessed via the following public static
   299      * final variables, and are the only {@code Class} objects for which
   300      * this method returns {@code true}.
   301      *
   302      * @return true if and only if this class represents a primitive type
   303      *
   304      * @see     java.lang.Boolean#TYPE
   305      * @see     java.lang.Character#TYPE
   306      * @see     java.lang.Byte#TYPE
   307      * @see     java.lang.Short#TYPE
   308      * @see     java.lang.Integer#TYPE
   309      * @see     java.lang.Long#TYPE
   310      * @see     java.lang.Float#TYPE
   311      * @see     java.lang.Double#TYPE
   312      * @see     java.lang.Void#TYPE
   313      * @since JDK1.1
   314      */
   315     public native boolean isPrimitive();
   316 
   317     /**
   318      * Returns true if this {@code Class} object represents an annotation
   319      * type.  Note that if this method returns true, {@link #isInterface()}
   320      * would also return true, as all annotation types are also interfaces.
   321      *
   322      * @return {@code true} if this class object represents an annotation
   323      *      type; {@code false} otherwise
   324      * @since 1.5
   325      */
   326     public boolean isAnnotation() {
   327         return (getModifiers() & ANNOTATION) != 0;
   328     }
   329 
   330     /**
   331      * Returns {@code true} if this class is a synthetic class;
   332      * returns {@code false} otherwise.
   333      * @return {@code true} if and only if this class is a synthetic class as
   334      *         defined by the Java Language Specification.
   335      * @since 1.5
   336      */
   337     public boolean isSynthetic() {
   338         return (getModifiers() & SYNTHETIC) != 0;
   339     }
   340 
   341     /**
   342      * Returns the  name of the entity (class, interface, array class,
   343      * primitive type, or void) represented by this {@code Class} object,
   344      * as a {@code String}.
   345      *
   346      * <p> If this class object represents a reference type that is not an
   347      * array type then the binary name of the class is returned, as specified
   348      * by
   349      * <cite>The Java&trade; Language Specification</cite>.
   350      *
   351      * <p> If this class object represents a primitive type or void, then the
   352      * name returned is a {@code String} equal to the Java language
   353      * keyword corresponding to the primitive type or void.
   354      *
   355      * <p> If this class object represents a class of arrays, then the internal
   356      * form of the name consists of the name of the element type preceded by
   357      * one or more '{@code [}' characters representing the depth of the array
   358      * nesting.  The encoding of element type names is as follows:
   359      *
   360      * <blockquote><table summary="Element types and encodings">
   361      * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
   362      * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
   363      * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
   364      * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
   365      * <tr><td> class or interface
   366      *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
   367      * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
   368      * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
   369      * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
   370      * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
   371      * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
   372      * </table></blockquote>
   373      *
   374      * <p> The class or interface name <i>classname</i> is the binary name of
   375      * the class specified above.
   376      *
   377      * <p> Examples:
   378      * <blockquote><pre>
   379      * String.class.getName()
   380      *     returns "java.lang.String"
   381      * byte.class.getName()
   382      *     returns "byte"
   383      * (new Object[3]).getClass().getName()
   384      *     returns "[Ljava.lang.Object;"
   385      * (new int[3][4][5][6][7][8][9]).getClass().getName()
   386      *     returns "[[[[[[[I"
   387      * </pre></blockquote>
   388      *
   389      * @return  the name of the class or interface
   390      *          represented by this object.
   391      */
   392     public String getName() {
   393         return jvmName().replace('/', '.');
   394     }
   395 
   396     @JavaScriptBody(args = "self", body = "return self.jvmName;")
   397     private native String jvmName();
   398 
   399     /**
   400      * Returns the {@code Class} representing the superclass of the entity
   401      * (class, interface, primitive type or void) represented by this
   402      * {@code Class}.  If this {@code Class} represents either the
   403      * {@code Object} class, an interface, a primitive type, or void, then
   404      * null is returned.  If this object represents an array class then the
   405      * {@code Class} object representing the {@code Object} class is
   406      * returned.
   407      *
   408      * @return the superclass of the class represented by this object.
   409      */
   410     @JavaScriptBody(args = "self", body = "return self.superclass;")
   411     public native Class<? super T> getSuperclass();
   412 
   413     /**
   414      * Returns the Java language modifiers for this class or interface, encoded
   415      * in an integer. The modifiers consist of the Java Virtual Machine's
   416      * constants for {@code public}, {@code protected},
   417      * {@code private}, {@code final}, {@code static},
   418      * {@code abstract} and {@code interface}; they should be decoded
   419      * using the methods of class {@code Modifier}.
   420      *
   421      * <p> If the underlying class is an array class, then its
   422      * {@code public}, {@code private} and {@code protected}
   423      * modifiers are the same as those of its component type.  If this
   424      * {@code Class} represents a primitive type or void, its
   425      * {@code public} modifier is always {@code true}, and its
   426      * {@code protected} and {@code private} modifiers are always
   427      * {@code false}. If this object represents an array class, a
   428      * primitive type or void, then its {@code final} modifier is always
   429      * {@code true} and its interface modifier is always
   430      * {@code false}. The values of its other modifiers are not determined
   431      * by this specification.
   432      *
   433      * <p> The modifier encodings are defined in <em>The Java Virtual Machine
   434      * Specification</em>, table 4.1.
   435      *
   436      * @return the {@code int} representing the modifiers for this class
   437      * @see     java.lang.reflect.Modifier
   438      * @since JDK1.1
   439      */
   440     public native int getModifiers();
   441 
   442 
   443     /**
   444      * Returns the simple name of the underlying class as given in the
   445      * source code. Returns an empty string if the underlying class is
   446      * anonymous.
   447      *
   448      * <p>The simple name of an array is the simple name of the
   449      * component type with "[]" appended.  In particular the simple
   450      * name of an array whose component type is anonymous is "[]".
   451      *
   452      * @return the simple name of the underlying class
   453      * @since 1.5
   454      */
   455     public String getSimpleName() {
   456         if (isArray())
   457             return getComponentType().getSimpleName()+"[]";
   458 
   459         String simpleName = getSimpleBinaryName();
   460         if (simpleName == null) { // top level class
   461             simpleName = getName();
   462             return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
   463         }
   464         // According to JLS3 "Binary Compatibility" (13.1) the binary
   465         // name of non-package classes (not top level) is the binary
   466         // name of the immediately enclosing class followed by a '$' followed by:
   467         // (for nested and inner classes): the simple name.
   468         // (for local classes): 1 or more digits followed by the simple name.
   469         // (for anonymous classes): 1 or more digits.
   470 
   471         // Since getSimpleBinaryName() will strip the binary name of
   472         // the immediatly enclosing class, we are now looking at a
   473         // string that matches the regular expression "\$[0-9]*"
   474         // followed by a simple name (considering the simple of an
   475         // anonymous class to be the empty string).
   476 
   477         // Remove leading "\$[0-9]*" from the name
   478         int length = simpleName.length();
   479         if (length < 1 || simpleName.charAt(0) != '$')
   480             throw new IllegalStateException("Malformed class name");
   481         int index = 1;
   482         while (index < length && isAsciiDigit(simpleName.charAt(index)))
   483             index++;
   484         // Eventually, this is the empty string iff this is an anonymous class
   485         return simpleName.substring(index);
   486     }
   487 
   488     /**
   489      * Returns the "simple binary name" of the underlying class, i.e.,
   490      * the binary name without the leading enclosing class name.
   491      * Returns {@code null} if the underlying class is a top level
   492      * class.
   493      */
   494     private String getSimpleBinaryName() {
   495         Class<?> enclosingClass = null; // XXX getEnclosingClass();
   496         if (enclosingClass == null) // top level class
   497             return null;
   498         // Otherwise, strip the enclosing class' name
   499         try {
   500             return getName().substring(enclosingClass.getName().length());
   501         } catch (IndexOutOfBoundsException ex) {
   502             throw new IllegalStateException("Malformed class name");
   503         }
   504     }
   505     
   506     /**
   507      * Character.isDigit answers {@code true} to some non-ascii
   508      * digits.  This one does not.
   509      */
   510     private static boolean isAsciiDigit(char c) {
   511         return '0' <= c && c <= '9';
   512     }
   513 
   514     /**
   515      * Returns the canonical name of the underlying class as
   516      * defined by the Java Language Specification.  Returns null if
   517      * the underlying class does not have a canonical name (i.e., if
   518      * it is a local or anonymous class or an array whose component
   519      * type does not have a canonical name).
   520      * @return the canonical name of the underlying class if it exists, and
   521      * {@code null} otherwise.
   522      * @since 1.5
   523      */
   524     public String getCanonicalName() {
   525         if (isArray()) {
   526             String canonicalName = getComponentType().getCanonicalName();
   527             if (canonicalName != null)
   528                 return canonicalName + "[]";
   529             else
   530                 return null;
   531         }
   532 //        if (isLocalOrAnonymousClass())
   533 //            return null;
   534 //        Class<?> enclosingClass = getEnclosingClass();
   535         Class<?> enclosingClass = null;
   536         if (enclosingClass == null) { // top level class
   537             return getName();
   538         } else {
   539             String enclosingName = enclosingClass.getCanonicalName();
   540             if (enclosingName == null)
   541                 return null;
   542             return enclosingName + "." + getSimpleName();
   543         }
   544     }
   545 
   546     /**
   547      * Finds a resource with a given name.  The rules for searching resources
   548      * associated with a given class are implemented by the defining
   549      * {@linkplain ClassLoader class loader} of the class.  This method
   550      * delegates to this object's class loader.  If this object was loaded by
   551      * the bootstrap class loader, the method delegates to {@link
   552      * ClassLoader#getSystemResourceAsStream}.
   553      *
   554      * <p> Before delegation, an absolute resource name is constructed from the
   555      * given resource name using this algorithm:
   556      *
   557      * <ul>
   558      *
   559      * <li> If the {@code name} begins with a {@code '/'}
   560      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
   561      * portion of the {@code name} following the {@code '/'}.
   562      *
   563      * <li> Otherwise, the absolute name is of the following form:
   564      *
   565      * <blockquote>
   566      *   {@code modified_package_name/name}
   567      * </blockquote>
   568      *
   569      * <p> Where the {@code modified_package_name} is the package name of this
   570      * object with {@code '/'} substituted for {@code '.'}
   571      * (<tt>'&#92;u002e'</tt>).
   572      *
   573      * </ul>
   574      *
   575      * @param  name name of the desired resource
   576      * @return      A {@link java.io.InputStream} object or {@code null} if
   577      *              no resource with this name is found
   578      * @throws  NullPointerException If {@code name} is {@code null}
   579      * @since  JDK1.1
   580      */
   581      public InputStream getResourceAsStream(String name) {
   582         name = resolveName(name);
   583         ClassLoader cl = getClassLoader0();
   584         if (cl==null) {
   585             // A system class.
   586             return ClassLoader.getSystemResourceAsStream(name);
   587         }
   588         return cl.getResourceAsStream(name);
   589     }
   590 
   591     /**
   592      * Finds a resource with a given name.  The rules for searching resources
   593      * associated with a given class are implemented by the defining
   594      * {@linkplain ClassLoader class loader} of the class.  This method
   595      * delegates to this object's class loader.  If this object was loaded by
   596      * the bootstrap class loader, the method delegates to {@link
   597      * ClassLoader#getSystemResource}.
   598      *
   599      * <p> Before delegation, an absolute resource name is constructed from the
   600      * given resource name using this algorithm:
   601      *
   602      * <ul>
   603      *
   604      * <li> If the {@code name} begins with a {@code '/'}
   605      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
   606      * portion of the {@code name} following the {@code '/'}.
   607      *
   608      * <li> Otherwise, the absolute name is of the following form:
   609      *
   610      * <blockquote>
   611      *   {@code modified_package_name/name}
   612      * </blockquote>
   613      *
   614      * <p> Where the {@code modified_package_name} is the package name of this
   615      * object with {@code '/'} substituted for {@code '.'}
   616      * (<tt>'&#92;u002e'</tt>).
   617      *
   618      * </ul>
   619      *
   620      * @param  name name of the desired resource
   621      * @return      A  {@link java.net.URL} object or {@code null} if no
   622      *              resource with this name is found
   623      * @since  JDK1.1
   624      */
   625     public java.net.URL getResource(String name) {
   626         name = resolveName(name);
   627         ClassLoader cl = getClassLoader0();
   628         if (cl==null) {
   629             // A system class.
   630             return ClassLoader.getSystemResource(name);
   631         }
   632         return cl.getResource(name);
   633     }
   634 
   635 
   636    /**
   637      * Add a package name prefix if the name is not absolute Remove leading "/"
   638      * if name is absolute
   639      */
   640     private String resolveName(String name) {
   641         if (name == null) {
   642             return name;
   643         }
   644         if (!name.startsWith("/")) {
   645             Class<?> c = this;
   646             while (c.isArray()) {
   647                 c = c.getComponentType();
   648             }
   649             String baseName = c.getName();
   650             int index = baseName.lastIndexOf('.');
   651             if (index != -1) {
   652                 name = baseName.substring(0, index).replace('.', '/')
   653                     +"/"+name;
   654             }
   655         } else {
   656             name = name.substring(1);
   657         }
   658         return name;
   659     }
   660     
   661     /**
   662      * Returns the class loader for the class.  Some implementations may use
   663      * null to represent the bootstrap class loader. This method will return
   664      * null in such implementations if this class was loaded by the bootstrap
   665      * class loader.
   666      *
   667      * <p> If a security manager is present, and the caller's class loader is
   668      * not null and the caller's class loader is not the same as or an ancestor of
   669      * the class loader for the class whose class loader is requested, then
   670      * this method calls the security manager's {@code checkPermission}
   671      * method with a {@code RuntimePermission("getClassLoader")}
   672      * permission to ensure it's ok to access the class loader for the class.
   673      *
   674      * <p>If this object
   675      * represents a primitive type or void, null is returned.
   676      *
   677      * @return  the class loader that loaded the class or interface
   678      *          represented by this object.
   679      * @throws SecurityException
   680      *    if a security manager exists and its
   681      *    {@code checkPermission} method denies
   682      *    access to the class loader for the class.
   683      * @see java.lang.ClassLoader
   684      * @see SecurityManager#checkPermission
   685      * @see java.lang.RuntimePermission
   686      */
   687     public ClassLoader getClassLoader() {
   688         throw new SecurityException();
   689     }
   690     
   691     // Package-private to allow ClassLoader access
   692     native ClassLoader getClassLoader0();    
   693 
   694     /**
   695      * Returns the {@code Class} representing the component type of an
   696      * array.  If this class does not represent an array class this method
   697      * returns null.
   698      *
   699      * @return the {@code Class} representing the component type of this
   700      * class if this class is an array
   701      * @see     java.lang.reflect.Array
   702      * @since JDK1.1
   703      */
   704     public Class<?> getComponentType() {
   705         return null;
   706     }
   707 
   708     /**
   709      * Returns true if and only if this class was declared as an enum in the
   710      * source code.
   711      *
   712      * @return true if and only if this class was declared as an enum in the
   713      *     source code
   714      * @since 1.5
   715      */
   716     public boolean isEnum() {
   717         // An enum must both directly extend java.lang.Enum and have
   718         // the ENUM bit set; classes for specialized enum constants
   719         // don't do the former.
   720         return (this.getModifiers() & ENUM) != 0 &&
   721         this.getSuperclass() == java.lang.Enum.class;
   722     }
   723 
   724     /**
   725      * Casts an object to the class or interface represented
   726      * by this {@code Class} object.
   727      *
   728      * @param obj the object to be cast
   729      * @return the object after casting, or null if obj is null
   730      *
   731      * @throws ClassCastException if the object is not
   732      * null and is not assignable to the type T.
   733      *
   734      * @since 1.5
   735      */
   736     public T cast(Object obj) {
   737         if (obj != null && !isInstance(obj))
   738             throw new ClassCastException(cannotCastMsg(obj));
   739         return (T) obj;
   740     }
   741 
   742     private String cannotCastMsg(Object obj) {
   743         return "Cannot cast " + obj.getClass().getName() + " to " + getName();
   744     }
   745 
   746     /**
   747      * Casts this {@code Class} object to represent a subclass of the class
   748      * represented by the specified class object.  Checks that that the cast
   749      * is valid, and throws a {@code ClassCastException} if it is not.  If
   750      * this method succeeds, it always returns a reference to this class object.
   751      *
   752      * <p>This method is useful when a client needs to "narrow" the type of
   753      * a {@code Class} object to pass it to an API that restricts the
   754      * {@code Class} objects that it is willing to accept.  A cast would
   755      * generate a compile-time warning, as the correctness of the cast
   756      * could not be checked at runtime (because generic types are implemented
   757      * by erasure).
   758      *
   759      * @return this {@code Class} object, cast to represent a subclass of
   760      *    the specified class object.
   761      * @throws ClassCastException if this {@code Class} object does not
   762      *    represent a subclass of the specified class (here "subclass" includes
   763      *    the class itself).
   764      * @since 1.5
   765      */
   766     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
   767         if (clazz.isAssignableFrom(this))
   768             return (Class<? extends U>) this;
   769         else
   770             throw new ClassCastException(this.toString());
   771     }
   772 
   773     /**
   774      * @throws NullPointerException {@inheritDoc}
   775      * @since 1.5
   776      */
   777     @JavaScriptBody(args = { "self", "ac" }, 
   778         body = 
   779           "if (self.anno) {"
   780         + "  return self.anno['L' + ac.jvmName + ';'];"
   781         + "}"
   782     )
   783     private Object getAnnotationData(Class<?> annotationClass) {
   784         throw new UnsupportedOperationException();
   785     }
   786     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
   787         Object data = getAnnotationData(annotationClass);
   788         return data == null ? null : AnnotationImpl.create(annotationClass, data);
   789     }
   790 
   791     /**
   792      * @throws NullPointerException {@inheritDoc}
   793      * @since 1.5
   794      */
   795     @JavaScriptBody(args = { "self", "ac" }, 
   796         body = "if (self.anno && self.anno['L' + ac.jvmName + ';']) { return true; }"
   797         + "else return false;"
   798     )
   799     public boolean isAnnotationPresent(
   800         Class<? extends Annotation> annotationClass) {
   801         if (annotationClass == null)
   802             throw new NullPointerException();
   803 
   804         return getAnnotation(annotationClass) != null;
   805     }
   806 
   807 
   808     /**
   809      * @since 1.5
   810      */
   811     public Annotation[] getAnnotations() {
   812         throw new UnsupportedOperationException();
   813     }
   814 
   815     /**
   816      * @since 1.5
   817      */
   818     public Annotation[] getDeclaredAnnotations()  {
   819         throw new UnsupportedOperationException();
   820     }
   821 
   822     static Class getPrimitiveClass(String type) {
   823         // XXX
   824         return Object.class;
   825     }
   826 
   827     public boolean desiredAssertionStatus() {
   828         return false;
   829     }
   830 }