Method: getName() : Method « java.lang.reflect « Java by API






Method: getName()

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

public class Main {
  public static void main(String[] args) {
    String name = "java.util.Date";
    try {
      Class cl = Class.forName(name);
      System.out.println("class " + name);
      System.out.println("Its methods:");
      printMethods(cl);
      System.out.println();
    } catch (ClassNotFoundException e) {
      System.out.println("Class not found.");
    }
  }
  public static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
      Method m = methods[i];
      Class retType = m.getReturnType();
      Class[] paramTypes = m.getParameterTypes();
      String name = m.getName();
      System.out.print(Modifier.toString(m.getModifiers()));
      System.out.print(" " + retType.getName() + " " + name + "(");
      for (int j = 0; j < paramTypes.length; j++) {
        if (j > 0)
          System.out.print(", ");
        System.out.print(paramTypes[j].getName());
      }
      System.out.println(");");
    }
  }
}

   
  








Related examples in the same category

1.Method: getAnnotation(Class < MyAnno > annotationClass)
2.Method: getExceptionTypes()
3.Method: getModifiers()
4.Method: getParameterTypes()
5.Method: getReturnType()
6.Method: invoke(Object obj, Object... args)
7.Method: setAccessible(boolean flag)
8.Method: toGenericString()
9.Method: toString()