changeset 14221:d79e9bf2bd4e

Make IAE message clear when module access fails
author alanb
date Thu, 15 Oct 2015 08:27:31 +0100
parents 7211ac32411a
children 7c5cfe773b7c
files src/java.base/share/classes/sun/reflect/Reflection.java
diffstat 1 files changed, 22 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/sun/reflect/Reflection.java	Thu Oct 15 08:14:43 2015 +0100
+++ b/src/java.base/share/classes/sun/reflect/Reflection.java	Thu Oct 15 08:27:31 2015 +0100
@@ -99,30 +99,32 @@
             String memberSuffix = "";
             Module m1 = currentClass.getModule();
             if (m1.isNamed())
-                currentSuffix = " (" + m1 + ")";
+                currentSuffix = " (in " + m1 + ")";
             Module m2 = memberClass.getModule();
             if (m2.isNamed())
-                memberSuffix = " (" + m2 + ")";
+                memberSuffix = " (in " + m2 + ")";
 
-            String msg = "Class " + currentClass.getName() +
-                    currentSuffix +
-                    " can not access a member of class " +
-                    memberClass.getName() + memberSuffix +
-                    " with modifiers \"" +
-                    Modifier.toString(modifiers) + "\"";
+            String memberPackageName = packageName(memberClass);
+            boolean canRead = m1.canRead(m2);
 
-            // Expand the message to help troubleshooting
-            if (!m1.canRead(m2)) {
-                msg += ", " + m1;
-                if (!m1.canRead(null))
-                    msg += " (strict module)";
-                msg += " does not read " + m2;
-            }
-            String pkg = packageName(memberClass);
-            if (!m2.isExported(pkg, m1)) {
-                msg += ", " + m2 + " does not export " + pkg;
-                if (m2.isNamed())
-                    msg += " to " + m1;
+            String msg = currentClass + currentSuffix + " cannot access ";
+            if (canRead && m2.isExported(memberPackageName, m1)) {
+
+                // module access okay so include the modifiers in the message
+                msg += "a member of " + memberClass + memberSuffix +
+                        " with modifiers \"" + Modifier.toString(modifiers) + "\"";
+
+            } else {
+
+                // module access failed
+                msg += memberClass + memberSuffix + " because ";
+                if (!canRead) {
+                    msg += m1 + " does not read " + m2;
+                } else {
+                    msg += m2 + " does not export package " + memberPackageName;
+                    if (m2.isNamed()) msg += " to " + m1;
+                }
+
             }
 
             throwIllegalAccessException(msg);