Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access protected Java method in thirdparty library?

Assume that you must access a protected method of a Java object that you receive somewhere in your code. What is your solution?

I know one approach: You can employ reflection and call setAccessible(true) on the Method object.

Any other idea?

like image 608
salman.mirghasemi Avatar asked Jun 20 '11 15:06

salman.mirghasemi


1 Answers

As per the Java access modifiers, besides extending the object (which you can't if you receive the object) is to access it from an object in the same package as the object you received. So your option is to create a wrapper class in the same package which retrieves the attribute via the protected method for you.

like image 189
rsp Avatar answered Oct 25 '22 14:10

rsp