-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReflectionExample.java
More file actions
56 lines (39 loc) · 2.09 KB
/
ReflectionExample.java
File metadata and controls
56 lines (39 loc) · 2.09 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
44
45
46
47
48
49
50
51
52
53
54
55
package reflection;
import java.lang.Class;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import jdk.nashorn.internal.ir.ForNode;
public class ReflectionExample {
public static void main(String[] args) throws ClassNotFoundException,NoSuchMethodException,SecurityException {
Class c = Class.forName("reflection.ReflectionStudent");
Class i = Class.forName("reflection.ReflectionInterface");
System.out.println("c.getName(): "+c.getName());
System.out.println("i.getName(): "+i.getName());
System.out.println("i.isInterface(): "+i.isInterface());
int[] ar = new int[5];
System.out.println("ar.getClass().isArray(): "+ar.getClass().isArray());
System.out.println("ar.getClass().isPrimitive(): "+ar.getClass().isPrimitive());
System.out.println("c.getSuperClass(): "+c.getSuperclass());
Field[] cFields = c.getDeclaredFields();
System.out.println("all declared fields in thestudent class: "+Arrays.toString(cFields));
Method[] cMethods = c.getDeclaredMethods();
System.out.println("all declared methods in the student class: "+Arrays.toString(cMethods));
Constructor<?> []cConstructors = c.getDeclaredConstructors();
System.out.println("all declared Constructors are: "+Arrays.toString(cConstructors));
Field []clF = c.getFields();
System.out.println("all public fields from specified class and its super class: "+Arrays.toString(clF));
Method []clM = c.getMethods();
System.out.println("all public methods from specified class and its super class: "+Arrays.toString(clM));
Constructor<?> []clC = c.getConstructors();
System.out.println("all public constructors from specified class and its super class: "+Arrays.toString(clC));
System.out.println("c.getMethod(\"display\",null): "+c.getMethod("display",null));
ReflectionStudent rs = new ReflectionStudent(102,"carter",45.0);
System.out.println(rs.getClass());
Class name = ReflectionStudent.class;
System.out.println(name);
Class x = Class.forName("java.lang.String");
System.out.println(x.getName());
}
}