emul/src/main/java/java/lang/Class.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 01 Dec 2012 12:35:30 +0100
branchreflection
changeset 229 fb4c53e0097f
parent 228 c40f70283620
child 230 b22dbc9329ec
permissions -rw-r--r--
Class.getSimpleName() works
     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     public T newInstance()
   195         throws InstantiationException, IllegalAccessException
   196     {
   197         throw new UnsupportedOperationException("Should be somehow supported");
   198     }
   199 
   200     /**
   201      * Determines if the specified {@code Object} is assignment-compatible
   202      * with the object represented by this {@code Class}.  This method is
   203      * the dynamic equivalent of the Java language {@code instanceof}
   204      * operator. The method returns {@code true} if the specified
   205      * {@code Object} argument is non-null and can be cast to the
   206      * reference type represented by this {@code Class} object without
   207      * raising a {@code ClassCastException.} It returns {@code false}
   208      * otherwise.
   209      *
   210      * <p> Specifically, if this {@code Class} object represents a
   211      * declared class, this method returns {@code true} if the specified
   212      * {@code Object} argument is an instance of the represented class (or
   213      * of any of its subclasses); it returns {@code false} otherwise. If
   214      * this {@code Class} object represents an array class, this method
   215      * returns {@code true} if the specified {@code Object} argument
   216      * can be converted to an object of the array class by an identity
   217      * conversion or by a widening reference conversion; it returns
   218      * {@code false} otherwise. If this {@code Class} object
   219      * represents an interface, this method returns {@code true} if the
   220      * class or any superclass of the specified {@code Object} argument
   221      * implements this interface; it returns {@code false} otherwise. If
   222      * this {@code Class} object represents a primitive type, this method
   223      * returns {@code false}.
   224      *
   225      * @param   obj the object to check
   226      * @return  true if {@code obj} is an instance of this class
   227      *
   228      * @since JDK1.1
   229      */
   230     public native boolean isInstance(Object obj);
   231 
   232 
   233     /**
   234      * Determines if the class or interface represented by this
   235      * {@code Class} object is either the same as, or is a superclass or
   236      * superinterface of, the class or interface represented by the specified
   237      * {@code Class} parameter. It returns {@code true} if so;
   238      * otherwise it returns {@code false}. If this {@code Class}
   239      * object represents a primitive type, this method returns
   240      * {@code true} if the specified {@code Class} parameter is
   241      * exactly this {@code Class} object; otherwise it returns
   242      * {@code false}.
   243      *
   244      * <p> Specifically, this method tests whether the type represented by the
   245      * specified {@code Class} parameter can be converted to the type
   246      * represented by this {@code Class} object via an identity conversion
   247      * or via a widening reference conversion. See <em>The Java Language
   248      * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
   249      *
   250      * @param cls the {@code Class} object to be checked
   251      * @return the {@code boolean} value indicating whether objects of the
   252      * type {@code cls} can be assigned to objects of this class
   253      * @exception NullPointerException if the specified Class parameter is
   254      *            null.
   255      * @since JDK1.1
   256      */
   257     public native boolean isAssignableFrom(Class<?> cls);
   258 
   259 
   260     /**
   261      * Determines if the specified {@code Class} object represents an
   262      * interface type.
   263      *
   264      * @return  {@code true} if this object represents an interface;
   265      *          {@code false} otherwise.
   266      */
   267     public native boolean isInterface();
   268 
   269 
   270     /**
   271      * Determines if this {@code Class} object represents an array class.
   272      *
   273      * @return  {@code true} if this object represents an array class;
   274      *          {@code false} otherwise.
   275      * @since   JDK1.1
   276      */
   277     public boolean isArray() {
   278         return false;
   279     }
   280 
   281 
   282     /**
   283      * Determines if the specified {@code Class} object represents a
   284      * primitive type.
   285      *
   286      * <p> There are nine predefined {@code Class} objects to represent
   287      * the eight primitive types and void.  These are created by the Java
   288      * Virtual Machine, and have the same names as the primitive types that
   289      * they represent, namely {@code boolean}, {@code byte},
   290      * {@code char}, {@code short}, {@code int},
   291      * {@code long}, {@code float}, and {@code double}.
   292      *
   293      * <p> These objects may only be accessed via the following public static
   294      * final variables, and are the only {@code Class} objects for which
   295      * this method returns {@code true}.
   296      *
   297      * @return true if and only if this class represents a primitive type
   298      *
   299      * @see     java.lang.Boolean#TYPE
   300      * @see     java.lang.Character#TYPE
   301      * @see     java.lang.Byte#TYPE
   302      * @see     java.lang.Short#TYPE
   303      * @see     java.lang.Integer#TYPE
   304      * @see     java.lang.Long#TYPE
   305      * @see     java.lang.Float#TYPE
   306      * @see     java.lang.Double#TYPE
   307      * @see     java.lang.Void#TYPE
   308      * @since JDK1.1
   309      */
   310     public native boolean isPrimitive();
   311 
   312     /**
   313      * Returns true if this {@code Class} object represents an annotation
   314      * type.  Note that if this method returns true, {@link #isInterface()}
   315      * would also return true, as all annotation types are also interfaces.
   316      *
   317      * @return {@code true} if this class object represents an annotation
   318      *      type; {@code false} otherwise
   319      * @since 1.5
   320      */
   321     public boolean isAnnotation() {
   322         return (getModifiers() & ANNOTATION) != 0;
   323     }
   324 
   325     /**
   326      * Returns {@code true} if this class is a synthetic class;
   327      * returns {@code false} otherwise.
   328      * @return {@code true} if and only if this class is a synthetic class as
   329      *         defined by the Java Language Specification.
   330      * @since 1.5
   331      */
   332     public boolean isSynthetic() {
   333         return (getModifiers() & SYNTHETIC) != 0;
   334     }
   335 
   336     /**
   337      * Returns the  name of the entity (class, interface, array class,
   338      * primitive type, or void) represented by this {@code Class} object,
   339      * as a {@code String}.
   340      *
   341      * <p> If this class object represents a reference type that is not an
   342      * array type then the binary name of the class is returned, as specified
   343      * by
   344      * <cite>The Java&trade; Language Specification</cite>.
   345      *
   346      * <p> If this class object represents a primitive type or void, then the
   347      * name returned is a {@code String} equal to the Java language
   348      * keyword corresponding to the primitive type or void.
   349      *
   350      * <p> If this class object represents a class of arrays, then the internal
   351      * form of the name consists of the name of the element type preceded by
   352      * one or more '{@code [}' characters representing the depth of the array
   353      * nesting.  The encoding of element type names is as follows:
   354      *
   355      * <blockquote><table summary="Element types and encodings">
   356      * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
   357      * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
   358      * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
   359      * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
   360      * <tr><td> class or interface
   361      *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
   362      * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
   363      * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
   364      * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
   365      * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
   366      * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
   367      * </table></blockquote>
   368      *
   369      * <p> The class or interface name <i>classname</i> is the binary name of
   370      * the class specified above.
   371      *
   372      * <p> Examples:
   373      * <blockquote><pre>
   374      * String.class.getName()
   375      *     returns "java.lang.String"
   376      * byte.class.getName()
   377      *     returns "byte"
   378      * (new Object[3]).getClass().getName()
   379      *     returns "[Ljava.lang.Object;"
   380      * (new int[3][4][5][6][7][8][9]).getClass().getName()
   381      *     returns "[[[[[[[I"
   382      * </pre></blockquote>
   383      *
   384      * @return  the name of the class or interface
   385      *          represented by this object.
   386      */
   387     public String getName() {
   388         return jvmName().replace('/', '.');
   389     }
   390 
   391     @JavaScriptBody(args = "self", body = "return self.jvmName;")
   392     private native String jvmName();
   393 
   394     /**
   395      * Returns the {@code Class} representing the superclass of the entity
   396      * (class, interface, primitive type or void) represented by this
   397      * {@code Class}.  If this {@code Class} represents either the
   398      * {@code Object} class, an interface, a primitive type, or void, then
   399      * null is returned.  If this object represents an array class then the
   400      * {@code Class} object representing the {@code Object} class is
   401      * returned.
   402      *
   403      * @return the superclass of the class represented by this object.
   404      */
   405     public native Class<? super T> getSuperclass();
   406 
   407     /**
   408      * Returns the Java language modifiers for this class or interface, encoded
   409      * in an integer. The modifiers consist of the Java Virtual Machine's
   410      * constants for {@code public}, {@code protected},
   411      * {@code private}, {@code final}, {@code static},
   412      * {@code abstract} and {@code interface}; they should be decoded
   413      * using the methods of class {@code Modifier}.
   414      *
   415      * <p> If the underlying class is an array class, then its
   416      * {@code public}, {@code private} and {@code protected}
   417      * modifiers are the same as those of its component type.  If this
   418      * {@code Class} represents a primitive type or void, its
   419      * {@code public} modifier is always {@code true}, and its
   420      * {@code protected} and {@code private} modifiers are always
   421      * {@code false}. If this object represents an array class, a
   422      * primitive type or void, then its {@code final} modifier is always
   423      * {@code true} and its interface modifier is always
   424      * {@code false}. The values of its other modifiers are not determined
   425      * by this specification.
   426      *
   427      * <p> The modifier encodings are defined in <em>The Java Virtual Machine
   428      * Specification</em>, table 4.1.
   429      *
   430      * @return the {@code int} representing the modifiers for this class
   431      * @see     java.lang.reflect.Modifier
   432      * @since JDK1.1
   433      */
   434     public native int getModifiers();
   435 
   436 
   437     /**
   438      * Returns the simple name of the underlying class as given in the
   439      * source code. Returns an empty string if the underlying class is
   440      * anonymous.
   441      *
   442      * <p>The simple name of an array is the simple name of the
   443      * component type with "[]" appended.  In particular the simple
   444      * name of an array whose component type is anonymous is "[]".
   445      *
   446      * @return the simple name of the underlying class
   447      * @since 1.5
   448      */
   449     public String getSimpleName() {
   450         if (isArray())
   451             return getComponentType().getSimpleName()+"[]";
   452 
   453         String simpleName = getSimpleBinaryName();
   454         if (simpleName == null) { // top level class
   455             simpleName = getName();
   456             return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
   457         }
   458         // According to JLS3 "Binary Compatibility" (13.1) the binary
   459         // name of non-package classes (not top level) is the binary
   460         // name of the immediately enclosing class followed by a '$' followed by:
   461         // (for nested and inner classes): the simple name.
   462         // (for local classes): 1 or more digits followed by the simple name.
   463         // (for anonymous classes): 1 or more digits.
   464 
   465         // Since getSimpleBinaryName() will strip the binary name of
   466         // the immediatly enclosing class, we are now looking at a
   467         // string that matches the regular expression "\$[0-9]*"
   468         // followed by a simple name (considering the simple of an
   469         // anonymous class to be the empty string).
   470 
   471         // Remove leading "\$[0-9]*" from the name
   472         int length = simpleName.length();
   473         if (length < 1 || simpleName.charAt(0) != '$')
   474             throw new IllegalStateException("Malformed class name");
   475         int index = 1;
   476         while (index < length && isAsciiDigit(simpleName.charAt(index)))
   477             index++;
   478         // Eventually, this is the empty string iff this is an anonymous class
   479         return simpleName.substring(index);
   480     }
   481 
   482     /**
   483      * Returns the "simple binary name" of the underlying class, i.e.,
   484      * the binary name without the leading enclosing class name.
   485      * Returns {@code null} if the underlying class is a top level
   486      * class.
   487      */
   488     private String getSimpleBinaryName() {
   489         Class<?> enclosingClass = null; // XXX getEnclosingClass();
   490         if (enclosingClass == null) // top level class
   491             return null;
   492         // Otherwise, strip the enclosing class' name
   493         try {
   494             return getName().substring(enclosingClass.getName().length());
   495         } catch (IndexOutOfBoundsException ex) {
   496             throw new IllegalStateException("Malformed class name");
   497         }
   498     }
   499     
   500     /**
   501      * Character.isDigit answers {@code true} to some non-ascii
   502      * digits.  This one does not.
   503      */
   504     private static boolean isAsciiDigit(char c) {
   505         return '0' <= c && c <= '9';
   506     }
   507 
   508     /**
   509      * Returns the canonical name of the underlying class as
   510      * defined by the Java Language Specification.  Returns null if
   511      * the underlying class does not have a canonical name (i.e., if
   512      * it is a local or anonymous class or an array whose component
   513      * type does not have a canonical name).
   514      * @return the canonical name of the underlying class if it exists, and
   515      * {@code null} otherwise.
   516      * @since 1.5
   517      */
   518     public String getCanonicalName() {
   519         if (isArray()) {
   520             String canonicalName = getComponentType().getCanonicalName();
   521             if (canonicalName != null)
   522                 return canonicalName + "[]";
   523             else
   524                 return null;
   525         }
   526 //        if (isLocalOrAnonymousClass())
   527 //            return null;
   528 //        Class<?> enclosingClass = getEnclosingClass();
   529         Class<?> enclosingClass = null;
   530         if (enclosingClass == null) { // top level class
   531             return getName();
   532         } else {
   533             String enclosingName = enclosingClass.getCanonicalName();
   534             if (enclosingName == null)
   535                 return null;
   536             return enclosingName + "." + getSimpleName();
   537         }
   538     }
   539 
   540     /**
   541      * Finds a resource with a given name.  The rules for searching resources
   542      * associated with a given class are implemented by the defining
   543      * {@linkplain ClassLoader class loader} of the class.  This method
   544      * delegates to this object's class loader.  If this object was loaded by
   545      * the bootstrap class loader, the method delegates to {@link
   546      * ClassLoader#getSystemResourceAsStream}.
   547      *
   548      * <p> Before delegation, an absolute resource name is constructed from the
   549      * given resource name using this algorithm:
   550      *
   551      * <ul>
   552      *
   553      * <li> If the {@code name} begins with a {@code '/'}
   554      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
   555      * portion of the {@code name} following the {@code '/'}.
   556      *
   557      * <li> Otherwise, the absolute name is of the following form:
   558      *
   559      * <blockquote>
   560      *   {@code modified_package_name/name}
   561      * </blockquote>
   562      *
   563      * <p> Where the {@code modified_package_name} is the package name of this
   564      * object with {@code '/'} substituted for {@code '.'}
   565      * (<tt>'&#92;u002e'</tt>).
   566      *
   567      * </ul>
   568      *
   569      * @param  name name of the desired resource
   570      * @return      A {@link java.io.InputStream} object or {@code null} if
   571      *              no resource with this name is found
   572      * @throws  NullPointerException If {@code name} is {@code null}
   573      * @since  JDK1.1
   574      */
   575      public InputStream getResourceAsStream(String name) {
   576         name = resolveName(name);
   577         ClassLoader cl = getClassLoader0();
   578         if (cl==null) {
   579             // A system class.
   580             return ClassLoader.getSystemResourceAsStream(name);
   581         }
   582         return cl.getResourceAsStream(name);
   583     }
   584 
   585     /**
   586      * Finds a resource with a given name.  The rules for searching resources
   587      * associated with a given class are implemented by the defining
   588      * {@linkplain ClassLoader class loader} of the class.  This method
   589      * delegates to this object's class loader.  If this object was loaded by
   590      * the bootstrap class loader, the method delegates to {@link
   591      * ClassLoader#getSystemResource}.
   592      *
   593      * <p> Before delegation, an absolute resource name is constructed from the
   594      * given resource name using this algorithm:
   595      *
   596      * <ul>
   597      *
   598      * <li> If the {@code name} begins with a {@code '/'}
   599      * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
   600      * portion of the {@code name} following the {@code '/'}.
   601      *
   602      * <li> Otherwise, the absolute name is of the following form:
   603      *
   604      * <blockquote>
   605      *   {@code modified_package_name/name}
   606      * </blockquote>
   607      *
   608      * <p> Where the {@code modified_package_name} is the package name of this
   609      * object with {@code '/'} substituted for {@code '.'}
   610      * (<tt>'&#92;u002e'</tt>).
   611      *
   612      * </ul>
   613      *
   614      * @param  name name of the desired resource
   615      * @return      A  {@link java.net.URL} object or {@code null} if no
   616      *              resource with this name is found
   617      * @since  JDK1.1
   618      */
   619     public java.net.URL getResource(String name) {
   620         name = resolveName(name);
   621         ClassLoader cl = getClassLoader0();
   622         if (cl==null) {
   623             // A system class.
   624             return ClassLoader.getSystemResource(name);
   625         }
   626         return cl.getResource(name);
   627     }
   628 
   629 
   630    /**
   631      * Add a package name prefix if the name is not absolute Remove leading "/"
   632      * if name is absolute
   633      */
   634     private String resolveName(String name) {
   635         if (name == null) {
   636             return name;
   637         }
   638         if (!name.startsWith("/")) {
   639             Class<?> c = this;
   640             while (c.isArray()) {
   641                 c = c.getComponentType();
   642             }
   643             String baseName = c.getName();
   644             int index = baseName.lastIndexOf('.');
   645             if (index != -1) {
   646                 name = baseName.substring(0, index).replace('.', '/')
   647                     +"/"+name;
   648             }
   649         } else {
   650             name = name.substring(1);
   651         }
   652         return name;
   653     }
   654     
   655     /**
   656      * Returns the class loader for the class.  Some implementations may use
   657      * null to represent the bootstrap class loader. This method will return
   658      * null in such implementations if this class was loaded by the bootstrap
   659      * class loader.
   660      *
   661      * <p> If a security manager is present, and the caller's class loader is
   662      * not null and the caller's class loader is not the same as or an ancestor of
   663      * the class loader for the class whose class loader is requested, then
   664      * this method calls the security manager's {@code checkPermission}
   665      * method with a {@code RuntimePermission("getClassLoader")}
   666      * permission to ensure it's ok to access the class loader for the class.
   667      *
   668      * <p>If this object
   669      * represents a primitive type or void, null is returned.
   670      *
   671      * @return  the class loader that loaded the class or interface
   672      *          represented by this object.
   673      * @throws SecurityException
   674      *    if a security manager exists and its
   675      *    {@code checkPermission} method denies
   676      *    access to the class loader for the class.
   677      * @see java.lang.ClassLoader
   678      * @see SecurityManager#checkPermission
   679      * @see java.lang.RuntimePermission
   680      */
   681     public ClassLoader getClassLoader() {
   682         throw new SecurityException();
   683     }
   684     
   685     // Package-private to allow ClassLoader access
   686     native ClassLoader getClassLoader0();    
   687 
   688     /**
   689      * Returns the {@code Class} representing the component type of an
   690      * array.  If this class does not represent an array class this method
   691      * returns null.
   692      *
   693      * @return the {@code Class} representing the component type of this
   694      * class if this class is an array
   695      * @see     java.lang.reflect.Array
   696      * @since JDK1.1
   697      */
   698     public Class<?> getComponentType() {
   699         return null;
   700     }
   701 
   702     /**
   703      * Returns true if and only if this class was declared as an enum in the
   704      * source code.
   705      *
   706      * @return true if and only if this class was declared as an enum in the
   707      *     source code
   708      * @since 1.5
   709      */
   710     public boolean isEnum() {
   711         // An enum must both directly extend java.lang.Enum and have
   712         // the ENUM bit set; classes for specialized enum constants
   713         // don't do the former.
   714         return (this.getModifiers() & ENUM) != 0 &&
   715         this.getSuperclass() == java.lang.Enum.class;
   716     }
   717 
   718     /**
   719      * Casts an object to the class or interface represented
   720      * by this {@code Class} object.
   721      *
   722      * @param obj the object to be cast
   723      * @return the object after casting, or null if obj is null
   724      *
   725      * @throws ClassCastException if the object is not
   726      * null and is not assignable to the type T.
   727      *
   728      * @since 1.5
   729      */
   730     public T cast(Object obj) {
   731         if (obj != null && !isInstance(obj))
   732             throw new ClassCastException(cannotCastMsg(obj));
   733         return (T) obj;
   734     }
   735 
   736     private String cannotCastMsg(Object obj) {
   737         return "Cannot cast " + obj.getClass().getName() + " to " + getName();
   738     }
   739 
   740     /**
   741      * Casts this {@code Class} object to represent a subclass of the class
   742      * represented by the specified class object.  Checks that that the cast
   743      * is valid, and throws a {@code ClassCastException} if it is not.  If
   744      * this method succeeds, it always returns a reference to this class object.
   745      *
   746      * <p>This method is useful when a client needs to "narrow" the type of
   747      * a {@code Class} object to pass it to an API that restricts the
   748      * {@code Class} objects that it is willing to accept.  A cast would
   749      * generate a compile-time warning, as the correctness of the cast
   750      * could not be checked at runtime (because generic types are implemented
   751      * by erasure).
   752      *
   753      * @return this {@code Class} object, cast to represent a subclass of
   754      *    the specified class object.
   755      * @throws ClassCastException if this {@code Class} object does not
   756      *    represent a subclass of the specified class (here "subclass" includes
   757      *    the class itself).
   758      * @since 1.5
   759      */
   760     public <U> Class<? extends U> asSubclass(Class<U> clazz) {
   761         if (clazz.isAssignableFrom(this))
   762             return (Class<? extends U>) this;
   763         else
   764             throw new ClassCastException(this.toString());
   765     }
   766 
   767     /**
   768      * @throws NullPointerException {@inheritDoc}
   769      * @since 1.5
   770      */
   771     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
   772         throw new UnsupportedOperationException();
   773     }
   774 
   775     /**
   776      * @throws NullPointerException {@inheritDoc}
   777      * @since 1.5
   778      */
   779     public boolean isAnnotationPresent(
   780         Class<? extends Annotation> annotationClass) {
   781         if (annotationClass == null)
   782             throw new NullPointerException();
   783 
   784         return getAnnotation(annotationClass) != null;
   785     }
   786 
   787 
   788     /**
   789      * @since 1.5
   790      */
   791     public Annotation[] getAnnotations() {
   792         throw new UnsupportedOperationException();
   793     }
   794 
   795     /**
   796      * @since 1.5
   797      */
   798     public Annotation[] getDeclaredAnnotations()  {
   799         throw new UnsupportedOperationException();
   800     }
   801 
   802     static Class getPrimitiveClass(String type) {
   803         // XXX
   804         return Object.class;
   805     }
   806 
   807     public boolean desiredAssertionStatus() {
   808         return false;
   809     }
   810 }