• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection  RSS feed

 
Nicolas Brasseur
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain me why this code throws an IllegalAccessException ...
package test;
import java.lang.reflect.*;
public class Ancestor {
public Object findField(String s){
Object result = null;
try {
Field field = this.getClass().getDeclaredField(s);
result = field.get(this);
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
package test;
import java.lang.reflect.*;
public class Subclass extends Ancestor {
private String privateString = "TEST !";
}
package test;
public class Main {
public static void main(String[] args) {
Subclass sub = new Subclass();
sub.findField("privateString");
}
}

When I move the find() method in the subclass it works ...
Thanks for the answer
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because it's private. No other class, even a parent class or sub-class can access it. make it either public or package-protected (i.e. don't write anything next to it).
 
Nicolas Brasseur
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you do not understand my problem ... I know the field is private.
Actually my question is why an inherited method cannot access the private field ?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's private rather than protected?
- Peter
 
Nicolas Brasseur
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
have a look at my classes, the field is in the class that try to access it ... Why an inherited method cannot access the private field ?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Apologies for being so thick! Now I see your problem.
This is in general a feature of the Java security mechanism: unprivileged code cannot boost its privileges by being called from privileged code. This prevents, among other things, system callbacks from opening up security holes.
In this particular case, Subclass is allowed to access its own private member variable, but Ancestor is not. The fact that Ancestor is called by Subclass doesn't change that one bit.
This also ties in with access rules in the Java language itself. Ancestor has no access to Subclass' private parts any more than Subclass has to Ancestor's private parts. Reflection does not provide you with a way to circumvent these language-level restrictions.
- Peter
 
Nicolas Brasseur
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer, any documentation concerning this ? Maybe in the specification ?
Thanks in advance
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
[...] Reflection does not provide you with a way to circumvent these language-level restrictions.
Please allow me to eat my words.
*munch*
*munch*
*spit*
YECH :roll:
The Java language security policy for applications is that any code may gain reflective access to all the members and constructors (including non-public members and constructors) of any class it may link against. By default, application code that gains reflective access to a member or constructor may only use the reflected member or constructor with standard Java language access control.
So far, so good. This is the Reflection Overview from the JDK, by the way, Security Model: Java Language Policy. But the spec continues with a bit that I've never paid attention to before:
The standard policy may be overridden by calling the reflected member's setAccessible method. The ability to call the setAccessible method is in turn controlled by the suppressAccessChecks target of the permission ReflectPermission.
So provided that your code is granted suppressAccessChecks, you can "switch off" the normal Java language rules and access private members. Or, at least, that's what it looks like. My apologies for spouting garbage.Could you try and let us know?
- Peter
 
Nicolas Brasseur
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Peter, it works fine using setAccessible().
Nicolas
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!