Get public methods of a Java class

9

I need to get the list of public methods of a Java class, but I do not want to get the wait , toString , hasCode , etc, that the Java class inherits. I just want to get the public methods of that class, not those inherited from their superclass.

Is there a method in the Java reflection to get only those methods?

Right now this is my code:

 String miclase;
 Class<?> clase;
 clase = Class.forName(miclase);
 Method[] allMethods = clase.getMethods();

And I do not want to get the methods wait , wait , wait , equals , toString , hasCode , getClass , notify and notifyAll

UPDATE This is my code now after using the first of the answers and now it does NOT print the legacy methods or the private methods when I read the parameters of the methods:

String miclase; 
Class<?> clase; 
clase = Class.forName(miclase); 
Method[] allMethods = clase.getDeclaredMethods();
for (Method method : allMethods){
  if (Modifier.isPublic(method.getModifiers())) {
                    Parameter[] params = method.getParameters();
                    for (Parameter parametro : params) {
                        System.out.println("name: "+parametro.getName()+" type:" +parametro.getType());
                    }

                } }
    
asked by jjmartinez 21.02.2018 в 13:28
source

2 answers

13

You could use getDeclaredMethods() that returns all the methods of the class, excluding the inherited ones. But they would not be just the public.

To filter the public you would have to use Modifier.isPublic(method.getModifiers()) for each method of the array.

Example:

Method[] methods = TuClase.class.getDeclaredMethods();
    for(Method method : methods) {
        if (Modifier.isPublic(method.getModifiers())) {
            System.out.println(method.getName());
        }
    }
}

Complete example with execution:

package prueba;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;

public class MiClase {

    public static void main(String[] args) throws ClassNotFoundException {

        String miclase = "prueba.MiClase";
        Class<?> clase;
        clase = Class.forName(miclase);
        Method[] allMethods = clase.getDeclaredMethods();
        for (Method method : allMethods) {
            if (Modifier.isPublic(method.getModifiers())) {
                Parameter[] params = method.getParameters();
                System.out.println("method: " + method.getName());
                for (Parameter parametro : params) {
                    System.out.println("param name: " + parametro.getName() + " type:" + parametro.getType());
                }

            }
        }

    }

    public void publico(int a, String b) {
    }

    private void privado(double c) {
    }
}

Result:

run:
method: publico
param name: arg0 type:int
param name: arg1 type:class java.lang.String
method: main
param name: arg0 type:class [Ljava.lang.String;
BUILD SUCCESSFUL (total time: 0 seconds)
    
answered by 21.02.2018 / 13:36
source
9

You can check that the method is declared in the relevant class:

for (Method m: allMethods) {
    if (m.getDeclaringClass.equals(clase) && Modifier.isPublic(m.getModifiers())) {
        ...
    }
}
    
answered by 21.02.2018 в 13:36