Java通过反射调用类的完整结构
Field、Method、Constructor、Superclass、Interface、Annotation
使用反射可以取得:
-
实现的全部接口
public Class<?>[] getInterfaces()
确定此对象所表示的类或接口实现的接口。 -
所继承的父类
public Class<? Super T> getSuperclass()
返回表示此 Class 所表示的实体(类、接口、基本类型)的父类的 Class。 -
全部的构造器
public Constructor[] getConstructors()
返回此 Class 对象所表示的类的所有public构造方法。
public Constructor[] getDeclaredConstructors()
返回此 Class 对象表示的类声明的所有构造方法。- Constructor类中:
取得修饰符: public int getModifiers();
取得方法名称: public String getName();
取得参数的类型:public Class<?>[] getParameterTypes();
- Constructor类中:
-
全部的方法
public Method[] getDeclaredMethods()
返回此Class对象所表示的类或接口的全部方法
public Method[] getMethods()
返回此Class对象所表示的类或接口的public的方法- Method类中:
public Class<?> getReturnType()取得全部的返回值
public Class<?>[] getParameterTypes()取得全部的参数
public int getModifiers()取得修饰符
public Class<?>[] getExceptionTypes()取得异常信息
- Method类中:
-
全部的Field
public Field[] getFields()
返回此Class对象所表示的类或接口的public的Field。
public Field[] getDeclaredFields()
返回此Class对象所表示的类或接口的全部Field。- Field方法中:
public int getModifiers() 以整数形式返回此Field的修饰符
public Class<?> getType() 得到Field的属性类型
public String getName() 返回Field的名称。
- Field方法中:
-
Annotation相关
get Annotation(Class annotationClass)
getDeclaredAnnotations() -
泛型相关
获取父类泛型类型:Type getGenericSuperclass()
泛型类型:ParameterizedType
获取实际的泛型类型参数数组:getActualTypeArguments() -
类所在的包 Package getPackage()
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
37public class TestField { //获取对应的运行时类的属性 @Test public void test1() { Class clazz = Person.class; //1. getFields():只能获取运行类中极其父类中,声明为public的 Field[] fields = clazz.getFields(); for (int i = 0; i < fields.length; i++) { System.out.println(fields[i]); } //2.getDeclaredFields():获取运行时类声明的所有方法 Field[] fields1 = clazz.getDeclaredFields(); for(Field f : fields1) { System.out.println(f.getName()); } } //权限修饰符 变量类型 变量名 //获取属性的各个部分的内容 @Test public void test2() { Class clazz = Person.class; Field[] fields1 = clazz.getDeclaredFields(); for(Field f : fields1) { //1.获取每个属性的权限修饰符 int i = f.getModifiers(); String str1 = Modifier.toString(i); System.out.println(str1); //2.获取每个属性变量类型 Class type = f.getType(); System.out.println(type.getName()); //3.获取属性名 System.out.println(f.getName()); } } }
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
52public class TestMethod { //1.获取运行时类的方法 @Test public void test1() { Class clazz = Person.class; //1.getMethods(): 获取运行时类及其父类所有的声明为public的方法 Method[] m1 = clazz.getMethods(); for(Method m : m1) { System.out.println(m); } //2.getDeclaredMethods():获取运行时类本身声明的所有方法 Method[] m2 = clazz.getDeclaredMethods(); for(Method m : m2) { System.out.println(m); } } //注解 权限修饰符 返回值类型 方法名 形参列表 异常 @Test public void test2() { Class clazz = Person.class; Method[] m2 = clazz.getDeclaredMethods(); for(Method m : m2) { //1.注解 Annotation[] ann = m.getAnnotations(); for(Annotation a : ann) { System.out.println(a); } //2.权限修饰符 String str = Modifier.toString(m.getModifiers()); System.out.print(str + " "); //3.返回值类型 Class returnType = m.getReturnType(); System.out.print(returnType.getName()+" "); //4.方法名 System.out.print(m.getName()+ ""); //5.形参列表 System.out.print("("); Class [] params = m.getParameterTypes(); for (int i = 0; i < params.length; i++) { System.out.print(params[i].getName()+" args-"+ i+ " "); } System.out.print(")"); //6.异常类型 Class[] exps = m.getExceptionTypes(); for (int i = 0; i < exps.length; i++) { System.out.print(exps[i].getName()+" exps-"+ i+ " "); } System.out.println(); } } }
1
2
3
4
5
6
7
8
9
10
11@Test public void test2() throws Exception { String className = "testReflection.Person"; Class clazz = Class.forName(className); Constructor[] cons = clazz.getDeclaredConstructors(); for(Constructor c : cons) { System.out.println(c); } }
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
58public class TestOthers { //6.获取注解 public void test6() { Class clazz = Person.class; Annotation [] anns = clazz.getAnnotations(); for(Annotation ann : anns) { System.out.println(ann); } } //5.获取所在的包 @Test public void test5() { Class clazz = Person.class; Package pack = clazz.getPackage(); System.out.println(pack);//package testReflection } //4.获取实现的接口 @Test public void test4() { Class clazz = Person.class; Class[] interfaces = clazz.getInterfaces(); for(Class i : interfaces) { System.out.println(i); /* * interface java.lang.Comparable interface testReflection.MyInterface */ } } //3.获取父类符泛型 @Test public void test3() { Class clazz = Person.class; Type type1 = clazz.getGenericSuperclass(); ParameterizedType param = (ParameterizedType)type1; Type[] ars = param.getActualTypeArguments(); System.out.println(((Class)ars[0]).getTypeName());//java.lang.String } //2获取带泛型的父类 @Test public void test2() { Class clazz = Person.class; Type type1 = clazz.getGenericSuperclass(); System.out.println(type1);//testReflection.Creature<java.lang.String> } //1.获取运行时类的父类 @Test public void test() { Class clazz = Person.class; Class superClass = clazz.getSuperclass(); System.out.println(superClass);//class testReflection.Creature } }
最后
以上就是高高大山最近收集整理的关于Java通过反射调用类的完整结构的全部内容,更多相关Java通过反射调用类内容请搜索靠谱客的其他文章。
发表评论 取消回复