--- old/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java 2017-03-19 18:36:28.425224957 +0100 +++ new/src/java.base/share/classes/java/lang/reflect/AccessibleObject.java 2017-03-19 18:36:28.288227293 +0100 @@ -483,10 +483,7 @@ } else { targetClass = Modifier.isStatic(modifiers) ? null : obj.getClass(); } - return Reflection.verifyMemberAccess(caller, - declaringClass, - targetClass, - modifiers); + return verifyAccess(caller, declaringClass, targetClass, modifiers); } /** @@ -527,7 +524,7 @@ return AnnotatedElement.super.isAnnotationPresent(annotationClass); } - /** + /** * @throws NullPointerException {@inheritDoc} * @since 1.8 */ @@ -598,8 +595,17 @@ 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; // ACCESS IS OK + return true; // ACCESS IS OK } Object cache = securityCheckCache; // read volatile if (targetClass != null // instance member or constructor @@ -610,38 +616,40 @@ Class[] cache2 = (Class[]) cache; if (cache2[1] == targetClass && cache2[0] == caller) { - return; // ACCESS IS OK + 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; // ACCESS IS OK + return true; // ACCESS IS OK } // If no return, fall through to the slow path. - slowCheckMemberAccess(caller, memberClass, targetClass, modifiers); + return slowVerifyAccess(caller, memberClass, targetClass, modifiers); } // Keep all this slow stuff out of line: - void slowCheckMemberAccess(Class caller, Class memberClass, - Class targetClass, int modifiers) - throws IllegalAccessException + private boolean slowVerifyAccess(Class caller, Class memberClass, + Class targetClass, int modifiers) { - Reflection.ensureMemberAccess(caller, memberClass, targetClass, modifiers); - - // Success: Update the cache. - Object cache = (targetClass != null - && Modifier.isProtected(modifiers) - && targetClass != memberClass) - ? new Class[] { caller, targetClass } - : caller; - - // 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 + if (Reflection.verifyMemberAccess(caller, memberClass, targetClass, modifiers)) { + // Success: Update the cache. + Object cache = (targetClass != null + && Modifier.isProtected(modifiers) + && targetClass != memberClass) + ? new Class[] { caller, targetClass } + : caller; + + // 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; + } } }