-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.1.6
-
None
-
sparc
-
solaris_2.6
Say you have:
class a { void f() }
and
class b extends a { private int x }
In normal static references there is no way for method f() to reference x in
class b. However, with the reflect methods f() could be looking at x if f()
was called on an object of type b. Shouldn't access to this private variable
be allowed in this case??
Here is some code which demonstrates this:
import java.lang.reflect.*;
public class test
{
public static void main(String argv[])
{
test2 t2 = new test2("hello", "goodbye");
t2.seeFields();
}
public void seeFields()
{
Object obj;
Field[] fields = getClass().getDeclaredFields();
for (int i=0; i < fields.length; i++)
{
String field = fields[i].getName();
System.out.println("getting " + field);
try
{
obj = fields[i].get(this);
}
catch (Exception ex)
{
ex.printStackTrace();
continue;
}
System.out.println(field + ": " + obj.toString());
}
}
}
class test2 extends test
{
protected String prot;
private String priv;
public test2(String x, String y) { prot = x; priv = y; }
}
The output here is:
getting prot
prot: hello
getting priv
java.lang.IllegalAccessException: test2
at test.seeFields(test.java:22)
at test.main(test.java:9)
class a { void f() }
and
class b extends a { private int x }
In normal static references there is no way for method f() to reference x in
class b. However, with the reflect methods f() could be looking at x if f()
was called on an object of type b. Shouldn't access to this private variable
be allowed in this case??
Here is some code which demonstrates this:
import java.lang.reflect.*;
public class test
{
public static void main(String argv[])
{
test2 t2 = new test2("hello", "goodbye");
t2.seeFields();
}
public void seeFields()
{
Object obj;
Field[] fields = getClass().getDeclaredFields();
for (int i=0; i < fields.length; i++)
{
String field = fields[i].getName();
System.out.println("getting " + field);
try
{
obj = fields[i].get(this);
}
catch (Exception ex)
{
ex.printStackTrace();
continue;
}
System.out.println(field + ": " + obj.toString());
}
}
}
class test2 extends test
{
protected String prot;
private String priv;
public test2(String x, String y) { prot = x; priv = y; }
}
The output here is:
getting prot
prot: hello
getting priv
java.lang.IllegalAccessException: test2
at test.seeFields(test.java:22)
at test.main(test.java:9)