Frames | No Frames |
1: /* java.lang.reflect.AccessibleObject 2: Copyright (C) 2001, 2005, 2006 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 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) 9: any later version. 10: 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. 15: 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 19: 02110-1301 USA. 20: 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 24: combination. 25: 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. */ 37: 38: 39: package java.lang.reflect; 40: 41: import java.lang.annotation.Annotation; 42: 43: /** 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. 50: * 51: * @author Tom Tromey (tromey@cygnus.com) 52: * @author Eric Blake (ebb9@email.byu.edu) 53: * @see Field 54: * @see Constructor 55: * @see Method 56: * @see ReflectPermission 57: * @since 1.2 58: * @status updated to 1.5 59: */ 60: public class AccessibleObject 61: implements AnnotatedElement 62: { 63: /** 64: * True if this object is marked accessible, which means the reflected 65: * object bypasses normal security checks. 66: */ 67: // default visibility for use by inherited classes 68: boolean flag = false; 69: 70: /** 71: * Only the three reflection classes that extend this can create an 72: * accessible object. This is not serializable for security reasons. 73: */ 74: protected AccessibleObject() 75: { 76: } 77: 78: /** 79: * Return the accessibility status of this object. 80: * 81: * @return true if this object bypasses security checks 82: */ 83: public boolean isAccessible() 84: { 85: return flag; 86: } 87: 88: /** 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> 92: * 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. 98: * 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 105: */ 106: public static void setAccessible(AccessibleObject[] array, boolean flag) 107: { 108: checkPermission(); 109: for (int i = 0; i < array.length; i++) 110: array[i].secureSetAccessible(flag); 111: } 112: 113: /** 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> 117: * 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. 120: * 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 126: */ 127: public void setAccessible(boolean flag) 128: { 129: checkPermission(); 130: secureSetAccessible(flag); 131: } 132: 133: /** 134: * Performs the specified security check, for 135: * <code>ReflectPermission("suppressAccessChecks")</code>. 136: * 137: * @throws SecurityException if permission is denied 138: */ 139: private static void checkPermission() 140: { 141: SecurityManager sm = System.getSecurityManager(); 142: if (sm != null) 143: sm.checkPermission(new ReflectPermission("suppressAccessChecks")); 144: } 145: 146: /** 147: * Performs the actual accessibility change, this must always be invoked 148: * after calling checkPermission. 149: * 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>. 153: */ 154: private void secureSetAccessible(boolean flag) 155: { 156: if (flag && 157: (this instanceof Constructor 158: && ((Constructor) this).getDeclaringClass() == Class.class)) 159: throw new SecurityException("Cannot make object accessible: " + this); 160: this.flag = flag; 161: } 162: 163: /** 164: * <p> 165: * Returns the element's annotation for the specified annotation type, 166: * or <code>null</code> if no such annotation exists. 167: * </p> 168: * <p> 169: * <strong>This method must be overridden by subclasses to provide 170: * appropriate behaviour.</strong> 171: * </p> 172: * 173: * @param annotationClass the type of annotation to look for. 174: * @return this element's annotation for the specified type, or 175: * <code>null</code> if no such annotation exists. 176: * @throws NullPointerException if the annotation class is <code>null</code>. 177: */ 178: public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 179: { 180: throw new AssertionError("Subclass must override this method"); 181: } 182: 183: /** 184: * Returns all annotations associated with the element. If there are 185: * no annotations associated with the element, then a zero-length array 186: * will be returned. The returned array may be modified by the client 187: * code, but this will have no effect on the annotation content of the 188: * element, and hence no effect on the return value of this method for 189: * future callers. 190: * 191: * @return this element's annotations. 192: */ 193: public Annotation[] getAnnotations() 194: { 195: return getDeclaredAnnotations(); 196: } 197: 198: /** 199: * <p> 200: * Returns all annotations directly defined by the element. If there are 201: * no annotations directly associated with the element, then a zero-length 202: * array will be returned. The returned array may be modified by the client 203: * code, but this will have no effect on the annotation content of this 204: * class, and hence no effect on the return value of this method for 205: * future callers. 206: * </p> 207: * <p> 208: * <strong>This method must be overridden by subclasses to provide 209: * appropriate behaviour.</strong> 210: * </p> 211: * 212: * @return the annotations directly defined by the element. 213: * @since 1.5 214: */ 215: public Annotation[] getDeclaredAnnotations() 216: { 217: throw new AssertionError("Subclass must override this method"); 218: } 219: 220: /** 221: * Returns true if an annotation for the specified type is associated 222: * with the element. This is primarily a short-hand for using marker 223: * annotations. 224: * 225: * @param annotationClass the type of annotation to look for. 226: * @return true if an annotation exists for the specified type. 227: * @since 1.5 228: */ 229: public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 230: { 231: return getAnnotation(annotationClass) != null; 232: } 233: }