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