How can it be ? The class.getMethod() function returns only public methods . Is there any way that we can get the protectected/private methods(instance of java.lang.reflect.Method) to call setAccessible(true).
So I think there is no way that ,we can access protected member from a nonderived class.
I started out with nothing..I still have most of it ...
This will returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private methods, but excludes inherited methods.
Be aware that you are deliberately subverting the original author's design. There is always a chance that he thought things through better than you did. You are adding a dependency to a method that the author expressly said he didn't want you to have and he won't take you into consideration when making future changes. I'd look long and hard for an alternative before putting this kind of thing in production code. There may be a place for this in tests, but even that is debated at times.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
just working on this a little, just for checking it out, i made the following code which works fine( and that is scary):
so, building on this, i dared to venture further and put the two classes in two different packages. here is the code:
and the other class in the package "one" is:
this also works fine. but if i change the access modifier of the class one.TryReflect_1_1, then i get the following runtime exception:
Exception in thread "main" java.lang.IllegalAccessException: Class TryReflect_1 can not access a member of class one.TryReflect_1_1 with modifiers "" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57) at java.lang.Class.newInstance0(Class.java:302) at java.lang.Class.newInstance(Class.java:261) at TryReflect_1.main(TryReflect_1.java:16)
yes, one.TryReflect_1_1 has no access modifier which means it is accessible only in the same class, but doing this