rt/emul/mini/src/main/java/java/lang/reflect/AccessibleObject.java
changeset 772 d382dacfd73f
parent 554 05224402145d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/AccessibleObject.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,167 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.reflect;
    1.30 +
    1.31 +import java.lang.annotation.Annotation;
    1.32 +
    1.33 +/**
    1.34 + * The AccessibleObject class is the base class for Field, Method and
    1.35 + * Constructor objects.  It provides the ability to flag a reflected
    1.36 + * object as suppressing default Java language access control checks
    1.37 + * when it is used.  The access checks--for public, default (package)
    1.38 + * access, protected, and private members--are performed when Fields,
    1.39 + * Methods or Constructors are used to set or get fields, to invoke
    1.40 + * methods, or to create and initialize new instances of classes,
    1.41 + * respectively.
    1.42 + *
    1.43 + * <p>Setting the {@code accessible} flag in a reflected object
    1.44 + * permits sophisticated applications with sufficient privilege, such
    1.45 + * as Java Object Serialization or other persistence mechanisms, to
    1.46 + * manipulate objects in a manner that would normally be prohibited.
    1.47 + *
    1.48 + * <p>By default, a reflected object is <em>not</em> accessible.
    1.49 + *
    1.50 + * @see Field
    1.51 + * @see Method
    1.52 + * @see Constructor
    1.53 + * @see ReflectPermission
    1.54 + *
    1.55 + * @since 1.2
    1.56 + */
    1.57 +public class AccessibleObject implements AnnotatedElement {
    1.58 +
    1.59 +    /**
    1.60 +     * Convenience method to set the {@code accessible} flag for an
    1.61 +     * array of objects with a single security check (for efficiency).
    1.62 +     *
    1.63 +     * <p>First, if there is a security manager, its
    1.64 +     * {@code checkPermission} method is called with a
    1.65 +     * {@code ReflectPermission("suppressAccessChecks")} permission.
    1.66 +     *
    1.67 +     * <p>A {@code SecurityException} is raised if {@code flag} is
    1.68 +     * {@code true} but accessibility of any of the elements of the input
    1.69 +     * {@code array} may not be changed (for example, if the element
    1.70 +     * object is a {@link Constructor} object for the class {@link
    1.71 +     * java.lang.Class}).  In the event of such a SecurityException, the
    1.72 +     * accessibility of objects is set to {@code flag} for array elements
    1.73 +     * upto (and excluding) the element for which the exception occurred; the
    1.74 +     * accessibility of elements beyond (and including) the element for which
    1.75 +     * the exception occurred is unchanged.
    1.76 +     *
    1.77 +     * @param array the array of AccessibleObjects
    1.78 +     * @param flag  the new value for the {@code accessible} flag
    1.79 +     *              in each object
    1.80 +     * @throws SecurityException if the request is denied.
    1.81 +     * @see SecurityManager#checkPermission
    1.82 +     * @see java.lang.RuntimePermission
    1.83 +     */
    1.84 +    public static void setAccessible(AccessibleObject[] array, boolean flag)
    1.85 +        throws SecurityException {
    1.86 +        throw new SecurityException();
    1.87 +    }
    1.88 +
    1.89 +    /**
    1.90 +     * Set the {@code accessible} flag for this object to
    1.91 +     * the indicated boolean value.  A value of {@code true} indicates that
    1.92 +     * the reflected object should suppress Java language access
    1.93 +     * checking when it is used.  A value of {@code false} indicates
    1.94 +     * that the reflected object should enforce Java language access checks.
    1.95 +     *
    1.96 +     * <p>First, if there is a security manager, its
    1.97 +     * {@code checkPermission} method is called with a
    1.98 +     * {@code ReflectPermission("suppressAccessChecks")} permission.
    1.99 +     *
   1.100 +     * <p>A {@code SecurityException} is raised if {@code flag} is
   1.101 +     * {@code true} but accessibility of this object may not be changed
   1.102 +     * (for example, if this element object is a {@link Constructor} object for
   1.103 +     * the class {@link java.lang.Class}).
   1.104 +     *
   1.105 +     * <p>A {@code SecurityException} is raised if this object is a {@link
   1.106 +     * java.lang.reflect.Constructor} object for the class
   1.107 +     * {@code java.lang.Class}, and {@code flag} is true.
   1.108 +     *
   1.109 +     * @param flag the new value for the {@code accessible} flag
   1.110 +     * @throws SecurityException if the request is denied.
   1.111 +     * @see SecurityManager#checkPermission
   1.112 +     * @see java.lang.RuntimePermission
   1.113 +     */
   1.114 +    public void setAccessible(boolean flag) throws SecurityException {
   1.115 +        throw new SecurityException();
   1.116 +    }
   1.117 +
   1.118 +    /**
   1.119 +     * Get the value of the {@code accessible} flag for this object.
   1.120 +     *
   1.121 +     * @return the value of the object's {@code accessible} flag
   1.122 +     */
   1.123 +    public boolean isAccessible() {
   1.124 +        return override;
   1.125 +    }
   1.126 +
   1.127 +    /**
   1.128 +     * Constructor: only used by the Java Virtual Machine.
   1.129 +     */
   1.130 +    protected AccessibleObject() {}
   1.131 +
   1.132 +    // Indicates whether language-level access checks are overridden
   1.133 +    // by this object. Initializes to "false". This field is used by
   1.134 +    // Field, Method, and Constructor.
   1.135 +    //
   1.136 +    // NOTE: for security purposes, this field must not be visible
   1.137 +    // outside this package.
   1.138 +    boolean override;
   1.139 +
   1.140 +    /**
   1.141 +     * @throws NullPointerException {@inheritDoc}
   1.142 +     * @since 1.5
   1.143 +     */
   1.144 +    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
   1.145 +        throw new AssertionError("All subclasses should override this method");
   1.146 +    }
   1.147 +
   1.148 +    /**
   1.149 +     * @throws NullPointerException {@inheritDoc}
   1.150 +     * @since 1.5
   1.151 +     */
   1.152 +    public boolean isAnnotationPresent(
   1.153 +        Class<? extends Annotation> annotationClass) {
   1.154 +        return getAnnotation(annotationClass) != null;
   1.155 +    }
   1.156 +
   1.157 +    /**
   1.158 +     * @since 1.5
   1.159 +     */
   1.160 +    public Annotation[] getAnnotations() {
   1.161 +        return getDeclaredAnnotations();
   1.162 +    }
   1.163 +
   1.164 +    /**
   1.165 +     * @since 1.5
   1.166 +     */
   1.167 +    public Annotation[] getDeclaredAnnotations()  {
   1.168 +        throw new AssertionError("All subclasses should override this method");
   1.169 +    }
   1.170 +}