Closed Bug 40688 Opened 25 years ago Closed 21 years ago

Exception accessing package protected member

Categories

(Rhino Graveyard :: Core, defect, P3)

x86
Windows NT
defect

Tracking

(Not tracked)

VERIFIED FIXED

People

(Reporter: norrisboyd, Assigned: norrisboyd)

Details

[rhino] cat A.java interface A { int a = 0; } [rhino] rhino js> Packages.A.a java.lang.RuntimeException: unexpected IllegalAccessException accessing Java fie ld at org/mozilla/javascript/JavaMembers.get (JavaMembers.java:102) at org/mozilla/javascript/NativeJavaClass.get (NativeJavaClass.java:94) at org/mozilla/javascript/ScriptRuntime.getProp (ScriptRuntime.java:691) at org/mozilla/javascript/Interpreter.interpret (Interpreter.java:1594) at org/mozilla/javascript/InterpretedScript.call (InterpretedScript.java :67) at org/mozilla/javascript/InterpretedScript.exec (InterpretedScript.java :54) at org/mozilla/javascript/Context.evaluateReader (Context.java:741) at org/mozilla/javascript/tools/shell/Main.evaluateReader (Main.java:347 ) at org/mozilla/javascript/tools/shell/Main.processSource (Main.java:284) at org/mozilla/javascript/tools/shell/Main.exec (Main.java:148) at org/mozilla/javascript/tools/shell/Main.main (Main.java:74) ERROR: java.lang.RuntimeException: unexpected IllegalAccessException accessing J ava field
reassign to other account
Assignee: norrisboyd → nboyd
Status: NEW → ASSIGNED
Hmm. Calling getModifiers on the java.lang.reflect.Field object for "a" returns 25, and Modifiers.isPublic(25) is true. This looks like a JDK bug: the modifiers values for "A" and "a" are the same for both interface A { int a = 0; } and interface A { public int a = 0; } Any ideas, Patrick?
Must be a JVM bug indeed. but it works with JDK1.3.1 and JDK1.4.0, so sun must have fixed it (though I couldnt find a reference to it in the bug parade). Packages.A.a returns 0 for me as it should. Whether you explicitly say a field is public or not it is still going to be public. From Java Specification about interfaces: 9.3 Field (Constant) Declarations .. Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.
The raeson for the bug is that interface is declared package-private, and with reflection it is not possible to access such field. With public interface it works under jdk 1.1-1.4: ~/tmp> cat A.java public interface A { int a = 0; } ~/tmp> CLASSPATH=. java -jar js.jar -e 'print(Packages.A.a)' 0 On the other hand, if one apply the following patch to omj/JavaMembers.java to print a detailed stack trace, then the original non=public A I got: java.lang.IllegalAccessException: Class org.mozilla.javascript.JavaMembers can not access a member of class A with modifiers "public static final" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57) at java.lang.reflect.Field.doSecurityCheck(Field.java:811) Under jdk 1.1, 1.3 the exception is the same but it does not have a detailed message. In principle this can be solved under JDK 1.2 via calling setAccessible on the field if its access trigers IllegalAccessException, but perhaps such change it is better to do consistently in places where reflection is used. Patch to enable stack printout: Index: JavaMembers.java =================================================================== RCS file: /cvsroot/mozilla/js/rhino/src/org/mozilla/javascript/JavaMembers.java,v retrieving revision 1.31 diff -u -r1.31 JavaMembers.java --- JavaMembers.java 9 Jun 2002 15:58:14 -0000 1.31 +++ JavaMembers.java 3 Apr 2003 14:39:37 -0000 @@ -104,11 +104,16 @@ type = bp.getter.getReturnType(); } else { Field field = (Field) member; - rval = field.get(isStatic ? null : javaObject); + if (isStatic) { + rval = field.get(null); + } else { + rval = field.get(javaObject); + } type = field.getType(); } } catch (IllegalAccessException accEx) { - throw new RuntimeException("unexpected IllegalAccessException "+ + accEx.printStackTrace(); + throw new RuntimeException("unexpected IllegalAccessException "+ "accessing Java field"); } catch (InvocationTargetException e) { // Since JavaScriptException is a checked exception, must
Marking as fixed: Rhino calls for some time setAccessible under JDK >= 1.2 after first failed access with IllegalAccessException
Status: ASSIGNED → RESOLVED
Closed: 21 years ago
Resolution: --- → FIXED
Rubber-stamp vrfy
Status: RESOLVED → VERIFIED
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
You need to log in before you can comment on or make changes to this bug.