emul/src/main/java/java/lang/reflect/AccessibleObject.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 04 Dec 2012 14:31:11 +0100
branchreflection
changeset 260 1d03cb35fbda
parent 258 21b390daf444
permissions -rw-r--r--
Removing default implementation from reflection APIs
jtulach@258
     1
/*
jtulach@258
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
jtulach@258
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@258
     4
 *
jtulach@258
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@258
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@258
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@258
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@258
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@258
    10
 *
jtulach@258
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@258
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@258
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@258
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@258
    15
 * accompanied this code).
jtulach@258
    16
 *
jtulach@258
    17
 * You should have received a copy of the GNU General Public License version
jtulach@258
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@258
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@258
    20
 *
jtulach@258
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@258
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@258
    23
 * questions.
jtulach@258
    24
 */
jtulach@258
    25
jtulach@258
    26
package java.lang.reflect;
jtulach@258
    27
jtulach@258
    28
import java.lang.annotation.Annotation;
jtulach@258
    29
jtulach@258
    30
/**
jtulach@258
    31
 * The AccessibleObject class is the base class for Field, Method and
jtulach@258
    32
 * Constructor objects.  It provides the ability to flag a reflected
jtulach@258
    33
 * object as suppressing default Java language access control checks
jtulach@258
    34
 * when it is used.  The access checks--for public, default (package)
jtulach@258
    35
 * access, protected, and private members--are performed when Fields,
jtulach@258
    36
 * Methods or Constructors are used to set or get fields, to invoke
jtulach@258
    37
 * methods, or to create and initialize new instances of classes,
jtulach@258
    38
 * respectively.
jtulach@258
    39
 *
jtulach@258
    40
 * <p>Setting the {@code accessible} flag in a reflected object
jtulach@258
    41
 * permits sophisticated applications with sufficient privilege, such
jtulach@258
    42
 * as Java Object Serialization or other persistence mechanisms, to
jtulach@258
    43
 * manipulate objects in a manner that would normally be prohibited.
jtulach@258
    44
 *
jtulach@258
    45
 * <p>By default, a reflected object is <em>not</em> accessible.
jtulach@258
    46
 *
jtulach@258
    47
 * @see Field
jtulach@258
    48
 * @see Method
jtulach@258
    49
 * @see Constructor
jtulach@258
    50
 * @see ReflectPermission
jtulach@258
    51
 *
jtulach@258
    52
 * @since 1.2
jtulach@258
    53
 */
jtulach@258
    54
public class AccessibleObject implements AnnotatedElement {
jtulach@258
    55
jtulach@258
    56
    /**
jtulach@258
    57
     * Convenience method to set the {@code accessible} flag for an
jtulach@258
    58
     * array of objects with a single security check (for efficiency).
jtulach@258
    59
     *
jtulach@258
    60
     * <p>First, if there is a security manager, its
jtulach@258
    61
     * {@code checkPermission} method is called with a
jtulach@258
    62
     * {@code ReflectPermission("suppressAccessChecks")} permission.
jtulach@258
    63
     *
jtulach@258
    64
     * <p>A {@code SecurityException} is raised if {@code flag} is
jtulach@258
    65
     * {@code true} but accessibility of any of the elements of the input
jtulach@258
    66
     * {@code array} may not be changed (for example, if the element
jtulach@258
    67
     * object is a {@link Constructor} object for the class {@link
jtulach@258
    68
     * java.lang.Class}).  In the event of such a SecurityException, the
jtulach@258
    69
     * accessibility of objects is set to {@code flag} for array elements
jtulach@258
    70
     * upto (and excluding) the element for which the exception occurred; the
jtulach@258
    71
     * accessibility of elements beyond (and including) the element for which
jtulach@258
    72
     * the exception occurred is unchanged.
jtulach@258
    73
     *
jtulach@258
    74
     * @param array the array of AccessibleObjects
jtulach@258
    75
     * @param flag  the new value for the {@code accessible} flag
jtulach@258
    76
     *              in each object
jtulach@258
    77
     * @throws SecurityException if the request is denied.
jtulach@258
    78
     * @see SecurityManager#checkPermission
jtulach@258
    79
     * @see java.lang.RuntimePermission
jtulach@258
    80
     */
jtulach@258
    81
    public static void setAccessible(AccessibleObject[] array, boolean flag)
jtulach@258
    82
        throws SecurityException {
jaroslav@260
    83
        throw new SecurityException();
jtulach@258
    84
    }
jtulach@258
    85
jtulach@258
    86
    /**
jtulach@258
    87
     * Set the {@code accessible} flag for this object to
jtulach@258
    88
     * the indicated boolean value.  A value of {@code true} indicates that
jtulach@258
    89
     * the reflected object should suppress Java language access
jtulach@258
    90
     * checking when it is used.  A value of {@code false} indicates
jtulach@258
    91
     * that the reflected object should enforce Java language access checks.
jtulach@258
    92
     *
jtulach@258
    93
     * <p>First, if there is a security manager, its
jtulach@258
    94
     * {@code checkPermission} method is called with a
jtulach@258
    95
     * {@code ReflectPermission("suppressAccessChecks")} permission.
jtulach@258
    96
     *
jtulach@258
    97
     * <p>A {@code SecurityException} is raised if {@code flag} is
jtulach@258
    98
     * {@code true} but accessibility of this object may not be changed
jtulach@258
    99
     * (for example, if this element object is a {@link Constructor} object for
jtulach@258
   100
     * the class {@link java.lang.Class}).
jtulach@258
   101
     *
jtulach@258
   102
     * <p>A {@code SecurityException} is raised if this object is a {@link
jtulach@258
   103
     * java.lang.reflect.Constructor} object for the class
jtulach@258
   104
     * {@code java.lang.Class}, and {@code flag} is true.
jtulach@258
   105
     *
jtulach@258
   106
     * @param flag the new value for the {@code accessible} flag
jtulach@258
   107
     * @throws SecurityException if the request is denied.
jtulach@258
   108
     * @see SecurityManager#checkPermission
jtulach@258
   109
     * @see java.lang.RuntimePermission
jtulach@258
   110
     */
jtulach@258
   111
    public void setAccessible(boolean flag) throws SecurityException {
jaroslav@260
   112
        throw new SecurityException();
jtulach@258
   113
    }
jtulach@258
   114
jtulach@258
   115
    /**
jtulach@258
   116
     * Get the value of the {@code accessible} flag for this object.
jtulach@258
   117
     *
jtulach@258
   118
     * @return the value of the object's {@code accessible} flag
jtulach@258
   119
     */
jtulach@258
   120
    public boolean isAccessible() {
jtulach@258
   121
        return override;
jtulach@258
   122
    }
jtulach@258
   123
jtulach@258
   124
    /**
jtulach@258
   125
     * Constructor: only used by the Java Virtual Machine.
jtulach@258
   126
     */
jtulach@258
   127
    protected AccessibleObject() {}
jtulach@258
   128
jtulach@258
   129
    // Indicates whether language-level access checks are overridden
jtulach@258
   130
    // by this object. Initializes to "false". This field is used by
jtulach@258
   131
    // Field, Method, and Constructor.
jtulach@258
   132
    //
jtulach@258
   133
    // NOTE: for security purposes, this field must not be visible
jtulach@258
   134
    // outside this package.
jtulach@258
   135
    boolean override;
jtulach@258
   136
jtulach@258
   137
    /**
jtulach@258
   138
     * @throws NullPointerException {@inheritDoc}
jtulach@258
   139
     * @since 1.5
jtulach@258
   140
     */
jtulach@258
   141
    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
jtulach@258
   142
        throw new AssertionError("All subclasses should override this method");
jtulach@258
   143
    }
jtulach@258
   144
jtulach@258
   145
    /**
jtulach@258
   146
     * @throws NullPointerException {@inheritDoc}
jtulach@258
   147
     * @since 1.5
jtulach@258
   148
     */
jtulach@258
   149
    public boolean isAnnotationPresent(
jtulach@258
   150
        Class<? extends Annotation> annotationClass) {
jtulach@258
   151
        return getAnnotation(annotationClass) != null;
jtulach@258
   152
    }
jtulach@258
   153
jtulach@258
   154
    /**
jtulach@258
   155
     * @since 1.5
jtulach@258
   156
     */
jtulach@258
   157
    public Annotation[] getAnnotations() {
jtulach@258
   158
        return getDeclaredAnnotations();
jtulach@258
   159
    }
jtulach@258
   160
jtulach@258
   161
    /**
jtulach@258
   162
     * @since 1.5
jtulach@258
   163
     */
jtulach@258
   164
    public Annotation[] getDeclaredAnnotations()  {
jtulach@258
   165
        throw new AssertionError("All subclasses should override this method");
jtulach@258
   166
    }
jtulach@258
   167
}