src/java.base/share/classes/jdk/internal/reflect/ReflectionFactory.java
author smarks
Fri, 22 Jun 2018 17:08:00 -0700
changeset 51158 e2bf86b88863
parent 51156 da85dc1f0162
child 51220 23167d80e0f2
permissions -rw-r--r--
8205491: adjust reflective access checks Reviewed-by: alanb, mchung, igerasim, rhalade, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51158
e2bf86b88863 8205491: adjust reflective access checks
smarks
parents: 51156
diff changeset
     2
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 33674
diff changeset
    26
package jdk.internal.reflect;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    28
import java.io.Externalizable;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    29
import java.io.ObjectInputStream;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    30
import java.io.ObjectOutputStream;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    31
import java.io.ObjectStreamClass;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    32
import java.io.OptionalDataException;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    33
import java.io.Serializable;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    34
import java.lang.invoke.MethodHandle;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    35
import java.lang.invoke.MethodHandles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Field;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 14342
diff changeset
    37
import java.lang.reflect.Executable;
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    38
import java.lang.reflect.InvocationTargetException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.PrivilegedAction;
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    43
import java.util.Objects;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
    44
import java.util.Properties;
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    45
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42997
diff changeset
    46
import jdk.internal.misc.VM;
21672
33014eb0cb79 8028055: (reflect) invoking Method/Constructor in anonymous classes breaks with -Dsun.reflect.noInflation=true
jfranck
parents: 15510
diff changeset
    47
import sun.reflect.misc.ReflectUtil;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
    48
import sun.security.action.GetPropertyAction;
49925
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 47216
diff changeset
    49
import sun.security.util.SecurityConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/** <P> The master factory for all reflective objects, both those in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    java.lang.reflect (Fields, Methods, Constructors) as well as their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    delegates (FieldAccessors, MethodAccessors, ConstructorAccessors).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    </P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    <P> The methods in this class are extremely unsafe and can cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    subversion of both the language and the verifier. For this reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    they are all instance methods, and access to the constructor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    this factory is guarded by a security check, in similar style to
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32268
diff changeset
    60
    {@link jdk.internal.misc.Unsafe}. </P>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class ReflectionFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static boolean initted = false;
27489
cf53b2c02ebb 8062771: Core reflection should use final fields whenever possible
martin
parents: 25859
diff changeset
    66
    private static final ReflectionFactory soleInstance = new ReflectionFactory();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // Provides access to package-private mechanisms in java.lang.reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static volatile LangReflectAccess langReflectAccess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    70
    /* Method for static class initializer <clinit>, or null */
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    71
    private static volatile Method hasStaticInitializerMethod;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // "Inflation" mechanism. Loading bytecodes to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // Method.invoke() and Constructor.newInstance() currently costs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // 3-4x more than an invocation via native code for the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // invocation (though subsequent invocations have been benchmarked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // to be over 20x faster). Unfortunately this cost increases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // startup time for certain applications that use reflection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // intensively (but only once per class) to bootstrap themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // To avoid this penalty we reuse the existing JVM entry points
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // for the first few invocations of Methods and Constructors and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // then switch to the bytecode-based implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Package-private to be accessible to NativeMethodAccessorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // and NativeConstructorAccessorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static boolean noInflation        = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static int     inflationThreshold = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
51156
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
    90
    // true if deserialization constructor checking is disabled
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
    91
    private static boolean disableSerialConstructorChecks = false;
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
    92
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private ReflectionFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * A convenience class for acquiring the capability to instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * reflective objects.  Use this instead of a raw call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * #getReflectionFactory} in order to avoid being limited by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * permissions of your callers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p>An instance of this class can be used as the argument of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>AccessController.doPrivileged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static final class GetReflectionFactoryAction
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   106
        implements PrivilegedAction<ReflectionFactory> {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   107
        public ReflectionFactory run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return getReflectionFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Provides the caller with the capability to instantiate reflective
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p> First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>checkPermission</code> method is called with a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * java.lang.RuntimePermission} with target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>"reflectionFactoryAccess"</code>.  This may result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * security exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <p> The returned <code>ReflectionFactory</code> object should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * carefully guarded by the caller, since it can be used to read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * write private data and invoke private methods, as well as to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * unverified bytecodes.  It must never be passed to untrusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *             <code>checkPermission</code> method doesn't allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *             access to the RuntimePermission "reflectionFactoryAccess".  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static ReflectionFactory getReflectionFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (security != null) {
49925
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 47216
diff changeset
   133
            security.checkPermission(
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 47216
diff changeset
   134
                SecurityConstants.REFLECTION_FACTORY_ACCESS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return soleInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
45027
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   139
    /**
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   140
     * Returns an alternate reflective Method instance for the given method
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   141
     * intended for reflection to invoke, if present.
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   142
     *
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   143
     * A trusted method can define an alternate implementation for a method `foo`
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   144
     * by defining a method named "reflected$foo" that will be invoked
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   145
     * reflectively.
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   146
     */
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   147
    private static Method findMethodForReflection(Method method) {
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   148
        String altName = "reflected$" + method.getName();
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   149
        try {
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   150
           return method.getDeclaringClass()
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   151
                        .getDeclaredMethod(altName, method.getParameterTypes());
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   152
        } catch (NoSuchMethodException ex) {
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   153
            return null;
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   154
        }
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   155
    }
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   156
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    //--------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // Routines used by java.lang.reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /** Called only by java.lang.reflect.Modifier's static initializer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void setLangReflectAccess(LangReflectAccess access) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        langReflectAccess = access;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Note: this routine can cause the declaring class for the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * be initialized and therefore must not be called until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * first get/set of this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param field the field
28056
0cab6eb92852 8065172: More core reflection final and volatile annotations
martin
parents: 27489
diff changeset
   173
     * @param override true if caller has overridden accessibility
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public FieldAccessor newFieldAccessor(Field field, boolean override) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        checkInitted();
50091
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   177
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   178
        Field root = langReflectAccess.getRoot(field);
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   179
        if (root != null) {
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   180
            // FieldAccessor will use the root unless the modifiers have
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   181
            // been overrridden
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   182
            if (root.getModifiers() == field.getModifiers() || !override) {
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   183
                field = root;
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   184
            }
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   185
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return UnsafeFieldAccessorFactory.newFieldAccessor(field, override);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public MethodAccessor newMethodAccessor(Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
45027
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   192
        if (Reflection.isCallerSensitive(method)) {
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   193
            Method altMethod = findMethodForReflection(method);
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   194
            if (altMethod != null) {
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   195
                method = altMethod;
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   196
            }
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   197
        }
0cf367e546fb 8020801: Apply the restriction of invoking MethodHandles.lookup to j.l.r.Method.invoke
mchung
parents: 44359
diff changeset
   198
50091
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   199
        // use the root Method that will not cache caller class
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   200
        Method root = langReflectAccess.getRoot(method);
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   201
        if (root != null) {
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   202
            method = root;
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   203
        }
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   204
21672
33014eb0cb79 8028055: (reflect) invoking Method/Constructor in anonymous classes breaks with -Dsun.reflect.noInflation=true
jfranck
parents: 15510
diff changeset
   205
        if (noInflation && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return new MethodAccessorGenerator().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                generateMethod(method.getDeclaringClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                               method.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                               method.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                               method.getReturnType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                               method.getExceptionTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                               method.getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            NativeMethodAccessorImpl acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                new NativeMethodAccessorImpl(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            DelegatingMethodAccessorImpl res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                new DelegatingMethodAccessorImpl(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            acc.setParent(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   223
    public ConstructorAccessor newConstructorAccessor(Constructor<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        checkInitted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   226
        Class<?> declaringClass = c.getDeclaringClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (Modifier.isAbstract(declaringClass.getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return new InstantiationExceptionConstructorAccessorImpl(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (declaringClass == Class.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return new InstantiationExceptionConstructorAccessorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                ("Can not instantiate java.lang.Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
50091
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   234
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   235
        // use the root Constructor that will not cache caller class
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   236
        Constructor<?> root = langReflectAccess.getRoot(c);
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   237
        if (root != null) {
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   238
            c = root;
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   239
        }
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   240
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // Bootstrapping issue: since we use Class.newInstance() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // the ConstructorAccessor generation process, we have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // break the cycle here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (Reflection.isSubclassOf(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                    ConstructorAccessorImpl.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return new BootstrapConstructorAccessorImpl(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
21672
33014eb0cb79 8028055: (reflect) invoking Method/Constructor in anonymous classes breaks with -Dsun.reflect.noInflation=true
jfranck
parents: 15510
diff changeset
   249
        if (noInflation && !ReflectUtil.isVMAnonymousClass(c.getDeclaringClass())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return new MethodAccessorGenerator().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                generateConstructor(c.getDeclaringClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                    c.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                    c.getExceptionTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                    c.getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            NativeConstructorAccessorImpl acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                new NativeConstructorAccessorImpl(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            DelegatingConstructorAccessorImpl res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                new DelegatingConstructorAccessorImpl(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            acc.setParent(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    //--------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    // Routines used by java.lang
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /** Creates a new java.lang.reflect.Field. Access checks as per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        java.lang.reflect.AccessibleObject are not overridden. */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   273
    public Field newField(Class<?> declaringClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                          String name,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   275
                          Class<?> type,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                          int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                          int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                          String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                          byte[] annotations)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return langReflectAccess().newField(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                            name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                            type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                            modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                            slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                            signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                            annotations);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /** Creates a new java.lang.reflect.Method. Access checks as per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        java.lang.reflect.AccessibleObject are not overridden. */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   292
    public Method newMethod(Class<?> declaringClass,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            String name,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   294
                            Class<?>[] parameterTypes,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   295
                            Class<?> returnType,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   296
                            Class<?>[] checkedExceptions,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            int modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                            int slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            String signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                            byte[] annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            byte[] parameterAnnotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            byte[] annotationDefault)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return langReflectAccess().newMethod(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                             name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                             parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                             returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                             checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                             modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                             slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                             signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                             annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                             parameterAnnotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                             annotationDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /** Creates a new java.lang.reflect.Constructor. Access checks as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        per java.lang.reflect.AccessibleObject are not overridden. */
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   319
    public Constructor<?> newConstructor(Class<?> declaringClass,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   320
                                         Class<?>[] parameterTypes,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   321
                                         Class<?>[] checkedExceptions,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   322
                                         int modifiers,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   323
                                         int slot,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   324
                                         String signature,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   325
                                         byte[] annotations,
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   326
                                         byte[] parameterAnnotations)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return langReflectAccess().newConstructor(declaringClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                                  parameterTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                                  checkedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                                  modifiers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                                  slot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                                  signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                                  annotations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                                  parameterAnnotations);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /** Gets the MethodAccessor object for a java.lang.reflect.Method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public MethodAccessor getMethodAccessor(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return langReflectAccess().getMethodAccessor(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /** Sets the MethodAccessor object for a java.lang.reflect.Method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public void setMethodAccessor(Method m, MethodAccessor accessor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        langReflectAccess().setMethodAccessor(m, accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /** Gets the ConstructorAccessor object for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        java.lang.reflect.Constructor */
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   350
    public ConstructorAccessor getConstructorAccessor(Constructor<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return langReflectAccess().getConstructorAccessor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /** Sets the ConstructorAccessor object for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        java.lang.reflect.Constructor */
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   356
    public void setConstructorAccessor(Constructor<?> c,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                       ConstructorAccessor accessor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        langReflectAccess().setConstructorAccessor(c, accessor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /** Makes a copy of the passed method. The returned method is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        "child" of the passed one; see the comments in Method.java for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        details. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public Method copyMethod(Method arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return langReflectAccess().copyMethod(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
32268
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 29986
diff changeset
   369
    /** Makes a copy of the passed method. The returned method is NOT
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 29986
diff changeset
   370
     * a "child" but a "sibling" of the Method in arg. Should only be
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 29986
diff changeset
   371
     * used on non-root methods. */
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 29986
diff changeset
   372
    public Method leafCopyMethod(Method arg) {
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 29986
diff changeset
   373
        return langReflectAccess().leafCopyMethod(arg);
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 29986
diff changeset
   374
    }
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 29986
diff changeset
   375
9fe7ee60d49d 8073056: Repeating annotations throws java.security.AccessControlException with a SecurityManager
jfranck
parents: 29986
diff changeset
   376
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /** Makes a copy of the passed field. The returned field is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        "child" of the passed one; see the comments in Field.java for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        details. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public Field copyField(Field arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return langReflectAccess().copyField(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /** Makes a copy of the passed constructor. The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        constructor is a "child" of the passed one; see the comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        in Constructor.java for details. */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   387
    public <T> Constructor<T> copyConstructor(Constructor<T> arg) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return langReflectAccess().copyConstructor(arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 14342
diff changeset
   391
    /** Gets the byte[] that encodes TypeAnnotations on an executable.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 14342
diff changeset
   392
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 14342
diff changeset
   393
    public byte[] getExecutableTypeAnnotationBytes(Executable ex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 14342
diff changeset
   394
        return langReflectAccess().getExecutableTypeAnnotationBytes(ex);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 14342
diff changeset
   395
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents: 14342
diff changeset
   396
42997
5f83f2054eca 8172190: Re-apply the fix for bugs 8062389, 8029459, 8061950
plevart
parents: 42948
diff changeset
   397
    public Class<?>[] getExecutableSharedParameterTypes(Executable ex) {
5f83f2054eca 8172190: Re-apply the fix for bugs 8062389, 8029459, 8061950
plevart
parents: 42948
diff changeset
   398
        return langReflectAccess().getExecutableSharedParameterTypes(ex);
5f83f2054eca 8172190: Re-apply the fix for bugs 8062389, 8029459, 8061950
plevart
parents: 42948
diff changeset
   399
    }
5f83f2054eca 8172190: Re-apply the fix for bugs 8062389, 8029459, 8061950
plevart
parents: 42948
diff changeset
   400
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    //--------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    // Routines used by serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   407
    public final Constructor<?> newConstructorForExternalization(Class<?> cl) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   408
        if (!Externalizable.class.isAssignableFrom(cl)) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   409
            return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   410
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   411
        try {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   412
            Constructor<?> cons = cl.getConstructor();
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   413
            cons.setAccessible(true);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   414
            return cons;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   415
        } catch (NoSuchMethodException ex) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   416
            return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   417
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   418
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   419
41827
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   420
    public final Constructor<?> newConstructorForSerialization(Class<?> cl,
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   421
                                                               Constructor<?> constructorToCall)
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   422
    {
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   423
        if (constructorToCall.getDeclaringClass() == cl) {
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   424
            constructorToCall.setAccessible(true);
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   425
            return constructorToCall;
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   426
        }
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   427
        return generateConstructor(cl, constructorToCall);
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   428
    }
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   429
51156
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   430
    /**
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   431
     * Given a class, determines whether its superclass has
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   432
     * any constructors that are accessible from the class.
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   433
     * This is a special purpose method intended to do access
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   434
     * checking for a serializable class and its superclasses
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   435
     * up to, but not including, the first non-serializable
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   436
     * superclass. This also implies that the superclass is
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   437
     * always non-null, because a serializable class must be a
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   438
     * class (not an interface) and Object is not serializable.
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   439
     *
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   440
     * @param cl the class from which access is checked
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   441
     * @return whether the superclass has a constructor accessible from cl
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   442
     */
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   443
    private boolean superHasAccessibleConstructor(Class<?> cl) {
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   444
        Class<?> superCl = cl.getSuperclass();
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   445
        assert Serializable.class.isAssignableFrom(cl);
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   446
        assert superCl != null;
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   447
        if (packageEquals(cl, superCl)) {
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   448
            // accessible if any non-private constructor is found
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   449
            for (Constructor<?> ctor : superCl.getDeclaredConstructors()) {
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   450
                if ((ctor.getModifiers() & Modifier.PRIVATE) == 0) {
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   451
                    return true;
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   452
                }
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   453
            }
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   454
            return false;
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   455
        } else {
51158
e2bf86b88863 8205491: adjust reflective access checks
smarks
parents: 51156
diff changeset
   456
            // sanity check to ensure the parent is protected or public
e2bf86b88863 8205491: adjust reflective access checks
smarks
parents: 51156
diff changeset
   457
            if ((superCl.getModifiers() & (Modifier.PROTECTED | Modifier.PUBLIC)) == 0) {
51156
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   458
                return false;
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   459
            }
51158
e2bf86b88863 8205491: adjust reflective access checks
smarks
parents: 51156
diff changeset
   460
            // accessible if any constructor is protected or public
51156
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   461
            for (Constructor<?> ctor : superCl.getDeclaredConstructors()) {
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   462
                if ((ctor.getModifiers() & (Modifier.PROTECTED | Modifier.PUBLIC)) != 0) {
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   463
                    return true;
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   464
                }
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   465
            }
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   466
            return false;
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   467
        }
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   468
    }
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   469
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   470
    /**
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   471
     * Returns a constructor that allocates an instance of cl and that then initializes
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   472
     * the instance by calling the no-arg constructor of its first non-serializable
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   473
     * superclass. This is specified in the Serialization Specification, section 3.1,
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   474
     * in step 11 of the deserialization process. If cl is not serializable, returns
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   475
     * cl's no-arg constructor. If no accessible constructor is found, or if the
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   476
     * class hierarchy is somehow malformed (e.g., a serializable class has no
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   477
     * superclass), null is returned.
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   478
     *
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   479
     * @param cl the class for which a constructor is to be found
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   480
     * @return the generated constructor, or null if none is available
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   481
     */
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   482
    public final Constructor<?> newConstructorForSerialization(Class<?> cl) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   483
        Class<?> initCl = cl;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   484
        while (Serializable.class.isAssignableFrom(initCl)) {
51156
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   485
            Class<?> prev = initCl;
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   486
            if ((initCl = initCl.getSuperclass()) == null ||
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   487
                (!disableSerialConstructorChecks && !superHasAccessibleConstructor(prev))) {
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   488
                return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   489
            }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   490
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   491
        Constructor<?> constructorToCall;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   492
        try {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   493
            constructorToCall = initCl.getDeclaredConstructor();
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   494
            int mods = constructorToCall.getModifiers();
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   495
            if ((mods & Modifier.PRIVATE) != 0 ||
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   496
                    ((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0 &&
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   497
                            !packageEquals(cl, initCl))) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   498
                return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   499
            }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   500
        } catch (NoSuchMethodException ex) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   501
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
41827
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   503
        return generateConstructor(cl, constructorToCall);
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   504
    }
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   505
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   506
    private final Constructor<?> generateConstructor(Class<?> cl,
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   507
                                                     Constructor<?> constructorToCall) {
710feffff038 8168980: Reinstate sun.reflect.ReflectionFactory.newConstructorForSerialization(Class,Constructor)
chegar
parents: 41608
diff changeset
   508
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        ConstructorAccessor acc = new MethodAccessorGenerator().
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   511
            generateSerializationConstructor(cl,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                             constructorToCall.getParameterTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                             constructorToCall.getExceptionTypes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                             constructorToCall.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                             constructorToCall.getDeclaringClass());
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   516
        Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   517
                                          constructorToCall.getParameterTypes(),
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   518
                                          constructorToCall.getExceptionTypes(),
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   519
                                          constructorToCall.getModifiers(),
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   520
                                          langReflectAccess().
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   521
                                          getConstructorSlot(constructorToCall),
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   522
                                          langReflectAccess().
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   523
                                          getConstructorSignature(constructorToCall),
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   524
                                          langReflectAccess().
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   525
                                          getConstructorAnnotations(constructorToCall),
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   526
                                          langReflectAccess().
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   527
                                          getConstructorParameterAnnotations(constructorToCall));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        setConstructorAccessor(c, acc);
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   529
        c.setAccessible(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   533
    public final MethodHandle readObjectForSerialization(Class<?> cl) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   534
        return findReadWriteObjectForSerialization(cl, "readObject", ObjectInputStream.class);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   535
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   536
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   537
    public final MethodHandle readObjectNoDataForSerialization(Class<?> cl) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   538
        return findReadWriteObjectForSerialization(cl, "readObjectNoData", ObjectInputStream.class);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   539
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   540
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   541
    public final MethodHandle writeObjectForSerialization(Class<?> cl) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   542
        return findReadWriteObjectForSerialization(cl, "writeObject", ObjectOutputStream.class);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   543
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   544
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   545
    private final MethodHandle findReadWriteObjectForSerialization(Class<?> cl,
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   546
                                                                   String methodName,
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   547
                                                                   Class<?> streamClass) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   548
        if (!Serializable.class.isAssignableFrom(cl)) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   549
            return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   550
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   551
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   552
        try {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   553
            Method meth = cl.getDeclaredMethod(methodName, streamClass);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   554
            int mods = meth.getModifiers();
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   555
            if (meth.getReturnType() != Void.TYPE ||
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   556
                    Modifier.isStatic(mods) ||
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   557
                    !Modifier.isPrivate(mods)) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   558
                return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   559
            }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   560
            meth.setAccessible(true);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   561
            return MethodHandles.lookup().unreflect(meth);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   562
        } catch (NoSuchMethodException ex) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   563
            return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   564
        } catch (IllegalAccessException ex1) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   565
            throw new InternalError("Error", ex1);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   566
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   567
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   568
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   569
    /**
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   570
     * Returns a MethodHandle for {@code writeReplace} on the serializable class
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   571
     * or null if no match found.
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   572
     * @param cl a serializable class
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   573
     * @returnss the {@code writeReplace} MethodHandle or {@code null} if not found
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   574
     */
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   575
    public final MethodHandle writeReplaceForSerialization(Class<?> cl) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   576
        return getReplaceResolveForSerialization(cl, "writeReplace");
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   577
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   578
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   579
    /**
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   580
     * Returns a MethodHandle for {@code readResolve} on the serializable class
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   581
     * or null if no match found.
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   582
     * @param cl a serializable class
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   583
     * @returns the {@code writeReplace} MethodHandle or {@code null} if not found
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   584
     */
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   585
    public final MethodHandle readResolveForSerialization(Class<?> cl) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   586
        return getReplaceResolveForSerialization(cl, "readResolve");
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   587
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   588
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   589
    /**
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   590
     * Lookup readResolve or writeReplace on a class with specified
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   591
     * signature constraints.
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   592
     * @param cl a serializable class
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   593
     * @param methodName the method name to find
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   594
     * @returns a MethodHandle for the method or {@code null} if not found or
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   595
     *       has the wrong signature.
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   596
     */
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   597
    private MethodHandle getReplaceResolveForSerialization(Class<?> cl,
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   598
                                                           String methodName) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   599
        if (!Serializable.class.isAssignableFrom(cl)) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   600
            return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   601
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   602
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   603
        Class<?> defCl = cl;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   604
        while (defCl != null) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   605
            try {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   606
                Method m = defCl.getDeclaredMethod(methodName);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   607
                if (m.getReturnType() != Object.class) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   608
                    return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   609
                }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   610
                int mods = m.getModifiers();
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   611
                if (Modifier.isStatic(mods) | Modifier.isAbstract(mods)) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   612
                    return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   613
                } else if (Modifier.isPublic(mods) | Modifier.isProtected(mods)) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   614
                    // fall through
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   615
                } else if (Modifier.isPrivate(mods) && (cl != defCl)) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   616
                    return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   617
                } else if (!packageEquals(cl, defCl)) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   618
                    return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   619
                }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   620
                try {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   621
                    // Normal return
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   622
                    m.setAccessible(true);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   623
                    return MethodHandles.lookup().unreflect(m);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   624
                } catch (IllegalAccessException ex0) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   625
                    // setAccessible should prevent IAE
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   626
                    throw new InternalError("Error", ex0);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   627
                }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   628
            } catch (NoSuchMethodException ex) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   629
                defCl = defCl.getSuperclass();
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   630
            }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   631
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   632
        return null;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   633
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   634
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   635
    /**
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   636
     * Returns true if the given class defines a static initializer method,
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   637
     * false otherwise.
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   638
     */
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   639
    public final boolean hasStaticInitializerForSerialization(Class<?> cl) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   640
        Method m = hasStaticInitializerMethod;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   641
        if (m == null) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   642
            try {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   643
                m = ObjectStreamClass.class.getDeclaredMethod("hasStaticInitializer",
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   644
                        new Class<?>[]{Class.class});
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   645
                m.setAccessible(true);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   646
                hasStaticInitializerMethod = m;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   647
            } catch (NoSuchMethodException ex) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   648
                throw new InternalError("No such method hasStaticInitializer on "
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   649
                        + ObjectStreamClass.class, ex);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   650
            }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   651
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   652
        try {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   653
            return (Boolean) m.invoke(null, cl);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   654
        } catch (InvocationTargetException | IllegalAccessException ex) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   655
            throw new InternalError("Exception invoking hasStaticInitializer", ex);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   656
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   657
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   658
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   659
    /**
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   660
     * Return the accessible constructor for OptionalDataException signaling eof.
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   661
     * @returns the eof constructor for OptionalDataException
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   662
     */
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   663
    public final Constructor<OptionalDataException> newOptionalDataExceptionForSerialization() {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   664
        try {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   665
            Constructor<OptionalDataException> boolCtor =
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   666
                    OptionalDataException.class.getDeclaredConstructor(Boolean.TYPE);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   667
            boolCtor.setAccessible(true);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   668
            return boolCtor;
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   669
        } catch (NoSuchMethodException ex) {
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   670
            throw new InternalError("Constructor not found", ex);
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   671
        }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   672
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   673
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    //--------------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    // Internals only below this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    static int inflationThreshold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return inflationThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /** We have to defer full initialization of this class until after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        the static initializer is run since java.lang.reflect.Method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        static initializer (more properly, that for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        java.lang.reflect.AccessibleObject) causes this class's to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        run, before the system properties are set up. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    private static void checkInitted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (initted) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42997
diff changeset
   691
        // Defer initialization until module system is initialized so as
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42997
diff changeset
   692
        // to avoid inflation and spinning bytecode in unnamed modules
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42997
diff changeset
   693
        // during early startup.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 42997
diff changeset
   694
        if (!VM.isModuleSystemInited()) {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   695
            return;
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   696
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   698
        Properties props = GetPropertyAction.privilegedGetProperties();
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   699
        String val = props.getProperty("sun.reflect.noInflation");
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   700
        if (val != null && val.equals("true")) {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   701
            noInflation = true;
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   702
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   704
        val = props.getProperty("sun.reflect.inflationThreshold");
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   705
        if (val != null) {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   706
            try {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   707
                inflationThreshold = Integer.parseInt(val);
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   708
            } catch (NumberFormatException e) {
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   709
                throw new RuntimeException("Unable to parse property sun.reflect.inflationThreshold", e);
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   710
            }
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   711
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
51156
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   713
        disableSerialConstructorChecks =
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   714
            "true".equals(props.getProperty("jdk.disableSerialConstructorChecks"));
da85dc1f0162 8197925: Better stack walking
smarks
parents: 50783
diff changeset
   715
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37363
diff changeset
   716
        initted = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    private static LangReflectAccess langReflectAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (langReflectAccess == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            // Call a static method to get class java.lang.reflect.Modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            // initialized. Its static initializer will cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            // setLangReflectAccess() to be called from the context of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            // java.lang.reflect package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            Modifier.isPublic(Modifier.PUBLIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return langReflectAccess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   729
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   730
    /**
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   731
     * Returns true if classes are defined in the classloader and same package, false
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   732
     * otherwise.
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   733
     * @param cl1 a class
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   734
     * @param cl2 another class
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   735
     * @returns true if the two classes are in the same classloader and package
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   736
     */
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   737
    private static boolean packageEquals(Class<?> cl1, Class<?> cl2) {
50783
0ed32e0d98e1 8205623: Replace use of Class::getPackage with Class::getPackageName
mchung
parents: 50091
diff changeset
   738
        assert !cl1.isArray() && !cl2.isArray();
0ed32e0d98e1 8205623: Replace use of Class::getPackage with Class::getPackageName
mchung
parents: 50091
diff changeset
   739
0ed32e0d98e1 8205623: Replace use of Class::getPackage with Class::getPackageName
mchung
parents: 50091
diff changeset
   740
        if (cl1 == cl2) {
0ed32e0d98e1 8205623: Replace use of Class::getPackage with Class::getPackageName
mchung
parents: 50091
diff changeset
   741
            return true;
0ed32e0d98e1 8205623: Replace use of Class::getPackage with Class::getPackageName
mchung
parents: 50091
diff changeset
   742
        }
0ed32e0d98e1 8205623: Replace use of Class::getPackage with Class::getPackageName
mchung
parents: 50091
diff changeset
   743
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   744
        return cl1.getClassLoader() == cl2.getClassLoader() &&
50783
0ed32e0d98e1 8205623: Replace use of Class::getPackage with Class::getPackageName
mchung
parents: 50091
diff changeset
   745
                Objects.equals(cl1.getPackageName(), cl2.getPackageName());
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   746
    }
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 37781
diff changeset
   747
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
}