jdk/src/share/classes/sun/reflect/ReflectionFactory.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 2001-2008 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 sun.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/** <P> The master factory for all reflective objects, both those in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    java.lang.reflect (Fields, Methods, Constructors) as well as their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    delegates (FieldAccessors, MethodAccessors, ConstructorAccessors).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    </P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    <P> The methods in this class are extremely unsafe and can cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    subversion of both the language and the verifier. For this reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    they are all instance methods, and access to the constructor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    this factory is guarded by a security check, in similar style to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    {@link sun.misc.Unsafe}. </P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class ReflectionFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static boolean initted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static Permission reflectionFactoryAccessPerm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        = new RuntimePermission("reflectionFactoryAccess");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static ReflectionFactory soleInstance = new ReflectionFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Provides access to package-private mechanisms in java.lang.reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static volatile LangReflectAccess langReflectAccess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // "Inflation" mechanism. Loading bytecodes to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Method.invoke() and Constructor.newInstance() currently costs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // 3-4x more than an invocation via native code for the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // invocation (though subsequent invocations have been benchmarked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // to be over 20x faster). Unfortunately this cost increases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // startup time for certain applications that use reflection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // intensively (but only once per class) to bootstrap themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // To avoid this penalty we reuse the existing JVM entry points
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // for the first few invocations of Methods and Constructors and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // then switch to the bytecode-based implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Package-private to be accessible to NativeMethodAccessorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // and NativeConstructorAccessorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static boolean noInflation        = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static int     inflationThreshold = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private ReflectionFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * A convenience class for acquiring the capability to instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * reflective objects.  Use this instead of a raw call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * #getReflectionFactory} in order to avoid being limited by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * permissions of your callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <p>An instance of this class can be used as the argument of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>AccessController.doPrivileged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final class GetReflectionFactoryAction
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    87
        implements PrivilegedAction<ReflectionFactory> {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    88
        public ReflectionFactory run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return getReflectionFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Provides the caller with the capability to instantiate reflective
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <p> First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <code>checkPermission</code> method is called with a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * java.lang.RuntimePermission} with target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <code>"reflectionFactoryAccess"</code>.  This may result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * security exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <p> The returned <code>ReflectionFactory</code> object should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * carefully guarded by the caller, since it can be used to read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * write private data and invoke private methods, as well as to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * unverified bytecodes.  It must never be passed to untrusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @exception SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *             <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *             access to the RuntimePermission "reflectionFactoryAccess".  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static ReflectionFactory getReflectionFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // TO DO: security.checkReflectionFactoryAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            security.checkPermission(reflectionFactoryAccessPerm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return soleInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    //--------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // Routines used by java.lang.reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /** Called only by java.lang.reflect.Modifier's static initializer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void setLangReflectAccess(LangReflectAccess access) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        langReflectAccess = access;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Note: this routine can cause the declaring class for the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * be initialized and therefore must not be called until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * first get/set of this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param field the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param override true if caller has overridden aaccessibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public FieldAccessor newFieldAccessor(Field field, boolean override) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return UnsafeFieldAccessorFactory.newFieldAccessor(field, override);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public MethodAccessor newMethodAccessor(Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (noInflation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return new MethodAccessorGenerator().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                generateMethod(method.getDeclaringClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                               method.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                               method.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                               method.getReturnType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                               method.getExceptionTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                               method.getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            NativeMethodAccessorImpl acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                new NativeMethodAccessorImpl(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            DelegatingMethodAccessorImpl res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                new DelegatingMethodAccessorImpl(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            acc.setParent(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public ConstructorAccessor newConstructorAccessor(Constructor c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   167
        Class<?> declaringClass = c.getDeclaringClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (Modifier.isAbstract(declaringClass.getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return new InstantiationExceptionConstructorAccessorImpl(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (declaringClass == Class.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return new InstantiationExceptionConstructorAccessorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                ("Can not instantiate java.lang.Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // Bootstrapping issue: since we use Class.newInstance() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // the ConstructorAccessor generation process, we have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // break the cycle here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (Reflection.isSubclassOf(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                    ConstructorAccessorImpl.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return new BootstrapConstructorAccessorImpl(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (noInflation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return new MethodAccessorGenerator().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                generateConstructor(c.getDeclaringClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                    c.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                    c.getExceptionTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                    c.getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            NativeConstructorAccessorImpl acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                new NativeConstructorAccessorImpl(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            DelegatingConstructorAccessorImpl res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                new DelegatingConstructorAccessorImpl(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            acc.setParent(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    //--------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    // Routines used by java.lang
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /** Creates a new java.lang.reflect.Field. Access checks as per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        java.lang.reflect.AccessibleObject are not overridden. */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   207
    public Field newField(Class<?> declaringClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                          String name,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   209
                          Class<?> type,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                          int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                          int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                          String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                          byte[] annotations)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return langReflectAccess().newField(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                            name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                            type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                            modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                            slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                            signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                            annotations);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /** Creates a new java.lang.reflect.Method. Access checks as per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        java.lang.reflect.AccessibleObject are not overridden. */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   226
    public Method newMethod(Class<?> declaringClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            String name,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   228
                            Class<?>[] parameterTypes,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   229
                            Class<?> returnType,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   230
                            Class<?>[] checkedExceptions,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                            byte[] annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                            byte[] parameterAnnotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                            byte[] annotationDefault)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return langReflectAccess().newMethod(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                             name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                             parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                             returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                             checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                             modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                             slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                             signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                             annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                             parameterAnnotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                             annotationDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /** Creates a new java.lang.reflect.Constructor. Access checks as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        per java.lang.reflect.AccessibleObject are not overridden. */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   253
    public Constructor newConstructor(Class<?> declaringClass,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   254
                                      Class<?>[] parameterTypes,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   255
                                      Class<?>[] checkedExceptions,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                      int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                      int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                      String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                      byte[] annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                      byte[] parameterAnnotations)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return langReflectAccess().newConstructor(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                                  parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                                  checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                                  modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                                  slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                                  signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                                  annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                                  parameterAnnotations);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /** Gets the MethodAccessor object for a java.lang.reflect.Method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public MethodAccessor getMethodAccessor(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return langReflectAccess().getMethodAccessor(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /** Sets the MethodAccessor object for a java.lang.reflect.Method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public void setMethodAccessor(Method m, MethodAccessor accessor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        langReflectAccess().setMethodAccessor(m, accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /** Gets the ConstructorAccessor object for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        java.lang.reflect.Constructor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public ConstructorAccessor getConstructorAccessor(Constructor c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return langReflectAccess().getConstructorAccessor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /** Sets the ConstructorAccessor object for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        java.lang.reflect.Constructor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public void setConstructorAccessor(Constructor c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                       ConstructorAccessor accessor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        langReflectAccess().setConstructorAccessor(c, accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /** Makes a copy of the passed method. The returned method is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        "child" of the passed one; see the comments in Method.java for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        details. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public Method copyMethod(Method arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return langReflectAccess().copyMethod(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /** Makes a copy of the passed field. The returned field is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        "child" of the passed one; see the comments in Field.java for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        details. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public Field copyField(Field arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return langReflectAccess().copyField(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /** Makes a copy of the passed constructor. The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        constructor is a "child" of the passed one; see the comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        in Constructor.java for details. */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   313
    public <T> Constructor<T> copyConstructor(Constructor<T> arg) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return langReflectAccess().copyConstructor(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    //--------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    // Routines used by serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public Constructor newConstructorForSerialization
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   324
        (Class<?> classToInstantiate, Constructor constructorToCall)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // Fast path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (constructorToCall.getDeclaringClass() == classToInstantiate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return constructorToCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        ConstructorAccessor acc = new MethodAccessorGenerator().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            generateSerializationConstructor(classToInstantiate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                             constructorToCall.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                             constructorToCall.getExceptionTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                             constructorToCall.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                             constructorToCall.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Constructor c = newConstructor(constructorToCall.getDeclaringClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                       constructorToCall.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                       constructorToCall.getExceptionTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                       constructorToCall.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                       langReflectAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                       getConstructorSlot(constructorToCall),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                       langReflectAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                       getConstructorSignature(constructorToCall),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                       langReflectAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                       getConstructorAnnotations(constructorToCall),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                       langReflectAccess().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                       getConstructorParameterAnnotations(constructorToCall));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        setConstructorAccessor(c, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    //--------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    // Internals only below this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    static int inflationThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return inflationThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /** We have to defer full initialization of this class until after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        the static initializer is run since java.lang.reflect.Method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        static initializer (more properly, that for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        java.lang.reflect.AccessibleObject) causes this class's to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        run, before the system properties are set up. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private static void checkInitted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (initted) return;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   369
        AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   370
            new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   371
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    // Tests to ensure the system properties table is fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    // initialized. This is needed because reflection code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    // called very early in the initialization process (before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    // command-line arguments have been parsed and therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    // these user-settable properties installed.) We assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    // if System.out is non-null then the System class has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    // fully initialized and that the bulk of the startup code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    // has been run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    if (System.out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        // java.lang.System not yet fully initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    String val = System.getProperty("sun.reflect.noInflation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    if (val != null && val.equals("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        noInflation = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    val = System.getProperty("sun.reflect.inflationThreshold");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    if (val != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                            inflationThreshold = Integer.parseInt(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                            throw (RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                new RuntimeException("Unable to parse property sun.reflect.inflationThreshold").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                    initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    initted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private static LangReflectAccess langReflectAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (langReflectAccess == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            // Call a static method to get class java.lang.reflect.Modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            // initialized. Its static initializer will cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            // setLangReflectAccess() to be called from the context of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            // java.lang.reflect package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            Modifier.isPublic(Modifier.PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return langReflectAccess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
}