Home Java反射的使用
Post
Cancel

Java反射的使用

Class定义

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface AnnotationA {

}

@AnnotationA
public class A {

    private String value1;
    public String value2;

    private A() {
        this("");
    }

    A(String str1) {
        this(str1, "");
    }

    public A(String str1, String str2) {
        this.value1 = str1;
        this.value2 = str2;
    }

    public String getValue1() {
        return value1;
    }

    void setValue1(String value1) {
        this.value1 = value1;
    }

    public String getValue2() {
        return value2;
    }

    private void setValue2(String value2) {
        this.value2 = value2;
    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface AnnotationB {

}


@AnnotationB
public class B extends A {

    String value3;
    public String value4;

    private B() {
        this("");
    }

    protected B(String str1) {
        this(str1, "");
    }

    public B(String str1, String str2) {
        super(str1, str2);
        this.value3 = str1;
        this.value4 = str2;
    }

    public String getValue3() {
        return value3;
    }

    void setValue3(String value3) {
        this.value3 = value3;
    }

    public String getValue4() {
        return value4;
    }

    private void setValue4(String value4) {
        this.value4 = value4;
    }

    @Override
    public String toString() {
        return "B{" +
                "value3='" + value3 + '\'' +
                ", value4='" + value4 + '\'' +
                '}';
    }
}

class类型

1
2
3
4
5
6
7
8
Class<B> bClass = B.class;

System.out.println("B class 类型:" + bClass);
System.out.println("B class 的 supperClass 类型:" +bClass.getSuperclass());

// 输出
// B class 类型:class B
// B class 的 supperClass 类型:class A

获取构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
Class<B> bClass = B.class;

// 获取public构造函数
Constructor<?>[] constructors = bClass.getConstructors();
System.out.println("B class public 构造函数:" + Arrays.toString(constructors));

// 获取所有构造函数
Constructor<?>[] declaredConstructors = bClass.getDeclaredConstructors();
System.out.println("B class 所有构造函数:" + Arrays.toString(declaredConstructors));

// 输出
// B class public 构造函数:[public B(java.lang.String,java.lang.String)]
// B class 所有构造函数:[public B(java.lang.String,java.lang.String), protected B(java.lang.String), private B()]

方法(不包括构造函数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 获取public方法(包括父类的)
Method[] methods = bClass.getMethods();
System.out.println("B class public 函数:" + Arrays.toString(methods));

// 获取所有方法(不包括父类)
Method[] declaredMethods = bClass.getDeclaredMethods();
System.out.println("B class 所有函数:" + Arrays.toString(declaredMethods));
// 输出
//B class public 函数:[public java.lang.String B.toString(), public java.lang.String B.getValue3(), public java.lang.String B.getValue4(), public java.lang.String A.getValue1(), public java.lang.String A.getValue2(), public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final void java.lang.Object.wait() throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll()]
//B class 所有函数:[public java.lang.String B.toString(), public java.lang.String B.getValue3(), void B.setValue3(java.lang.String), public java.lang.String B.getValue4(), private void B.setValue4(java.lang.String)]

Method method = bClass.getDeclaredMethod("setValue3", String.class);
method.setAccessible(true);
// 若此方法是静态方法,则第一个参数穿null也可以
method.invoke(new B("str13", "str24"), "method_str1");

method.getReturnType();
method.getParameterTypes();

成员变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 获取public参数(包括父类的)
Field[] fields = bClass.getFields();
System.out.println("B class public 参数:" + Arrays.toString(fields));


// 获取所有成员变量(不包括父类)
Field[] declaredFields = bClass.getDeclaredFields();
System.out.println("B class 所有参数:" + Arrays.toString(declaredFields));

// 输出
// B class public 参数:[public java.lang.String B.value4, public java.lang.String A.value2]
// B class 所有参数:[java.lang.String B.value3, public java.lang.String B.value4]

Field field = bClass.getDeclaredField("value3");
field.setAccessible(true);
// 设置值
field.set(new B("str13", "str24"), "method_str1");
// 获取变量注解
Annotation[] fieldAnnotations = field.getAnnotations();

注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 获取public参数(包括父类的)
Annotation[] annotations = bClass.getAnnotations();
System.out.println("B class public 参数:" + Arrays.toString(annotations));

// 获取所有成员变量(不包括父类)
Annotation[] declaredAnnotations = bClass.getDeclaredAnnotations();
System.out.println("B class 所有参数:" + Arrays.toString(declaredAnnotations));

// 输出
// B class public 参数:[@AnnotationB()]
//B class 所有参数:[@AnnotationB()]

// 获取type
System.out.println(annotations[0].annotationType());

// 输出:interface AnnotationB
This post is licensed under CC BY 4.0 by the author.