using [java.lang.reflect.Method]{style="font-family: courier new,courier;"} you can get metadata/signature of methods (including private) and can invoke accessible methods at runtime.
Class Method provides a great tool to list all methods and find information about methods. following tutorial will provide you more detail of class Method and invoking a method at runtime.
- Getting List of Methods
- Getting metadata of methods
- Getting Method Object
- Invoke methods at Runtime
- Ways of invoking methods at Runtime
Getting List of Method Objects
//Getting all methods of class java.lang.reflect.Method itself. by loading a class at run time
Method[] methodList = Class.forName("java.lang.reflect.Method").getDeclaredMethods();
//you can also use object of a class, but in this case you need to create an object of class
Method[] methodList = myClass.getDeclaredMethods();
Getting information/metadata about methods
Expand following code snippet.
Following code will print whole method signature. Alternatively you can use specific method to get parameters, returnType and exceptions.
for (Method method : methodList){
System.out.println(method.toString);
}
Following methods are availble in class java.lang.reflect.Method
/*
invoke
equals
toString
hashCode
getModifiers
getName
getAnnotation
getDeclaredAnnotations
getDeclaringClass
getFactory
getGenericInfo
getGenericInfo
getParameterTypes
getReturnType
getTypeParameters
isSynthetic
toGenericString
copy
getGenericSignature
getGenericParameterTypes
getParameterAnnotations
getParameterCount
isVarArgs
acquireMethodAccessor
getAnnotatedReturnType
getAnnotationBytes
getDefaultValue
getExceptionTypes
getGenericExceptionTypes
getGenericReturnType
getMethodAccessor
handleParameterNumberMismatch
hasGenericInformation
isBridge
isDefault
setMethodAccessor
specificToGenericStringHeader
specificToStringHeader
*/
getting a Method object
//Load a class and get method object
Class clazz = Class.forName("theGeekyWay.myClass");
Method method = clazz.getMethod("myPublicMethod", String.class);
//Note: getMethod throws NoSuchMethodException (if method not available) , SecurityException (if method not accessible)
Invoking methods at Runtime
public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException
Method invoke take variable length parameter, so you need to provide parameter type and number of parameter as calling method.
MyClass myclassobj = new MyClass();
Method method = myclassobj .getMethod("myPublicMethod", String.class);
method.invoke(myclassobj , "invoked a method through reflection");
//invoke method throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
If method is static just pass null
Class clazz1 = Class.forName("theGeekyWay.MyClass");
Method method1 = clazz1.getMethod("aStaticMethod"); //No mathod parameter
method1.invoke(null);
//Note: my static method doesn't have any parameter
Ways of loading class at run time and invoking method at run time
//invoking a method using 100% Reflection
try {
Class clazz = Class.forName("theGeekyWay.MyClass");
Method method = clazz.getMethod("myPublicMethod", String.class);
method.invoke((theGeekyWay.MyClass)clazz.newInstance(), "invoked a method through reflection");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
Another way to load class at run time and call method
try {
Class clzz = Class.forName("theGeekyWay.MyClass");
((MyClass)clzz.newInstance()).myPublicMethod("hkh........"); // make sure
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Not sure what exactly the difference between these two ways of invoking method. In both way loading of class happening at runtime. In 1st way invoke is being used while in second ways object.method is being used.