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