Closed
Bug 40688
Opened 25 years ago
Closed 21 years ago
Exception accessing package protected member
Categories
(Rhino Graveyard :: Core, defect, P3)
Tracking
(Not tracked)
VERIFIED
FIXED
1.5R5
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
Assignee | ||
Updated•25 years ago
|
Status: NEW → ASSIGNED
Assignee | ||
Comment 2•25 years ago
|
||
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?
Comment 3•22 years ago
|
||
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.
Comment 4•22 years ago
|
||
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
Comment 5•21 years ago
|
||
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
You need to log in
before you can comment on or make changes to this bug.
Description
•