-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMethodInvocationDemo.java
More file actions
37 lines (32 loc) · 1.12 KB
/
Copy pathMethodInvocationDemo.java
File metadata and controls
37 lines (32 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
class X{
public void objectMethod(String arg){
System.out.println("Instance method: " + arg);
}
public static void classMethod(){
System.out.println("Class method.");
}
}
public class MethodInvocationDemo{
public static void main(String[] args){
try{
Class<?> clazz = Class.forName("X");
X x = (X)clazz.newInstance();
Method method = clazz.getMethod("objectMethod", new Class[]{String.class});
method.invoke(x, "This is method argument.");
method = clazz.getMethod("classMethod", (Class[])null); // (Class[])null is equivalent to the empty array.
method.invoke(null, (Object[])null); // second parameter is equivalent to an empty array. It is prefered instead of an empty array's allocation.
}catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}catch(InstantiationException ie){
ie.printStackTrace();
}catch(NoSuchMethodException nsme){
nsme.printStackTrace();
}catch(IllegalAccessException iae){
iae.printStackTrace();
}catch(InvocationTargetException ite){
ite.printStackTrace();
}
}
}