emul/src/main/java/java/lang/Class.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 04 Dec 2012 14:49:01 +0100
branchreflection
changeset 261 5d1e20215d12
parent 260 1d03cb35fbda
child 262 683719ffcfe7
permissions -rw-r--r--
Test to verify reflective call on a method of a class
jaroslav@56
     1
/*
jaroslav@56
     2
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@56
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@56
     4
 *
jaroslav@56
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@56
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@56
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@56
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@56
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@56
    10
 *
jaroslav@56
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@56
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@56
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@56
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@56
    15
 * accompanied this code).
jaroslav@56
    16
 *
jaroslav@56
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@56
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@56
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@56
    20
 *
jaroslav@56
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@56
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@56
    23
 * questions.
jaroslav@56
    24
 */
jaroslav@56
    25
jaroslav@56
    26
package java.lang;
jaroslav@56
    27
jaroslav@122
    28
import java.io.InputStream;
jaroslav@56
    29
import java.lang.annotation.Annotation;
jaroslav@261
    30
import java.lang.reflect.Field;
jaroslav@261
    31
import java.lang.reflect.Method;
jaroslav@260
    32
import java.lang.reflect.TypeVariable;
jaroslav@225
    33
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@56
    34
jaroslav@56
    35
/**
jaroslav@56
    36
 * Instances of the class {@code Class} represent classes and
jaroslav@56
    37
 * interfaces in a running Java application.  An enum is a kind of
jaroslav@56
    38
 * class and an annotation is a kind of interface.  Every array also
jaroslav@56
    39
 * belongs to a class that is reflected as a {@code Class} object
jaroslav@56
    40
 * that is shared by all arrays with the same element type and number
jaroslav@56
    41
 * of dimensions.  The primitive Java types ({@code boolean},
jaroslav@56
    42
 * {@code byte}, {@code char}, {@code short},
jaroslav@56
    43
 * {@code int}, {@code long}, {@code float}, and
jaroslav@56
    44
 * {@code double}), and the keyword {@code void} are also
jaroslav@56
    45
 * represented as {@code Class} objects.
jaroslav@56
    46
 *
jaroslav@56
    47
 * <p> {@code Class} has no public constructor. Instead {@code Class}
jaroslav@56
    48
 * objects are constructed automatically by the Java Virtual Machine as classes
jaroslav@56
    49
 * are loaded and by calls to the {@code defineClass} method in the class
jaroslav@56
    50
 * loader.
jaroslav@56
    51
 *
jaroslav@56
    52
 * <p> The following example uses a {@code Class} object to print the
jaroslav@56
    53
 * class name of an object:
jaroslav@56
    54
 *
jaroslav@56
    55
 * <p> <blockquote><pre>
jaroslav@56
    56
 *     void printClassName(Object obj) {
jaroslav@56
    57
 *         System.out.println("The class of " + obj +
jaroslav@56
    58
 *                            " is " + obj.getClass().getName());
jaroslav@56
    59
 *     }
jaroslav@56
    60
 * </pre></blockquote>
jaroslav@56
    61
 *
jaroslav@56
    62
 * <p> It is also possible to get the {@code Class} object for a named
jaroslav@56
    63
 * type (or for void) using a class literal.  See Section 15.8.2 of
jaroslav@56
    64
 * <cite>The Java&trade; Language Specification</cite>.
jaroslav@56
    65
 * For example:
jaroslav@56
    66
 *
jaroslav@56
    67
 * <p> <blockquote>
jaroslav@56
    68
 *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
jaroslav@56
    69
 * </blockquote>
jaroslav@56
    70
 *
jaroslav@56
    71
 * @param <T> the type of the class modeled by this {@code Class}
jaroslav@56
    72
 * object.  For example, the type of {@code String.class} is {@code
jaroslav@56
    73
 * Class<String>}.  Use {@code Class<?>} if the class being modeled is
jaroslav@56
    74
 * unknown.
jaroslav@56
    75
 *
jaroslav@56
    76
 * @author  unascribed
jaroslav@56
    77
 * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
jaroslav@56
    78
 * @since   JDK1.0
jaroslav@56
    79
 */
jaroslav@56
    80
public final
jaroslav@260
    81
    class Class<T> implements java.io.Serializable,
jaroslav@260
    82
                              java.lang.reflect.GenericDeclaration,
jaroslav@260
    83
                              java.lang.reflect.Type,
jaroslav@260
    84
                              java.lang.reflect.AnnotatedElement {
jaroslav@56
    85
    private static final int ANNOTATION= 0x00002000;
jaroslav@56
    86
    private static final int ENUM      = 0x00004000;
jaroslav@56
    87
    private static final int SYNTHETIC = 0x00001000;
jaroslav@56
    88
jaroslav@56
    89
    /*
jaroslav@56
    90
     * Constructor. Only the Java Virtual Machine creates Class
jaroslav@56
    91
     * objects.
jaroslav@56
    92
     */
jaroslav@56
    93
    private Class() {}
jaroslav@56
    94
jaroslav@56
    95
jaroslav@56
    96
    /**
jaroslav@56
    97
     * Converts the object to a string. The string representation is the
jaroslav@56
    98
     * string "class" or "interface", followed by a space, and then by the
jaroslav@56
    99
     * fully qualified name of the class in the format returned by
jaroslav@56
   100
     * {@code getName}.  If this {@code Class} object represents a
jaroslav@56
   101
     * primitive type, this method returns the name of the primitive type.  If
jaroslav@56
   102
     * this {@code Class} object represents void this method returns
jaroslav@56
   103
     * "void".
jaroslav@56
   104
     *
jaroslav@56
   105
     * @return a string representation of this class object.
jaroslav@56
   106
     */
jaroslav@56
   107
    public String toString() {
jaroslav@56
   108
        return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
jaroslav@56
   109
            + getName();
jaroslav@56
   110
    }
jaroslav@56
   111
jaroslav@56
   112
jaroslav@56
   113
    /**
jaroslav@56
   114
     * Returns the {@code Class} object associated with the class or
jaroslav@56
   115
     * interface with the given string name.  Invoking this method is
jaroslav@56
   116
     * equivalent to:
jaroslav@56
   117
     *
jaroslav@56
   118
     * <blockquote>
jaroslav@56
   119
     *  {@code Class.forName(className, true, currentLoader)}
jaroslav@56
   120
     * </blockquote>
jaroslav@56
   121
     *
jaroslav@56
   122
     * where {@code currentLoader} denotes the defining class loader of
jaroslav@56
   123
     * the current class.
jaroslav@56
   124
     *
jaroslav@56
   125
     * <p> For example, the following code fragment returns the
jaroslav@56
   126
     * runtime {@code Class} descriptor for the class named
jaroslav@56
   127
     * {@code java.lang.Thread}:
jaroslav@56
   128
     *
jaroslav@56
   129
     * <blockquote>
jaroslav@56
   130
     *   {@code Class t = Class.forName("java.lang.Thread")}
jaroslav@56
   131
     * </blockquote>
jaroslav@56
   132
     * <p>
jaroslav@56
   133
     * A call to {@code forName("X")} causes the class named
jaroslav@56
   134
     * {@code X} to be initialized.
jaroslav@56
   135
     *
jaroslav@56
   136
     * @param      className   the fully qualified name of the desired class.
jaroslav@56
   137
     * @return     the {@code Class} object for the class with the
jaroslav@56
   138
     *             specified name.
jaroslav@56
   139
     * @exception LinkageError if the linkage fails
jaroslav@56
   140
     * @exception ExceptionInInitializerError if the initialization provoked
jaroslav@56
   141
     *            by this method fails
jaroslav@56
   142
     * @exception ClassNotFoundException if the class cannot be located
jaroslav@56
   143
     */
jaroslav@56
   144
    public static Class<?> forName(String className)
jaroslav@56
   145
                throws ClassNotFoundException {
jaroslav@65
   146
        throw new UnsupportedOperationException();
jaroslav@56
   147
    }
jaroslav@56
   148
jaroslav@56
   149
jaroslav@56
   150
    /**
jaroslav@56
   151
     * Creates a new instance of the class represented by this {@code Class}
jaroslav@56
   152
     * object.  The class is instantiated as if by a {@code new}
jaroslav@56
   153
     * expression with an empty argument list.  The class is initialized if it
jaroslav@56
   154
     * has not already been initialized.
jaroslav@56
   155
     *
jaroslav@56
   156
     * <p>Note that this method propagates any exception thrown by the
jaroslav@56
   157
     * nullary constructor, including a checked exception.  Use of
jaroslav@56
   158
     * this method effectively bypasses the compile-time exception
jaroslav@56
   159
     * checking that would otherwise be performed by the compiler.
jaroslav@56
   160
     * The {@link
jaroslav@56
   161
     * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
jaroslav@56
   162
     * Constructor.newInstance} method avoids this problem by wrapping
jaroslav@56
   163
     * any exception thrown by the constructor in a (checked) {@link
jaroslav@56
   164
     * java.lang.reflect.InvocationTargetException}.
jaroslav@56
   165
     *
jaroslav@56
   166
     * @return     a newly allocated instance of the class represented by this
jaroslav@56
   167
     *             object.
jaroslav@56
   168
     * @exception  IllegalAccessException  if the class or its nullary
jaroslav@56
   169
     *               constructor is not accessible.
jaroslav@56
   170
     * @exception  InstantiationException
jaroslav@56
   171
     *               if this {@code Class} represents an abstract class,
jaroslav@56
   172
     *               an interface, an array class, a primitive type, or void;
jaroslav@56
   173
     *               or if the class has no nullary constructor;
jaroslav@56
   174
     *               or if the instantiation fails for some other reason.
jaroslav@56
   175
     * @exception  ExceptionInInitializerError if the initialization
jaroslav@56
   176
     *               provoked by this method fails.
jaroslav@56
   177
     * @exception  SecurityException
jaroslav@56
   178
     *             If a security manager, <i>s</i>, is present and any of the
jaroslav@56
   179
     *             following conditions is met:
jaroslav@56
   180
     *
jaroslav@56
   181
     *             <ul>
jaroslav@56
   182
     *
jaroslav@56
   183
     *             <li> invocation of
jaroslav@56
   184
     *             {@link SecurityManager#checkMemberAccess
jaroslav@56
   185
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
jaroslav@56
   186
     *             creation of new instances of this class
jaroslav@56
   187
     *
jaroslav@56
   188
     *             <li> the caller's class loader is not the same as or an
jaroslav@56
   189
     *             ancestor of the class loader for the current class and
jaroslav@56
   190
     *             invocation of {@link SecurityManager#checkPackageAccess
jaroslav@56
   191
     *             s.checkPackageAccess()} denies access to the package
jaroslav@56
   192
     *             of this class
jaroslav@56
   193
     *
jaroslav@56
   194
     *             </ul>
jaroslav@56
   195
     *
jaroslav@56
   196
     */
jaroslav@231
   197
    @JavaScriptBody(args = "self", body =
jaroslav@231
   198
          "var inst = self.cnstr();"
jaroslav@251
   199
        + "inst.cons__V(inst);"
jaroslav@231
   200
        + "return inst;"
jaroslav@231
   201
    )
jaroslav@56
   202
    public T newInstance()
jaroslav@56
   203
        throws InstantiationException, IllegalAccessException
jaroslav@56
   204
    {
jaroslav@231
   205
        throw new UnsupportedOperationException();
jaroslav@56
   206
    }
jaroslav@56
   207
jaroslav@56
   208
    /**
jaroslav@56
   209
     * Determines if the specified {@code Object} is assignment-compatible
jaroslav@56
   210
     * with the object represented by this {@code Class}.  This method is
jaroslav@56
   211
     * the dynamic equivalent of the Java language {@code instanceof}
jaroslav@56
   212
     * operator. The method returns {@code true} if the specified
jaroslav@56
   213
     * {@code Object} argument is non-null and can be cast to the
jaroslav@56
   214
     * reference type represented by this {@code Class} object without
jaroslav@56
   215
     * raising a {@code ClassCastException.} It returns {@code false}
jaroslav@56
   216
     * otherwise.
jaroslav@56
   217
     *
jaroslav@56
   218
     * <p> Specifically, if this {@code Class} object represents a
jaroslav@56
   219
     * declared class, this method returns {@code true} if the specified
jaroslav@56
   220
     * {@code Object} argument is an instance of the represented class (or
jaroslav@56
   221
     * of any of its subclasses); it returns {@code false} otherwise. If
jaroslav@56
   222
     * this {@code Class} object represents an array class, this method
jaroslav@56
   223
     * returns {@code true} if the specified {@code Object} argument
jaroslav@56
   224
     * can be converted to an object of the array class by an identity
jaroslav@56
   225
     * conversion or by a widening reference conversion; it returns
jaroslav@56
   226
     * {@code false} otherwise. If this {@code Class} object
jaroslav@56
   227
     * represents an interface, this method returns {@code true} if the
jaroslav@56
   228
     * class or any superclass of the specified {@code Object} argument
jaroslav@56
   229
     * implements this interface; it returns {@code false} otherwise. If
jaroslav@56
   230
     * this {@code Class} object represents a primitive type, this method
jaroslav@56
   231
     * returns {@code false}.
jaroslav@56
   232
     *
jaroslav@56
   233
     * @param   obj the object to check
jaroslav@56
   234
     * @return  true if {@code obj} is an instance of this class
jaroslav@56
   235
     *
jaroslav@56
   236
     * @since JDK1.1
jaroslav@56
   237
     */
jaroslav@56
   238
    public native boolean isInstance(Object obj);
jaroslav@56
   239
jaroslav@56
   240
jaroslav@56
   241
    /**
jaroslav@56
   242
     * Determines if the class or interface represented by this
jaroslav@56
   243
     * {@code Class} object is either the same as, or is a superclass or
jaroslav@56
   244
     * superinterface of, the class or interface represented by the specified
jaroslav@56
   245
     * {@code Class} parameter. It returns {@code true} if so;
jaroslav@56
   246
     * otherwise it returns {@code false}. If this {@code Class}
jaroslav@56
   247
     * object represents a primitive type, this method returns
jaroslav@56
   248
     * {@code true} if the specified {@code Class} parameter is
jaroslav@56
   249
     * exactly this {@code Class} object; otherwise it returns
jaroslav@56
   250
     * {@code false}.
jaroslav@56
   251
     *
jaroslav@56
   252
     * <p> Specifically, this method tests whether the type represented by the
jaroslav@56
   253
     * specified {@code Class} parameter can be converted to the type
jaroslav@56
   254
     * represented by this {@code Class} object via an identity conversion
jaroslav@56
   255
     * or via a widening reference conversion. See <em>The Java Language
jaroslav@56
   256
     * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
jaroslav@56
   257
     *
jaroslav@56
   258
     * @param cls the {@code Class} object to be checked
jaroslav@56
   259
     * @return the {@code boolean} value indicating whether objects of the
jaroslav@56
   260
     * type {@code cls} can be assigned to objects of this class
jaroslav@56
   261
     * @exception NullPointerException if the specified Class parameter is
jaroslav@56
   262
     *            null.
jaroslav@56
   263
     * @since JDK1.1
jaroslav@56
   264
     */
jaroslav@56
   265
    public native boolean isAssignableFrom(Class<?> cls);
jaroslav@56
   266
jaroslav@56
   267
jaroslav@56
   268
    /**
jaroslav@56
   269
     * Determines if the specified {@code Class} object represents an
jaroslav@56
   270
     * interface type.
jaroslav@56
   271
     *
jaroslav@56
   272
     * @return  {@code true} if this object represents an interface;
jaroslav@56
   273
     *          {@code false} otherwise.
jaroslav@56
   274
     */
jaroslav@56
   275
    public native boolean isInterface();
jaroslav@56
   276
jaroslav@56
   277
jaroslav@56
   278
    /**
jaroslav@56
   279
     * Determines if this {@code Class} object represents an array class.
jaroslav@56
   280
     *
jaroslav@56
   281
     * @return  {@code true} if this object represents an array class;
jaroslav@56
   282
     *          {@code false} otherwise.
jaroslav@56
   283
     * @since   JDK1.1
jaroslav@56
   284
     */
jaroslav@228
   285
    public boolean isArray() {
jaroslav@228
   286
        return false;
jaroslav@228
   287
    }
jaroslav@56
   288
jaroslav@56
   289
jaroslav@56
   290
    /**
jaroslav@56
   291
     * Determines if the specified {@code Class} object represents a
jaroslav@56
   292
     * primitive type.
jaroslav@56
   293
     *
jaroslav@56
   294
     * <p> There are nine predefined {@code Class} objects to represent
jaroslav@56
   295
     * the eight primitive types and void.  These are created by the Java
jaroslav@56
   296
     * Virtual Machine, and have the same names as the primitive types that
jaroslav@56
   297
     * they represent, namely {@code boolean}, {@code byte},
jaroslav@56
   298
     * {@code char}, {@code short}, {@code int},
jaroslav@56
   299
     * {@code long}, {@code float}, and {@code double}.
jaroslav@56
   300
     *
jaroslav@56
   301
     * <p> These objects may only be accessed via the following public static
jaroslav@56
   302
     * final variables, and are the only {@code Class} objects for which
jaroslav@56
   303
     * this method returns {@code true}.
jaroslav@56
   304
     *
jaroslav@56
   305
     * @return true if and only if this class represents a primitive type
jaroslav@56
   306
     *
jaroslav@56
   307
     * @see     java.lang.Boolean#TYPE
jaroslav@56
   308
     * @see     java.lang.Character#TYPE
jaroslav@56
   309
     * @see     java.lang.Byte#TYPE
jaroslav@56
   310
     * @see     java.lang.Short#TYPE
jaroslav@56
   311
     * @see     java.lang.Integer#TYPE
jaroslav@56
   312
     * @see     java.lang.Long#TYPE
jaroslav@56
   313
     * @see     java.lang.Float#TYPE
jaroslav@56
   314
     * @see     java.lang.Double#TYPE
jaroslav@56
   315
     * @see     java.lang.Void#TYPE
jaroslav@56
   316
     * @since JDK1.1
jaroslav@56
   317
     */
jaroslav@56
   318
    public native boolean isPrimitive();
jaroslav@56
   319
jaroslav@56
   320
    /**
jaroslav@56
   321
     * Returns true if this {@code Class} object represents an annotation
jaroslav@56
   322
     * type.  Note that if this method returns true, {@link #isInterface()}
jaroslav@56
   323
     * would also return true, as all annotation types are also interfaces.
jaroslav@56
   324
     *
jaroslav@56
   325
     * @return {@code true} if this class object represents an annotation
jaroslav@56
   326
     *      type; {@code false} otherwise
jaroslav@56
   327
     * @since 1.5
jaroslav@56
   328
     */
jaroslav@56
   329
    public boolean isAnnotation() {
jaroslav@56
   330
        return (getModifiers() & ANNOTATION) != 0;
jaroslav@56
   331
    }
jaroslav@56
   332
jaroslav@56
   333
    /**
jaroslav@56
   334
     * Returns {@code true} if this class is a synthetic class;
jaroslav@56
   335
     * returns {@code false} otherwise.
jaroslav@56
   336
     * @return {@code true} if and only if this class is a synthetic class as
jaroslav@56
   337
     *         defined by the Java Language Specification.
jaroslav@56
   338
     * @since 1.5
jaroslav@56
   339
     */
jaroslav@56
   340
    public boolean isSynthetic() {
jaroslav@56
   341
        return (getModifiers() & SYNTHETIC) != 0;
jaroslav@56
   342
    }
jaroslav@56
   343
jaroslav@56
   344
    /**
jaroslav@56
   345
     * Returns the  name of the entity (class, interface, array class,
jaroslav@56
   346
     * primitive type, or void) represented by this {@code Class} object,
jaroslav@56
   347
     * as a {@code String}.
jaroslav@56
   348
     *
jaroslav@56
   349
     * <p> If this class object represents a reference type that is not an
jaroslav@56
   350
     * array type then the binary name of the class is returned, as specified
jaroslav@56
   351
     * by
jaroslav@56
   352
     * <cite>The Java&trade; Language Specification</cite>.
jaroslav@56
   353
     *
jaroslav@56
   354
     * <p> If this class object represents a primitive type or void, then the
jaroslav@56
   355
     * name returned is a {@code String} equal to the Java language
jaroslav@56
   356
     * keyword corresponding to the primitive type or void.
jaroslav@56
   357
     *
jaroslav@56
   358
     * <p> If this class object represents a class of arrays, then the internal
jaroslav@56
   359
     * form of the name consists of the name of the element type preceded by
jaroslav@56
   360
     * one or more '{@code [}' characters representing the depth of the array
jaroslav@56
   361
     * nesting.  The encoding of element type names is as follows:
jaroslav@56
   362
     *
jaroslav@56
   363
     * <blockquote><table summary="Element types and encodings">
jaroslav@56
   364
     * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
jaroslav@56
   365
     * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
jaroslav@56
   366
     * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
jaroslav@56
   367
     * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
jaroslav@56
   368
     * <tr><td> class or interface
jaroslav@56
   369
     *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
jaroslav@56
   370
     * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
jaroslav@56
   371
     * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
jaroslav@56
   372
     * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
jaroslav@56
   373
     * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
jaroslav@56
   374
     * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
jaroslav@56
   375
     * </table></blockquote>
jaroslav@56
   376
     *
jaroslav@56
   377
     * <p> The class or interface name <i>classname</i> is the binary name of
jaroslav@56
   378
     * the class specified above.
jaroslav@56
   379
     *
jaroslav@56
   380
     * <p> Examples:
jaroslav@56
   381
     * <blockquote><pre>
jaroslav@56
   382
     * String.class.getName()
jaroslav@56
   383
     *     returns "java.lang.String"
jaroslav@56
   384
     * byte.class.getName()
jaroslav@56
   385
     *     returns "byte"
jaroslav@56
   386
     * (new Object[3]).getClass().getName()
jaroslav@56
   387
     *     returns "[Ljava.lang.Object;"
jaroslav@56
   388
     * (new int[3][4][5][6][7][8][9]).getClass().getName()
jaroslav@56
   389
     *     returns "[[[[[[[I"
jaroslav@56
   390
     * </pre></blockquote>
jaroslav@56
   391
     *
jaroslav@56
   392
     * @return  the name of the class or interface
jaroslav@56
   393
     *          represented by this object.
jaroslav@56
   394
     */
jaroslav@56
   395
    public String getName() {
jaroslav@225
   396
        return jvmName().replace('/', '.');
jaroslav@56
   397
    }
jaroslav@56
   398
jaroslav@225
   399
    @JavaScriptBody(args = "self", body = "return self.jvmName;")
jaroslav@225
   400
    private native String jvmName();
jaroslav@225
   401
jaroslav@260
   402
    
jaroslav@260
   403
    /**
jaroslav@260
   404
     * Returns an array of {@code TypeVariable} objects that represent the
jaroslav@260
   405
     * type variables declared by the generic declaration represented by this
jaroslav@260
   406
     * {@code GenericDeclaration} object, in declaration order.  Returns an
jaroslav@260
   407
     * array of length 0 if the underlying generic declaration declares no type
jaroslav@260
   408
     * variables.
jaroslav@260
   409
     *
jaroslav@260
   410
     * @return an array of {@code TypeVariable} objects that represent
jaroslav@260
   411
     *     the type variables declared by this generic declaration
jaroslav@260
   412
     * @throws java.lang.reflect.GenericSignatureFormatError if the generic
jaroslav@260
   413
     *     signature of this generic declaration does not conform to
jaroslav@260
   414
     *     the format specified in
jaroslav@260
   415
     *     <cite>The Java&trade; Virtual Machine Specification</cite>
jaroslav@260
   416
     * @since 1.5
jaroslav@260
   417
     */
jaroslav@260
   418
    public TypeVariable<Class<T>>[] getTypeParameters() {
jaroslav@260
   419
        throw new UnsupportedOperationException();
jaroslav@260
   420
    }
jaroslav@260
   421
 
jaroslav@56
   422
    /**
jaroslav@56
   423
     * Returns the {@code Class} representing the superclass of the entity
jaroslav@56
   424
     * (class, interface, primitive type or void) represented by this
jaroslav@56
   425
     * {@code Class}.  If this {@code Class} represents either the
jaroslav@56
   426
     * {@code Object} class, an interface, a primitive type, or void, then
jaroslav@56
   427
     * null is returned.  If this object represents an array class then the
jaroslav@56
   428
     * {@code Class} object representing the {@code Object} class is
jaroslav@56
   429
     * returned.
jaroslav@56
   430
     *
jaroslav@56
   431
     * @return the superclass of the class represented by this object.
jaroslav@56
   432
     */
jaroslav@230
   433
    @JavaScriptBody(args = "self", body = "return self.superclass;")
jaroslav@56
   434
    public native Class<? super T> getSuperclass();
jaroslav@56
   435
jaroslav@56
   436
    /**
jaroslav@56
   437
     * Returns the Java language modifiers for this class or interface, encoded
jaroslav@56
   438
     * in an integer. The modifiers consist of the Java Virtual Machine's
jaroslav@56
   439
     * constants for {@code public}, {@code protected},
jaroslav@56
   440
     * {@code private}, {@code final}, {@code static},
jaroslav@56
   441
     * {@code abstract} and {@code interface}; they should be decoded
jaroslav@56
   442
     * using the methods of class {@code Modifier}.
jaroslav@56
   443
     *
jaroslav@56
   444
     * <p> If the underlying class is an array class, then its
jaroslav@56
   445
     * {@code public}, {@code private} and {@code protected}
jaroslav@56
   446
     * modifiers are the same as those of its component type.  If this
jaroslav@56
   447
     * {@code Class} represents a primitive type or void, its
jaroslav@56
   448
     * {@code public} modifier is always {@code true}, and its
jaroslav@56
   449
     * {@code protected} and {@code private} modifiers are always
jaroslav@56
   450
     * {@code false}. If this object represents an array class, a
jaroslav@56
   451
     * primitive type or void, then its {@code final} modifier is always
jaroslav@56
   452
     * {@code true} and its interface modifier is always
jaroslav@56
   453
     * {@code false}. The values of its other modifiers are not determined
jaroslav@56
   454
     * by this specification.
jaroslav@56
   455
     *
jaroslav@56
   456
     * <p> The modifier encodings are defined in <em>The Java Virtual Machine
jaroslav@56
   457
     * Specification</em>, table 4.1.
jaroslav@56
   458
     *
jaroslav@56
   459
     * @return the {@code int} representing the modifiers for this class
jaroslav@56
   460
     * @see     java.lang.reflect.Modifier
jaroslav@56
   461
     * @since JDK1.1
jaroslav@56
   462
     */
jaroslav@56
   463
    public native int getModifiers();
jaroslav@56
   464
jaroslav@56
   465
jaroslav@56
   466
    /**
jaroslav@56
   467
     * Returns the simple name of the underlying class as given in the
jaroslav@56
   468
     * source code. Returns an empty string if the underlying class is
jaroslav@56
   469
     * anonymous.
jaroslav@56
   470
     *
jaroslav@56
   471
     * <p>The simple name of an array is the simple name of the
jaroslav@56
   472
     * component type with "[]" appended.  In particular the simple
jaroslav@56
   473
     * name of an array whose component type is anonymous is "[]".
jaroslav@56
   474
     *
jaroslav@56
   475
     * @return the simple name of the underlying class
jaroslav@56
   476
     * @since 1.5
jaroslav@56
   477
     */
jaroslav@56
   478
    public String getSimpleName() {
jaroslav@229
   479
        if (isArray())
jaroslav@229
   480
            return getComponentType().getSimpleName()+"[]";
jaroslav@229
   481
jaroslav@229
   482
        String simpleName = getSimpleBinaryName();
jaroslav@229
   483
        if (simpleName == null) { // top level class
jaroslav@229
   484
            simpleName = getName();
jaroslav@229
   485
            return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
jaroslav@229
   486
        }
jaroslav@229
   487
        // According to JLS3 "Binary Compatibility" (13.1) the binary
jaroslav@229
   488
        // name of non-package classes (not top level) is the binary
jaroslav@229
   489
        // name of the immediately enclosing class followed by a '$' followed by:
jaroslav@229
   490
        // (for nested and inner classes): the simple name.
jaroslav@229
   491
        // (for local classes): 1 or more digits followed by the simple name.
jaroslav@229
   492
        // (for anonymous classes): 1 or more digits.
jaroslav@229
   493
jaroslav@229
   494
        // Since getSimpleBinaryName() will strip the binary name of
jaroslav@229
   495
        // the immediatly enclosing class, we are now looking at a
jaroslav@229
   496
        // string that matches the regular expression "\$[0-9]*"
jaroslav@229
   497
        // followed by a simple name (considering the simple of an
jaroslav@229
   498
        // anonymous class to be the empty string).
jaroslav@229
   499
jaroslav@229
   500
        // Remove leading "\$[0-9]*" from the name
jaroslav@229
   501
        int length = simpleName.length();
jaroslav@229
   502
        if (length < 1 || simpleName.charAt(0) != '$')
jaroslav@229
   503
            throw new IllegalStateException("Malformed class name");
jaroslav@229
   504
        int index = 1;
jaroslav@229
   505
        while (index < length && isAsciiDigit(simpleName.charAt(index)))
jaroslav@229
   506
            index++;
jaroslav@229
   507
        // Eventually, this is the empty string iff this is an anonymous class
jaroslav@229
   508
        return simpleName.substring(index);
jaroslav@56
   509
    }
jaroslav@56
   510
jaroslav@56
   511
    /**
jaroslav@229
   512
     * Returns the "simple binary name" of the underlying class, i.e.,
jaroslav@229
   513
     * the binary name without the leading enclosing class name.
jaroslav@229
   514
     * Returns {@code null} if the underlying class is a top level
jaroslav@229
   515
     * class.
jaroslav@229
   516
     */
jaroslav@229
   517
    private String getSimpleBinaryName() {
jaroslav@229
   518
        Class<?> enclosingClass = null; // XXX getEnclosingClass();
jaroslav@229
   519
        if (enclosingClass == null) // top level class
jaroslav@229
   520
            return null;
jaroslav@229
   521
        // Otherwise, strip the enclosing class' name
jaroslav@229
   522
        try {
jaroslav@229
   523
            return getName().substring(enclosingClass.getName().length());
jaroslav@229
   524
        } catch (IndexOutOfBoundsException ex) {
jaroslav@229
   525
            throw new IllegalStateException("Malformed class name");
jaroslav@229
   526
        }
jaroslav@229
   527
    }
jaroslav@261
   528
jaroslav@261
   529
    /**
jaroslav@261
   530
     * Returns an array containing {@code Field} objects reflecting all
jaroslav@261
   531
     * the accessible public fields of the class or interface represented by
jaroslav@261
   532
     * this {@code Class} object.  The elements in the array returned are
jaroslav@261
   533
     * not sorted and are not in any particular order.  This method returns an
jaroslav@261
   534
     * array of length 0 if the class or interface has no accessible public
jaroslav@261
   535
     * fields, or if it represents an array class, a primitive type, or void.
jaroslav@261
   536
     *
jaroslav@261
   537
     * <p> Specifically, if this {@code Class} object represents a class,
jaroslav@261
   538
     * this method returns the public fields of this class and of all its
jaroslav@261
   539
     * superclasses.  If this {@code Class} object represents an
jaroslav@261
   540
     * interface, this method returns the fields of this interface and of all
jaroslav@261
   541
     * its superinterfaces.
jaroslav@261
   542
     *
jaroslav@261
   543
     * <p> The implicit length field for array class is not reflected by this
jaroslav@261
   544
     * method. User code should use the methods of class {@code Array} to
jaroslav@261
   545
     * manipulate arrays.
jaroslav@261
   546
     *
jaroslav@261
   547
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
jaroslav@261
   548
     *
jaroslav@261
   549
     * @return the array of {@code Field} objects representing the
jaroslav@261
   550
     * public fields
jaroslav@261
   551
     * @exception  SecurityException
jaroslav@261
   552
     *             If a security manager, <i>s</i>, is present and any of the
jaroslav@261
   553
     *             following conditions is met:
jaroslav@261
   554
     *
jaroslav@261
   555
     *             <ul>
jaroslav@261
   556
     *
jaroslav@261
   557
     *             <li> invocation of
jaroslav@261
   558
     *             {@link SecurityManager#checkMemberAccess
jaroslav@261
   559
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
jaroslav@261
   560
     *             access to the fields within this class
jaroslav@261
   561
     *
jaroslav@261
   562
     *             <li> the caller's class loader is not the same as or an
jaroslav@261
   563
     *             ancestor of the class loader for the current class and
jaroslav@261
   564
     *             invocation of {@link SecurityManager#checkPackageAccess
jaroslav@261
   565
     *             s.checkPackageAccess()} denies access to the package
jaroslav@261
   566
     *             of this class
jaroslav@261
   567
     *
jaroslav@261
   568
     *             </ul>
jaroslav@261
   569
     *
jaroslav@261
   570
     * @since JDK1.1
jaroslav@261
   571
     */
jaroslav@261
   572
    public Field[] getFields() throws SecurityException {
jaroslav@261
   573
        throw new SecurityException();
jaroslav@261
   574
    }
jaroslav@261
   575
jaroslav@261
   576
    /**
jaroslav@261
   577
     * Returns an array containing {@code Method} objects reflecting all
jaroslav@261
   578
     * the public <em>member</em> methods of the class or interface represented
jaroslav@261
   579
     * by this {@code Class} object, including those declared by the class
jaroslav@261
   580
     * or interface and those inherited from superclasses and
jaroslav@261
   581
     * superinterfaces.  Array classes return all the (public) member methods
jaroslav@261
   582
     * inherited from the {@code Object} class.  The elements in the array
jaroslav@261
   583
     * returned are not sorted and are not in any particular order.  This
jaroslav@261
   584
     * method returns an array of length 0 if this {@code Class} object
jaroslav@261
   585
     * represents a class or interface that has no public member methods, or if
jaroslav@261
   586
     * this {@code Class} object represents a primitive type or void.
jaroslav@261
   587
     *
jaroslav@261
   588
     * <p> The class initialization method {@code <clinit>} is not
jaroslav@261
   589
     * included in the returned array. If the class declares multiple public
jaroslav@261
   590
     * member methods with the same parameter types, they are all included in
jaroslav@261
   591
     * the returned array.
jaroslav@261
   592
     *
jaroslav@261
   593
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
jaroslav@261
   594
     *
jaroslav@261
   595
     * @return the array of {@code Method} objects representing the
jaroslav@261
   596
     * public methods of this class
jaroslav@261
   597
     * @exception  SecurityException
jaroslav@261
   598
     *             If a security manager, <i>s</i>, is present and any of the
jaroslav@261
   599
     *             following conditions is met:
jaroslav@261
   600
     *
jaroslav@261
   601
     *             <ul>
jaroslav@261
   602
     *
jaroslav@261
   603
     *             <li> invocation of
jaroslav@261
   604
     *             {@link SecurityManager#checkMemberAccess
jaroslav@261
   605
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
jaroslav@261
   606
     *             access to the methods within this class
jaroslav@261
   607
     *
jaroslav@261
   608
     *             <li> the caller's class loader is not the same as or an
jaroslav@261
   609
     *             ancestor of the class loader for the current class and
jaroslav@261
   610
     *             invocation of {@link SecurityManager#checkPackageAccess
jaroslav@261
   611
     *             s.checkPackageAccess()} denies access to the package
jaroslav@261
   612
     *             of this class
jaroslav@261
   613
     *
jaroslav@261
   614
     *             </ul>
jaroslav@261
   615
     *
jaroslav@261
   616
     * @since JDK1.1
jaroslav@261
   617
     */
jaroslav@261
   618
    public Method[] getMethods() throws SecurityException {
jaroslav@261
   619
        throw new SecurityException();
jaroslav@261
   620
    }
jaroslav@261
   621
jaroslav@261
   622
    /**
jaroslav@261
   623
     * Returns a {@code Field} object that reflects the specified public
jaroslav@261
   624
     * member field of the class or interface represented by this
jaroslav@261
   625
     * {@code Class} object. The {@code name} parameter is a
jaroslav@261
   626
     * {@code String} specifying the simple name of the desired field.
jaroslav@261
   627
     *
jaroslav@261
   628
     * <p> The field to be reflected is determined by the algorithm that
jaroslav@261
   629
     * follows.  Let C be the class represented by this object:
jaroslav@261
   630
     * <OL>
jaroslav@261
   631
     * <LI> If C declares a public field with the name specified, that is the
jaroslav@261
   632
     *      field to be reflected.</LI>
jaroslav@261
   633
     * <LI> If no field was found in step 1 above, this algorithm is applied
jaroslav@261
   634
     *      recursively to each direct superinterface of C. The direct
jaroslav@261
   635
     *      superinterfaces are searched in the order they were declared.</LI>
jaroslav@261
   636
     * <LI> If no field was found in steps 1 and 2 above, and C has a
jaroslav@261
   637
     *      superclass S, then this algorithm is invoked recursively upon S.
jaroslav@261
   638
     *      If C has no superclass, then a {@code NoSuchFieldException}
jaroslav@261
   639
     *      is thrown.</LI>
jaroslav@261
   640
     * </OL>
jaroslav@261
   641
     *
jaroslav@261
   642
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
jaroslav@261
   643
     *
jaroslav@261
   644
     * @param name the field name
jaroslav@261
   645
     * @return  the {@code Field} object of this class specified by
jaroslav@261
   646
     * {@code name}
jaroslav@261
   647
     * @exception NoSuchFieldException if a field with the specified name is
jaroslav@261
   648
     *              not found.
jaroslav@261
   649
     * @exception NullPointerException if {@code name} is {@code null}
jaroslav@261
   650
     * @exception  SecurityException
jaroslav@261
   651
     *             If a security manager, <i>s</i>, is present and any of the
jaroslav@261
   652
     *             following conditions is met:
jaroslav@261
   653
     *
jaroslav@261
   654
     *             <ul>
jaroslav@261
   655
     *
jaroslav@261
   656
     *             <li> invocation of
jaroslav@261
   657
     *             {@link SecurityManager#checkMemberAccess
jaroslav@261
   658
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
jaroslav@261
   659
     *             access to the field
jaroslav@261
   660
     *
jaroslav@261
   661
     *             <li> the caller's class loader is not the same as or an
jaroslav@261
   662
     *             ancestor of the class loader for the current class and
jaroslav@261
   663
     *             invocation of {@link SecurityManager#checkPackageAccess
jaroslav@261
   664
     *             s.checkPackageAccess()} denies access to the package
jaroslav@261
   665
     *             of this class
jaroslav@261
   666
     *
jaroslav@261
   667
     *             </ul>
jaroslav@261
   668
     *
jaroslav@261
   669
     * @since JDK1.1
jaroslav@261
   670
     */
jaroslav@261
   671
    public Field getField(String name)
jaroslav@261
   672
        throws SecurityException {
jaroslav@261
   673
        throw new SecurityException();
jaroslav@261
   674
    }
jaroslav@229
   675
    
jaroslav@261
   676
    
jaroslav@261
   677
    /**
jaroslav@261
   678
     * Returns a {@code Method} object that reflects the specified public
jaroslav@261
   679
     * member method of the class or interface represented by this
jaroslav@261
   680
     * {@code Class} object. The {@code name} parameter is a
jaroslav@261
   681
     * {@code String} specifying the simple name of the desired method. The
jaroslav@261
   682
     * {@code parameterTypes} parameter is an array of {@code Class}
jaroslav@261
   683
     * objects that identify the method's formal parameter types, in declared
jaroslav@261
   684
     * order. If {@code parameterTypes} is {@code null}, it is
jaroslav@261
   685
     * treated as if it were an empty array.
jaroslav@261
   686
     *
jaroslav@261
   687
     * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
jaroslav@261
   688
     * {@code NoSuchMethodException} is raised. Otherwise, the method to
jaroslav@261
   689
     * be reflected is determined by the algorithm that follows.  Let C be the
jaroslav@261
   690
     * class represented by this object:
jaroslav@261
   691
     * <OL>
jaroslav@261
   692
     * <LI> C is searched for any <I>matching methods</I>. If no matching
jaroslav@261
   693
     *      method is found, the algorithm of step 1 is invoked recursively on
jaroslav@261
   694
     *      the superclass of C.</LI>
jaroslav@261
   695
     * <LI> If no method was found in step 1 above, the superinterfaces of C
jaroslav@261
   696
     *      are searched for a matching method. If any such method is found, it
jaroslav@261
   697
     *      is reflected.</LI>
jaroslav@261
   698
     * </OL>
jaroslav@261
   699
     *
jaroslav@261
   700
     * To find a matching method in a class C:&nbsp; If C declares exactly one
jaroslav@261
   701
     * public method with the specified name and exactly the same formal
jaroslav@261
   702
     * parameter types, that is the method reflected. If more than one such
jaroslav@261
   703
     * method is found in C, and one of these methods has a return type that is
jaroslav@261
   704
     * more specific than any of the others, that method is reflected;
jaroslav@261
   705
     * otherwise one of the methods is chosen arbitrarily.
jaroslav@261
   706
     *
jaroslav@261
   707
     * <p>Note that there may be more than one matching method in a
jaroslav@261
   708
     * class because while the Java language forbids a class to
jaroslav@261
   709
     * declare multiple methods with the same signature but different
jaroslav@261
   710
     * return types, the Java virtual machine does not.  This
jaroslav@261
   711
     * increased flexibility in the virtual machine can be used to
jaroslav@261
   712
     * implement various language features.  For example, covariant
jaroslav@261
   713
     * returns can be implemented with {@linkplain
jaroslav@261
   714
     * java.lang.reflect.Method#isBridge bridge methods}; the bridge
jaroslav@261
   715
     * method and the method being overridden would have the same
jaroslav@261
   716
     * signature but different return types.
jaroslav@261
   717
     *
jaroslav@261
   718
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
jaroslav@261
   719
     *
jaroslav@261
   720
     * @param name the name of the method
jaroslav@261
   721
     * @param parameterTypes the list of parameters
jaroslav@261
   722
     * @return the {@code Method} object that matches the specified
jaroslav@261
   723
     * {@code name} and {@code parameterTypes}
jaroslav@261
   724
     * @exception NoSuchMethodException if a matching method is not found
jaroslav@261
   725
     *            or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
jaroslav@261
   726
     * @exception NullPointerException if {@code name} is {@code null}
jaroslav@261
   727
     * @exception  SecurityException
jaroslav@261
   728
     *             If a security manager, <i>s</i>, is present and any of the
jaroslav@261
   729
     *             following conditions is met:
jaroslav@261
   730
     *
jaroslav@261
   731
     *             <ul>
jaroslav@261
   732
     *
jaroslav@261
   733
     *             <li> invocation of
jaroslav@261
   734
     *             {@link SecurityManager#checkMemberAccess
jaroslav@261
   735
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
jaroslav@261
   736
     *             access to the method
jaroslav@261
   737
     *
jaroslav@261
   738
     *             <li> the caller's class loader is not the same as or an
jaroslav@261
   739
     *             ancestor of the class loader for the current class and
jaroslav@261
   740
     *             invocation of {@link SecurityManager#checkPackageAccess
jaroslav@261
   741
     *             s.checkPackageAccess()} denies access to the package
jaroslav@261
   742
     *             of this class
jaroslav@261
   743
     *
jaroslav@261
   744
     *             </ul>
jaroslav@261
   745
     *
jaroslav@261
   746
     * @since JDK1.1
jaroslav@261
   747
     */
jaroslav@261
   748
    public Method getMethod(String name, Class<?>... parameterTypes)
jaroslav@261
   749
        throws SecurityException {
jaroslav@261
   750
        throw new SecurityException();
jaroslav@261
   751
    }
jaroslav@261
   752
jaroslav@229
   753
    /**
jaroslav@56
   754
     * Character.isDigit answers {@code true} to some non-ascii
jaroslav@56
   755
     * digits.  This one does not.
jaroslav@56
   756
     */
jaroslav@56
   757
    private static boolean isAsciiDigit(char c) {
jaroslav@56
   758
        return '0' <= c && c <= '9';
jaroslav@56
   759
    }
jaroslav@56
   760
jaroslav@56
   761
    /**
jaroslav@56
   762
     * Returns the canonical name of the underlying class as
jaroslav@56
   763
     * defined by the Java Language Specification.  Returns null if
jaroslav@56
   764
     * the underlying class does not have a canonical name (i.e., if
jaroslav@56
   765
     * it is a local or anonymous class or an array whose component
jaroslav@56
   766
     * type does not have a canonical name).
jaroslav@56
   767
     * @return the canonical name of the underlying class if it exists, and
jaroslav@56
   768
     * {@code null} otherwise.
jaroslav@56
   769
     * @since 1.5
jaroslav@56
   770
     */
jaroslav@56
   771
    public String getCanonicalName() {
jaroslav@228
   772
        if (isArray()) {
jaroslav@228
   773
            String canonicalName = getComponentType().getCanonicalName();
jaroslav@228
   774
            if (canonicalName != null)
jaroslav@228
   775
                return canonicalName + "[]";
jaroslav@228
   776
            else
jaroslav@228
   777
                return null;
jaroslav@228
   778
        }
jaroslav@65
   779
//        if (isLocalOrAnonymousClass())
jaroslav@65
   780
//            return null;
jaroslav@65
   781
//        Class<?> enclosingClass = getEnclosingClass();
jaroslav@228
   782
        Class<?> enclosingClass = null;
jaroslav@228
   783
        if (enclosingClass == null) { // top level class
jaroslav@228
   784
            return getName();
jaroslav@228
   785
        } else {
jaroslav@228
   786
            String enclosingName = enclosingClass.getCanonicalName();
jaroslav@228
   787
            if (enclosingName == null)
jaroslav@228
   788
                return null;
jaroslav@228
   789
            return enclosingName + "." + getSimpleName();
jaroslav@228
   790
        }
jaroslav@56
   791
    }
jaroslav@56
   792
jaroslav@56
   793
    /**
jaroslav@56
   794
     * Finds a resource with a given name.  The rules for searching resources
jaroslav@56
   795
     * associated with a given class are implemented by the defining
jaroslav@56
   796
     * {@linkplain ClassLoader class loader} of the class.  This method
jaroslav@56
   797
     * delegates to this object's class loader.  If this object was loaded by
jaroslav@56
   798
     * the bootstrap class loader, the method delegates to {@link
jaroslav@56
   799
     * ClassLoader#getSystemResourceAsStream}.
jaroslav@56
   800
     *
jaroslav@56
   801
     * <p> Before delegation, an absolute resource name is constructed from the
jaroslav@56
   802
     * given resource name using this algorithm:
jaroslav@56
   803
     *
jaroslav@56
   804
     * <ul>
jaroslav@56
   805
     *
jaroslav@56
   806
     * <li> If the {@code name} begins with a {@code '/'}
jaroslav@56
   807
     * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
jaroslav@56
   808
     * portion of the {@code name} following the {@code '/'}.
jaroslav@56
   809
     *
jaroslav@56
   810
     * <li> Otherwise, the absolute name is of the following form:
jaroslav@56
   811
     *
jaroslav@56
   812
     * <blockquote>
jaroslav@56
   813
     *   {@code modified_package_name/name}
jaroslav@56
   814
     * </blockquote>
jaroslav@56
   815
     *
jaroslav@56
   816
     * <p> Where the {@code modified_package_name} is the package name of this
jaroslav@56
   817
     * object with {@code '/'} substituted for {@code '.'}
jaroslav@56
   818
     * (<tt>'&#92;u002e'</tt>).
jaroslav@56
   819
     *
jaroslav@56
   820
     * </ul>
jaroslav@56
   821
     *
jaroslav@56
   822
     * @param  name name of the desired resource
jaroslav@56
   823
     * @return      A {@link java.io.InputStream} object or {@code null} if
jaroslav@56
   824
     *              no resource with this name is found
jaroslav@56
   825
     * @throws  NullPointerException If {@code name} is {@code null}
jaroslav@56
   826
     * @since  JDK1.1
jaroslav@56
   827
     */
jaroslav@122
   828
     public InputStream getResourceAsStream(String name) {
jaroslav@122
   829
        name = resolveName(name);
jaroslav@122
   830
        ClassLoader cl = getClassLoader0();
jaroslav@122
   831
        if (cl==null) {
jaroslav@122
   832
            // A system class.
jaroslav@122
   833
            return ClassLoader.getSystemResourceAsStream(name);
jaroslav@122
   834
        }
jaroslav@122
   835
        return cl.getResourceAsStream(name);
jaroslav@122
   836
    }
jaroslav@56
   837
jaroslav@56
   838
    /**
jaroslav@56
   839
     * Finds a resource with a given name.  The rules for searching resources
jaroslav@56
   840
     * associated with a given class are implemented by the defining
jaroslav@56
   841
     * {@linkplain ClassLoader class loader} of the class.  This method
jaroslav@56
   842
     * delegates to this object's class loader.  If this object was loaded by
jaroslav@56
   843
     * the bootstrap class loader, the method delegates to {@link
jaroslav@56
   844
     * ClassLoader#getSystemResource}.
jaroslav@56
   845
     *
jaroslav@56
   846
     * <p> Before delegation, an absolute resource name is constructed from the
jaroslav@56
   847
     * given resource name using this algorithm:
jaroslav@56
   848
     *
jaroslav@56
   849
     * <ul>
jaroslav@56
   850
     *
jaroslav@56
   851
     * <li> If the {@code name} begins with a {@code '/'}
jaroslav@56
   852
     * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
jaroslav@56
   853
     * portion of the {@code name} following the {@code '/'}.
jaroslav@56
   854
     *
jaroslav@56
   855
     * <li> Otherwise, the absolute name is of the following form:
jaroslav@56
   856
     *
jaroslav@56
   857
     * <blockquote>
jaroslav@56
   858
     *   {@code modified_package_name/name}
jaroslav@56
   859
     * </blockquote>
jaroslav@56
   860
     *
jaroslav@56
   861
     * <p> Where the {@code modified_package_name} is the package name of this
jaroslav@56
   862
     * object with {@code '/'} substituted for {@code '.'}
jaroslav@56
   863
     * (<tt>'&#92;u002e'</tt>).
jaroslav@56
   864
     *
jaroslav@56
   865
     * </ul>
jaroslav@56
   866
     *
jaroslav@56
   867
     * @param  name name of the desired resource
jaroslav@56
   868
     * @return      A  {@link java.net.URL} object or {@code null} if no
jaroslav@56
   869
     *              resource with this name is found
jaroslav@56
   870
     * @since  JDK1.1
jaroslav@56
   871
     */
jaroslav@122
   872
    public java.net.URL getResource(String name) {
jaroslav@122
   873
        name = resolveName(name);
jaroslav@122
   874
        ClassLoader cl = getClassLoader0();
jaroslav@122
   875
        if (cl==null) {
jaroslav@122
   876
            // A system class.
jaroslav@122
   877
            return ClassLoader.getSystemResource(name);
jaroslav@122
   878
        }
jaroslav@122
   879
        return cl.getResource(name);
jaroslav@122
   880
    }
jaroslav@56
   881
jaroslav@56
   882
jaroslav@122
   883
   /**
jaroslav@122
   884
     * Add a package name prefix if the name is not absolute Remove leading "/"
jaroslav@122
   885
     * if name is absolute
jaroslav@122
   886
     */
jaroslav@122
   887
    private String resolveName(String name) {
jaroslav@122
   888
        if (name == null) {
jaroslav@122
   889
            return name;
jaroslav@122
   890
        }
jaroslav@122
   891
        if (!name.startsWith("/")) {
jaroslav@122
   892
            Class<?> c = this;
jaroslav@122
   893
            while (c.isArray()) {
jaroslav@122
   894
                c = c.getComponentType();
jaroslav@122
   895
            }
jaroslav@122
   896
            String baseName = c.getName();
jaroslav@122
   897
            int index = baseName.lastIndexOf('.');
jaroslav@122
   898
            if (index != -1) {
jaroslav@122
   899
                name = baseName.substring(0, index).replace('.', '/')
jaroslav@122
   900
                    +"/"+name;
jaroslav@122
   901
            }
jaroslav@122
   902
        } else {
jaroslav@122
   903
            name = name.substring(1);
jaroslav@122
   904
        }
jaroslav@122
   905
        return name;
jaroslav@122
   906
    }
jaroslav@122
   907
    
jaroslav@122
   908
    /**
jaroslav@122
   909
     * Returns the class loader for the class.  Some implementations may use
jaroslav@122
   910
     * null to represent the bootstrap class loader. This method will return
jaroslav@122
   911
     * null in such implementations if this class was loaded by the bootstrap
jaroslav@122
   912
     * class loader.
jaroslav@122
   913
     *
jaroslav@122
   914
     * <p> If a security manager is present, and the caller's class loader is
jaroslav@122
   915
     * not null and the caller's class loader is not the same as or an ancestor of
jaroslav@122
   916
     * the class loader for the class whose class loader is requested, then
jaroslav@122
   917
     * this method calls the security manager's {@code checkPermission}
jaroslav@122
   918
     * method with a {@code RuntimePermission("getClassLoader")}
jaroslav@122
   919
     * permission to ensure it's ok to access the class loader for the class.
jaroslav@122
   920
     *
jaroslav@122
   921
     * <p>If this object
jaroslav@122
   922
     * represents a primitive type or void, null is returned.
jaroslav@122
   923
     *
jaroslav@122
   924
     * @return  the class loader that loaded the class or interface
jaroslav@122
   925
     *          represented by this object.
jaroslav@122
   926
     * @throws SecurityException
jaroslav@122
   927
     *    if a security manager exists and its
jaroslav@122
   928
     *    {@code checkPermission} method denies
jaroslav@122
   929
     *    access to the class loader for the class.
jaroslav@122
   930
     * @see java.lang.ClassLoader
jaroslav@122
   931
     * @see SecurityManager#checkPermission
jaroslav@122
   932
     * @see java.lang.RuntimePermission
jaroslav@122
   933
     */
jaroslav@122
   934
    public ClassLoader getClassLoader() {
jaroslav@122
   935
        throw new SecurityException();
jaroslav@122
   936
    }
jaroslav@122
   937
    
jaroslav@122
   938
    // Package-private to allow ClassLoader access
jaroslav@122
   939
    native ClassLoader getClassLoader0();    
jaroslav@56
   940
jaroslav@122
   941
    /**
jaroslav@122
   942
     * Returns the {@code Class} representing the component type of an
jaroslav@122
   943
     * array.  If this class does not represent an array class this method
jaroslav@122
   944
     * returns null.
jaroslav@122
   945
     *
jaroslav@122
   946
     * @return the {@code Class} representing the component type of this
jaroslav@122
   947
     * class if this class is an array
jaroslav@122
   948
     * @see     java.lang.reflect.Array
jaroslav@122
   949
     * @since JDK1.1
jaroslav@122
   950
     */
jaroslav@228
   951
    public Class<?> getComponentType() {
jaroslav@228
   952
        return null;
jaroslav@228
   953
    }
jaroslav@56
   954
jaroslav@56
   955
    /**
jaroslav@56
   956
     * Returns true if and only if this class was declared as an enum in the
jaroslav@56
   957
     * source code.
jaroslav@56
   958
     *
jaroslav@56
   959
     * @return true if and only if this class was declared as an enum in the
jaroslav@56
   960
     *     source code
jaroslav@56
   961
     * @since 1.5
jaroslav@56
   962
     */
jaroslav@56
   963
    public boolean isEnum() {
jaroslav@56
   964
        // An enum must both directly extend java.lang.Enum and have
jaroslav@56
   965
        // the ENUM bit set; classes for specialized enum constants
jaroslav@56
   966
        // don't do the former.
jaroslav@56
   967
        return (this.getModifiers() & ENUM) != 0 &&
jaroslav@56
   968
        this.getSuperclass() == java.lang.Enum.class;
jaroslav@56
   969
    }
jaroslav@56
   970
jaroslav@56
   971
    /**
jaroslav@56
   972
     * Casts an object to the class or interface represented
jaroslav@56
   973
     * by this {@code Class} object.
jaroslav@56
   974
     *
jaroslav@56
   975
     * @param obj the object to be cast
jaroslav@56
   976
     * @return the object after casting, or null if obj is null
jaroslav@56
   977
     *
jaroslav@56
   978
     * @throws ClassCastException if the object is not
jaroslav@56
   979
     * null and is not assignable to the type T.
jaroslav@56
   980
     *
jaroslav@56
   981
     * @since 1.5
jaroslav@56
   982
     */
jaroslav@56
   983
    public T cast(Object obj) {
jaroslav@56
   984
        if (obj != null && !isInstance(obj))
jaroslav@56
   985
            throw new ClassCastException(cannotCastMsg(obj));
jaroslav@56
   986
        return (T) obj;
jaroslav@56
   987
    }
jaroslav@56
   988
jaroslav@56
   989
    private String cannotCastMsg(Object obj) {
jaroslav@56
   990
        return "Cannot cast " + obj.getClass().getName() + " to " + getName();
jaroslav@56
   991
    }
jaroslav@56
   992
jaroslav@56
   993
    /**
jaroslav@56
   994
     * Casts this {@code Class} object to represent a subclass of the class
jaroslav@56
   995
     * represented by the specified class object.  Checks that that the cast
jaroslav@56
   996
     * is valid, and throws a {@code ClassCastException} if it is not.  If
jaroslav@56
   997
     * this method succeeds, it always returns a reference to this class object.
jaroslav@56
   998
     *
jaroslav@56
   999
     * <p>This method is useful when a client needs to "narrow" the type of
jaroslav@56
  1000
     * a {@code Class} object to pass it to an API that restricts the
jaroslav@56
  1001
     * {@code Class} objects that it is willing to accept.  A cast would
jaroslav@56
  1002
     * generate a compile-time warning, as the correctness of the cast
jaroslav@56
  1003
     * could not be checked at runtime (because generic types are implemented
jaroslav@56
  1004
     * by erasure).
jaroslav@56
  1005
     *
jaroslav@56
  1006
     * @return this {@code Class} object, cast to represent a subclass of
jaroslav@56
  1007
     *    the specified class object.
jaroslav@56
  1008
     * @throws ClassCastException if this {@code Class} object does not
jaroslav@56
  1009
     *    represent a subclass of the specified class (here "subclass" includes
jaroslav@56
  1010
     *    the class itself).
jaroslav@56
  1011
     * @since 1.5
jaroslav@56
  1012
     */
jaroslav@56
  1013
    public <U> Class<? extends U> asSubclass(Class<U> clazz) {
jaroslav@56
  1014
        if (clazz.isAssignableFrom(this))
jaroslav@56
  1015
            return (Class<? extends U>) this;
jaroslav@56
  1016
        else
jaroslav@56
  1017
            throw new ClassCastException(this.toString());
jaroslav@56
  1018
    }
jaroslav@56
  1019
jaroslav@235
  1020
    @JavaScriptBody(args = { "self", "ac" }, 
jaroslav@235
  1021
        body = 
jaroslav@235
  1022
          "if (self.anno) {"
jaroslav@235
  1023
        + "  return self.anno['L' + ac.jvmName + ';'];"
jaroslav@235
  1024
        + "}"
jaroslav@235
  1025
    )
jaroslav@235
  1026
    private Object getAnnotationData(Class<?> annotationClass) {
jaroslav@235
  1027
        throw new UnsupportedOperationException();
jaroslav@235
  1028
    }
jaroslav@237
  1029
    /**
jaroslav@237
  1030
     * @throws NullPointerException {@inheritDoc}
jaroslav@237
  1031
     * @since 1.5
jaroslav@237
  1032
     */
jaroslav@56
  1033
    public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
jaroslav@235
  1034
        Object data = getAnnotationData(annotationClass);
jaroslav@235
  1035
        return data == null ? null : AnnotationImpl.create(annotationClass, data);
jaroslav@56
  1036
    }
jaroslav@56
  1037
jaroslav@56
  1038
    /**
jaroslav@56
  1039
     * @throws NullPointerException {@inheritDoc}
jaroslav@56
  1040
     * @since 1.5
jaroslav@56
  1041
     */
jaroslav@235
  1042
    @JavaScriptBody(args = { "self", "ac" }, 
jaroslav@235
  1043
        body = "if (self.anno && self.anno['L' + ac.jvmName + ';']) { return true; }"
jaroslav@235
  1044
        + "else return false;"
jaroslav@235
  1045
    )
jaroslav@56
  1046
    public boolean isAnnotationPresent(
jaroslav@56
  1047
        Class<? extends Annotation> annotationClass) {
jaroslav@56
  1048
        if (annotationClass == null)
jaroslav@56
  1049
            throw new NullPointerException();
jaroslav@56
  1050
jaroslav@56
  1051
        return getAnnotation(annotationClass) != null;
jaroslav@56
  1052
    }
jaroslav@56
  1053
jaroslav@238
  1054
    @JavaScriptBody(args = "self", body = "return self.anno;")
jaroslav@238
  1055
    private Object getAnnotationData() {
jaroslav@238
  1056
        throw new UnsupportedOperationException();
jaroslav@238
  1057
    }
jaroslav@56
  1058
jaroslav@56
  1059
    /**
jaroslav@56
  1060
     * @since 1.5
jaroslav@56
  1061
     */
jaroslav@56
  1062
    public Annotation[] getAnnotations() {
jaroslav@238
  1063
        Object data = getAnnotationData();
jaroslav@238
  1064
        return data == null ? new Annotation[0] : AnnotationImpl.create(data);
jaroslav@56
  1065
    }
jaroslav@56
  1066
jaroslav@56
  1067
    /**
jaroslav@56
  1068
     * @since 1.5
jaroslav@56
  1069
     */
jaroslav@56
  1070
    public Annotation[] getDeclaredAnnotations()  {
jaroslav@65
  1071
        throw new UnsupportedOperationException();
jaroslav@56
  1072
    }
jaroslav@56
  1073
jaroslav@84
  1074
    static Class getPrimitiveClass(String type) {
jaroslav@93
  1075
        // XXX
jaroslav@93
  1076
        return Object.class;
jaroslav@80
  1077
    }
jaroslav@88
  1078
jaroslav@88
  1079
    public boolean desiredAssertionStatus() {
jaroslav@88
  1080
        return false;
jaroslav@88
  1081
    }
jaroslav@56
  1082
}