Skip to content

Commit 3b7bc78

Browse files
committed
添加反射例子到reflectiontutorial
1 parent 42d06f7 commit 3b7bc78

6 files changed

Lines changed: 158 additions & 17 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.byhieg.reflectiontutorial;
2+
3+
/**
4+
* Created by byhieg on 17/1/9.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
@MyAnnotation(name="byhieg",value = "hello world")
8+
public class AnnotationObject {
9+
10+
@MyAnnotation(name="field",value = "变量")
11+
public String field;
12+
13+
@MyAnnotation(name="method",value = "方法")
14+
public void doSomeThing(){
15+
System.out.println("做一些事情");
16+
}
17+
18+
public void doOtherThing(@MyAnnotation(name="param",value = "参数") String param){
19+
20+
}
21+
}

src/main/java/cn/byhieg/reflectiontutorial/ExampleObject.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* Created by shiqifeng on 2017/1/9.
55
* Mail byhieg@gmail.com
66
*/
7-
public class ExampleObject extends FatherObject{
7+
public class ExampleObject extends FatherObject {
88
public int age = 30;
99
public String name = "byhieg";
1010
private Integer score = 60;
1111

12-
public void printName(){
12+
public void printName() {
1313
System.out.println(name);
1414
}
1515

@@ -37,21 +37,25 @@ public void setScore(Integer score) {
3737
this.score = score;
3838
}
3939

40-
41-
public ExampleObject(){
40+
public ExampleObject() {
4241

4342
}
4443

45-
public ExampleObject(String name){
44+
public ExampleObject(String name) {
4645

4746
}
4847

49-
public ExampleObject(int age,Integer score){
48+
public ExampleObject(int age, Integer score) {
5049

5150
}
5251

5352
@Override
5453
public void doSomething() {
5554
super.doSomething();
5655
}
56+
57+
@Override
58+
public void run() {
59+
System.out.println("run......");
60+
}
5761
}

src/main/java/cn/byhieg/reflectiontutorial/FatherObject.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
* Created by shiqifeng on 2017/1/9.
55
* Mail byhieg@gmail.com
66
*/
7-
public class FatherObject implements Runnable{
7+
public abstract class FatherObject implements Runnable{
88

99

10-
public void run() {
11-
System.out.println("运行中......");
12-
}
13-
10+
protected String father = "";
1411
public void doSomething(){
1512
System.out.println("做事情......");
1613
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.byhieg.reflectiontutorial;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by byhieg on 17/1/9.
10+
* Mail to byhieg@gmail.com
11+
*/
12+
13+
@Retention(RetentionPolicy.RUNTIME)
14+
public @interface MyAnnotation {
15+
public String name();
16+
public String value();
17+
18+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cn.byhieg.reflectiontutorialtest;
2+
3+
import cn.byhieg.reflectiontutorial.AnnotationObject;
4+
import cn.byhieg.reflectiontutorial.MyAnnotation;
5+
import junit.framework.TestCase;
6+
7+
import java.lang.annotation.Annotation;
8+
import java.lang.reflect.Field;
9+
import java.lang.reflect.Method;
10+
11+
/**
12+
* Created by byhieg on 17/1/9.
13+
* Mail to byhieg@gmail.com
14+
*/
15+
public class AnnotationObjectTest extends TestCase {
16+
17+
public void testAnnotation() throws Exception{
18+
Class clz = AnnotationObject.class;
19+
Annotation[] annotationsInClass = clz.getAnnotations();
20+
for (Annotation annotation : annotationsInClass){
21+
if (annotation instanceof MyAnnotation){
22+
MyAnnotation myAnnotation = (MyAnnotation)annotation;
23+
System.out.println("name: " + myAnnotation.name());
24+
System.out.println("value:" + myAnnotation.value());
25+
}
26+
}
27+
28+
Method method = clz.getMethod("doSomeThing");
29+
Annotation[] annotationsInMethod = method.getDeclaredAnnotations();
30+
for (Annotation annotation : annotationsInMethod){
31+
if (annotation instanceof MyAnnotation){
32+
MyAnnotation myAnnotation = (MyAnnotation)annotation;
33+
System.out.println("name: " + myAnnotation.name());
34+
System.out.println("value:" + myAnnotation.value());
35+
}
36+
}
37+
38+
Field field = clz.getField("field");
39+
Annotation[] annotationsInField = field.getDeclaredAnnotations();
40+
for (Annotation annotation : annotationsInField){
41+
if (annotation instanceof MyAnnotation){
42+
MyAnnotation myAnnotation = (MyAnnotation)annotation;
43+
System.out.println("name: " + myAnnotation.name());
44+
System.out.println("value:" + myAnnotation.value());
45+
}
46+
}
47+
48+
Method method1 = clz.getMethod("doOtherThing",String.class);
49+
Annotation[][] annotationInParam = method1.getParameterAnnotations();
50+
Class[] params = method1.getParameterTypes();
51+
int i = 0;
52+
for (Annotation[] annotations: annotationInParam){
53+
Class para = params[i++];
54+
for (Annotation annotation : annotations){
55+
if(annotation instanceof MyAnnotation){
56+
MyAnnotation myAnnotation = (MyAnnotation) annotation;
57+
System.out.println("param: " + para.getName());
58+
System.out.println("name : " + myAnnotation.name());
59+
System.out.println("value :" + myAnnotation.value());
60+
}
61+
62+
}
63+
}
64+
65+
}
66+
}

src/test/java/cn/byhieg/reflectiontutorialtest/ExampleObjectTest.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class ExampleObjectTest extends TestCase {
1717

1818
public void testReflect()throws Exception{
19-
Class exampleObjectClass = ExampleObject.class;
19+
Class exampleObjectClass = Class.forName("cn.byhieg.reflectiontutorial.ExampleObject");
2020
//得到类的名字
2121
String fullClassName = exampleObjectClass.getName();
2222
String simpleClassName = exampleObjectClass.getSimpleName();
@@ -30,6 +30,20 @@ public void testReflect()throws Exception{
3030
System.out.println("method = "+ method.getName());
3131
}
3232

33+
Method method = exampleObjectClass.getMethod("setAge",int.class);
34+
System.out.println(method.getName());
35+
for (Class clz : method.getParameterTypes()){
36+
System.out.println("方法的参数" + clz.getName());
37+
}
38+
System.out.println(method.getReturnType().getName());
39+
40+
System.out.println(method.invoke(exampleObjectClass.newInstance(),1));
41+
42+
43+
44+
45+
46+
3347
//得到类的关键字
3448
int modifiers = exampleObjectClass.getMethods()[0].getModifiers();
3549
System.out.println(Modifier.isPublic(modifiers));
@@ -43,26 +57,47 @@ public void testReflect()throws Exception{
4357
//得到父类
4458
Class superClass = exampleObjectClass.getSuperclass();
4559
System.out.println(superClass.getSimpleName());
46-
60+
System.out.println("父类是不是抽象类 " + Modifier.isAbstract(superClass.getModifiers()));
61+
Field field2 = superClass.getDeclaredField("father");
62+
System.out.println(field2.getName());
4763

4864
//得到接口
4965
Class[] classes = superClass.getInterfaces();
5066
System.out.println("父类的接口" + classes[0]);
5167

5268
//构造器
5369
Constructor[] constructors = exampleObjectClass.getConstructors();
54-
for (Constructor constructor : constructors){
55-
System.out.println(constructor.toString());
70+
for (int i = 0;i < constructors.length;i++){
71+
Class[] parameterTypes = constructors[i].getParameterTypes();
72+
System.out.println("构造器参数如下========================");
73+
for (Class clz : parameterTypes){
74+
System.out.println("参数类型 " + clz.toString());
75+
}
5676
}
77+
//得到构造器的参数类型
78+
Constructor constructor = exampleObjectClass.getConstructor(int.class,Integer.class);
79+
System.out.println(constructor.getParameterTypes()[0].toString());
80+
System.out.println(constructor.toString());
81+
82+
Constructor constructor1 = exampleObjectClass.getConstructor(String.class);
83+
System.out.println(constructor1.newInstance("byhieg"));
84+
5785

5886
//得到变量
5987
Field[] fields = exampleObjectClass.getFields();
6088
for (Field field : fields){
61-
System.out.println(field.toString());
89+
System.out.println("变量为: " + field.getName());
6290
}
91+
Field field1 = exampleObjectClass.getField("age");
92+
System.out.println("变量为:" + field1.toString());
93+
94+
ExampleObject object = ((ExampleObject) constructor1.newInstance("byhieg"));
95+
System.out.println("原先的age是 " + object.age);
96+
field1.set(object,10);
97+
System.out.println("更改之后的age是" + object.age);
6398

6499
//得到注解
65-
Annotation[] annotations = exampleObjectClass.getMethod("doSomething").getAnnotations();
100+
Annotation[] annotations = exampleObjectClass.getAnnotations();
66101
for (Annotation annotation : annotations){
67102
System.out.println(annotation.toString());
68103
}

0 commit comments

Comments
 (0)