-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflection.java
More file actions
38 lines (31 loc) · 1.92 KB
/
Copy pathReflection.java
File metadata and controls
38 lines (31 loc) · 1.92 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
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Reflection {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
PrintHello reflectionClassWithoutReflection = new PrintHello();
reflectionClassWithoutReflection.setValue("Hello Java without Reflection !!!");
System.out.println(reflectionClassWithoutReflection.getValue());
Object reflectionClassWithReflection = PrintHello.class.getDeclaredConstructor().newInstance();
Method setValue = reflectionClassWithReflection.getClass().getDeclaredMethod("setValue", String.class);
setValue.invoke(reflectionClassWithReflection, "Hello Java with Reflection !!!");
Method method = reflectionClassWithReflection.getClass().getDeclaredMethod("printHello");
method.setAccessible(true);
method.invoke(reflectionClassWithReflection);
Field declaredField = reflectionClassWithReflection.getClass().getDeclaredField("value");
System.out.println("Name of the declared field value name is: " + declaredField.getName());
Field[] fields = reflectionClassWithReflection.getClass().getDeclaredFields();
System.out.print("Declared fields of PrintHello class are: ");
int fieldNumber = fields.length;
int i = 0;
for(Field field : fields) {
System.out.print(field.getName());
if (++i != fieldNumber) System.out.print(", ");
}
System.out.println();
Object executeTestClass = ReflectionGetClassInstanceTest.class.getDeclaredConstructor().newInstance();
Method executeTestMethod = executeTestClass.getClass().getDeclaredMethod("executeTest");
executeTestMethod.setAccessible(true);
executeTestMethod.invoke(executeTestClass);
}
}