Java - Reflection Method Reflection

Introduction

The following four methods in the Class class returns information about the methods of a class:

Method
Description
Method[] getMethods()

returns all the accessible public methods of the class. The accessible public methods
include any public method declared in the class or inherited from the superclass.
Method[] getDeclaredMethods()

returns all the methods declared only in the class. It does not return any methods that are inherited from the
superclass.
Method getMethod(String name,
Class... parameterTypes)
get the Method object if you know the name of the method and its parameter types.

Method getDeclaredMethod(String name,
Class... parameterTypes)
get the Method object if you know the name of the method and its parameter types.It does not return any methods that are inherited from the
superclass.

getReturnType() method of the Method class returns the Class object, which contains information about the return type of the method.

The following code illustrates how to get information about the methods of a class.

Demo

import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    Class<String> c = String.class;

    // Get the declared methods
    ArrayList<String> methodsDesciption = getDeclaredMethodsList(c);
    System.out.println("Declared Methods for " + c.getName());
    for (String desc : methodsDesciption) {
      System.out.println(desc);/*ww w .  j ava2  s .  c o m*/
    }
    methodsDesciption = getMethodsList(c);

    System.out.println("\nMethods for " + c.getName());
    for (String desc : methodsDesciption) {
      System.out.println(desc);
    }

  }

  public static ArrayList<String> getMethodsList(Class c) {
    Method[] methods = c.getMethods();
    ArrayList<String> methodsList = getMethodsDesciption(methods);
    return methodsList;
  }

  public static ArrayList<String> getDeclaredMethodsList(Class c) {
    Method[] methods = c.getDeclaredMethods();
    ArrayList<String> methodsList = getMethodsDesciption(methods);
    return methodsList;
  }

  public static ArrayList<String> getMethodsDesciption(Method[] methods) {
    ArrayList<String> methodList = new ArrayList<>();

    for (Method m : methods) {
      String modifiers = ExecutableUtil.getModifiers(m);

      // Get the method return type
      Class returnType = m.getReturnType();
      String returnTypeName = returnType.getSimpleName();

      // Get the name of the method
      String methodName = m.getName();

      // Get the parameters of the method
      ArrayList<String> paramsList = ExecutableUtil.getParameters(m);
      String params = ExecutableUtil.arrayListToString(paramsList, ",");

      // Get the Exceptions thrown by method
      String throwsClause = ExecutableUtil.getThrowsClause(m);

      methodList.add(modifiers + " " + returnTypeName + " " + methodName + "(" + params + ") " + throwsClause);
    }

    return methodList;
  }
}

class ExecutableUtil {
  public static ArrayList<String> getParameters(Executable exec) {
    Parameter[] parms = exec.getParameters();
    ArrayList<String> parmList = new ArrayList<>();
    for (int i = 0; i < parms.length; i++) {
      // Get modifiers, type, and name of teh parameter
      int mod = parms[i].getModifiers() & Modifier.parameterModifiers();
      String modifiers = Modifier.toString(mod);
      String parmType = parms[i].getType().getSimpleName();
      String parmName = parms[i].getName();
      String temp = modifiers + " " + parmType + " " + parmName;

      // Trim it as it may have leading spaces when modifiers are absent
      parmList.add(temp.trim());
    }
    return parmList;
  }

  public static ArrayList<String> getExceptionList(Executable exec) {
    ArrayList<String> exceptionList = new ArrayList<>();
    for (Class<?> c : exec.getExceptionTypes()) {
      exceptionList.add(c.getSimpleName());
    }
    return exceptionList;
  }

  public static String getThrowsClause(Executable exec) {
    ArrayList<String> exceptionList = getExceptionList(exec);
    String exceptions = ExecutableUtil.arrayListToString(exceptionList, ",");
    String throwsClause = "";
    if (exceptionList.size() > 0) {
      throwsClause = "throws " + exceptions;
    }

    return throwsClause;
  }

  public static String getModifiers(Executable exec) {
    // Get the modifiers for the class
    int mod = exec.getModifiers();
    if (exec instanceof Method) {
      mod = mod & Modifier.methodModifiers();
    } else if (exec instanceof Constructor) {
      mod = mod & Modifier.constructorModifiers();
    }
    return Modifier.toString(mod);
  }

  public static String arrayListToString(ArrayList<String> list, String saparator) {
    String[] tempArray = new String[list.size()];
    tempArray = list.toArray(tempArray);
    String str = String.join(saparator, tempArray);
    return str;
  }
}

Result