1 /* java.lang.reflect.AccessibleObject
2 Copyright (C) 2001, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.lang.reflect;
41 import java.lang.annotation.Annotation;
44 * This class is the superclass of various reflection classes, and
45 * allows sufficiently trusted code to bypass normal restrictions to
46 * do necessary things like invoke private methods outside of the
47 * class during Serialization. If you don't have a good reason
48 * to mess with this, don't try. Fortunately, there are adequate
49 * security checks before you can set a reflection object as accessible.
51 * @author Tom Tromey (tromey@cygnus.com)
52 * @author Eric Blake (ebb9@email.byu.edu)
56 * @see ReflectPermission
58 * @status updated to 1.5
60 public class AccessibleObject
61 implements AnnotatedElement
64 * True if this object is marked accessible, which means the reflected
65 * object bypasses normal security checks.
67 // default visibility for use by inherited classes
71 * Only the three reflection classes that extend this can create an
72 * accessible object. This is not serializable for security reasons.
74 protected AccessibleObject()
79 * Return the accessibility status of this object.
81 * @return true if this object bypasses security checks
83 public boolean isAccessible()
89 * Convenience method to set the flag on a number of objects with a single
90 * security check. If a security manager exists, it is checked for
91 * <code>ReflectPermission("suppressAccessChecks")</code>.<p>
93 * It is forbidden to set the accessibility flag to true on any constructor
94 * for java.lang.Class. This will result in a SecurityException. If the
95 * SecurityException is thrown for any of the passed AccessibleObjects,
96 * the accessibility flag will be set on AccessibleObjects in the array prior
97 * to the one which resulted in the exception.
99 * @param array the array of accessible objects
100 * @param flag the desired state of accessibility, true to bypass security
101 * @throws NullPointerException if array is null
102 * @throws SecurityException if the request is denied
103 * @see SecurityManager#checkPermission(java.security.Permission)
104 * @see RuntimePermission
106 public static void setAccessible(AccessibleObject[] array, boolean flag)
109 for (int i = 0; i < array.length; i++)
110 array[i].secureSetAccessible(flag);
114 * Sets the accessibility flag for this reflection object. If a security
115 * manager exists, it is checked for
116 * <code>ReflectPermission("suppressAccessChecks")</code>.<p>
118 * It is forbidden to set the accessibility flag to true on any constructor for
119 * java.lang.Class. This will result in a SecurityException.
121 * @param flag the desired state of accessibility, true to bypass security
122 * @throws NullPointerException if array is null
123 * @throws SecurityException if the request is denied
124 * @see SecurityManager#checkPermission(java.security.Permission)
125 * @see RuntimePermission
127 public void setAccessible(boolean flag)
130 secureSetAccessible(flag);
134 * Performs the specified security check, for
135 * <code>ReflectPermission("suppressAccessChecks")</code>.
137 * @throws SecurityException if permission is denied
139 private static void checkPermission()
141 SecurityManager sm = System.getSecurityManager();
143 sm.checkPermission(new ReflectPermission("suppressAccessChecks"));
147 * Performs the actual accessibility change, this must always be invoked
148 * after calling checkPermission.
150 * @param flag the desired status
151 * @throws SecurityException if flag is true and this is a constructor
152 * for <code>java.lang.Class</code>.
154 private void secureSetAccessible(boolean flag)
157 (this instanceof Constructor
158 && ((Constructor) this).getDeclaringClass() == Class.class))
159 throw new SecurityException("Cannot make object accessible: " + this);
163 /* FIXME[GENERICS]: <T extends Annotation> T getAnnotation(Class <T>) */
164 public Annotation getAnnotation(Class annotationClass)
166 throw new AssertionError("Subclass must override this method");
169 public Annotation[] getAnnotations()
171 return getDeclaredAnnotations();
174 public Annotation[] getDeclaredAnnotations()
176 throw new AssertionError("Subclass must override this method");
179 /* FIXME[GENERICS]: Signature is Class<? extends Annotation> */
180 public boolean isAnnotationPresent(Class annotationClass)
182 return getAnnotation(annotationClass) != null;