1 package Reflection;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.lang.reflect.Modifier;
7
8 class Demo{
9
10 }
11
12 interface China {
13 public static final String name = "Rollen";
14 public static int age = 20;
15
16 public void sayChina();
17
18 public void sayHello(String name, int age);
19 }
20
21 class Person implements China{
22
23 /*must have a none parameter constructor*/
24 public Person(){
25
26 }
27
28 public Person(String name, int age){
29 this.name = name;
30 this.age = age;
31 }
32
33 public String getName() {
34 return name;
35 }
36
37 public void setName(String name) {
38 this.name = name;
39 }
40
41 public int getAge() {
42 return age;
43 }
44
45 public void setAge(int age) {
46 this.age = age;
47 }
48
49 @Override
50 public String toString() {
51 return "[" + this.name + " " + this.age + "]";
52 }
53
54 private String name;
55 private int age;
56 @Override
57 public void sayChina() {
58 // TODO Auto-generated method stub
59
60 }
61
62 @Override
63 public void sayHello(String name, int age) {
64 // TODO Auto-generated method stub
65
66 }
67 }
68
69
70 public class Reflection {
71
72 public static void main(String[] args) {
73 /**
74 * for class Demo
75 */
76 Demo demo = new Demo();
77 System.out.println(demo.getClass().getName());
78 Class<?> demo1 = null;
79 Class<?> demo2 = null;
80 Class<?> demo3 = null;
81 try {
82 demo1 = Class.forName("Reflection.Demo");
83 demo2 = new Demo().getClass();
84 demo3 = Demo.class;
85 } catch (ClassNotFoundException e) {
86 // TODO Auto-generated catch block
87 e.printStackTrace();
88 }
89 System.out.println(demo1.getSimpleName()+" "+demo2.getName()+" "+demo3.getName());
90
91
92 /**
93 * For class Person
94 */
95 Class<?> person = Person.class;
96 Person p = null;
97 try {
98 p = (Person)person.newInstance();
99 } catch (InstantiationException | IllegalAccessException e) {
100 // TODO Auto-generated catch block
101 e.printStackTrace();
102 }
103 p.setName("Kelvin");
104 p.setAge(24);
105 System.out.println(p.toString());
106
107 Class<?> ps = new Person().getClass();
108 Constructor<?>[] cons = ps.getConstructors();
109 for(int i = 0;i< cons.length;i++){
110 int modifier = cons[i].getModifiers();
111
112 System.out.println("constructor:"+ cons[i] );
113 System.out.println("constructor:"+Modifier.toString(modifier)+" "+ cons[i] );
114 }
115 try {
116 Person p1 = (Person) cons[0].newInstance();
117 System.out.println(p1.toString());
118 Person p2 = (Person) cons[1].newInstance("summer",22);
119 System.out.println(p2.toString());
120 Class<?>[] ifs = p2.getClass().getInterfaces();
121 System.out.println(ifs[0].getSimpleName());
122
123 Person p3 = (Person) cons[2].newInstance();
124 Person p4 = (Person) cons[3].newInstance();
125 } catch (InstantiationException | IllegalAccessException
126 | IllegalArgumentException | InvocationTargetException e) {
127 // TODO Auto-generated catch block
128 e.printStackTrace();
129 }
130
131 /**
132 * To get all methods in Person
133 */
134 Method[] methods = new Person().getClass().getMethods();
135 for(int i =0;i < methods.length;i++){
136 System.out.println(methods[i].getName());
137 }
138
139 try {
140 Method method = new Person().getClass().getMethod("setName", String.class);
141 Person pm = new Person();
142 try {
143 method.invoke(pm, "jean");
144 System.out.println(pm.toString());
145 } catch (IllegalAccessException | IllegalArgumentException
146 | InvocationTargetException e) {
147 // TODO Auto-generated catch block
148 e.printStackTrace();
149 }
150 } catch (NoSuchMethodException | SecurityException e) {
151 // TODO Auto-generated catch block
152 e.printStackTrace();
153 }
154
155 }
156
157 }