import java.lang.reflect.*; // gain access to Core Reflection API public class GetMethods { //------------------------------------------------------------------- // instantiate a GetMethods object and find its constructor and main() //------------------------------------------------------------------- public static void main (String[] args) { Object anObject; Class objectClass; Constructor myConstructor; Method myMain; anObject = new GetMethods(); objectClass = anObject.getClass(); myConstructor = getDefaultConstructor(objectClass); System.out.println("got Constructor object: " + myConstructor); myMain = getMainMethod(objectClass); System.out.println("got Method object : " + myMain); } //------------------------------------------------------------------- // default GetMethods Constructor //------------------------------------------------------------------- public GetMethods() { // default constructor } //------------------------------------------------------------------- // obtain a Constructor object incarnating our own default constructor //------------------------------------------------------------------- public static Constructor getDefaultConstructor(Class ourselves) { Constructor myConstructor=null; // construct default constructor's argument list (i.e. empty!) Class[] constructorArgTypes = { }; // now try and find the constructor's Constructor instance try { myConstructor = ourselves.getConstructor(constructorArgTypes); } catch (NoSuchMethodException goingCrazy) { System.out.println("Couldn't find my own default constructor!!"); System.exit(100); } return myConstructor; } //------------------------------------------------------------------- // obtain a Method object incarnating our own main() entry point //------------------------------------------------------------------- public static Method getMainMethod(Class ourselves) { Method myMain=null; // construct static main()'s argument list (i.e. array of String) Class[] mainArgTypes = new Class[1]; mainArgTypes[0] = java.lang.String[].class; // now try and find the method's Method instance try { myMain = ourselves.getMethod("main", mainArgTypes); } catch (NoSuchMethodException goingCrazy) { System.out.println("Couldn't find my own main()!!"); System.exit(100); } return myMain; } } // End of Class GetMethods