Methods

This application asks the Mystery class for all of the methods declared within it, along with the return value and argument list for each.

import java.lang.reflect.*;

public class ReflectMethods  {


  public ReflectMethods  ()  {
    super ();
  }  // end ReflectMethods constructor

  public static void main ( String [ ] args )  {

    try  {

        Class theClass = Class.forName("Mystery");
        String s = theClass.getName();
        System.out.println("Name of class: " + s);
        System.out.println();

        System.out.println("Methods declared in class: " );
        Method[] theMethods = theClass.getDeclaredMethods();
        for (int i = 0; i < theMethods.length; i++) {
            String methodName = theMethods[i].getName();
            System.out.println(i+" Method Name: " + methodName);
            String returnString = theMethods[i].getReturnType().getName();
            System.out.println("   Return Type: " + returnString);
            Class[] parmTypes = theMethods[i].getParameterTypes();
            System.out.print("   Parameter Types:");
            for (int j = 0; j < parmTypes.length; j++) {
                String parmString = parmTypes[j].getName();
                System.out.print(" " + parmString);
            }  // end for: j
            System.out.println();
            System.out.println();
        }  // end for: i


    } // end try

    catch ( ClassNotFoundException e ) {
        System.out.println ( "Class not found: " + e );
    }


  }  // end main


}  // end ReflectMethods