< prev index next >

src/java.base/share/classes/java/lang/reflect/AccessibleObject.java

Print this page

        

*** 481,494 **** if (this instanceof Constructor) { targetClass = declaringClass; } else { targetClass = Modifier.isStatic(modifiers) ? null : obj.getClass(); } ! return Reflection.verifyMemberAccess(caller, ! declaringClass, ! targetClass, ! modifiers); } /** * Constructor: only used by the Java Virtual Machine. */ --- 481,491 ---- if (this instanceof Constructor) { targetClass = declaringClass; } else { targetClass = Modifier.isStatic(modifiers) ? null : obj.getClass(); } ! return verifyAccess(caller, declaringClass, targetClass, modifiers); } /** * Constructor: only used by the Java Virtual Machine. */
*** 596,638 **** final void checkAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers) throws IllegalAccessException { if (caller == memberClass) { // quick check ! return; // ACCESS IS OK } Object cache = securityCheckCache; // read volatile if (targetClass != null // instance member or constructor && Modifier.isProtected(modifiers) && targetClass != memberClass) { // Must match a 2-list of { caller, targetClass }. if (cache instanceof Class[]) { Class<?>[] cache2 = (Class<?>[]) cache; if (cache2[1] == targetClass && cache2[0] == caller) { ! return; // ACCESS IS OK } // (Test cache[1] first since range check for [1] // subsumes range check for [0].) } } else if (cache == caller) { // Non-protected case (or targetClass == memberClass or static member). ! return; // ACCESS IS OK } // If no return, fall through to the slow path. ! slowCheckMemberAccess(caller, memberClass, targetClass, modifiers); } // Keep all this slow stuff out of line: ! void slowCheckMemberAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers) - throws IllegalAccessException { ! Reflection.ensureMemberAccess(caller, memberClass, targetClass, modifiers); ! // Success: Update the cache. Object cache = (targetClass != null && Modifier.isProtected(modifiers) && targetClass != memberClass) ? new Class<?>[] { caller, targetClass } --- 593,642 ---- final void checkAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers) throws IllegalAccessException { + if (!verifyAccess(caller, memberClass, targetClass, modifiers)) { + Reflection.throwIllegalAccessException( + caller, memberClass, targetClass, modifiers); + } + } + + final boolean verifyAccess(Class<?> caller, Class<?> memberClass, + Class<?> targetClass, int modifiers) + { if (caller == memberClass) { // quick check ! return true; // ACCESS IS OK } Object cache = securityCheckCache; // read volatile if (targetClass != null // instance member or constructor && Modifier.isProtected(modifiers) && targetClass != memberClass) { // Must match a 2-list of { caller, targetClass }. if (cache instanceof Class[]) { Class<?>[] cache2 = (Class<?>[]) cache; if (cache2[1] == targetClass && cache2[0] == caller) { ! return true; // ACCESS IS OK } // (Test cache[1] first since range check for [1] // subsumes range check for [0].) } } else if (cache == caller) { // Non-protected case (or targetClass == memberClass or static member). ! return true; // ACCESS IS OK } // If no return, fall through to the slow path. ! return slowVerifyAccess(caller, memberClass, targetClass, modifiers); } // Keep all this slow stuff out of line: ! private boolean slowVerifyAccess(Class<?> caller, Class<?> memberClass, Class<?> targetClass, int modifiers) { ! if (Reflection.verifyMemberAccess(caller, memberClass, targetClass, modifiers)) { // Success: Update the cache. Object cache = (targetClass != null && Modifier.isProtected(modifiers) && targetClass != memberClass) ? new Class<?>[] { caller, targetClass }
*** 641,647 **** --- 645,655 ---- // Note: The two cache elements are not volatile, // but they are effectively final. The Java memory model // guarantees that the initializing stores for the cache // elements will occur before the volatile write. securityCheckCache = cache; // write volatile + return true; + } else { + return false; + } } }
< prev index next >