Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of accessible methods for a given class via reflection

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:

  • all of its own methods
  • 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?

like image 898
ChssPly76 Avatar asked Dec 07 '09 04:12

ChssPly76


People also ask

Which method is used to get methods using reflection?

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.

Which method is accessible only in the classes in which it is defined?

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”.

How do you find the class object of associated class using reflection?

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.

How do you create an instance of a class using reflection?

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.


1 Answers

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());     }   } } 
like image 84
cletus Avatar answered Sep 28 '22 05:09

cletus