295 */ 296 public Annotation[] getDeclaredAnnotations() { 297 throw new AssertionError("All subclasses should override this method"); 298 } 299 300 301 // Shared access checking logic. 302 303 // For non-public members or members in package-private classes, 304 // it is necessary to perform somewhat expensive security checks. 305 // If the security check succeeds for a given class, it will 306 // always succeed (it is not affected by the granting or revoking 307 // of permissions); we speed up the check in the common case by 308 // remembering the last Class for which the check succeeded. 309 // 310 // The simple security check for Constructor is to see if 311 // the caller has already been seen, verified, and cached. 312 // (See also Class.newInstance(), which uses a similar method.) 313 // 314 // A more complicated security check cache is needed for Method and Field 315 // The cache can be either null (empty cache), a 2-array of {caller,target}, 316 // or a caller (with target implicitly equal to this.clazz). 317 // In the 2-array case, the target is always different from the clazz. 318 volatile Object securityCheckCache; 319 320 void checkAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers) 321 throws IllegalAccessException 322 { 323 if (caller == clazz) { // quick check 324 return; // ACCESS IS OK 325 } 326 Object cache = securityCheckCache; // read volatile 327 Class<?> targetClass = clazz; 328 if (obj != null 329 && Modifier.isProtected(modifiers) 330 && ((targetClass = obj.getClass()) != clazz)) { 331 // Must match a 2-list of { caller, targetClass }. 332 if (cache instanceof Class[]) { 333 Class<?>[] cache2 = (Class<?>[]) cache; 334 if (cache2[1] == targetClass && 335 cache2[0] == caller) { 336 return; // ACCESS IS OK 337 } 338 // (Test cache[1] first since range check for [1] 339 // subsumes range check for [0].) 340 } 341 } else if (cache == caller) { 342 // Non-protected case (or obj.class == this.clazz). 343 return; // ACCESS IS OK 344 } 345 346 // If no return, fall through to the slow path. 347 slowCheckMemberAccess(caller, clazz, obj, modifiers, targetClass); 348 } 349 350 // Keep all this slow stuff out of line: 351 void slowCheckMemberAccess(Class<?> caller, Class<?> clazz, Object obj, int modifiers, 352 Class<?> targetClass) 353 throws IllegalAccessException 354 { 355 Reflection.ensureMemberAccess(caller, clazz, obj, modifiers); 356 357 // Success: Update the cache. 358 Object cache = ((targetClass == clazz) 359 ? caller 360 : new Class<?>[] { caller, targetClass }); 361 362 // Note: The two cache elements are not volatile, 363 // but they are effectively final. The Java memory model 364 // guarantees that the initializing stores for the cache 365 // elements will occur before the volatile write. 366 securityCheckCache = cache; // write volatile 367 } 368 } | 295 */ 296 public Annotation[] getDeclaredAnnotations() { 297 throw new AssertionError("All subclasses should override this method"); 298 } 299 300 301 // Shared access checking logic. 302 303 // For non-public members or members in package-private classes, 304 // it is necessary to perform somewhat expensive security checks. 305 // If the security check succeeds for a given class, it will 306 // always succeed (it is not affected by the granting or revoking 307 // of permissions); we speed up the check in the common case by 308 // remembering the last Class for which the check succeeded. 309 // 310 // The simple security check for Constructor is to see if 311 // the caller has already been seen, verified, and cached. 312 // (See also Class.newInstance(), which uses a similar method.) 313 // 314 // A more complicated security check cache is needed for Method and Field 315 // The cache can be either null (empty cache), a 2-array of {caller,targetClass}, 316 // or a caller (with targetClass implicitly equal to memberClass). 317 // In the 2-array case, the targetClass is always different from the memberClass. 318 volatile Object securityCheckCache; 319 320 final void checkAccess(Class<?> caller, Class<?> memberClass, 321 Class<?> targetClass, int modifiers) 322 throws IllegalAccessException 323 { 324 if (caller == memberClass) { // quick check 325 return; // ACCESS IS OK 326 } 327 Object cache = securityCheckCache; // read volatile 328 if (targetClass != null // instance member or constructor 329 && Modifier.isProtected(modifiers) 330 && targetClass != memberClass) { 331 // Must match a 2-list of { caller, targetClass }. 332 if (cache instanceof Class[]) { 333 Class<?>[] cache2 = (Class<?>[]) cache; 334 if (cache2[1] == targetClass && 335 cache2[0] == caller) { 336 return; // ACCESS IS OK 337 } 338 // (Test cache[1] first since range check for [1] 339 // subsumes range check for [0].) 340 } 341 } else if (cache == caller) { 342 // Non-protected case (or targetClass == memberClass or static member). 343 return; // ACCESS IS OK 344 } 345 346 // If no return, fall through to the slow path. 347 slowCheckMemberAccess(caller, memberClass, targetClass, modifiers); 348 } 349 350 // Keep all this slow stuff out of line: 351 void slowCheckMemberAccess(Class<?> caller, Class<?> memberClass, 352 Class<?> targetClass, int modifiers) 353 throws IllegalAccessException 354 { 355 Reflection.ensureMemberAccess(caller, memberClass, targetClass, modifiers); 356 357 // Success: Update the cache. 358 Object cache = (targetClass != null 359 && Modifier.isProtected(modifiers) 360 && targetClass != memberClass) 361 ? new Class<?>[] { caller, targetClass } 362 : caller; 363 364 // Note: The two cache elements are not volatile, 365 // but they are effectively final. The Java memory model 366 // guarantees that the initializing stores for the cache 367 // elements will occur before the volatile write. 368 securityCheckCache = cache; // write volatile 369 } 370 } |