jdk/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java
author alanb
Fri, 16 Dec 2016 06:19:16 +0000
changeset 42703 20c39ea4a507
parent 42338 a60f280f803c
child 43712 5dfd0950317c
permissions -rw-r--r--
8170987: Module system implementation refresh (12/2016) 8170859: Run time and tool support for ModuleResolution Reviewed-by: redestad, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, chris.hegarty@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
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
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
    28
import java.lang.annotation.Annotation;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    30
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    31
import jdk.internal.misc.VM;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    32
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    33
import jdk.internal.reflect.Reflection;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    34
import jdk.internal.reflect.ReflectionFactory;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    35
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The AccessibleObject class is the base class for Field, Method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Constructor objects.  It provides the ability to flag a reflected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * object as suppressing default Java language access control checks
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    41
 * when it is used. The access checks -- <em>module boundaries</em>,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    42
 * public, default (package) access, protected, and private members --
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    43
 * are performed when Fields, Methods or Constructors are used to set
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    44
 * or get fields, to invoke methods or to create and initialize new
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    45
 * instances of classes, respectively. Unlike access control specified
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    46
 * in the <cite>The Java&trade; Language Specification</cite> and
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    47
 * <cite>The Java Virtual Machine Specification</cite>, access checks
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    48
 * with reflected objects assume {@link Module#canRead readability}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>Setting the {@code accessible} flag in a reflected object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * permits sophisticated applications with sufficient privilege, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * as Java Object Serialization or other persistence mechanisms, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * manipulate objects in a manner that would normally be prohibited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
4042
6fc4d5c2a456 6648344: (reflect spec) State default of isAccessible for reflective objects
darcy
parents: 3959
diff changeset
    55
 * <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
    56
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see ReflectPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class AccessibleObject implements AnnotatedElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The Permission object that is used to check whether a client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * has sufficient privilege to defeat Java language access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * control checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 27783
diff changeset
    71
    private static final java.security.Permission ACCESS_PERMISSION =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        new ReflectPermission("suppressAccessChecks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    74
    static void checkPermission() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    75
        SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    76
        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    77
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    78
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Convenience method to set the {@code accessible} flag for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * array of objects with a single security check (for efficiency).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    83
     * <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
    84
     * {@link Member member} of a class in a different module to the caller and
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    85
     * where the class is in a package that is not exported to the caller's
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
    86
     * module. Additionally, if the member is non-public or its declaring
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
    87
     * class is non-public, then this method can only be used to enable access
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
    88
     * if the package is {@link Module#isOpen(String,Module) open} to at least
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
    89
     * the caller's module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    90
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    91
     * <p>If there is a security manager, its
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    92
     * {@code checkPermission} method is first called with a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * {@code ReflectPermission("suppressAccessChecks")} permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    95
     * <p>A {@code SecurityException} is also thrown if any of the elements of
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    96
     * the input {@code array} is a {@link java.lang.reflect.Constructor}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    97
     * object for the class {@code java.lang.Class} and {@code flag} is true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param array the array of AccessibleObjects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param flag  the new value for the {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *              in each object
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   102
     * @throws InaccessibleObjectException if access cannot be enabled
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws SecurityException if the request is denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see SecurityManager#checkPermission
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   105
     * @see ReflectPermission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   107
    @CallerSensitive
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   108
    public static void setAccessible(AccessibleObject[] array, boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   109
        checkPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   110
        if (flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   111
            Class<?> caller = Reflection.getCallerClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   112
            array = array.clone();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   113
            for (AccessibleObject ao : array) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   114
                ao.checkCanSetAccessible(caller);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   115
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   116
        }
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 16051
diff changeset
   117
        for (AccessibleObject ao : array) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   118
            ao.setAccessible0(flag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Set the {@code accessible} flag for this object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the indicated boolean value.  A value of {@code true} indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the reflected object should suppress Java language access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * checking when it is used.  A value of {@code false} indicates
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   127
     * that the reflected object should enforce Java language access checks
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   128
     * while assuming readability (as noted in the class description).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   130
     * <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
   131
     * {@link Member member} of a class in a different module to the caller and
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   132
     * where the class is in a package that is not exported to the caller's
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   133
     * module. Additionally, if the member is non-public or its declaring
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   134
     * class is non-public, then this method can only be used to enable access
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   135
     * if the package is {@link Module#isOpen(String,Module) open} to at least
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   136
     * the caller's module.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   138
     * <p>If there is a security manager, its
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   139
     * {@code checkPermission} method is first called with a
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   140
     * {@code ReflectPermission("suppressAccessChecks")} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param flag the new value for the {@code accessible} flag
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   143
     * @throws InaccessibleObjectException if access cannot be enabled
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   144
     * @throws SecurityException if the request is denied
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see SecurityManager#checkPermission
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   146
     * @see ReflectPermission
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   147
     * @see java.lang.invoke.MethodHandles#privateLookupIn
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   149
    public void setAccessible(boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   150
        AccessibleObject.checkPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   151
        setAccessible0(flag);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   152
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   154
    void setAccessible0(boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   155
        this.override = flag;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   156
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   158
   /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   159
    * If the given AccessibleObject is a {@code Constructor}, {@code Method}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   160
    * or {@code Field} then checks that its declaring class is in a package
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   161
    * that can be accessed by the given caller of setAccessible.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   162
    */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   163
    void checkCanSetAccessible(Class<?> caller) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   164
        // do nothing, needs to be overridden by Constructor, Method, Field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   167
    void checkCanSetAccessible(Class<?> caller, Class<?> declaringClass) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   168
        Module callerModule = caller.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   169
        Module declaringModule = declaringClass.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   170
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   171
        if (callerModule == declaringModule) return;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   172
        if (callerModule == Object.class.getModule()) return;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   173
        if (!declaringModule.isNamed()) return;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   174
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   175
        // package is open to caller
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   176
        String pn = packageName(declaringClass);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   177
        if (declaringModule.isOpen(pn, callerModule)) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   178
            printStackTraceIfOpenedReflectively(declaringModule, pn, callerModule);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   179
            return;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   180
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   181
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   182
        // package is exported to caller and class/member is public
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   183
        boolean isExported = declaringModule.isExported(pn, callerModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   184
        boolean isClassPublic = Modifier.isPublic(declaringClass.getModifiers());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   185
        int modifiers;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   186
        if (this instanceof Executable) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   187
            modifiers = ((Executable) this).getModifiers();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   188
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   189
            modifiers = ((Field) this).getModifiers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   191
        boolean isMemberPublic = Modifier.isPublic(modifiers);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   192
        if (isExported && isClassPublic && isMemberPublic) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   193
            printStackTraceIfExportedReflectively(declaringModule, pn, callerModule);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   194
            return;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   195
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   196
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   197
        // not accessible
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   198
        String msg = "Unable to make ";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   199
        if (this instanceof Field)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   200
            msg += "field ";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   201
        msg += this + " accessible: " + declaringModule + " does not \"";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   202
        if (isClassPublic && isMemberPublic)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   203
            msg += "exports";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   204
        else
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   205
            msg += "opens";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   206
        msg += " " + pn + "\" to " + callerModule;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   207
        InaccessibleObjectException e = new InaccessibleObjectException(msg);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   208
        if (Reflection.printStackTraceWhenAccessFails()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   209
            e.printStackTrace(System.err);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   210
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   211
        throw e;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   212
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   213
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   214
    private void printStackTraceIfOpenedReflectively(Module module,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   215
                                                     String pn,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   216
                                                     Module other) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   217
        printStackTraceIfExposedReflectively(module, pn, other, true);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   218
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   219
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   220
    private void printStackTraceIfExportedReflectively(Module module,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   221
                                                       String pn,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   222
                                                       Module other) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   223
        printStackTraceIfExposedReflectively(module, pn, other, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   224
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   225
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   226
    private void printStackTraceIfExposedReflectively(Module module,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   227
                                                      String pn,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   228
                                                      Module other,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   229
                                                      boolean open)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   230
    {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   231
        if (Reflection.printStackTraceWhenAccessSucceeds()
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   232
            && !module.isStaticallyExportedOrOpen(pn, other, open))
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   233
        {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   234
            String msg = other + " allowed to invoke setAccessible on ";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   235
            if (this instanceof Field)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   236
                msg += "field ";
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   237
            msg += this;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   238
            new Exception(msg) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   239
                private static final long serialVersionUID = 42L;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   240
                public String toString() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   241
                    return "WARNING: " + getMessage();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   242
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   243
            }.printStackTrace(System.err);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   244
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   245
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   246
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   247
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   248
     * Returns the package name of the given class.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   249
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   250
    private static String packageName(Class<?> c) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   251
        while (c.isArray()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   252
            c = c.getComponentType();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   253
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   254
        String pn = c.getPackageName();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   255
        return (pn != null) ? pn : "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Get the value of the {@code accessible} flag for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return the value of the object's {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public boolean isAccessible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Constructor: only used by the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    protected AccessibleObject() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    // Indicates whether language-level access checks are overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    // by this object. Initializes to "false". This field is used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // Field, Method, and Constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // NOTE: for security purposes, this field must not be visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // outside this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    boolean override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    // Reflection factory used by subclasses for creating field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // method, and constructor accessors. Note that this is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    // very early in the bootstrapping process.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   283
    static final ReflectionFactory reflectionFactory =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   284
        AccessController.doPrivileged(
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
   285
            new ReflectionFactory.GetReflectionFactoryAction());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
16051
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   296
     * {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   297
     * @throws NullPointerException {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   298
     * @since 1.5
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   299
     */
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   300
    @Override
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   301
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   302
        return AnnotatedElement.super.isAnnotationPresent(annotationClass);
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   303
    }
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   304
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   305
   /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @throws NullPointerException {@inheritDoc}
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   307
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   308
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   309
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   310
    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
   311
        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
   312
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   313
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   314
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public Annotation[] getAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return getDeclaredAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   322
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   323
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   324
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   325
    @Override
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   326
    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
   327
        // 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
   328
        // objects getDeclaredAnnotation is the same as
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   329
        // getAnnotation.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   330
        return getAnnotation(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   331
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   332
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   333
    /**
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   334
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   335
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   336
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   337
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   338
    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
   339
        // 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
   340
        // objects getDeclaredAnnotationsByType is the same as
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   341
        // getAnnotationsByType.
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   342
        return getAnnotationsByType(annotationClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   343
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   344
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   345
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public Annotation[] getDeclaredAnnotations()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   351
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   352
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   353
    // Shared access checking logic.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   354
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   355
    // 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
   356
    // 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
   357
    // 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
   358
    // 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
   359
    // 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
   360
    // 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
   361
    //
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   362
    // 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
   363
    // 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
   364
    // (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
   365
    //
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   366
    // 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
   367
    // 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
   368
    // 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
   369
    // 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
   370
    volatile Object securityCheckCache;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   371
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   372
    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
   373
                           Class<?> targetClass, int modifiers)
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   374
        throws IllegalAccessException
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   375
    {
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   376
        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
   377
            return;             // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   378
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   379
        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
   380
        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
   381
            && Modifier.isProtected(modifiers)
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   382
            && targetClass != memberClass) {
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   383
            // 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
   384
            if (cache instanceof Class[]) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   385
                Class<?>[] cache2 = (Class<?>[]) cache;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   386
                if (cache2[1] == targetClass &&
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   387
                    cache2[0] == caller) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   388
                    return;     // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   389
                }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   390
                // (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
   391
                // subsumes range check for [0].)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   392
            }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   393
        } 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
   394
            // 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
   395
            return;             // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   396
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   397
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   398
        // 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
   399
        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
   400
    }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   401
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   402
    // 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
   403
    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
   404
                               Class<?> targetClass, int modifiers)
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   405
        throws IllegalAccessException
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   406
    {
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   407
        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
   408
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   409
        // 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
   410
        Object cache = (targetClass != null
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   411
                        && Modifier.isProtected(modifiers)
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   412
                        && targetClass != memberClass)
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   413
                        ? new Class<?>[] { caller, targetClass }
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   414
                        : caller;
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   415
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   416
        // 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
   417
        // 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
   418
        // 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
   419
        // 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
   420
        securityCheckCache = cache;         // write volatile
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   421
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
}