jdk/src/share/classes/java/lang/reflect/AccessibleObject.java
author martin
Mon, 10 Mar 2008 15:07:09 -0700
changeset 51 6fe31bc95bbc
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6600143: Remove another 450 unnecessary casts Reviewed-by: alanb, iris, lmalvent, bristor, peterjones, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.reflect.ReflectionFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The AccessibleObject class is the base class for Field, Method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Constructor objects.  It provides the ability to flag a reflected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * object as suppressing default Java language access control checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * when it is used.  The access checks--for public, default (package)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * access, protected, and private members--are performed when Fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Methods or Constructors are used to set or get fields, to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * methods, or to create and initialize new instances of classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>Setting the {@code accessible} flag in a reflected object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * permits sophisticated applications with sufficient privilege, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * as Java Object Serialization or other persistence mechanisms, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * manipulate objects in a manner that would normally be prohibited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see Field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see ReflectPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class AccessibleObject implements AnnotatedElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The Permission object that is used to check whether a client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * has sufficient privilege to defeat Java language access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * control checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static final private java.security.Permission ACCESS_PERMISSION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        new ReflectPermission("suppressAccessChecks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Convenience method to set the {@code accessible} flag for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * array of objects with a single security check (for efficiency).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * {@code checkPermission} method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * {@code ReflectPermission("suppressAccessChecks")} permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p>A {@code SecurityException} is raised if {@code flag} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * {@code true} but accessibility of any of the elements of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * {@code array} may not be changed (for example, if the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * object is a {@link Constructor} object for the class {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * java.lang.Class}).  In the event of such a SecurityException, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * accessibility of objects is set to {@code flag} for array elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * upto (and excluding) the element for which the exception occurred; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * accessibility of elements beyond (and including) the element for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * the exception occurred is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param array the array of AccessibleObjects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param flag  the new value for the {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *              in each object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @throws SecurityException if the request is denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static void setAccessible(AccessibleObject[] array, boolean flag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        for (int i = 0; i < array.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            setAccessible0(array[i], flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Set the {@code accessible} flag for this object to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * the indicated boolean value.  A value of {@code true} indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * the reflected object should suppress Java language access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * checking when it is used.  A value of {@code false} indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * that the reflected object should enforce Java language access checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>First, if there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * {@code checkPermission} method is called with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * {@code ReflectPermission("suppressAccessChecks")} permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>A {@code SecurityException} is raised if {@code flag} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * {@code true} but accessibility of this object may not be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * (for example, if this element object is a {@link Constructor} object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the class {@link java.lang.Class}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>A {@code SecurityException} is raised if this object is a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * java.lang.reflect.Constructor} object for the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * {@code java.lang.Class}, and {@code flag} is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param flag the new value for the {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws SecurityException if the request is denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see java.lang.RuntimePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public void setAccessible(boolean flag) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        setAccessible0(this, flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /* Check that you aren't exposing java.lang.Class.<init>. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static void setAccessible0(AccessibleObject obj, boolean flag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (obj instanceof Constructor && flag == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Constructor c = (Constructor)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (c.getDeclaringClass() == Class.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new SecurityException("Can not make a java.lang.Class" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                            " constructor accessible");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        obj.override = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Get the value of the {@code accessible} flag for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the value of the object's {@code accessible} flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public boolean isAccessible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Constructor: only used by the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected AccessibleObject() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // Indicates whether language-level access checks are overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // by this object. Initializes to "false". This field is used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // Field, Method, and Constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // NOTE: for security purposes, this field must not be visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // outside this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    boolean override;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // Reflection factory used by subclasses for creating field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // method, and constructor accessors. Note that this is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // very early in the bootstrapping process.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   168
    static final ReflectionFactory reflectionFactory =
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   169
        AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   170
            new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public boolean isAnnotationPresent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Class<? extends Annotation> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return getAnnotation(annotationClass) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public Annotation[] getAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return getDeclaredAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public Annotation[] getDeclaredAnnotations()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        throw new AssertionError("All subclasses should override this method");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}