Is there a way to get a list of methods that would be accessible (not necessarily public) by a given class? The code in question will be in a completely different class.
Example:
public class A { public void methodA1(); protected void methodA2(); void methodA3(); private void methodA4(); } public class B extends A { public void methodB1(); protected void methodB2(); private void methodB3(); }
For class B
I'd like to get:
methodA1
and methodA2
from class A
methodA3
if and only if class B
is in the same package as A
methodA4
should never be included in results because it's inaccessible to class B
. To clarify once again, code that needs to find and return the above methods will be in a completely different class / package.
Now, Class.getMethods()
only returns public methods and thus won't do what I want; Class.getDeclaredMethods()
only returns methods for current class. While I can certainly use the latter and walk the class hierarchy up checking the visibility rules manually, I'd rather not if there's a better solution. Am I missing something glaringly obvious here?
The getConstructors() method is used to get the public constructors of the class to which an object belongs. The getMethods() method is used to get the public methods of the class to which an object belongs. We can invoke a method through reflection if we know its name and parameter types.
The methods or data members declared as private are accessible only within the class in which they are declared. Any other class of the same package will not be able to access these members. private means “only visible within the enclosing class”.
8. How to get the class object of associated class using Reflection? Explanation: forName(String className) returns the Class object associated with the class or interface with the given string name.
We can use newInstance() method on the constructor object to instantiate a new instance of the class. Since we use reflection when we don't have the classes information at compile time, we can assign it to Object and then further use reflection to access it's fields and invoke it's methods.
Use Class.getDeclaredMethods()
to get a list of all methods (private or otherwise) from the class or interface.
Class c = ob.getClass(); for (Method method : c.getDeclaredMethods()) { if (method.getAnnotation(PostConstruct.class) != null) { System.out.println(method.getName()); } }
Note: this excludes inherited methods. Use Class.getMethods()
for that. It will return all public methods (inherited or not).
To do a comprehensive list of everything a class can access (including inherited methods), you will need to traverse the tree of classes it extends. So:
Class c = ob.getClass(); for (Class c = ob.getClass(); c != null; c = c.getSuperclass()) { for (Method method : c.getDeclaredMethods()) { if (method.getAnnotation(PostConstruct.class) != null) { System.out.println(c.getName() + "." + method.getName()); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With