1 7 8 package java.lang.reflect; 9 10 import java.security.AccessController ; 11 import sun.reflect.ReflectionFactory; 12 import java.lang.annotation.Annotation ; 13 14 36 public class AccessibleObject implements AnnotatedElement { 37 38 43 static final private java.security.Permission ACCESS_PERMISSION = 44 new ReflectPermission ("suppressAccessChecks"); 45 46 71 public static void setAccessible(AccessibleObject [] array, boolean flag) 72 throws SecurityException { 73 SecurityManager sm = System.getSecurityManager(); 74 if (sm != null) sm.checkPermission(ACCESS_PERMISSION); 75 for (int i = 0; i < array.length; i++) { 76 setAccessible0(array[i], flag); 77 } 78 } 79 80 105 public void setAccessible(boolean flag) throws SecurityException { 106 SecurityManager sm = System.getSecurityManager(); 107 if (sm != null) sm.checkPermission(ACCESS_PERMISSION); 108 setAccessible0(this, flag); 109 } 110 111 112 private static void setAccessible0(AccessibleObject obj, boolean flag) 113 throws SecurityException 114 { 115 if (obj instanceof Constructor && flag == true) { 116 Constructor c = (Constructor )obj; 117 if (c.getDeclaringClass() == Class .class) { 118 throw new SecurityException ("Can not make a java.lang.Class" + 119 " constructor accessible"); 120 } 121 } 122 obj.override = flag; 123 } 124 125 130 public boolean isAccessible() { 131 return override; 132 } 133 134 137 protected AccessibleObject() {} 138 139 141 volatile Class securityCheckCache; 152 153 boolean override; 160 161 static final ReflectionFactory reflectionFactory = (ReflectionFactory) 165 AccessController.doPrivileged 166 (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction()); 167 168 public <T extends Annotation > T getAnnotation(Class <T> annotationClass) { 169 throw new AssertionError ("All subclasses should override this method"); 170 } 171 172 public boolean isAnnotationPresent( 173 Class <? extends Annotation > annotationClass) 174 { 175 return getAnnotation(annotationClass) != null; 176 } 177 178 public Annotation [] getAnnotations() { 179 return getDeclaredAnnotations(); 180 } 181 182 public Annotation [] getDeclaredAnnotations() { 183 throw new AssertionError ("All subclasses should override this method"); 184 } 185 } 186 | Popular Tags |