-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflection.java
More file actions
43 lines (30 loc) · 1.29 KB
/
Reflection.java
File metadata and controls
43 lines (30 loc) · 1.29 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
38
39
40
41
42
43
import java.lang.reflect.Method;
class main {
public static void main (String[] args) {
ReflectionExampleNoReturnType noReturnType = new ReflectionExampleNoReturnType();
ReflectionExampleWithReturnType returnType = new ReflectionExampleWithReturnType();
String methodName = "reflectionPerfection";
try {
System.out.println("<< One");
Method method = noReturnType.getClass().getMethod(methodName);
method.invoke(noReturnType);
System.out.println(" ");
System.out.println("<< Two");
method = noReturnType.getClass().getMethod(methodName, String.class);
method.invoke(noReturnType, "Parameter Value");
System.out.println(" ");
System.out.println("<< Three");
method = returnType.getClass().getMethod(methodName, String.class);
returned = (String) method.invoke(returnType, "Parameter Value");
System.out.println("Returned value: '" + returned + "'");
System.out.println(" ");
System.out.println("<< Four");
method = returnType.getClass().getMethod(methodName);
String returned = (String) method.invoke(returnType);
System.out.println("Returned value: '" + returned + "'");
} catch (NoSuchMethodException e) {
System.out.println("Reflection says no");
} catch (Exception e) {
}
}
}