/** * If the given AccessibleObject is a {@code Constructor}, {@code Method} * or {@code Field} then checks that its declaring class is in a package * that can be accessed by the given caller of setAccessible. */ voidcheckCanSetAccessible(Class<?> caller) { // do nothing, needs to be overridden by Constructor, Method, Field }
if (callerModule == declaringModule) returntrue; if (callerModule == Object.class.getModule()) returntrue; if (!declaringModule.isNamed()) returntrue;
Stringpn= declaringClass.getPackageName(); int modifiers; if (thisinstanceof Executable) { modifiers = ((Executable) this).getModifiers(); } else { modifiers = ((Field) this).getModifiers(); }
// class is public and package is exported to caller booleanisClassPublic= Modifier.isPublic(declaringClass.getModifiers()); if (isClassPublic && declaringModule.isExported(pn, callerModule)) { // member is public if (Modifier.isPublic(modifiers)) { logIfExportedForIllegalAccess(caller, declaringClass); returntrue; }
// member is protected-static if (Modifier.isProtected(modifiers) && Modifier.isStatic(modifiers) && isSubclassOf(caller, declaringClass)) { logIfExportedForIllegalAccess(caller, declaringClass); returntrue; } }
// package is open to caller if (declaringModule.isOpen(pn, callerModule)) { logIfOpenedForIllegalAccess(caller, declaringClass); returntrue; }
if (throwExceptionIfDenied) { // not accessible Stringmsg="Unable to make "; if (thisinstanceof Field) msg += "field "; msg += this + " accessible: " + declaringModule + " does not \""; if (isClassPublic && Modifier.isPublic(modifiers)) msg += "exports"; else msg += "opens"; msg += " " + pn + "\" to " + callerModule; InaccessibleObjectExceptione=newInaccessibleObjectException(msg); if (printStackTraceWhenAccessFails()) { e.printStackTrace(System.err); } throw e; } returnfalse; }