jdk/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java
author plevart
Tue, 18 Oct 2016 20:28:58 +0200
changeset 41560 a66e7ee16cf9
parent 37363 329dba26ffd2
child 42338 a60f280f803c
permissions -rw-r--r--
6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
     2
 * Copyright (c) 1997, 2016, 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: 4042
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: 4042
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: 4042
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4042
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4042
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    29
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    30
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    31
import jdk.internal.reflect.Reflection;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    32
import jdk.internal.reflect.ReflectionFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The AccessibleObject class is the base class for Field, Method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Constructor objects.  It provides the ability to flag a reflected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * object as suppressing default Java language access control checks
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    39
 * when it is used. The access checks -- <em>module boundaries</em>,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    40
 * public, default (package) access, protected, and private members --
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    41
 * are performed when Fields, Methods or Constructors are used to set
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    42
 * or get fields, to invoke methods or to create and initialize new
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    43
 * instances of classes, respectively. Unlike access control specified
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    44
 * in the <cite>The Java&trade; Language Specification</cite> and
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    45
 * <cite>The Java Virtual Machine Specification</cite>, access checks
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    46
 * with reflected objects assume {@link Module#canRead readability}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Setting the {@code accessible} flag in a reflected object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * permits sophisticated applications with sufficient privilege, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * as Java Object Serialization or other persistence mechanisms, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * manipulate objects in a manner that would normally be prohibited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
4042
6fc4d5c2a456 6648344: (reflect spec) State default of isAccessible for reflective objects
darcy
parents: 3959
diff changeset
    53
 * <p>By default, a reflected object is <em>not</em> accessible.
6fc4d5c2a456 6648344: (reflect spec) State default of isAccessible for reflective objects
darcy
parents: 3959
diff changeset
    54
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see ReflectPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class AccessibleObject implements AnnotatedElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The Permission object that is used to check whether a client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * has sufficient privilege to defeat Java language access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * control checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 27783
diff changeset
    69
    private static final java.security.Permission ACCESS_PERMISSION =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        new ReflectPermission("suppressAccessChecks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    72
    static void checkPermission() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    73
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    74
        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    75
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Convenience method to set the {@code accessible} flag for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * array of objects with a single security check (for efficiency).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    81
     * <p>This method cannot be used to enable access to an object that is a
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    82
     * {@link Member member} of a class in a different module to the caller and
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    83
     * where the class is in a package that is not exported to the caller's
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    84
     * module. Additionally, this method cannot be used to enable access to
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    85
     * non-public members of {@code AccessibleObject} or {@link Module}.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    86
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    87
     * <p>If there is a security manager, its
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    88
     * {@code checkPermission} method is first called with a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * {@code ReflectPermission("suppressAccessChecks")} permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    91
     * <p>A {@code SecurityException} is also thrown if any of the elements of
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    92
     * the input {@code array} is a {@link java.lang.reflect.Constructor}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    93
     * object for the class {@code java.lang.Class} and {@code flag} is true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param array the array of AccessibleObjects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param flag  the new value for the {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *              in each object
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    98
     * @throws InaccessibleObjectException if access cannot be enabled
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @throws SecurityException if the request is denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @see SecurityManager#checkPermission
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   101
     * @see ReflectPermission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   103
    @CallerSensitive
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   104
    public static void setAccessible(AccessibleObject[] array, boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   105
        checkPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   106
        if (flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   107
            Class<?> caller = Reflection.getCallerClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   108
            array = array.clone();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   109
            for (AccessibleObject ao : array) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   110
                ao.checkCanSetAccessible(caller);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   111
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   112
        }
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 16051
diff changeset
   113
        for (AccessibleObject ao : array) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   114
            ao.setAccessible0(flag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Set the {@code accessible} flag for this object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * the indicated boolean value.  A value of {@code true} indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * the reflected object should suppress Java language access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * checking when it is used.  A value of {@code false} indicates
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   123
     * that the reflected object should enforce Java language access checks
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   124
     * while assuming readability (as noted in the class description).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   126
     * <p>This method cannot be used to enable access to an object that is a
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   127
     * {@link Member member} of a class in a different module to the caller and
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   128
     * where the class is in a package that is not exported to the caller's
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   129
     * module. Additionally, this method cannot be used to enable access to
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   130
     * non-public members of {@code AccessibleObject} or {@link Module}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   132
     * <p>If there is a security manager, its
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   133
     * {@code checkPermission} method is first called with a
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   134
     * {@code ReflectPermission("suppressAccessChecks")} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param flag the new value for the {@code accessible} flag
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   137
     * @throws InaccessibleObjectException if access cannot be enabled
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   138
     * @throws SecurityException if the request is denied
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see SecurityManager#checkPermission
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   140
     * @see ReflectPermission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   142
    public void setAccessible(boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   143
        AccessibleObject.checkPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   144
        setAccessible0(flag);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   145
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   146
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   147
    void setAccessible0(boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   148
        this.override = flag;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   149
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   150
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   151
   /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   152
    * If the given AccessibleObject is a {@code Constructor}, {@code Method}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   153
    * or {@code Field} then checks that its declaring class is in a package
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   154
    * that can be accessed by the given caller of setAccessible.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   155
    */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   156
    void checkCanSetAccessible(Class<?> caller) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   157
        // do nothing, needs to be overridden by Constructor, Method, Field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   160
    void checkCanSetAccessible(Class<?> caller, Class<?> declaringClass) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   161
        Module callerModule = caller.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   162
        Module declaringModule = declaringClass.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   163
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   164
        if (callerModule != declaringModule
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   165
                && callerModule != Object.class.getModule()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   166
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   167
            // check exports to target module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   168
            String pn = packageName(declaringClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   169
            if (!declaringModule.isExported(pn, callerModule)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   170
                String msg = "Unable to make member of "
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   171
                        + declaringClass + " accessible:  "
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   172
                        + declaringModule + " does not export "
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   173
                        + pn + " to " + callerModule;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   174
                Reflection.throwInaccessibleObjectException(msg);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   175
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   176
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   177
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   178
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   179
        if (declaringClass == Module.class
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   180
                || declaringClass == AccessibleObject.class) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   181
            int modifiers;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   182
            if (this instanceof Executable) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   183
                modifiers = ((Executable) this).getModifiers();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   184
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   185
                modifiers = ((Field) this).getModifiers();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   186
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   187
            if (!Modifier.isPublic(modifiers)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   188
                String msg = "Cannot make a non-public member of "
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   189
                        + declaringClass + " accessible";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   190
                Reflection.throwInaccessibleObjectException(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   193
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   194
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   195
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   196
     * Returns the package name of the given class.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   197
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   198
    private static String packageName(Class<?> c) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   199
        while (c.isArray()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   200
            c = c.getComponentType();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   201
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   202
        String pn = c.getPackageName();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   203
        return (pn != null) ? pn : "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Get the value of the {@code accessible} flag for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the value of the object's {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public boolean isAccessible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Constructor: only used by the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    protected AccessibleObject() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    // Indicates whether language-level access checks are overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    // by this object. Initializes to "false". This field is used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    // Field, Method, and Constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    // NOTE: for security purposes, this field must not be visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    // outside this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    boolean override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    // Reflection factory used by subclasses for creating field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // method, and constructor accessors. Note that this is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // very early in the bootstrapping process.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   231
    static final ReflectionFactory reflectionFactory =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   232
        AccessController.doPrivileged(
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
   233
            new ReflectionFactory.GetReflectionFactoryAction());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
16051
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   244
     * {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   245
     * @throws NullPointerException {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   246
     * @since 1.5
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   247
     */
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   248
    @Override
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   249
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   250
        return AnnotatedElement.super.isAnnotationPresent(annotationClass);
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   251
    }
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   252
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   253
   /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws NullPointerException {@inheritDoc}
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   255
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   256
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   257
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   258
    public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   259
        throw new AssertionError("All subclasses should override this method");
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   260
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   261
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   262
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public Annotation[] getAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return getDeclaredAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   270
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   271
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   272
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   273
    @Override
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   274
    public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   275
        // Only annotations on classes are inherited, for all other
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   276
        // objects getDeclaredAnnotation is the same as
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   277
        // getAnnotation.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   278
        return getAnnotation(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   279
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   280
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   281
    /**
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   282
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   283
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   284
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   285
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   286
    public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   287
        // Only annotations on classes are inherited, for all other
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   288
        // objects getDeclaredAnnotationsByType is the same as
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   289
        // getAnnotationsByType.
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   290
        return getAnnotationsByType(annotationClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   291
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   292
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   293
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public Annotation[] getDeclaredAnnotations()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   299
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   300
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   301
    // Shared access checking logic.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   302
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   303
    // For non-public members or members in package-private classes,
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   304
    // it is necessary to perform somewhat expensive security checks.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   305
    // If the security check succeeds for a given class, it will
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   306
    // always succeed (it is not affected by the granting or revoking
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   307
    // of permissions); we speed up the check in the common case by
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   308
    // remembering the last Class for which the check succeeded.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   309
    //
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   310
    // The simple security check for Constructor is to see if
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   311
    // the caller has already been seen, verified, and cached.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   312
    // (See also Class.newInstance(), which uses a similar method.)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   313
    //
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   314
    // A more complicated security check cache is needed for Method and Field
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   315
    // The cache can be either null (empty cache), a 2-array of {caller,targetClass},
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   316
    // or a caller (with targetClass implicitly equal to memberClass).
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   317
    // In the 2-array case, the targetClass is always different from the memberClass.
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   318
    volatile Object securityCheckCache;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   319
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   320
    final void checkAccess(Class<?> caller, Class<?> memberClass,
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   321
                           Class<?> targetClass, int modifiers)
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   322
        throws IllegalAccessException
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   323
    {
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   324
        if (caller == memberClass) {  // quick check
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   325
            return;             // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   326
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   327
        Object cache = securityCheckCache;  // read volatile
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   328
        if (targetClass != null // instance member or constructor
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   329
            && Modifier.isProtected(modifiers)
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   330
            && targetClass != memberClass) {
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   331
            // Must match a 2-list of { caller, targetClass }.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   332
            if (cache instanceof Class[]) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   333
                Class<?>[] cache2 = (Class<?>[]) cache;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   334
                if (cache2[1] == targetClass &&
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   335
                    cache2[0] == caller) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   336
                    return;     // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   337
                }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   338
                // (Test cache[1] first since range check for [1]
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   339
                // subsumes range check for [0].)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   340
            }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   341
        } else if (cache == caller) {
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   342
            // Non-protected case (or targetClass == memberClass or static member).
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   343
            return;             // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   344
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   345
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   346
        // If no return, fall through to the slow path.
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   347
        slowCheckMemberAccess(caller, memberClass, targetClass, modifiers);
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   348
    }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   349
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   350
    // Keep all this slow stuff out of line:
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   351
    void slowCheckMemberAccess(Class<?> caller, Class<?> memberClass,
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   352
                               Class<?> targetClass, int modifiers)
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   353
        throws IllegalAccessException
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   354
    {
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   355
        Reflection.ensureMemberAccess(caller, memberClass, targetClass, modifiers);
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   356
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   357
        // Success: Update the cache.
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   358
        Object cache = (targetClass != null
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   359
                        && Modifier.isProtected(modifiers)
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   360
                        && targetClass != memberClass)
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   361
                        ? new Class<?>[] { caller, targetClass }
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   362
                        : caller;
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   363
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   364
        // Note:  The two cache elements are not volatile,
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   365
        // but they are effectively final.  The Java memory model
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   366
        // guarantees that the initializing stores for the cache
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   367
        // elements will occur before the volatile write.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   368
        securityCheckCache = cache;         // write volatile
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   369
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}