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