• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing protected methods from non-derived classes  RSS feed

 
Deepa Guha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible, any workaround thro which a protected member of a class can be accessed from non-derived classes?.

Will type casting an object to its class type help in any way?

thanks!!
 
Alan Jump
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AFAIK, once a method is declared protected, the only way to access the method from outside its package is through inheritance.

I just might be wrong, though...anyone point me to something that says it can be done otherwise?
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at setAccessible(true) in java.lang.reflect.Method

This can change the access modifiers on a method using reflection. You can use it to invoke a method that is private or protected.

Geoffrey
 
Anil Chandran
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use getDeclaredMethods()in the java.lang.Class

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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Object obj = Class.forName("one.TryReflect_1_1").newInstance();
does not cause an exception, there should be a way of running the above code as well???
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj,

So what you mean by this is if the called method has private modifier and is called from inside the same package, it will work fine. and if that is in different package it won't work.

and yeah obviously what i am talking about is through reflection.

Please do correct me if I am wrong.

Cheers,
Rahul
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!