File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Reflection" Watch "Reflection" New topic
Author

Reflection

Nicolas Brasseur
Ranch Hand

Joined: Nov 09, 2001
Posts: 45
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


<a href="http://www.javablackbelt.com/?utm_source=javaranch&utm_medium=signature&utm_campaign=Forum%2BBuzz" target="_blank" rel="nofollow">BlackBeltFactory Communtiy</a> founder - Building better developers<br /><b>Free</b> courses and mock exams
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
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

Joined: Nov 09, 2001
Posts: 45
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

Joined: Apr 20, 2000
Posts: 3252
Because it's private rather than protected?
- Peter
Nicolas Brasseur
Ranch Hand

Joined: Nov 09, 2001
Posts: 45
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Nov 09, 2001
Posts: 45
Thanks for your answer, any documentation concerning this ? Maybe in the specification ?
Thanks in advance
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Nov 09, 2001
Posts: 45
Thanks a lot Peter, it works fine using setAccessible().
Nicolas
 
 
subject: Reflection
 
Threads others viewed
Performance issue with Java 'File' class
Polymorphism
Access Private Variables outside the class?
Properties in Java
Color Parameter
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture