src/java.base/share/classes/java/lang/reflect/AccessibleObject.java
author dholmes
Sat, 23 Jun 2018 01:32:41 -0400
changeset 50735 2f2af62dfac7
parent 50091 05979f6ba560
child 52020 4c247dde38ed
permissions -rw-r--r--
8010319: Implementation of JEP 181: Nest-Based Access Control Reviewed-by: alanb, psandoz, mchung, coleenp, acorn, mcimadamore, forax, jlahoda, sspitsyn, abuckley Contributed-by: alex.buckley@oracle.com, maurizio.mimadamore@oracle.com, mandy.chung@oracle.com, tobias.hartmann@oracle.com, david.holmes@oracle.com, vladimir.x.ivanov@oracle.com, karen.kinnear@oracle.com, vladimir.kozlov@oracle.com, john.r.rose@oracle.com, daniel.smith@oracle.com, serguei.spitsyn@oracle.com, kumardotsrinivasan@gmail.com, boris.ulasevich@bell-sw.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48948
514c88e62cd8 8196830: publicLookup().findVirtual should not return method handle to AccessibleObject.setAccessible
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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;
48948
514c88e62cd8 8196830: publicLookup().findVirtual should not return method handle to AccessibleObject.setAccessible
alanb
parents: 47216
diff changeset
    29
import java.lang.invoke.MethodHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    31
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    32
import jdk.internal.misc.VM;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    33
import jdk.internal.module.IllegalAccessLogger;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    34
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    35
import jdk.internal.reflect.Reflection;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    36
import jdk.internal.reflect.ReflectionFactory;
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
    37
import sun.security.action.GetPropertyAction;
49925
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 48948
diff changeset
    38
import sun.security.util.SecurityConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    41
 * The {@code AccessibleObject} class is the base class for {@code Field},
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    42
 * {@code Method}, and {@code Constructor} objects (known as <em>reflected
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    43
 * objects</em>). It provides the ability to flag a reflected object as
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    44
 * suppressing checks for Java language access control when it is used. This
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    45
 * permits sophisticated applications with sufficient privilege, such as Java
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    46
 * Object Serialization or other persistence mechanisms, to manipulate objects
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    47
 * in a manner that would normally be prohibited.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    49
 * <p> Java language access control prevents use of private members outside
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 50091
diff changeset
    50
 * their top-level class; package access members outside their package; protected members
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    51
 * outside their package or subclasses; and public members outside their
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    52
 * module unless they are declared in an {@link Module#isExported(String,Module)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    53
 * exported} package and the user {@link Module#canRead reads} their module. By
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    54
 * default, Java language access control is enforced (with one variation) when
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    55
 * {@code Field}s, {@code Method}s, or {@code Constructor}s are used to get or
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    56
 * set fields, to invoke methods, or to create and initialize new instances of
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    57
 * classes, respectively. Every reflected object checks that the code using it
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    58
 * is in an appropriate class, package, or module. </p>
4042
6fc4d5c2a456 6648344: (reflect spec) State default of isAccessible for reflective objects
darcy
parents: 3959
diff changeset
    59
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    60
 * <p> The one variation from Java language access control is that the checks
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    61
 * by reflected objects assume readability. That is, the module containing
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    62
 * the use of a reflected object is assumed to read the module in which
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    63
 * the underlying field, method, or constructor is declared. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    65
 * <p> Whether the checks for Java language access control can be suppressed
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    66
 * (and thus, whether access can be enabled) depends on whether the reflected
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    67
 * object corresponds to a member in an exported or open package
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    68
 * (see {@link #setAccessible(boolean)}). </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    69
 *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    70
 * @jls 6.6 Access Control
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @since 1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    72
 * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    73
 * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public class AccessibleObject implements AnnotatedElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    77
    static void checkPermission() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    78
        SecurityManager sm = System.getSecurityManager();
49925
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 48948
diff changeset
    79
        if (sm != null) {
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 48948
diff changeset
    80
            // SecurityConstants.ACCESS_PERMISSION is used to check
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 48948
diff changeset
    81
            // whether a client has sufficient privilege to defeat Java
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 48948
diff changeset
    82
            // language access control checks.
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 48948
diff changeset
    83
            sm.checkPermission(SecurityConstants.ACCESS_PERMISSION);
3deb300f0e55 8202419: Avoid creating Permission constants early
redestad
parents: 48948
diff changeset
    84
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    85
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Convenience method to set the {@code accessible} flag for an
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    89
     * array of reflected objects with a single security check (for efficiency).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    91
     * <p> This method may be used to enable access to all reflected objects in
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    92
     * the array when access to each reflected object can be enabled as
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    93
     * specified by {@link #setAccessible(boolean) setAccessible(boolean)}. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    94
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    95
     * <p>If there is a security manager, its
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    96
     * {@code checkPermission} method is first called with a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * {@code ReflectPermission("suppressAccessChecks")} permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
    99
     * <p>A {@code SecurityException} is also thrown if any of the elements of
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   100
     * the input {@code array} is a {@link java.lang.reflect.Constructor}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   101
     * object for the class {@code java.lang.Class} and {@code flag} is true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param array the array of AccessibleObjects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param flag  the new value for the {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *              in each object
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   106
     * @throws InaccessibleObjectException if access cannot be enabled for all
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   107
     *         objects in the array
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   108
     * @throws SecurityException if the request is denied by the security manager
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   109
     *         or an element in the array is a constructor for {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   110
     *         java.lang.Class}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see SecurityManager#checkPermission
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   112
     * @see ReflectPermission
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   113
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   114
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   116
    @CallerSensitive
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   117
    public static void setAccessible(AccessibleObject[] array, boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   118
        checkPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   119
        if (flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   120
            Class<?> caller = Reflection.getCallerClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   121
            array = array.clone();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   122
            for (AccessibleObject ao : array) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   123
                ao.checkCanSetAccessible(caller);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   124
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   125
        }
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 16051
diff changeset
   126
        for (AccessibleObject ao : array) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   127
            ao.setAccessible0(flag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   132
     * Set the {@code accessible} flag for this reflected object to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * the indicated boolean value.  A value of {@code true} indicates that
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   134
     * the reflected object should suppress checks for Java language access
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   135
     * control when it is used. A value of {@code false} indicates that
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   136
     * the reflected object should enforce checks for Java language access
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   137
     * control when it is used, with the variation noted in the class description.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   138
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   139
     * <p> This method may be used by a caller in class {@code C} to enable
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   140
     * access to a {@link Member member} of {@link Member#getDeclaringClass()
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   141
     * declaring class} {@code D} if any of the following hold: </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   142
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   143
     * <ul>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   144
     *     <li> {@code C} and {@code D} are in the same module. </li>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   145
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   146
     *     <li> The member is {@code public} and {@code D} is {@code public} in
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   147
     *     a package that the module containing {@code D} {@link
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   148
     *     Module#isExported(String,Module) exports} to at least the module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   149
     *     containing {@code C}. </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   151
     *     <li> The member is {@code protected} {@code static}, {@code D} is
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   152
     *     {@code public} in a package that the module containing {@code D}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   153
     *     exports to at least the module containing {@code C}, and {@code C}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   154
     *     is a subclass of {@code D}. </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   156
     *     <li> {@code D} is in a package that the module containing {@code D}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   157
     *     {@link Module#isOpen(String,Module) opens} to at least the module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   158
     *     containing {@code C}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   159
     *     All packages in unnamed and open modules are open to all modules and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   160
     *     so this method always succeeds when {@code D} is in an unnamed or
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   161
     *     open module. </li>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   162
     * </ul>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   163
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   164
     * <p> This method cannot be used to enable access to private members,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   165
     * members with default (package) access, protected instance members, or
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   166
     * protected constructors when the declaring class is in a different module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   167
     * to the caller and the package containing the declaring class is not open
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   168
     * to the caller's module. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   169
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   170
     * <p> If there is a security manager, its
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   171
     * {@code checkPermission} method is first called with a
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   172
     * {@code ReflectPermission("suppressAccessChecks")} permission.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param flag the new value for the {@code accessible} flag
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   175
     * @throws InaccessibleObjectException if access cannot be enabled
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   176
     * @throws SecurityException if the request is denied by the security manager
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   177
     * @see #trySetAccessible
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   178
     * @see java.lang.invoke.MethodHandles#privateLookupIn
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   179
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   180
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
48948
514c88e62cd8 8196830: publicLookup().findVirtual should not return method handle to AccessibleObject.setAccessible
alanb
parents: 47216
diff changeset
   182
    @CallerSensitive   // overrides in Method/Field/Constructor are @CS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   183
    public void setAccessible(boolean flag) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   184
        AccessibleObject.checkPermission();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   185
        setAccessible0(flag);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   186
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   187
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   188
    /**
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   189
     * Sets the accessible flag and returns the new value
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   190
     */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   191
    boolean setAccessible0(boolean flag) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   192
        this.override = flag;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   193
        return flag;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   194
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   195
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   196
    /**
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   197
     * Set the {@code accessible} flag for this reflected object to {@code true}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   198
     * if possible. This method sets the {@code accessible} flag, as if by
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   199
     * invoking {@link #setAccessible(boolean) setAccessible(true)}, and returns
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   200
     * the possibly-updated value for the {@code accessible} flag. If access
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   201
     * cannot be enabled, i.e. the checks or Java language access control cannot
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   202
     * be suppressed, this method returns {@code false} (as opposed to {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   203
     * setAccessible(true)} throwing {@code InaccessibleObjectException} when
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   204
     * it fails).
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   205
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   206
     * <p> This method is a no-op if the {@code accessible} flag for
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   207
     * this reflected object is {@code true}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   208
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   209
     * <p> For example, a caller can invoke {@code trySetAccessible}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   210
     * on a {@code Method} object for a private instance method
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   211
     * {@code p.T::privateMethod} to suppress the checks for Java language access
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   212
     * control when the {@code Method} is invoked.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   213
     * If {@code p.T} class is in a different module to the caller and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   214
     * package {@code p} is open to at least the caller's module,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   215
     * the code below successfully sets the {@code accessible} flag
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   216
     * to {@code true}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   217
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   218
     * <pre>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   219
     * {@code
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   220
     *     p.T obj = ....;  // instance of p.T
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   221
     *     :
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   222
     *     Method m = p.T.class.getDeclaredMethod("privateMethod");
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   223
     *     if (m.trySetAccessible()) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   224
     *         m.invoke(obj);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   225
     *     } else {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   226
     *         // package p is not opened to the caller to access private member of T
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   227
     *         ...
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   228
     *     }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   229
     * }</pre>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   230
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   231
     * <p> If there is a security manager, its {@code checkPermission} method
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   232
     * is first called with a {@code ReflectPermission("suppressAccessChecks")}
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   233
     * permission. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   234
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   235
     * @return {@code true} if the {@code accessible} flag is set to {@code true};
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   236
     *         {@code false} if access cannot be enabled.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   237
     * @throws SecurityException if the request is denied by the security manager
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   238
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   239
     * @since 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   240
     * @spec JPMS
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   241
     * @see java.lang.invoke.MethodHandles#privateLookupIn
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   242
     */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   243
    @CallerSensitive
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   244
    public final boolean trySetAccessible() {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   245
        AccessibleObject.checkPermission();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   246
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   247
        if (override == true) return true;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   248
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   249
        // if it's not a Constructor, Method, Field then no access check
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   250
        if (!Member.class.isInstance(this)) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   251
            return setAccessible0(true);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   252
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   253
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   254
        // does not allow to suppress access check for Class's constructor
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   255
        Class<?> declaringClass = ((Member) this).getDeclaringClass();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   256
        if (declaringClass == Class.class && this instanceof Constructor) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   257
            return false;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   258
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   259
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   260
        if (checkCanSetAccessible(Reflection.getCallerClass(),
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   261
                                  declaringClass,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   262
                                  false)) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   263
            return setAccessible0(true);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   264
        } else {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   265
            return false;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   266
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   267
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   268
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   269
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   270
   /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   271
    * If the given AccessibleObject is a {@code Constructor}, {@code Method}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   272
    * or {@code Field} then checks that its declaring class is in a package
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   273
    * that can be accessed by the given caller of setAccessible.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   274
    */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   275
    void checkCanSetAccessible(Class<?> caller) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   276
        // do nothing, needs to be overridden by Constructor, Method, Field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
48948
514c88e62cd8 8196830: publicLookup().findVirtual should not return method handle to AccessibleObject.setAccessible
alanb
parents: 47216
diff changeset
   279
    final void checkCanSetAccessible(Class<?> caller, Class<?> declaringClass) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   280
        checkCanSetAccessible(caller, declaringClass, true);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   281
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   282
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   283
    private boolean checkCanSetAccessible(Class<?> caller,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   284
                                          Class<?> declaringClass,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   285
                                          boolean throwExceptionIfDenied) {
48948
514c88e62cd8 8196830: publicLookup().findVirtual should not return method handle to AccessibleObject.setAccessible
alanb
parents: 47216
diff changeset
   286
        if (caller == MethodHandle.class) {
514c88e62cd8 8196830: publicLookup().findVirtual should not return method handle to AccessibleObject.setAccessible
alanb
parents: 47216
diff changeset
   287
            throw new IllegalCallerException();   // should not happen
514c88e62cd8 8196830: publicLookup().findVirtual should not return method handle to AccessibleObject.setAccessible
alanb
parents: 47216
diff changeset
   288
        }
514c88e62cd8 8196830: publicLookup().findVirtual should not return method handle to AccessibleObject.setAccessible
alanb
parents: 47216
diff changeset
   289
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   290
        Module callerModule = caller.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   291
        Module declaringModule = declaringClass.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   292
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   293
        if (callerModule == declaringModule) return true;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   294
        if (callerModule == Object.class.getModule()) return true;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   295
        if (!declaringModule.isNamed()) return true;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   296
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   297
        String pn = declaringClass.getPackageName();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   298
        int modifiers;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   299
        if (this instanceof Executable) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   300
            modifiers = ((Executable) this).getModifiers();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   301
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   302
            modifiers = ((Field) this).getModifiers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   304
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   305
        // class is public and package is exported to caller
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   306
        boolean isClassPublic = Modifier.isPublic(declaringClass.getModifiers());
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   307
        if (isClassPublic && declaringModule.isExported(pn, callerModule)) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   308
            // member is public
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   309
            if (Modifier.isPublic(modifiers)) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 44359
diff changeset
   310
                logIfExportedForIllegalAccess(caller, declaringClass);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   311
                return true;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   312
            }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   313
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   314
            // member is protected-static
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   315
            if (Modifier.isProtected(modifiers)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   316
                && Modifier.isStatic(modifiers)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   317
                && isSubclassOf(caller, declaringClass)) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 44359
diff changeset
   318
                logIfExportedForIllegalAccess(caller, declaringClass);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   319
                return true;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   320
            }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   321
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41560
diff changeset
   322
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   323
        // package is open to caller
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   324
        if (declaringModule.isOpen(pn, callerModule)) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 44359
diff changeset
   325
            logIfOpenedForIllegalAccess(caller, declaringClass);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   326
            return true;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   327
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   328
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   329
        if (throwExceptionIfDenied) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   330
            // not accessible
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   331
            String msg = "Unable to make ";
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   332
            if (this instanceof Field)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   333
                msg += "field ";
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   334
            msg += this + " accessible: " + declaringModule + " does not \"";
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   335
            if (isClassPublic && Modifier.isPublic(modifiers))
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   336
                msg += "exports";
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   337
            else
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   338
                msg += "opens";
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   339
            msg += " " + pn + "\" to " + callerModule;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   340
            InaccessibleObjectException e = new InaccessibleObjectException(msg);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   341
            if (printStackTraceWhenAccessFails()) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   342
                e.printStackTrace(System.err);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   343
            }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   344
            throw e;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   345
        }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   346
        return false;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   347
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   348
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   349
    private boolean isSubclassOf(Class<?> queryClass, Class<?> ofClass) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   350
        while (queryClass != null) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   351
            if (queryClass == ofClass) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   352
                return true;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   353
            }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   354
            queryClass = queryClass.getSuperclass();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   355
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   356
        return false;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   357
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   358
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 44359
diff changeset
   359
    private void logIfOpenedForIllegalAccess(Class<?> caller, Class<?> declaringClass) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   360
        Module callerModule = caller.getModule();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   361
        Module targetModule = declaringClass.getModule();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   362
        // callerModule is null during early startup
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   363
        if (callerModule != null && !callerModule.isNamed() && targetModule.isNamed()) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   364
            IllegalAccessLogger logger = IllegalAccessLogger.illegalAccessLogger();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   365
            if (logger != null) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 44359
diff changeset
   366
                logger.logIfOpenedForIllegalAccess(caller, declaringClass, this::toShortString);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   367
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   368
        }
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   369
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   370
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 44359
diff changeset
   371
    private void logIfExportedForIllegalAccess(Class<?> caller, Class<?> declaringClass) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   372
        Module callerModule = caller.getModule();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   373
        Module targetModule = declaringClass.getModule();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   374
        // callerModule is null during early startup
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   375
        if (callerModule != null && !callerModule.isNamed() && targetModule.isNamed()) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   376
            IllegalAccessLogger logger = IllegalAccessLogger.illegalAccessLogger();
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   377
            if (logger != null) {
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 44359
diff changeset
   378
                logger.logIfExportedForIllegalAccess(caller, declaringClass, this::toShortString);
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   379
            }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   380
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   381
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   382
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   383
    /**
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   384
     * Returns a short descriptive string to describe this object in log messages.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32649
diff changeset
   385
     */
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   386
    String toShortString() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   387
        return toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   391
     * Get the value of the {@code accessible} flag for this reflected object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @return the value of the object's {@code accessible} flag
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   394
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   395
     * @deprecated
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   396
     * This method is deprecated because its name hints that it checks
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   397
     * if the reflected object is accessible when it actually indicates
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   398
     * if the checks for Java language access control are suppressed.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   399
     * This method may return {@code false} on a reflected object that is
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   400
     * accessible to the caller. To test if this reflected object is accessible,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   401
     * it should use {@link #canAccess(Object)}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   402
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   403
     * @revised 9
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   404
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   406
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public boolean isAccessible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   412
     * Test if the caller can access this reflected object. If this reflected
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   413
     * object corresponds to an instance method or field then this method tests
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   414
     * if the caller can access the given {@code obj} with the reflected object.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   415
     * For instance methods or fields then the {@code obj} argument must be an
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   416
     * instance of the {@link Member#getDeclaringClass() declaring class}. For
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   417
     * static members and constructors then {@code obj} must be {@code null}.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   418
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   419
     * <p> This method returns {@code true} if the {@code accessible} flag
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   420
     * is set to {@code true}, i.e. the checks for Java language access control
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   421
     * are suppressed, or if the caller can access the member as
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   422
     * specified in <cite>The Java&trade; Language Specification</cite>,
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   423
     * with the variation noted in the class description. </p>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   424
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   425
     * @param obj an instance object of the declaring class of this reflected
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   426
     *            object if it is an instance method or field
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   427
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   428
     * @return {@code true} if the caller can access this reflected object.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   429
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   430
     * @throws IllegalArgumentException
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   431
     *         <ul>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   432
     *         <li> if this reflected object is a static member or constructor and
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   433
     *              the given {@code obj} is non-{@code null}, or </li>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   434
     *         <li> if this reflected object is an instance method or field
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   435
     *              and the given {@code obj} is {@code null} or of type
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   436
     *              that is not a subclass of the {@link Member#getDeclaringClass()
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   437
     *              declaring class} of the member.</li>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   438
     *         </ul>
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   439
     *
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   440
     * @since 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   441
     * @spec JPMS
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   442
     * @jls 6.6 Access Control
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   443
     * @see #trySetAccessible
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   444
     * @see #setAccessible(boolean)
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   445
     */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   446
    @CallerSensitive
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   447
    public final boolean canAccess(Object obj) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   448
        if (!Member.class.isInstance(this)) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   449
            return override;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   450
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   451
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   452
        Class<?> declaringClass = ((Member) this).getDeclaringClass();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   453
        int modifiers = ((Member) this).getModifiers();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   454
        if (!Modifier.isStatic(modifiers) &&
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   455
                (this instanceof Method || this instanceof Field)) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   456
            if (obj == null) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   457
                throw new IllegalArgumentException("null object for " + this);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   458
            }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   459
            // if this object is an instance member, the given object
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   460
            // must be a subclass of the declaring class of this reflected object
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   461
            if (!declaringClass.isAssignableFrom(obj.getClass())) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   462
                throw new IllegalArgumentException("object is not an instance of "
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   463
                                                   + declaringClass.getName());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   464
            }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   465
        } else if (obj != null) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   466
            throw new IllegalArgumentException("non-null object for " + this);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   467
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   468
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   469
        // access check is suppressed
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   470
        if (override) return true;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   471
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   472
        Class<?> caller = Reflection.getCallerClass();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   473
        Class<?> targetClass;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   474
        if (this instanceof Constructor) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   475
            targetClass = declaringClass;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   476
        } else {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   477
            targetClass = Modifier.isStatic(modifiers) ? null : obj.getClass();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   478
        }
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   479
        return verifyAccess(caller, declaringClass, targetClass, modifiers);
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   480
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   481
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   482
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Constructor: only used by the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    protected AccessibleObject() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    // Indicates whether language-level access checks are overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    // by this object. Initializes to "false". This field is used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    // Field, Method, and Constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    // NOTE: for security purposes, this field must not be visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    // outside this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    boolean override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    // Reflection factory used by subclasses for creating field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    // method, and constructor accessors. Note that this is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    // very early in the bootstrapping process.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   498
    static final ReflectionFactory reflectionFactory =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   499
        AccessController.doPrivileged(
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
   500
            new ReflectionFactory.GetReflectionFactoryAction());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
16051
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   511
     * {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   512
     * @throws NullPointerException {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   513
     * @since 1.5
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   514
     */
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   515
    @Override
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   516
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   517
        return AnnotatedElement.super.isAnnotationPresent(annotationClass);
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   518
    }
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   519
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   520
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @throws NullPointerException {@inheritDoc}
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   522
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   523
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   524
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   525
    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
   526
        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
   527
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   528
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   529
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public Annotation[] getAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return getDeclaredAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   537
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   538
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   539
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   540
    @Override
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   541
    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
   542
        // 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
   543
        // objects getDeclaredAnnotation is the same as
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   544
        // getAnnotation.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   545
        return getAnnotation(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   546
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   547
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   548
    /**
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   549
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   550
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   551
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   552
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   553
    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
   554
        // 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
   555
        // objects getDeclaredAnnotationsByType is the same as
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   556
        // getAnnotationsByType.
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   557
        return getAnnotationsByType(annotationClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   558
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   559
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   560
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public Annotation[] getDeclaredAnnotations()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   566
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   567
    // Shared access checking logic.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   568
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   569
    // 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
   570
    // 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
   571
    // 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
   572
    // 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
   573
    // 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
   574
    // 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
   575
    //
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   576
    // 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
   577
    // 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
   578
    // (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
   579
    //
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   580
    // 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
   581
    // 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
   582
    // 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
   583
    // 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
   584
    volatile Object securityCheckCache;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   585
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   586
    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
   587
                           Class<?> targetClass, int modifiers)
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   588
        throws IllegalAccessException
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   589
    {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   590
        if (!verifyAccess(caller, memberClass, targetClass, modifiers)) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   591
            IllegalAccessException e = Reflection.newIllegalAccessException(
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   592
                caller, memberClass, targetClass, modifiers);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   593
            if (printStackTraceWhenAccessFails()) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   594
                e.printStackTrace(System.err);
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   595
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   596
            throw e;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   597
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   598
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   599
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   600
    final boolean verifyAccess(Class<?> caller, Class<?> memberClass,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   601
                               Class<?> targetClass, int modifiers)
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   602
    {
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   603
        if (caller == memberClass) {  // quick check
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   604
            return true;             // ACCESS IS OK
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   605
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   606
        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
   607
        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
   608
            && Modifier.isProtected(modifiers)
41560
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   609
            && targetClass != memberClass) {
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   610
            // 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
   611
            if (cache instanceof Class[]) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   612
                Class<?>[] cache2 = (Class<?>[]) cache;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   613
                if (cache2[1] == targetClass &&
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   614
                    cache2[0] == caller) {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   615
                    return true;     // ACCESS IS OK
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   616
                }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   617
                // (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
   618
                // subsumes range check for [0].)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   619
            }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   620
        } 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
   621
            // Non-protected case (or targetClass == memberClass or static member).
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   622
            return true;             // ACCESS IS OK
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   623
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   624
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   625
        // If no return, fall through to the slow path.
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   626
        return slowVerifyAccess(caller, memberClass, targetClass, modifiers);
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   627
    }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   628
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   629
    // Keep all this slow stuff out of line:
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   630
    private boolean slowVerifyAccess(Class<?> caller, Class<?> memberClass,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   631
                                     Class<?> targetClass, int modifiers)
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   632
    {
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   633
        if (!Reflection.verifyMemberAccess(caller, memberClass, targetClass, modifiers)) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   634
            // access denied
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   635
            return false;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   636
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   637
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   638
        // access okay
45652
33342314ce89 8181087: Module system implementation refresh (6/2017)
alanb
parents: 44359
diff changeset
   639
        logIfExportedForIllegalAccess(caller, memberClass);
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   640
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   641
        // 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
   642
        Object cache = (targetClass != null
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   643
                        && Modifier.isProtected(modifiers)
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   644
                        && targetClass != memberClass)
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   645
                        ? new Class<?>[] { caller, targetClass }
a66e7ee16cf9 6378384: (reflect) subclass can’t access superclass’s protected fields and methods by reflection
plevart
parents: 37363
diff changeset
   646
                        : caller;
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   647
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   648
        // 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
   649
        // 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
   650
        // 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
   651
        // 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
   652
        securityCheckCache = cache;         // write volatile
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   653
        return true;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   654
    }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   655
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   656
    // true to print a stack trace when access fails
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   657
    private static volatile boolean printStackWhenAccessFails;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   658
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   659
    // true if printStack* values are initialized
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   660
    private static volatile boolean printStackPropertiesSet;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   661
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   662
    /**
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   663
     * Returns true if a stack trace should be printed when access fails.
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   664
     */
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   665
    private static boolean printStackTraceWhenAccessFails() {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   666
        if (!printStackPropertiesSet && VM.initLevel() >= 1) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   667
            String s = GetPropertyAction.privilegedGetProperty(
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   668
                    "sun.reflect.debugModuleAccessChecks");
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   669
            if (s != null) {
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   670
                printStackWhenAccessFails = !s.equalsIgnoreCase("false");
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   671
            }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   672
            printStackPropertiesSet = true;
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   673
        }
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   674
        return printStackWhenAccessFails;
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   675
    }
50091
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   676
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   677
    /**
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   678
     * Returns the root AccessibleObject; or null if this object is the root.
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   679
     *
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   680
     * All subclasses override this method.
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   681
     */
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   682
    AccessibleObject getRoot() {
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   683
        throw new InternalError();
05979f6ba560 8202113: Reflection API is causing caller classes to leak
mchung
parents: 49925
diff changeset
   684
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
}