jdk/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 27783 6317ec69db47
child 36511 9d0388c6b336
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 22581
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4042
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4042
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4042
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4042
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4042
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
    29
import sun.reflect.Reflection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.reflect.ReflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The AccessibleObject class is the base class for Field, Method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Constructor objects.  It provides the ability to flag a reflected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * object as suppressing default Java language access control checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * when it is used.  The access checks--for public, default (package)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * access, protected, and private members--are performed when Fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Methods or Constructors are used to set or get fields, to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * methods, or to create and initialize new instances of classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>Setting the {@code accessible} flag in a reflected object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * permits sophisticated applications with sufficient privilege, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * as Java Object Serialization or other persistence mechanisms, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * manipulate objects in a manner that would normally be prohibited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
4042
6fc4d5c2a456 6648344: (reflect spec) State default of isAccessible for reflective objects
darcy
parents: 3959
diff changeset
    48
 * <p>By default, a reflected object is <em>not</em> accessible.
6fc4d5c2a456 6648344: (reflect spec) State default of isAccessible for reflective objects
darcy
parents: 3959
diff changeset
    49
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see ReflectPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class AccessibleObject implements AnnotatedElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The Permission object that is used to check whether a client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * has sufficient privilege to defeat Java language access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * control checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 27783
diff changeset
    64
    private static final java.security.Permission ACCESS_PERMISSION =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        new ReflectPermission("suppressAccessChecks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Convenience method to set the {@code accessible} flag for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * array of objects with a single security check (for efficiency).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * {@code checkPermission} method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * {@code ReflectPermission("suppressAccessChecks")} permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <p>A {@code SecurityException} is raised if {@code flag} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * {@code true} but accessibility of any of the elements of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * {@code array} may not be changed (for example, if the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * object is a {@link Constructor} object for the class {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * java.lang.Class}).  In the event of such a SecurityException, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * accessibility of objects is set to {@code flag} for array elements
25979
42e5d9f8087e 8054857: Fix typos in java.lang.** packages
prappo
parents: 25392
diff changeset
    81
     * up to (and excluding) the element for which the exception occurred; the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * accessibility of elements beyond (and including) the element for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * the exception occurred is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param array the array of AccessibleObjects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param flag  the new value for the {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *              in each object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @throws SecurityException if the request is denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static void setAccessible(AccessibleObject[] array, boolean flag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
22581
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 16051
diff changeset
    96
        for (AccessibleObject ao : array) {
e868cde95050 8032779: Update code in java.lang to use newer language features
psandoz
parents: 16051
diff changeset
    97
            setAccessible0(ao, flag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Set the {@code accessible} flag for this object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the indicated boolean value.  A value of {@code true} indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * the reflected object should suppress Java language access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * checking when it is used.  A value of {@code false} indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * that the reflected object should enforce Java language access checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * {@code checkPermission} method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * {@code ReflectPermission("suppressAccessChecks")} permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>A {@code SecurityException} is raised if {@code flag} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * {@code true} but accessibility of this object may not be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * (for example, if this element object is a {@link Constructor} object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * the class {@link java.lang.Class}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <p>A {@code SecurityException} is raised if this object is a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * java.lang.reflect.Constructor} object for the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * {@code java.lang.Class}, and {@code flag} is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param flag the new value for the {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throws SecurityException if the request is denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void setAccessible(boolean flag) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        setAccessible0(this, flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 22581
diff changeset
   132
    /* Check that you aren't exposing java.lang.Class.<init> or sensitive
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 22581
diff changeset
   133
       fields in java.lang.Class. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static void setAccessible0(AccessibleObject obj, boolean flag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (obj instanceof Constructor && flag == true) {
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 715
diff changeset
   138
            Constructor<?> c = (Constructor<?>)obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (c.getDeclaringClass() == Class.class) {
25392
0eabdbb887aa 6642881: Improve performance of Class.getClassLoader()
coleenp
parents: 22581
diff changeset
   140
                throw new SecurityException("Cannot make a java.lang.Class" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                            " constructor accessible");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        obj.override = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Get the value of the {@code accessible} flag for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the value of the object's {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public boolean isAccessible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Constructor: only used by the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected AccessibleObject() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // Indicates whether language-level access checks are overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // by this object. Initializes to "false". This field is used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // Field, Method, and Constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // NOTE: for security purposes, this field must not be visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // outside this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    boolean override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // Reflection factory used by subclasses for creating field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // method, and constructor accessors. Note that this is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // very early in the bootstrapping process.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   172
    static final ReflectionFactory reflectionFactory =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   173
        AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   174
            new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
16051
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   185
     * {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   186
     * @throws NullPointerException {@inheritDoc}
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   187
     * @since 1.5
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   188
     */
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   189
    @Override
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   190
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   191
        return AnnotatedElement.super.isAnnotationPresent(annotationClass);
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   192
    }
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   193
649a03329639 8009267: Restore isAnnotationPresent methods in public AnnotatedElement implementations
darcy
parents: 15659
diff changeset
   194
   /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @throws NullPointerException {@inheritDoc}
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   196
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   197
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   198
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   199
    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
   200
        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
   201
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   202
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   203
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public Annotation[] getAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return getDeclaredAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   211
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   212
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   213
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   214
    @Override
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   215
    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
   216
        // 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
   217
        // objects getDeclaredAnnotation is the same as
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   218
        // getAnnotation.
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   219
        return getAnnotation(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   220
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   221
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   222
    /**
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   223
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   224
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   225
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   226
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   227
    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
   228
        // 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
   229
        // objects getDeclaredAnnotationsByType is the same as
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   230
        // getAnnotationsByType.
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   231
        return getAnnotationsByType(annotationClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   232
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   233
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   234
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public Annotation[] getDeclaredAnnotations()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
9029
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   240
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   241
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   242
    // Shared access checking logic.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   243
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   244
    // 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
   245
    // 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
   246
    // 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
   247
    // 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
   248
    // 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
   249
    // 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
   250
    //
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   251
    // 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
   252
    // 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
   253
    // (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
   254
    //
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   255
    // A more complicated security check cache is needed for Method and Field
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   256
    // The cache can be either null (empty cache), a 2-array of {caller,target},
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   257
    // or a caller (with target implicitly equal to this.clazz).
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   258
    // In the 2-array case, the target is always different from the clazz.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   259
    volatile Object securityCheckCache;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   260
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   261
    void checkAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   262
        throws IllegalAccessException
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   263
    {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   264
        if (caller == clazz) {  // quick check
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   265
            return;             // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   266
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   267
        Object cache = securityCheckCache;  // read volatile
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   268
        Class<?> targetClass = clazz;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   269
        if (obj != null
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   270
            && Modifier.isProtected(modifiers)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   271
            && ((targetClass = obj.getClass()) != clazz)) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   272
            // 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
   273
            if (cache instanceof Class[]) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   274
                Class<?>[] cache2 = (Class<?>[]) cache;
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   275
                if (cache2[1] == targetClass &&
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   276
                    cache2[0] == caller) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   277
                    return;     // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   278
                }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   279
                // (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
   280
                // subsumes range check for [0].)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   281
            }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   282
        } else if (cache == caller) {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   283
            // Non-protected case (or obj.class == this.clazz).
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   284
            return;             // ACCESS IS OK
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   285
        }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   286
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   287
        // If no return, fall through to the slow path.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   288
        slowCheckMemberAccess(caller, clazz, obj, modifiers, targetClass);
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   289
    }
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   290
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   291
    // Keep all this slow stuff out of line:
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   292
    void slowCheckMemberAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers,
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   293
                               Class<?> targetClass)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   294
        throws IllegalAccessException
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   295
    {
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   296
        Reflection.ensureMemberAccess(caller, clazz, obj, modifiers);
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   297
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   298
        // Success: Update the cache.
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   299
        Object cache = ((targetClass == clazz)
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   300
                        ? caller
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   301
                        : new Class<?>[] { caller, targetClass });
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   302
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   303
        // 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
   304
        // 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
   305
        // 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
   306
        // 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
   307
        securityCheckCache = cache;         // write volatile
e92fcf58f684 6565585: Remove critical section in Method.invoke, Constructor.newInstance, Field.getFieldAccessor improving performance
mduigou
parents: 5506
diff changeset
   308
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}