jdk/src/share/classes/java/lang/Class.java
author valeriep
Mon, 06 Apr 2009 18:46:20 -0700
changeset 2448 1e8128f3ff61
parent 2174 0ffce164e4a4
child 3219 a348b237cbf8
permissions -rw-r--r--
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating Summary: Added support for parallel-capable class loaders Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
     2
 * Copyright 1994-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.GenericArrayType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Member;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.GenericDeclaration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.TypeVariable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.LinkedHashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.reflect.ConstantPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.reflect.Reflection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.reflect.ReflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.reflect.SignatureIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.reflect.generics.factory.CoreReflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.reflect.generics.factory.GenericsFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.reflect.generics.repository.ClassRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.reflect.generics.repository.MethodRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import sun.reflect.generics.repository.ConstructorRepository;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import sun.reflect.generics.scope.ClassScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.reflect.annotation.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Instances of the class {@code Class} represent classes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * interfaces in a running Java application.  An enum is a kind of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * class and an annotation is a kind of interface.  Every array also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * belongs to a class that is reflected as a {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * that is shared by all arrays with the same element type and number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * of dimensions.  The primitive Java types ({@code boolean},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * {@code byte}, {@code char}, {@code short},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * {@code int}, {@code long}, {@code float}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * {@code double}), and the keyword {@code void} are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * represented as {@code Class} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> {@code Class} has no public constructor. Instead {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * objects are constructed automatically by the Java Virtual Machine as classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * are loaded and by calls to the {@code defineClass} method in the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p> The following example uses a {@code Class} object to print the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * class name of an object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p> <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     void printClassName(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *         System.out.println("The class of " + obj +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *                            " is " + obj.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <p> It is also possible to get the {@code Class} object for a named
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * type (or for void) using a class literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * (JLS Section <A HREF="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530">15.8.2</A>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p> <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *     {@code System.out.println("The name of class Foo is: "+Foo.class.getName());}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @param <T> the type of the class modeled by this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * object.  For example, the type of {@code String.class} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Class<String>}.  Use {@code Class<?>} if the class being modeled is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    class Class<T> implements java.io.Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                              java.lang.reflect.GenericDeclaration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                              java.lang.reflect.Type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                              java.lang.reflect.AnnotatedElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final int ANNOTATION= 0x00002000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final int ENUM      = 0x00004000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static final int SYNTHETIC = 0x00001000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static native void registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Constructor. Only the Java Virtual Machine creates Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private Class() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Converts the object to a string. The string representation is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * string "class" or "interface", followed by a space, and then by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * fully qualified name of the class in the format returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * {@code getName}.  If this {@code Class} object represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * primitive type, this method returns the name of the primitive type.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * this {@code Class} object represents void this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * "void".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @return a string representation of this class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            + getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns the {@code Class} object associated with the class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * interface with the given string name.  Invoking this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *  {@code Class.forName(className, true, currentLoader)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * where {@code currentLoader} denotes the defining class loader of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * the current class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p> For example, the following code fragment returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * runtime {@code Class} descriptor for the class named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * {@code java.lang.Thread}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *   {@code Class t = Class.forName("java.lang.Thread")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * A call to {@code forName("X")} causes the class named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * {@code X} to be initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param      className   the fully qualified name of the desired class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return     the {@code Class} object for the class with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *             specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception LinkageError if the linkage fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception ExceptionInInitializerError if the initialization provoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *            by this method fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @exception ClassNotFoundException if the class cannot be located
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public static Class<?> forName(String className)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return forName0(className, true, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Returns the {@code Class} object associated with the class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * interface with the given string name, using the given class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Given the fully qualified name for a class or interface (in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * format returned by {@code getName}) this method attempts to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * locate, load, and link the class or interface.  The specified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * loader is used to load the class or interface.  If the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * {@code loader} is null, the class is loaded through the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * class loader.  The class is initialized only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * {@code initialize} parameter is {@code true} and if it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * not been initialized earlier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p> If {@code name} denotes a primitive type or void, an attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * will be made to locate a user-defined class in the unnamed package whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * name is {@code name}. Therefore, this method cannot be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * obtain any of the {@code Class} objects representing primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * types or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p> If {@code name} denotes an array class, the component type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * the array class is loaded but not initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <p> For example, in an instance method the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *  {@code Class.forName("Foo")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *  {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Note that this method throws errors related to loading, linking or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Java Language Specification</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Note that this method does not check whether the requested class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * is accessible to its caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p> If the {@code loader} is {@code null}, and a security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * manager is present, and the caller's class loader is not null, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * method calls the security manager's {@code checkPermission} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * with a {@code RuntimePermission("getClassLoader")} permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * ensure it's ok to access the bootstrap class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param name       fully qualified name of the desired class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param initialize whether the class must be initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param loader     class loader from which the class must be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return           class object representing the desired class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception LinkageError if the linkage fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception ExceptionInInitializerError if the initialization provoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *            by this method fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception ClassNotFoundException if the class cannot be located by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *            the specified class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @see       java.lang.Class#forName(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see       java.lang.ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @since     1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public static Class<?> forName(String name, boolean initialize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                   ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                ClassLoader ccl = ClassLoader.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                if (ccl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    sm.checkPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        SecurityConstants.GET_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return forName0(name, initialize, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /** Called after security checks have been made. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private static native Class forName0(String name, boolean initialize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                            ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        throws ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Creates a new instance of the class represented by this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * object.  The class is instantiated as if by a {@code new}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * expression with an empty argument list.  The class is initialized if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * has not already been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>Note that this method propagates any exception thrown by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * nullary constructor, including a checked exception.  Use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * this method effectively bypasses the compile-time exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * checking that would otherwise be performed by the compiler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * java.lang.reflect.Constructor#newInstance(java.lang.Object...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Constructor.newInstance} method avoids this problem by wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * any exception thrown by the constructor in a (checked) {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * java.lang.reflect.InvocationTargetException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @return     a newly allocated instance of the class represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *             object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @exception  IllegalAccessException  if the class or its nullary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *               constructor is not accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @exception  InstantiationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *               if this {@code Class} represents an abstract class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *               an interface, an array class, a primitive type, or void;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *               or if the class has no nullary constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *               or if the instantiation fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @exception  ExceptionInInitializerError if the initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *               provoked by this method fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *             creation of new instances of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public T newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        throws InstantiationException, IllegalAccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (System.getSecurityManager() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return newInstance0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private T newInstance0()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        throws InstantiationException, IllegalAccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // NOTE: the following code may not be strictly correct under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // the current Java memory model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // Constructor lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (cachedConstructor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (this == Class.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                throw new IllegalAccessException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    "Can not call newInstance() on the Class for java.lang.Class"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                Class[] empty = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                // Disable accessibility checks on the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                // since we have to do the security check here anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                // (the stack depth is wrong for the Constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                // security check to work)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   348
                java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   349
                    new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   350
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                c.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                cachedConstructor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                throw new InstantiationException(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        Constructor<T> tmpConstructor = cachedConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // Security check (same as in java.lang.reflect.Constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        int modifiers = tmpConstructor.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            Class caller = Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (newInstanceCallerCache != caller) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                Reflection.ensureMemberAccess(caller, this, null, modifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                newInstanceCallerCache = caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        // Run constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return tmpConstructor.newInstance((Object[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            Unsafe.getUnsafe().throwException(e.getTargetException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            // Not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    private volatile transient Constructor<T> cachedConstructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private volatile transient Class       newInstanceCallerCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Determines if the specified {@code Object} is assignment-compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * with the object represented by this {@code Class}.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * the dynamic equivalent of the Java language {@code instanceof}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * operator. The method returns {@code true} if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * {@code Object} argument is non-null and can be cast to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * reference type represented by this {@code Class} object without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * raising a {@code ClassCastException.} It returns {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <p> Specifically, if this {@code Class} object represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * declared class, this method returns {@code true} if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * {@code Object} argument is an instance of the represented class (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * of any of its subclasses); it returns {@code false} otherwise. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * this {@code Class} object represents an array class, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * returns {@code true} if the specified {@code Object} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * can be converted to an object of the array class by an identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * conversion or by a widening reference conversion; it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * {@code false} otherwise. If this {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * represents an interface, this method returns {@code true} if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * class or any superclass of the specified {@code Object} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * implements this interface; it returns {@code false} otherwise. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * this {@code Class} object represents a primitive type, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * returns {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param   obj the object to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @return  true if {@code obj} is an instance of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public native boolean isInstance(Object obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Determines if the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * {@code Class} object is either the same as, or is a superclass or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * superinterface of, the class or interface represented by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * {@code Class} parameter. It returns {@code true} if so;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * otherwise it returns {@code false}. If this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * object represents a primitive type, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * {@code true} if the specified {@code Class} parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * exactly this {@code Class} object; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <p> Specifically, this method tests whether the type represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * specified {@code Class} parameter can be converted to the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * represented by this {@code Class} object via an identity conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * or via a widening reference conversion. See <em>The Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param cls the {@code Class} object to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @return the {@code boolean} value indicating whether objects of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * type {@code cls} can be assigned to objects of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @exception NullPointerException if the specified Class parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *            null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public native boolean isAssignableFrom(Class<?> cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Determines if the specified {@code Class} object represents an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * interface type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return  {@code true} if this object represents an interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public native boolean isInterface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Determines if this {@code Class} object represents an array class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @return  {@code true} if this object represents an array class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public native boolean isArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Determines if the specified {@code Class} object represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * <p> There are nine predefined {@code Class} objects to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * the eight primitive types and void.  These are created by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Virtual Machine, and have the same names as the primitive types that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * they represent, namely {@code boolean}, {@code byte},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * {@code char}, {@code short}, {@code int},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * {@code long}, {@code float}, and {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p> These objects may only be accessed via the following public static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * final variables, and are the only {@code Class} objects for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * this method returns {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @return true if and only if this class represents a primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see     java.lang.Boolean#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @see     java.lang.Character#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @see     java.lang.Byte#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @see     java.lang.Short#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @see     java.lang.Integer#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @see     java.lang.Long#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @see     java.lang.Float#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @see     java.lang.Double#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @see     java.lang.Void#TYPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public native boolean isPrimitive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Returns true if this {@code Class} object represents an annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * type.  Note that if this method returns true, {@link #isInterface()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * would also return true, as all annotation types are also interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @return {@code true} if this class object represents an annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *      type; {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public boolean isAnnotation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return (getModifiers() & ANNOTATION) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Returns {@code true} if this class is a synthetic class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * returns {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @return {@code true} if and only if this class is a synthetic class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *         defined by the Java Language Specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public boolean isSynthetic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return (getModifiers() & SYNTHETIC) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Returns the  name of the entity (class, interface, array class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * primitive type, or void) represented by this {@code Class} object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * as a {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * <p> If this class object represents a reference type that is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * array type then the binary name of the class is returned, as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * by the Java Language Specification, Second Edition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * <p> If this class object represents a primitive type or void, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * name returned is a {@code String} equal to the Java language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * keyword corresponding to the primitive type or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * <p> If this class object represents a class of arrays, then the internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * form of the name consists of the name of the element type preceded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * one or more '{@code [}' characters representing the depth of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * nesting.  The encoding of element type names is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <blockquote><table summary="Element types and encodings">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <tr><th> Element Type <th> &nbsp;&nbsp;&nbsp; <th> Encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <tr><td> boolean      <td> &nbsp;&nbsp;&nbsp; <td align=center> Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <tr><td> byte         <td> &nbsp;&nbsp;&nbsp; <td align=center> B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * <tr><td> char         <td> &nbsp;&nbsp;&nbsp; <td align=center> C
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <tr><td> class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *                       <td> &nbsp;&nbsp;&nbsp; <td align=center> L<i>classname</i>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <tr><td> double       <td> &nbsp;&nbsp;&nbsp; <td align=center> D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * <tr><td> float        <td> &nbsp;&nbsp;&nbsp; <td align=center> F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * <tr><td> int          <td> &nbsp;&nbsp;&nbsp; <td align=center> I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <tr><td> long         <td> &nbsp;&nbsp;&nbsp; <td align=center> J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * <tr><td> short        <td> &nbsp;&nbsp;&nbsp; <td align=center> S
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <p> The class or interface name <i>classname</i> is the binary name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * the class specified above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <p> Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * String.class.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *     returns "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * byte.class.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *     returns "byte"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * (new Object[3]).getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *     returns "[Ljava.lang.Object;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * (new int[3][4][5][6][7][8][9]).getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *     returns "[[[[[[[I"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @return  the name of the class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *          represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            name = getName0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    // cache the name to reduce the number of calls into the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    private transient String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    private native String getName0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Returns the class loader for the class.  Some implementations may use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * null to represent the bootstrap class loader. This method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * null in such implementations if this class was loaded by the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <p> If a security manager is present, and the caller's class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * not null and the caller's class loader is not the same as or an ancestor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * the class loader for the class whose class loader is requested, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * this method calls the security manager's {@code checkPermission}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * method with a {@code RuntimePermission("getClassLoader")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * permission to ensure it's ok to access the class loader for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <p>If this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * represents a primitive type or void, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @return  the class loader that loaded the class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *          represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *    if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *    {@code checkPermission} method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *    access to the class loader for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @see java.lang.ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public ClassLoader getClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        ClassLoader cl = getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (cl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            ClassLoader ccl = ClassLoader.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    // Package-private to allow ClassLoader access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    native ClassLoader getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Returns an array of {@code TypeVariable} objects that represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * type variables declared by the generic declaration represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * {@code GenericDeclaration} object, in declaration order.  Returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * array of length 0 if the underlying generic declaration declares no type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @return an array of {@code TypeVariable} objects that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *     the type variables declared by this generic declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @throws GenericSignatureFormatError if the generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *     signature of this generic declaration does not conform to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *     the format specified in the Java Virtual Machine Specification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *     3rd edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public TypeVariable<Class<T>>[] getTypeParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (getGenericSignature() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            return (TypeVariable<Class<T>>[])getGenericInfo().getTypeParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            return (TypeVariable<Class<T>>[])new TypeVariable[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Returns the {@code Class} representing the superclass of the entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * (class, interface, primitive type or void) represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * {@code Class}.  If this {@code Class} represents either the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * {@code Object} class, an interface, a primitive type, or void, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * null is returned.  If this object represents an array class then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * {@code Class} object representing the {@code Object} class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @return the superclass of the class represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public native Class<? super T> getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Returns the {@code Type} representing the direct superclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * the entity (class, interface, primitive type or void) represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * this {@code Class}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * <p>If the superclass is a parameterized type, the {@code Type}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * object returned must accurately reflect the actual type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * parameters used in the source code. The parameterized type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * representing the superclass is created if it had not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * created before. See the declaration of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * java.lang.reflect.ParameterizedType ParameterizedType} for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * semantics of the creation process for parameterized types.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * this {@code Class} represents either the {@code Object}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * class, an interface, a primitive type, or void, then null is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * returned.  If this object represents an array class then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * {@code Class} object representing the {@code Object} class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @throws GenericSignatureFormatError if the generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *     class signature does not conform to the format specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *     Java Virtual Machine Specification, 3rd edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @throws TypeNotPresentException if the generic superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *     refers to a non-existent type declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @throws MalformedParameterizedTypeException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *     generic superclass refers to a parameterized type that cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *     instantiated  for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @return the superclass of the class represented by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public Type getGenericSuperclass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (getGenericSignature() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            // Historical irregularity:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            // Generic signature marks interfaces with superclass = Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            // but this API returns null for interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            if (isInterface())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            return getGenericInfo().getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            return getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * Gets the package for this class.  The class loader of this class is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * to find the package.  If the class was loaded by the bootstrap class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * loader the set of packages loaded from CLASSPATH is searched to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * package of the class. Null is returned if no package object was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * by the class loader of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <p> Packages have attributes for versions and specifications only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * information was defined in the manifests that accompany the classes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * if the class loader created the package instance with the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * from the manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @return the package of the class, or null if no package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *         information is available from the archive or codebase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public Package getPackage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return Package.getPackage(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Determines the interfaces implemented by the class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * <p> If this object represents a class, the return value is an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * containing objects representing all interfaces implemented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * class. The order of the interface objects in the array corresponds to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * the order of the interface names in the {@code implements} clause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * of the declaration of the class represented by this object. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * example, given the declaration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * {@code class Shimmer implements FloorWax, DessertTopping { ... }}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * suppose the value of {@code s} is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * {@code Shimmer}; the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * {@code s.getClass().getInterfaces()[0]}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * is the {@code Class} object that represents interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * {@code FloorWax}; and the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * {@code s.getClass().getInterfaces()[1]}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * is the {@code Class} object that represents interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * {@code DessertTopping}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * <p> If this object represents an interface, the array contains objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * representing all interfaces extended by the interface. The order of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * interface objects in the array corresponds to the order of the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * names in the {@code extends} clause of the declaration of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * interface represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * <p> If this object represents a class or interface that implements no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * interfaces, the method returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * <p> If this object represents a primitive type or void, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @return an array of interfaces implemented by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public native Class<?>[] getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * Returns the {@code Type}s representing the interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * directly implemented by the class or interface represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * <p>If a superinterface is a parameterized type, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * {@code Type} object returned for it must accurately reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * the actual type parameters used in the source code. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * parameterized type representing each superinterface is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * if it had not been created before. See the declaration of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * {@link java.lang.reflect.ParameterizedType ParameterizedType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * for the semantics of the creation process for parameterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * <p> If this object represents a class, the return value is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * array containing objects representing all interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * implemented by the class. The order of the interface objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * the array corresponds to the order of the interface names in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * the {@code implements} clause of the declaration of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * represented by this object.  In the case of an array class, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * interfaces {@code Cloneable} and {@code Serializable} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * returned in that order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * <p>If this object represents an interface, the array contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * objects representing all interfaces directly extended by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * interface.  The order of the interface objects in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * corresponds to the order of the interface names in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * {@code extends} clause of the declaration of the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * represented by this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * <p>If this object represents a class or interface that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * implements no interfaces, the method returns an array of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * <p>If this object represents a primitive type or void, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * method returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @throws GenericSignatureFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *     if the generic class signature does not conform to the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *     specified in the Java Virtual Machine Specification, 3rd edition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @throws TypeNotPresentException if any of the generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *     superinterfaces refers to a non-existent type declaration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @throws MalformedParameterizedTypeException if any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *     generic superinterfaces refer to a parameterized type that cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *     be instantiated  for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @return an array of interfaces implemented by this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    public Type[] getGenericInterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        if (getGenericSignature() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return getGenericInfo().getSuperInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            return getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Returns the {@code Class} representing the component type of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * array.  If this class does not represent an array class this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @return the {@code Class} representing the component type of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * class if this class is an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @see     java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public native Class<?> getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Returns the Java language modifiers for this class or interface, encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * in an integer. The modifiers consist of the Java Virtual Machine's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * constants for {@code public}, {@code protected},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * {@code private}, {@code final}, {@code static},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * {@code abstract} and {@code interface}; they should be decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * using the methods of class {@code Modifier}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * <p> If the underlying class is an array class, then its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * {@code public}, {@code private} and {@code protected}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * modifiers are the same as those of its component type.  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * {@code Class} represents a primitive type or void, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * {@code public} modifier is always {@code true}, and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * {@code protected} and {@code private} modifiers are always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * {@code false}. If this object represents an array class, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * primitive type or void, then its {@code final} modifier is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * {@code true} and its interface modifier is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * {@code false}. The values of its other modifiers are not determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * by this specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <p> The modifier encodings are defined in <em>The Java Virtual Machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Specification</em>, table 4.1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @return the {@code int} representing the modifiers for this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @see     java.lang.reflect.Modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public native int getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * Gets the signers of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @return  the signers of this class, or null if there are no signers.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *          particular, this method returns null if this object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *          a primitive type or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    public native Object[] getSigners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Set the signers of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    native void setSigners(Object[] signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * If this {@code Class} object represents a local or anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * class within a method, returns a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * java.lang.reflect.Method Method} object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * immediately enclosing method of the underlying class. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * In particular, this method returns {@code null} if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * class is a local or anonymous class immediately enclosed by a type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * declaration, instance initializer or static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @return the immediately enclosing method of the underlying class, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *     that class is a local or anonymous class; otherwise {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    public Method getEnclosingMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        if (enclosingInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            if (!enclosingInfo.isMethod())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                                              getFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            Class      returnType       = toClass(typeInfo.getReturnType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            Type []    parameterTypes   = typeInfo.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            // Convert Types to Classes; returned types *should*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            // be class objects since the methodDescriptor's used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            // don't have generics information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            for(int i = 0; i < parameterClasses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                parameterClasses[i] = toClass(parameterTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
             * Loop over all declared methods; match method name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
             * number of and type of parameters, *and* return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
             * type.  Matching return type is also necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
             * because of covariant returns, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            for(Method m: enclosingInfo.getEnclosingClass().getDeclaredMethods()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                if (m.getName().equals(enclosingInfo.getName()) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    Class<?>[] candidateParamClasses = m.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                    if (candidateParamClasses.length == parameterClasses.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                        boolean matches = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                        for(int i = 0; i < candidateParamClasses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                            if (!candidateParamClasses[i].equals(parameterClasses[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                                matches = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                        if (matches) { // finally, check return type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                            if (m.getReturnType().equals(returnType) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                                return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            throw new InternalError("Enclosing method not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    private native Object[] getEnclosingMethod0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    private EnclosingMethodInfo getEnclosingMethodInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        Object[] enclosingInfo = getEnclosingMethod0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (enclosingInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            return new EnclosingMethodInfo(enclosingInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    private final static class EnclosingMethodInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        private Class<?> enclosingClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        private String descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        private EnclosingMethodInfo(Object[] enclosingInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            if (enclosingInfo.length != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                throw new InternalError("Malformed enclosing method information");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                // The array is expected to have three elements:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                // the immediately enclosing class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                enclosingClass = (Class<?>) enclosingInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                assert(enclosingClass != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                // the immediately enclosing method or constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                // name (can be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                name            = (String)   enclosingInfo[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                // the immediately enclosing method or constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                // descriptor (null iff name is).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                descriptor      = (String)   enclosingInfo[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                assert((name != null && descriptor != null) || name == descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                throw new InternalError("Invalid type in enclosing method information");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        boolean isPartial() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            return enclosingClass == null || name == null || descriptor == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        boolean isConstructor() { return !isPartial() && "<init>".equals(name); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        boolean isMethod() { return !isPartial() && !isConstructor() && !"<clinit>".equals(name); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        Class<?> getEnclosingClass() { return enclosingClass; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        String getName() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        String getDescriptor() { return descriptor; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    private static Class toClass(Type o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        if (o instanceof GenericArrayType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                                     0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                .getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        return (Class)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * If this {@code Class} object represents a local or anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * class within a constructor, returns a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * java.lang.reflect.Constructor Constructor} object representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * the immediately enclosing constructor of the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * class. Returns {@code null} otherwise.  In particular, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * method returns {@code null} if the underlying class is a local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * or anonymous class immediately enclosed by a type declaration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * instance initializer or static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @return the immediately enclosing constructor of the underlying class, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *     that class is a local or anonymous class; otherwise {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    public Constructor<?> getEnclosingConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (enclosingInfo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            if (!enclosingInfo.isConstructor())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                                                                        getFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            Type []    parameterTypes   = typeInfo.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            // Convert Types to Classes; returned types *should*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            // be class objects since the methodDescriptor's used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            // don't have generics information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            for(int i = 0; i < parameterClasses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                parameterClasses[i] = toClass(parameterTypes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
             * Loop over all declared constructors; match number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
             * of and type of parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            for(Constructor c: enclosingInfo.getEnclosingClass().getDeclaredConstructors()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                Class<?>[] candidateParamClasses = c.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                if (candidateParamClasses.length == parameterClasses.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    boolean matches = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                    for(int i = 0; i < candidateParamClasses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                        if (!candidateParamClasses[i].equals(parameterClasses[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                            matches = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    if (matches)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            throw new InternalError("Enclosing constructor not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * If the class or interface represented by this {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * is a member of another class, returns the {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * representing the class in which it was declared.  This method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * null if this class or interface is not a member of any other class.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * this {@code Class} object represents an array class, a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * type, or void,then this method returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * @return the declaring class for this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    public native Class<?> getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Returns the immediately enclosing class of the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * class.  If the underlying class is a top level class this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * method returns {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @return the immediately enclosing class of the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    public Class<?> getEnclosingClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        // There are five kinds of classes (or interfaces):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        // a) Top level classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        // b) Nested classes (static member classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        // c) Inner classes (non-static member classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        // d) Local classes (named classes declared within a method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        // e) Anonymous classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        // JVM Spec 4.8.6: A class must have an EnclosingMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        // attribute if and only if it is a local class or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        // anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (enclosingInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            // This is a top level or a nested class or an inner class (a, b, or c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            return getDeclaringClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            Class<?> enclosingClass = enclosingInfo.getEnclosingClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            // This is a local class or an anonymous class (d or e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            if (enclosingClass == this || enclosingClass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                throw new InternalError("Malformed enclosing method information");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                return enclosingClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * Returns the simple name of the underlying class as given in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * source code. Returns an empty string if the underlying class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * anonymous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * <p>The simple name of an array is the simple name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * component type with "[]" appended.  In particular the simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * name of an array whose component type is anonymous is "[]".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @return the simple name of the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    public String getSimpleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        if (isArray())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            return getComponentType().getSimpleName()+"[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        String simpleName = getSimpleBinaryName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if (simpleName == null) { // top level class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            simpleName = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            return simpleName.substring(simpleName.lastIndexOf(".")+1); // strip the package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        // According to JLS3 "Binary Compatibility" (13.1) the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        // name of non-package classes (not top level) is the binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        // name of the immediately enclosing class followed by a '$' followed by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        // (for nested and inner classes): the simple name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        // (for local classes): 1 or more digits followed by the simple name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        // (for anonymous classes): 1 or more digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        // Since getSimpleBinaryName() will strip the binary name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        // the immediatly enclosing class, we are now looking at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        // string that matches the regular expression "\$[0-9]*"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        // followed by a simple name (considering the simple of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        // anonymous class to be the empty string).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        // Remove leading "\$[0-9]*" from the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        int length = simpleName.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        if (length < 1 || simpleName.charAt(0) != '$')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            throw new InternalError("Malformed class name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        int index = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        while (index < length && isAsciiDigit(simpleName.charAt(index)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        // Eventually, this is the empty string iff this is an anonymous class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        return simpleName.substring(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Character.isDigit answers {@code true} to some non-ascii
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * digits.  This one does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    private static boolean isAsciiDigit(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        return '0' <= c && c <= '9';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * Returns the canonical name of the underlying class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * defined by the Java Language Specification.  Returns null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * the underlying class does not have a canonical name (i.e., if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * it is a local or anonymous class or an array whose component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * type does not have a canonical name).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @return the canonical name of the underlying class if it exists, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    public String getCanonicalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        if (isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            String canonicalName = getComponentType().getCanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            if (canonicalName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                return canonicalName + "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if (isLocalOrAnonymousClass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        Class<?> enclosingClass = getEnclosingClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        if (enclosingClass == null) { // top level class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            return getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            String enclosingName = enclosingClass.getCanonicalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            if (enclosingName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            return enclosingName + "." + getSimpleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * Returns {@code true} if and only if the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * is an anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * @return {@code true} if and only if this class is an anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    public boolean isAnonymousClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        return "".equals(getSimpleName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * Returns {@code true} if and only if the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * is a local class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * @return {@code true} if and only if this class is a local class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    public boolean isLocalClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        return isLocalOrAnonymousClass() && !isAnonymousClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * Returns {@code true} if and only if the underlying class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * is a member class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @return {@code true} if and only if this class is a member class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    public boolean isMemberClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        return getSimpleBinaryName() != null && !isLocalOrAnonymousClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * Returns the "simple binary name" of the underlying class, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * the binary name without the leading enclosing class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * Returns {@code null} if the underlying class is a top level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    private String getSimpleBinaryName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        Class<?> enclosingClass = getEnclosingClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        if (enclosingClass == null) // top level class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        // Otherwise, strip the enclosing class' name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            return getName().substring(enclosingClass.getName().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        } catch (IndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            throw new InternalError("Malformed class name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * Returns {@code true} if this is a local class or an anonymous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * class.  Returns {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    private boolean isLocalOrAnonymousClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        // JVM Spec 4.8.6: A class must have an EnclosingMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        // attribute if and only if it is a local class or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        // anonymous class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        return getEnclosingMethodInfo() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * Returns an array containing {@code Class} objects representing all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * the public classes and interfaces that are members of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * represented by this {@code Class} object.  This includes public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * class and interface members inherited from superclasses and public class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * and interface members declared by the class.  This method returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * array of length 0 if this {@code Class} object has no public member
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * classes or interfaces.  This method also returns an array of length 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * this {@code Class} object represents a primitive type, an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * class, or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * @return the array of {@code Class} objects representing the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * members of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     *             s.checkMemberAccess(this, Member.PUBLIC)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     *             denies access to the classes within this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    public Class<?>[] getClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        // Privileged so this implementation can look at DECLARED classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        // something the caller might not have privilege to do.  The code here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        // is allowed to look at DECLARED classes because (1) it does not hand
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        // out anything other than public members and (2) public member access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        // has already been ok'd by the SecurityManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1305
        return java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1306
            new java.security.PrivilegedAction<Class[]>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1307
                public Class[] run() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1308
                    List<Class> list = new ArrayList<Class>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                    Class currentClass = Class.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                    while (currentClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                        Class[] members = currentClass.getDeclaredClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                        for (int i = 0; i < members.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                            if (Modifier.isPublic(members[i].getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                                list.add(members[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                        currentClass = currentClass.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                    }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1319
                    return list.toArray(new Class[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * Returns an array containing {@code Field} objects reflecting all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * the accessible public fields of the class or interface represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * this {@code Class} object.  The elements in the array returned are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * not sorted and are not in any particular order.  This method returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * array of length 0 if the class or interface has no accessible public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * fields, or if it represents an array class, a primitive type, or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * <p> Specifically, if this {@code Class} object represents a class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * this method returns the public fields of this class and of all its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * superclasses.  If this {@code Class} object represents an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * interface, this method returns the fields of this interface and of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * its superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * <p> The implicit length field for array class is not reflected by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * method. User code should use the methods of class {@code Array} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * manipulate arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * @return the array of {@code Field} objects representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * public fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     *             access to the fields within this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    public Field[] getFields() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        return copyFields(privateGetPublicFields(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * Returns an array containing {@code Method} objects reflecting all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * the public <em>member</em> methods of the class or interface represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * by this {@code Class} object, including those declared by the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * or interface and those inherited from superclasses and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * superinterfaces.  Array classes return all the (public) member methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * inherited from the {@code Object} class.  The elements in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * returned are not sorted and are not in any particular order.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * method returns an array of length 0 if this {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * represents a class or interface that has no public member methods, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * this {@code Class} object represents a primitive type or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * <p> The class initialization method {@code <clinit>} is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * included in the returned array. If the class declares multiple public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * member methods with the same parameter types, they are all included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * @return the array of {@code Method} objects representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * public methods of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     *             access to the methods within this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    public Method[] getMethods() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        return copyMethods(privateGetPublicMethods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * Returns an array containing {@code Constructor} objects reflecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * all the public constructors of the class represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * {@code Class} object.  An array of length 0 is returned if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * class has no public constructors, or if the class is an array class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * if the class reflects a primitive type or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * Note that while this method returns an array of {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * Constructor<T>} objects (that is an array of constructors from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * this class), the return type of this method is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * Constructor<?>[]} and <em>not</em> {@code Constructor<T>[]} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * might be expected.  This less informative return type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * necessary since after being returned from this method, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * array could be modified to hold {@code Constructor} objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * different classes, which would violate the type guarantees of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * {@code Constructor<T>[]}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * @return the array of {@code Constructor} objects representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     *  public constructors of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     *             access to the constructors within this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    public Constructor<?>[] getConstructors() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        return copyConstructors(privateGetDeclaredConstructors(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * Returns a {@code Field} object that reflects the specified public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * member field of the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * {@code Class} object. The {@code name} parameter is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * {@code String} specifying the simple name of the desired field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * <p> The field to be reflected is determined by the algorithm that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * follows.  Let C be the class represented by this object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * <OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * <LI> If C declares a public field with the name specified, that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     *      field to be reflected.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * <LI> If no field was found in step 1 above, this algorithm is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     *      recursively to each direct superinterface of C. The direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     *      superinterfaces are searched in the order they were declared.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * <LI> If no field was found in steps 1 and 2 above, and C has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *      superclass S, then this algorithm is invoked recursively upon S.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     *      If C has no superclass, then a {@code NoSuchFieldException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     *      is thrown.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * @param name the field name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * @return  the {@code Field} object of this class specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * {@code name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * @exception NoSuchFieldException if a field with the specified name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     *              not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * @exception NullPointerException if {@code name} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     *             access to the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    public Field getField(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        throws NoSuchFieldException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        Field field = getField0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        if (field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            throw new NoSuchFieldException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * Returns a {@code Method} object that reflects the specified public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * member method of the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * {@code Class} object. The {@code name} parameter is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * {@code String} specifying the simple name of the desired method. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * {@code parameterTypes} parameter is an array of {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * objects that identify the method's formal parameter types, in declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * order. If {@code parameterTypes} is {@code null}, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * treated as if it were an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * <p> If the {@code name} is "{@code <init>};"or "{@code <clinit>}" a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * {@code NoSuchMethodException} is raised. Otherwise, the method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * be reflected is determined by the algorithm that follows.  Let C be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * class represented by this object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * <OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * <LI> C is searched for any <I>matching methods</I>. If no matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     *      method is found, the algorithm of step 1 is invoked recursively on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     *      the superclass of C.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * <LI> If no method was found in step 1 above, the superinterfaces of C
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     *      are searched for a matching method. If any such method is found, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     *      is reflected.</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * To find a matching method in a class C:&nbsp; If C declares exactly one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * public method with the specified name and exactly the same formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * parameter types, that is the method reflected. If more than one such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * method is found in C, and one of these methods has a return type that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * more specific than any of the others, that method is reflected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * otherwise one of the methods is chosen arbitrarily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * <p>Note that there may be more than one matching method in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * class because while the Java language forbids a class to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * declare multiple methods with the same signature but different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * return types, the Java virtual machine does not.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * increased flexibility in the virtual machine can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * implement various language features.  For example, covariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * returns can be implemented with {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * java.lang.reflect.Method#isBridge bridge methods}; the bridge
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * method and the method being overridden would have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * signature but different return types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * @param name the name of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * @param parameterTypes the list of parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * @return the {@code Method} object that matches the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * {@code name} and {@code parameterTypes}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * @exception NoSuchMethodException if a matching method is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     *            or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * @exception NullPointerException if {@code name} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     *             access to the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    public Method getMethod(String name, Class<?>... parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        throws NoSuchMethodException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        Method method = getMethod0(name, parameterTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * Returns a {@code Constructor} object that reflects the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * public constructor of the class represented by this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * object. The {@code parameterTypes} parameter is an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * {@code Class} objects that identify the constructor's formal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * parameter types, in declared order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * If this {@code Class} object represents an inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * declared in a non-static context, the formal parameter types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * include the explicit enclosing instance as the first parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * <p> The constructor to reflect is the public constructor of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * represented by this {@code Class} object whose formal parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * types match those specified by {@code parameterTypes}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * @param parameterTypes the parameter array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * @return the {@code Constructor} object of the public constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * matches the specified {@code parameterTypes}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * @exception NoSuchMethodException if a matching method is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     *             s.checkMemberAccess(this, Member.PUBLIC)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     *             access to the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    public Constructor<T> getConstructor(Class<?>... parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        throws NoSuchMethodException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        return getConstructor0(parameterTypes, Member.PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * Returns an array of {@code Class} objects reflecting all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * classes and interfaces declared as members of the class represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * this {@code Class} object. This includes public, protected, default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * (package) access, and private classes and interfaces declared by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * class, but excludes inherited classes and interfaces.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     * returns an array of length 0 if the class declares no classes or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     * interfaces as members, or if this {@code Class} object represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * primitive type, an array class, or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * @return the array of {@code Class} objects representing all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * declared members of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     *             s.checkMemberAccess(this, Member.DECLARED)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     *             access to the declared classes within this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    public Class<?>[] getDeclaredClasses() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        return getDeclaredClasses0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * Returns an array of {@code Field} objects reflecting all the fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * declared by the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * {@code Class} object. This includes public, protected, default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * (package) access, and private fields, but excludes inherited fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * The elements in the array returned are not sorted and are not in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * particular order.  This method returns an array of length 0 if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * or interface declares no fields, or if this {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * represents a primitive type, an array class, or void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * @return    the array of {@code Field} objects representing all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * declared fields of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     *             s.checkMemberAccess(this, Member.DECLARED)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     *             access to the declared fields within this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    public Field[] getDeclaredFields() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        return copyFields(privateGetDeclaredFields(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * Returns an array of {@code Method} objects reflecting all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * methods declared by the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * {@code Class} object. This includes public, protected, default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * (package) access, and private methods, but excludes inherited methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * The elements in the array returned are not sorted and are not in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * particular order.  This method returns an array of length 0 if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * or interface declares no methods, or if this {@code Class} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * represents a primitive type, an array class, or void.  The class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * initialization method {@code <clinit>} is not included in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * returned array. If the class declares multiple public member methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * with the same parameter types, they are all included in the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * <p> See <em>The Java Language Specification</em>, section 8.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @return    the array of {@code Method} objects representing all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * declared methods of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     *             s.checkMemberAccess(this, Member.DECLARED)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     *             access to the declared methods within this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    public Method[] getDeclaredMethods() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        return copyMethods(privateGetDeclaredMethods(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * Returns an array of {@code Constructor} objects reflecting all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * constructors declared by the class represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * {@code Class} object. These are public, protected, default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * (package) access, and private constructors.  The elements in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * returned are not sorted and are not in any particular order.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * class has a default constructor, it is included in the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * This method returns an array of length 0 if this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * object represents an interface, a primitive type, an array class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * <p> See <em>The Java Language Specification</em>, section 8.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * @return    the array of {@code Constructor} objects representing all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * declared constructors of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     *             s.checkMemberAccess(this, Member.DECLARED)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     *             access to the declared constructors within this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        return copyConstructors(privateGetDeclaredConstructors(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * Returns a {@code Field} object that reflects the specified declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * field of the class or interface represented by this {@code Class}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * object. The {@code name} parameter is a {@code String} that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * specifies the simple name of the desired field.  Note that this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * will not reflect the {@code length} field of an array class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * @param name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * @return the {@code Field} object for the specified field in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * @exception NoSuchFieldException if a field with the specified name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     *              not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * @exception NullPointerException if {@code name} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     *             s.checkMemberAccess(this, Member.DECLARED)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     *             access to the declared field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    public Field getDeclaredField(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        throws NoSuchFieldException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        Field field = searchFields(privateGetDeclaredFields(false), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        if (field == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            throw new NoSuchFieldException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * Returns a {@code Method} object that reflects the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * declared method of the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * {@code Class} object. The {@code name} parameter is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * {@code String} that specifies the simple name of the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * method, and the {@code parameterTypes} parameter is an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * {@code Class} objects that identify the method's formal parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * types, in declared order.  If more than one method with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * parameter types is declared in a class, and one of these methods has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * return type that is more specific than any of the others, that method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * returned; otherwise one of the methods is chosen arbitrarily.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * name is "&lt;init&gt;"or "&lt;clinit&gt;" a {@code NoSuchMethodException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * is raised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * @param name the name of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * @param parameterTypes the parameter array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * @return    the {@code Method} object for the method of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * matching the specified name and parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * @exception NoSuchMethodException if a matching method is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * @exception NullPointerException if {@code name} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     *             s.checkMemberAccess(this, Member.DECLARED)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     *             access to the declared method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        throws NoSuchMethodException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * Returns a {@code Constructor} object that reflects the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * constructor of the class or interface represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * {@code Class} object.  The {@code parameterTypes} parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * an array of {@code Class} objects that identify the constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * formal parameter types, in declared order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * If this {@code Class} object represents an inner class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * declared in a non-static context, the formal parameter types
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * include the explicit enclosing instance as the first parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * @param parameterTypes the parameter array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * @return    The {@code Constructor} object for the constructor with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * specified parameter list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * @exception NoSuchMethodException if a matching method is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * @exception  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *             If a security manager, <i>s</i>, is present and any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     *             following conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     *             <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     *             <li> invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     *             {@link SecurityManager#checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *             s.checkMemberAccess(this, Member.DECLARED)} denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     *             access to the declared constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     *             <li> the caller's class loader is not the same as or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     *             ancestor of the class loader for the current class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     *             invocation of {@link SecurityManager#checkPackageAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     *             s.checkPackageAccess()} denies access to the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     *             of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     *             </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        throws NoSuchMethodException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        // be very careful not to change the stack depth of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        // checkMemberAccess call for security reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        // see java.lang.SecurityManager.checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        return getConstructor0(parameterTypes, Member.DECLARED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * Finds a resource with a given name.  The rules for searching resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * associated with a given class are implemented by the defining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * {@linkplain ClassLoader class loader} of the class.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * delegates to this object's class loader.  If this object was loaded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * the bootstrap class loader, the method delegates to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * ClassLoader#getSystemResourceAsStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * <p> Before delegation, an absolute resource name is constructed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * given resource name using this algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * <li> If the {@code name} begins with a {@code '/'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * portion of the {@code name} following the {@code '/'}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * <li> Otherwise, the absolute name is of the following form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     *   {@code modified_package_name/name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * <p> Where the {@code modified_package_name} is the package name of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * object with {@code '/'} substituted for {@code '.'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * (<tt>'&#92;u002e'</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * @param  name name of the desired resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * @return      A {@link java.io.InputStream} object or {@code null} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     *              no resource with this name is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * @throws  NullPointerException If {@code name} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * @since  JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     public InputStream getResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        name = resolveName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        ClassLoader cl = getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        if (cl==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            // A system class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            return ClassLoader.getSystemResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        return cl.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * Finds a resource with a given name.  The rules for searching resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * associated with a given class are implemented by the defining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     * {@linkplain ClassLoader class loader} of the class.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * delegates to this object's class loader.  If this object was loaded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * the bootstrap class loader, the method delegates to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * ClassLoader#getSystemResource}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * <p> Before delegation, an absolute resource name is constructed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * given resource name using this algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * <li> If the {@code name} begins with a {@code '/'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * (<tt>'&#92;u002f'</tt>), then the absolute name of the resource is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * portion of the {@code name} following the {@code '/'}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * <li> Otherwise, the absolute name is of the following form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     *   {@code modified_package_name/name}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * <p> Where the {@code modified_package_name} is the package name of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * object with {@code '/'} substituted for {@code '.'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * (<tt>'&#92;u002e'</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * @param  name name of the desired resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * @return      A  {@link java.net.URL} object or {@code null} if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     *              resource with this name is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * @since  JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
    public java.net.URL getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        name = resolveName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        ClassLoader cl = getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        if (cl==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            // A system class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            return ClassLoader.getSystemResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        return cl.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    /** protection domain returned when the internal domain is null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    private static java.security.ProtectionDomain allPermDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * Returns the {@code ProtectionDomain} of this class.  If there is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * security manager installed, this method first calls the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * manager's {@code checkPermission} method with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * {@code RuntimePermission("getProtectionDomain")} permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * ensure it's ok to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * {@code ProtectionDomain}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * @return the ProtectionDomain of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     *        {@code checkPermission} method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     *        getting the ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    public java.security.ProtectionDomain getProtectionDomain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            sm.checkPermission(SecurityConstants.GET_PD_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        java.security.ProtectionDomain pd = getProtectionDomain0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        if (pd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            if (allPermDomain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                java.security.Permissions perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                    new java.security.Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                perms.add(SecurityConstants.ALL_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                allPermDomain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                    new java.security.ProtectionDomain(null, perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            pd = allPermDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        return pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * Returns the ProtectionDomain of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
    private native java.security.ProtectionDomain getProtectionDomain0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     * Set the ProtectionDomain for this class. Called by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * ClassLoader.defineClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    native void setProtectionDomain0(java.security.ProtectionDomain pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * Return the Virtual Machine's Class object for the named
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
    static native Class getPrimitiveClass(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * Check if client is allowed to access members.  If access is denied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * throw a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * Be very careful not to change the stack depth of this checkMemberAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * call for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * See java.lang.SecurityManager.checkMemberAccess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * <p> Default policy: allow all clients access with normal Java access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    private void checkMemberAccess(int which, ClassLoader ccl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        SecurityManager s = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            s.checkMemberAccess(this, which);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            ClassLoader cl = getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            if ((ccl != null) && (ccl != cl) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                  ((cl == null) || !cl.isAncestor(ccl))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                String name = this.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                    s.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * Add a package name prefix if the name is not absolute Remove leading "/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * if name is absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    private String resolveName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        if (!name.startsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            Class c = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            while (c.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                c = c.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
            String baseName = c.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            int index = baseName.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                name = baseName.substring(0, index).replace('.', '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                    +"/"+name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            name = name.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * Reflection support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    // Caches for certain reflective results
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
    private static boolean useCaches = true;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2215
    private volatile transient SoftReference<Field[]> declaredFields;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2216
    private volatile transient SoftReference<Field[]> publicFields;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2217
    private volatile transient SoftReference<Method[]> declaredMethods;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2218
    private volatile transient SoftReference<Method[]> publicMethods;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2219
    private volatile transient SoftReference<Constructor<T>[]> declaredConstructors;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2220
    private volatile transient SoftReference<Constructor<T>[]> publicConstructors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    // Intermediate results for getFields and getMethods
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2222
    private volatile transient SoftReference<Field[]> declaredPublicFields;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2223
    private volatile transient SoftReference<Method[]> declaredPublicMethods;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    // Incremented by the VM on each call to JVM TI RedefineClasses()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
    // that redefines this class or a superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    private volatile transient int classRedefinedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    // Value of classRedefinedCount when we last cleared the cached values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
    // that are sensitive to class redefinition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    private volatile transient int lastRedefinedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    // Clears cached values that might possibly have been obsoleted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    // a class redefinition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    private void clearCachesOnClassRedefinition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        if (lastRedefinedCount != classRedefinedCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            declaredFields = publicFields = declaredPublicFields = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            declaredMethods = publicMethods = declaredPublicMethods = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            declaredConstructors = publicConstructors = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            annotations = declaredAnnotations = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            // Use of "volatile" (and synchronization by caller in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            // of annotations) ensures that no thread sees the update to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            // lastRedefinedCount before seeing the caches cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            // We do not guard against brief windows during which multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            // threads might redundantly work to fill an empty cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            lastRedefinedCount = classRedefinedCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
    // Generic signature handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    private native String getGenericSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    // Generic info repository; lazily initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
    private transient ClassRepository genericInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
    // accessor for factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    private GenericsFactory getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        // create scope and factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        return CoreReflectionFactory.make(this, ClassScope.make(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
    // accessor for generic info repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
    private ClassRepository getGenericInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        // lazily initialize repository if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        if (genericInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            // create and cache generic info repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            genericInfo = ClassRepository.make(getGenericSignature(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                                               getFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        return genericInfo; //return cached repository
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
    // Annotations handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    private native byte[] getRawAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    native ConstantPool getConstantPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
    // java.lang.reflect.Field handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
    // Returns an array of "root" fields. These Field objects must NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
    // be propagated to the outside world, but must instead be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
    // via ReflectionFactory.copyField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
    private Field[] privateGetDeclaredFields(boolean publicOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        Field[] res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            clearCachesOnClassRedefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            if (publicOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                if (declaredPublicFields != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2295
                    res = declaredPublicFields.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                if (declaredFields != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2299
                    res = declaredFields.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        // No cached value available; request value from VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
        res = Reflection.filterFields(this, getDeclaredFields0(publicOnly));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            if (publicOnly) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2308
                declaredPublicFields = new SoftReference<Field[]>(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2310
                declaredFields = new SoftReference<Field[]>(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
    // Returns an array of "root" fields. These Field objects must NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    // be propagated to the outside world, but must instead be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    // via ReflectionFactory.copyField.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2319
    private Field[] privateGetPublicFields(Set<Class<?>> traversedInterfaces) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        Field[] res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            clearCachesOnClassRedefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            if (publicFields != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2325
                res = publicFields.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        // No cached value available; compute value recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        // Traverse in correct order for getField().
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2332
        List<Field> fields = new ArrayList<Field>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        if (traversedInterfaces == null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2334
            traversedInterfaces = new HashSet<Class<?>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        // Local fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        Field[] tmp = privateGetDeclaredFields(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        addAll(fields, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        // Direct superinterfaces, recursively
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2342
        for (Class<?> c : getInterfaces()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            if (!traversedInterfaces.contains(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                traversedInterfaces.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                addAll(fields, c.privateGetPublicFields(traversedInterfaces));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        // Direct superclass, recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        if (!isInterface()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2351
            Class<?> c = getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                addAll(fields, c.privateGetPublicFields(traversedInterfaces));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        res = new Field[fields.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        fields.toArray(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        if (useCaches) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2360
            publicFields = new SoftReference<Field[]>(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2365
    private static void addAll(Collection<Field> c, Field[] o) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        for (int i = 0; i < o.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            c.add(o[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
    // java.lang.reflect.Constructor handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    // Returns an array of "root" constructors. These Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
    // objects must NOT be propagated to the outside world, but must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    // instead be copied via ReflectionFactory.copyConstructor.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2381
    private Constructor<T>[] privateGetDeclaredConstructors(boolean publicOnly) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        checkInitted();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2383
        Constructor<T>[] res = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            clearCachesOnClassRedefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
            if (publicOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                if (publicConstructors != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2388
                    res = publicConstructors.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                if (declaredConstructors != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2392
                    res = declaredConstructors.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        // No cached value available; request value from VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        if (isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            res = new Constructor[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            res = getDeclaredConstructors0(publicOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            if (publicOnly) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2405
                publicConstructors = new SoftReference<Constructor<T>[]>(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2407
                declaredConstructors = new SoftReference<Constructor<T>[]>(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
    // java.lang.reflect.Method handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
    // Returns an array of "root" methods. These Method objects must NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
    // be propagated to the outside world, but must instead be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    // via ReflectionFactory.copyMethod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
    private Method[] privateGetDeclaredMethods(boolean publicOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        Method[] res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
            clearCachesOnClassRedefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            if (publicOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                if (declaredPublicMethods != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2429
                    res = declaredPublicMethods.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                if (declaredMethods != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2433
                    res = declaredMethods.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        // No cached value available; request value from VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            if (publicOnly) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2442
                declaredPublicMethods = new SoftReference<Method[]>(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2444
                declaredMethods = new SoftReference<Method[]>(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
    static class MethodArray {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        private Method[] methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        private int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        MethodArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            methods = new Method[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
            length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        void add(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
            if (length == methods.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                methods = Arrays.copyOf(methods, 2 * methods.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
            methods[length++] = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
        void addAll(Method[] ma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            for (int i = 0; i < ma.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                add(ma[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
        void addAll(MethodArray ma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            for (int i = 0; i < ma.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                add(ma.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
        void addIfNotPresent(Method newMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                Method m = methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                if (m == newMethod || (m != null && m.equals(newMethod))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
            add(newMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
        void addAllIfNotPresent(MethodArray newMethods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            for (int i = 0; i < newMethods.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                Method m = newMethods.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                if (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                    addIfNotPresent(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        Method get(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            return methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        void removeByNameAndSignature(Method toRemove) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                Method m = methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                if (m != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                    m.getReturnType() == toRemove.getReturnType() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                    m.getName() == toRemove.getName() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                    arrayContentsEq(m.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                                    toRemove.getParameterTypes())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                    methods[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
        void compactAndTrim() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
            int newPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
            // Get rid of null slots
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            for (int pos = 0; pos < length; pos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                Method m = methods[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                if (m != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                    if (pos != newPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                        methods[newPos] = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                    newPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
            if (newPos != methods.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                methods = Arrays.copyOf(methods, newPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        Method[] getArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
            return methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
    // Returns an array of "root" methods. These Method objects must NOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
    // be propagated to the outside world, but must instead be copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
    // via ReflectionFactory.copyMethod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    private Method[] privateGetPublicMethods() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        Method[] res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
        if (useCaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
            clearCachesOnClassRedefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
            if (publicMethods != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2550
                res = publicMethods.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            if (res != null) return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        // No cached value available; compute value recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        // Start by fetching public declared methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
        MethodArray methods = new MethodArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            Method[] tmp = privateGetDeclaredMethods(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            methods.addAll(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
        // Now recur over superclass and direct superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        // Go over superinterfaces first so we can more easily filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        // out concrete implementations inherited from superclasses at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        // the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        MethodArray inheritedMethods = new MethodArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
        Class[] interfaces = getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
            inheritedMethods.addAll(interfaces[i].privateGetPublicMethods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        if (!isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
            Class c = getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                MethodArray supers = new MethodArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                supers.addAll(c.privateGetPublicMethods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                // Filter out concrete implementations of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                // interface methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                for (int i = 0; i < supers.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                    Method m = supers.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                    if (m != null && !Modifier.isAbstract(m.getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                        inheritedMethods.removeByNameAndSignature(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                // Insert superclass's inherited methods before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                // superinterfaces' to satisfy getMethod's search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                // order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                supers.addAll(inheritedMethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                inheritedMethods = supers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
        // Filter out all local methods from inherited ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        for (int i = 0; i < methods.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
            Method m = methods.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
            inheritedMethods.removeByNameAndSignature(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
        methods.addAllIfNotPresent(inheritedMethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
        methods.compactAndTrim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        res = methods.getArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
        if (useCaches) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2600
            publicMethods = new SoftReference<Method[]>(res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    // Helpers for fetchers of one field, method, or constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
    private Field searchFields(Field[] fields, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        String internedName = name.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            if (fields[i].getName() == internedName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                return getReflectionFactory().copyField(fields[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
    private Field getField0(String name) throws NoSuchFieldException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        // Note: the intent is that the search algorithm this routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
        // uses be equivalent to the ordering imposed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        // privateGetPublicFields(). It fetches only the declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        // public fields for each class, however, to reduce the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        // of Field objects which have to be created for the common
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        // case where the field being requested is declared in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
        // class which is being queried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
        Field res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        // Search declared public fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
        if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
        // Direct superinterfaces, recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
        Class[] interfaces = getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            Class c = interfaces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
            if ((res = c.getField0(name)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
        // Direct superclass, recursively
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
        if (!isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
            Class c = getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                if ((res = c.getField0(name)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    private static Method searchMethods(Method[] methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                                        String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                                        Class[] parameterTypes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
        Method res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        String internedName = name.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
        for (int i = 0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            Method m = methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            if (m.getName() == internedName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                && arrayContentsEq(parameterTypes, m.getParameterTypes())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                && (res == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                    || res.getReturnType().isAssignableFrom(m.getReturnType())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                res = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        return (res == null ? res : getReflectionFactory().copyMethod(res));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
    private Method getMethod0(String name, Class[] parameterTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
        // Note: the intent is that the search algorithm this routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        // uses be equivalent to the ordering imposed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
        // privateGetPublicMethods(). It fetches only the declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
        // public methods for each class, however, to reduce the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
        // number of Method objects which have to be created for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
        // common case where the method being requested is declared in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
        // the class which is being queried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        Method res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        // Search declared public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
        if ((res = searchMethods(privateGetDeclaredMethods(true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                                 name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
                                 parameterTypes)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        // Search superclass's methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
        if (!isInterface()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
            Class c = getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
                if ((res = c.getMethod0(name, parameterTypes)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
        // Search superinterfaces' methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
        Class[] interfaces = getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
            Class c = interfaces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
            if ((res = c.getMethod0(name, parameterTypes)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
        // Not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
    private Constructor<T> getConstructor0(Class[] parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
                                        int which) throws NoSuchMethodException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2711
        Constructor<T>[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2712
        for (Constructor<T> constructor : constructors) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
            if (arrayContentsEq(parameterTypes,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2714
                                constructor.getParameterTypes())) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2715
                return getReflectionFactory().copyConstructor(constructor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
        throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
    // Other helpers and base implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
    private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
        if (a1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
            return a2 == null || a2.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        if (a2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            return a1.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        if (a1.length != a2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        for (int i = 0; i < a1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
            if (a1[i] != a2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
    private static Field[] copyFields(Field[] arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        Field[] out = new Field[arg.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
        ReflectionFactory fact = getReflectionFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
        for (int i = 0; i < arg.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
            out[i] = fact.copyField(arg[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
    private static Method[] copyMethods(Method[] arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
        Method[] out = new Method[arg.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
        ReflectionFactory fact = getReflectionFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
        for (int i = 0; i < arg.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
            out[i] = fact.copyMethod(arg[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2765
    private static <U> Constructor<U>[] copyConstructors(Constructor<U>[] arg) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2766
        Constructor<U>[] out = arg.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
        ReflectionFactory fact = getReflectionFactory();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2768
        for (int i = 0; i < out.length; i++) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2769
            out[i] = fact.copyConstructor(out[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
    private native Field[]       getDeclaredFields0(boolean publicOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
    private native Method[]      getDeclaredMethods0(boolean publicOnly);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2776
    private native Constructor<T>[] getDeclaredConstructors0(boolean publicOnly);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
    private native Class[]   getDeclaredClasses0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
    private static String        argumentTypesToString(Class[] argTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        buf.append("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
        if (argTypes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
            for (int i = 0; i < argTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                    buf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                Class c = argTypes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                buf.append((c == null) ? "null" : c.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
    /** use serialVersionUID from JDK 1.1 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
    private static final long serialVersionUID = 3206093459760846163L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
     * Class Class is special cased within the Serialization Stream Protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
     * A Class instance is written initially into an ObjectOutputStream in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
     * following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
     *      {@code TC_CLASS} ClassDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     *      A ClassDescriptor is a special cased serialization of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     *      a {@code java.io.ObjectStreamClass} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     * A new handle is generated for the initial time the class descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * is written into the stream. Future references to the class descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * are written as references to the initial class descriptor instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
     * @see java.io.ObjectStreamClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
    private static final ObjectStreamField[] serialPersistentFields =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
        new ObjectStreamField[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
     * Returns the assertion status that would be assigned to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
     * class if it were to be initialized at the time this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
     * If this class has had its assertion status set, the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
     * setting will be returned; otherwise, if any package default assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
     * status pertains to this class, the most recent setting for the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
     * specific pertinent package default assertion status is returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
     * otherwise, if this class is not a system class (i.e., it has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
     * class loader) its class loader's default assertion status is returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
     * otherwise, the system class default assertion status is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
     * Few programmers will have any need for this method; it is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
     * for the benefit of the JRE itself.  (It allows a class to determine at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
     * the time that it is initialized whether assertions should be enabled.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
     * Note that this method is not guaranteed to return the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
     * assertion status that was (or will be) associated with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
     * class when it was (or will be) initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
     * @return the desired assertion status of the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
     * @see    java.lang.ClassLoader#setClassAssertionStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     * @see    java.lang.ClassLoader#setPackageAssertionStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     * @see    java.lang.ClassLoader#setDefaultAssertionStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
    public boolean desiredAssertionStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        ClassLoader loader = getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
        // If the loader is null this is a system class, so ask the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
        if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
            return desiredAssertionStatus0(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  2849
        // If the classloader has been initialized with the assertion
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  2850
        // directives, ask it. Otherwise, ask the VM.
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  2851
        synchronized(loader.assertionLock) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  2852
            if (loader.classAssertionStatus != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  2853
                return loader.desiredAssertionStatus(getName());
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  2854
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2174
diff changeset
  2856
        return desiredAssertionStatus0(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
    // Retrieves the desired assertion status of this class from the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
    private static native boolean desiredAssertionStatus0(Class clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
     * Returns true if and only if this class was declared as an enum in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
     * source code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
     * @return true if and only if this class was declared as an enum in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
     *     source code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
    public boolean isEnum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
        // An enum must both directly extend java.lang.Enum and have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        // the ENUM bit set; classes for specialized enum constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        // don't do the former.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        return (this.getModifiers() & ENUM) != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
        this.getSuperclass() == java.lang.Enum.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
    // Fetches the factory for reflective objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
    private static ReflectionFactory getReflectionFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        if (reflectionFactory == null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2881
            reflectionFactory =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                    (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
        return reflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
    private static ReflectionFactory reflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
    // To be able to query system properties as soon as they're available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
    private static boolean initted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
    private static void checkInitted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
        if (initted) return;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2893
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2894
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                    // Tests to ensure the system properties table is fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                    // initialized. This is needed because reflection code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                    // called very early in the initialization process (before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                    // command-line arguments have been parsed and therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                    // these user-settable properties installed.) We assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                    // if System.out is non-null then the System class has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                    // fully initialized and that the bulk of the startup code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
                    // has been run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                    if (System.out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                        // java.lang.System not yet fully initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
                    String val =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                        System.getProperty("sun.reflect.noCaches");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                    if (val != null && val.equals("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                        useCaches = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                    initted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
     * Returns the elements of this enum class or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
     * Class object does not represent an enum type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
     * @return an array containing the values comprising the enum class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
     *     represented by this Class object in the order they're
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     *     declared, or null if this Class object does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
     *     represent an enum type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
    public T[] getEnumConstants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
        T[] values = getEnumConstantsShared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
        return (values != null) ? values.clone() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
     * Returns the elements of this enum class or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
     * Class object does not represent an enum type;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2939
     * identical to getEnumConstants except that the result is
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2940
     * uncloned, cached, and shared by all callers.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
    T[] getEnumConstantsShared() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        if (enumConstants == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
            if (!isEnum()) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                final Method values = getMethod("values");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2947
                java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2948
                    new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2949
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
                                values.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                enumConstants = (T[])values.invoke(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
            // These can happen when users concoct enum-like classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
            // that don't comply with the enum spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
            catch (InvocationTargetException ex) { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
            catch (NoSuchMethodException ex) { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
            catch (IllegalAccessException ex) { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        return enumConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
    private volatile transient T[] enumConstants = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
     * Returns a map from simple name to enum constant.  This package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
     * method is used internally by Enum to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
     *     public static <T extends Enum<T>> T valueOf(Class<T>, String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
     * efficiently.  Note that the map is returned by this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
     * created lazily on first use.  Typically it won't ever get created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
    Map<String, T> enumConstantDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
        if (enumConstantDirectory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            T[] universe = getEnumConstantsShared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
            if (universe == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                    getName() + " is not an enum type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            Map<String, T> m = new HashMap<String, T>(2 * universe.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            for (T constant : universe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                m.put(((Enum)constant).name(), constant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
            enumConstantDirectory = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        return enumConstantDirectory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
    private volatile transient Map<String, T> enumConstantDirectory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
     * Casts an object to the class or interface represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
     * by this {@code Class} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
     * @param obj the object to be cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
     * @return the object after casting, or null if obj is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
     * @throws ClassCastException if the object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
     * null and is not assignable to the type T.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
    public T cast(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
        if (obj != null && !isInstance(obj))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
            throw new ClassCastException(cannotCastMsg(obj));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
        return (T) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
    private String cannotCastMsg(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        return "Cannot cast " + obj.getClass().getName() + " to " + getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
     * Casts this {@code Class} object to represent a subclass of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
     * represented by the specified class object.  Checks that that the cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
     * is valid, and throws a {@code ClassCastException} if it is not.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
     * this method succeeds, it always returns a reference to this class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
     * <p>This method is useful when a client needs to "narrow" the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
     * a {@code Class} object to pass it to an API that restricts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
     * {@code Class} objects that it is willing to accept.  A cast would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
     * generate a compile-time warning, as the correctness of the cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
     * could not be checked at runtime (because generic types are implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
     * by erasure).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
     * @return this {@code Class} object, cast to represent a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
     *    the specified class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
     * @throws ClassCastException if this {@code Class} object does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
     *    represent a subclass of the specified class (here "subclass" includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
     *    the class itself).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
    public <U> Class<? extends U> asSubclass(Class<U> clazz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
        if (clazz.isAssignableFrom(this))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
            return (Class<? extends U>) this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
            throw new ClassCastException(this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
    public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
        if (annotationClass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
        initAnnotationsIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        return (A) annotations.get(annotationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
    public boolean isAnnotationPresent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        Class<? extends Annotation> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
        if (annotationClass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
        return getAnnotation(annotationClass) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
    public Annotation[] getAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
        initAnnotationsIfNecessary();
2174
0ffce164e4a4 6799230: Lazily load java.lang.annotation.Annotation class
mchung
parents: 715
diff changeset
  3067
        return AnnotationParser.toArray(annotations);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
    public Annotation[] getDeclaredAnnotations()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
        initAnnotationsIfNecessary();
2174
0ffce164e4a4 6799230: Lazily load java.lang.annotation.Annotation class
mchung
parents: 715
diff changeset
  3075
        return AnnotationParser.toArray(declaredAnnotations);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
    // Annotations cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
    private transient Map<Class, Annotation> annotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
    private transient Map<Class, Annotation> declaredAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
    private synchronized void initAnnotationsIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        clearCachesOnClassRedefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
        if (annotations != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        declaredAnnotations = AnnotationParser.parseAnnotations(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
            getRawAnnotations(), getConstantPool(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
        Class<?> superClass = getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        if (superClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
            annotations = declaredAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
            annotations = new HashMap<Class, Annotation>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
            superClass.initAnnotationsIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
            for (Map.Entry<Class, Annotation> e : superClass.annotations.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                Class annotationClass = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                if (AnnotationType.getInstance(annotationClass).isInherited())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
                    annotations.put(annotationClass, e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
            annotations.putAll(declaredAnnotations);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
    // Annotation types cache their internal (AnnotationType) form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
    private AnnotationType annotationType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
    void setAnnotationType(AnnotationType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
        annotationType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
    AnnotationType getAnnotationType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
        return annotationType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
}