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