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