< prev index next >
src/java.base/share/classes/jdk/internal/reflect/Reflection.java
Print this page
@@ -29,10 +29,11 @@
import java.lang.reflect.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import jdk.internal.HotSpotIntrinsicCandidate;
+import jdk.internal.misc.SharedSecrets;
import jdk.internal.misc.VM;
import sun.security.action.GetPropertyAction;
/** Common utility routines used by both java.lang and
java.lang.reflect */
@@ -216,12 +217,20 @@
c = c.getComponentType();
}
if (c.isPrimitive())
return true;
- // check that memberModule exports the package to currentModule
- return memberModule.isExported(c.getPackageName(), currentModule);
+ String pkg = c.getPackageName();
+ boolean allowed = memberModule.isExported(pkg, currentModule);
+ if (allowed && memberModule.isNamed() && printStackTraceWhenAccessSucceeds()) {
+ if (!SharedSecrets.getJavaLangReflectModuleAccess()
+ .isStaticallyExported(memberModule, pkg, currentModule)) {
+ String msg = currentModule + " allowed access to member of " + memberClass;
+ new Exception(msg).printStackTrace(System.err);
+ }
+ }
+ return allowed;
}
/**
* Returns true if two classes in the same package.
*/
@@ -346,27 +355,45 @@
}
return false;
}
- // true to print a stack trace when IAE is thrown
+ // true to print a stack trace when access fails
private static volatile boolean printStackWhenAccessFails;
- // true if printStackWhenAccessFails has been initialized
- private static volatile boolean printStackWhenAccessFailsSet;
+ // true to print a stack trace when access succeeds
+ private static volatile boolean printStackWhenAccessSucceeds;
- private static void printStackTraceIfNeeded(Throwable e) {
- if (!printStackWhenAccessFailsSet && VM.initLevel() >= 1) {
+ // true if printStack* values are initialized
+ private static volatile boolean printStackPropertiesSet;
+
+ private static void ensurePrintStackPropertiesSet() {
+ if (!printStackPropertiesSet && VM.initLevel() >= 1) {
String s = GetPropertyAction.privilegedGetProperty(
"sun.reflect.debugModuleAccessChecks");
- printStackWhenAccessFails =
- (s != null && !s.equalsIgnoreCase("false"));
- printStackWhenAccessFailsSet = true;
+ if (s != null) {
+ printStackWhenAccessFails = !s.equalsIgnoreCase("false");
+ printStackWhenAccessSucceeds = s.equalsIgnoreCase("access");
}
- if (printStackWhenAccessFails) {
- e.printStackTrace();
+ printStackPropertiesSet = true;
+ }
+ }
+
+ public static void enableStackTraces() {
+ printStackWhenAccessFails = true;
+ printStackWhenAccessSucceeds = true;
+ printStackPropertiesSet = true;
+ }
+
+ public static boolean printStackTraceWhenAccessFails() {
+ ensurePrintStackPropertiesSet();
+ return printStackWhenAccessFails;
}
+
+ public static boolean printStackTraceWhenAccessSucceeds() {
+ ensurePrintStackPropertiesSet();
+ return printStackWhenAccessSucceeds;
}
/**
* Throws IllegalAccessException with the an exception message based on
* the access that is denied.
@@ -414,19 +441,12 @@
*/
public static void throwIllegalAccessException(String msg)
throws IllegalAccessException
{
IllegalAccessException e = new IllegalAccessException(msg);
- printStackTraceIfNeeded(e);
- throw e;
+ ensurePrintStackPropertiesSet();
+ if (printStackWhenAccessFails) {
+ e.printStackTrace(System.err);
}
-
- /**
- * Throws InaccessibleObjectException with the given exception message.
- */
- public static void throwInaccessibleObjectException(String msg) {
- InaccessibleObjectException e = new InaccessibleObjectException(msg);
- printStackTraceIfNeeded(e);
throw e;
}
-
}
< prev index next >