首页 > 代码库 > 通过反射来获取对应运行时类的完整结构

通过反射来获取对应运行时类的完整结构

Class 对象可以调用其方法获取以下这些类的对象,这些类型分别对应着运行时类的某个结构:Field、Method、Constructor、Superclass、Interface、Annotation


通过这些类的对象,可以获取对应运行时类的:

  • 实现的所有接口
  • 所继承的父类
  • 所有构造器
  • 所有方法
  • 所有属性
  • 泛型
  • 注解

  1. package com.cdf.reflection;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7. import java.lang.reflect.ParameterizedType;
  8. import java.lang.reflect.Type;
  9. public class TestRe {
  10. public static void main(String[] args) {
  11. Class<MyClass> clazz = MyClass.class;
  12. // 一.获取属性
  13. Field[] fields1 = clazz.getFields();// 获取运行时类及其继承的父类以 public 修饰的属性
  14. for (Field f : fields1) {
  15. System.out.println(f);
  16. }
  17. System.out.println("------------------------------------");
  18. Field[] fields2 = clazz.getDeclaredFields();// 获取运行时类的所有属性
  19. for (Field f : fields2) {
  20. // 1.获取该属性的权限修饰符
  21. System.out.print(Modifier.toString(f.getModifiers()) + "\n");
  22. // 2.获取该属性的类型
  23. System.out.print(f.getType() + "\n");
  24. // 3.获取该属性的属性名
  25. System.out.print(f.getName() + "\n");
  26. // 4.获取完整的该属性描述
  27. System.out.println(f);
  28. System.out.println("------------------------------------");
  29. }
  30. System.out.println("------------------------------------");
  31. // 二.获取方法
  32. Method[] methods1 = clazz.getMethods();// 获取运行时类及其继承的父类以 public 修饰的方法
  33. for (Method m : methods1) {
  34. System.out.println(m);
  35. }
  36. System.out.println("------------------------------------");
  37. Method[] methods2 = clazz.getDeclaredMethods();// 获取运行时类的所有方法
  38. for (Method m : methods2) {
  39. // 1.获取该方法的注释
  40. Annotation[] annotation = m.getAnnotations();
  41. for (Annotation a : annotation) {
  42. System.out.println(a.annotationType().getName() + " ");
  43. }
  44. // 2.获取该方法的权限修饰符
  45. System.out.print(Modifier.toString(m.getModifiers()) + " ");
  46. // 3.获取该方法的返回值类型
  47. System.out.print(m.getReturnType().getName() + " ");
  48. // 4.获取该方法的方法名
  49. System.out.print(m.getName() + " ");
  50. // 5.获取该方法的形参列表
  51. Class[] params = m.getParameterTypes();
  52. System.out.print("(");
  53. for (Class p : params) {
  54. System.out.print(p.getName() + " ");
  55. }
  56. System.out.print(")");
  57. // 6.获取该方法的异常类型
  58. Class[] exps = m.getExceptionTypes();
  59. if (exps.length != 0) {
  60. System.out.print(" throws ");
  61. }
  62. for (Class e : exps) {
  63. System.out.print(e.getName() + " ");
  64. }
  65. System.out.println();
  66. // 7.获取完整的该方法描述
  67. System.out.println(m);
  68. System.out.println("------------------------------------");
  69. }
  70. System.out.println("------------------------------------");
  71. // 三.获取构造器
  72. Constructor[] constructors = clazz.getConstructors();
  73. for (Constructor c : constructors) {
  74. System.out.println(c);
  75. }
  76. System.out.println("------------------------------------");
  77. System.out.println("------------------------------------");
  78. // 四.获取父类
  79. Class superClass = clazz.getSuperclass();
  80. System.out.println(superClass);
  81. Type genericSuperClassType = clazz.getGenericSuperclass();// 带泛型
  82. System.out.println(genericSuperClassType);
  83. ParameterizedType parameterizedType = (ParameterizedType) genericSuperClassType;
  84. Type[] type = parameterizedType.getActualTypeArguments();
  85. for (Type t : type) {
  86. System.out.println(t);
  87. }
  88. System.out.println("------------------------------------");
  89. System.out.println("------------------------------------");
  90. // 五.获取实现的接口
  91. Class[] interfaces = clazz.getInterfaces();
  92. for (Class i : interfaces) {
  93. System.out.println(i);
  94. }
  95. System.out.println("------------------------------------");
  96. System.out.println("------------------------------------");
  97. // 六.获取所在的包
  98. Package pack = clazz.getPackage();
  99. System.out.println(pack);
  100. System.out.println("------------------------------------");
  101. System.out.println("------------------------------------");
  102. // 七.获取注释
  103. Annotation[] annotations = clazz.getAnnotations();
  104. for (Annotation a : annotations) {
  105. System.out.println(a);
  106. }
  107. }
  108. }
  109. interface MyInterface extends Comparable<Object> {
  110. }
  111. class MySuperClass<String> {
  112. public String superName;
  113. @Override
  114. public java.lang.String toString() {
  115. return "MySuperClass [superName=" + superName + "]";
  116. }
  117. public String getSuperName() {
  118. return superName;
  119. }
  120. public void setSuperName(String superName) {
  121. this.superName = superName;
  122. }
  123. public MySuperClass(String superName) {
  124. super();
  125. this.superName = superName;
  126. }
  127. public MySuperClass() {
  128. super();
  129. }
  130. private void superMethod() {
  131. System.out.println("This is my superMethod in MySuperClass");
  132. }
  133. }
  134. @MyAnnotation(value = "cdf")
  135. class MyClass extends MySuperClass<String> implements MyInterface {
  136. public int id;
  137. String name;
  138. private double balance;
  139. public MyClass() {
  140. super();
  141. }
  142. public MyClass(int id, String name, double balance) {
  143. super();
  144. this.id = id;
  145. this.name = name;
  146. this.balance = balance;
  147. }
  148. public int getId() {
  149. return id;
  150. }
  151. public void setId(int id) {
  152. this.id = id;
  153. }
  154. public String getName() {
  155. return name;
  156. }
  157. public void setName(String name) {
  158. this.name = name;
  159. }
  160. public double getBalance() {
  161. return balance;
  162. }
  163. public void setBalance(double balance) {
  164. this.balance = balance;
  165. }
  166. @Override
  167. public String toString() {
  168. return "MyClass [id=" + id + ", name=" + name + ", balance=" + balance + "]";
  169. }
  170. @MyAnnotation(value = "123")
  171. public void method() throws Exception {
  172. System.out.println("This is my method in MyClass");
  173. }
  174. @Override
  175. public int compareTo(Object o) {
  176. return 0;
  177. }
  178. }

其中的注释声明如下(注意生命周期)
  1. package com.cdf.reflection;
  2. import static java.lang.annotation.ElementType.CONSTRUCTOR;
  3. import static java.lang.annotation.ElementType.FIELD;
  4. import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
  5. import static java.lang.annotation.ElementType.METHOD;
  6. import static java.lang.annotation.ElementType.PARAMETER;
  7. import static java.lang.annotation.ElementType.TYPE;
  8. import java.lang.annotation.Retention;
  9. import java.lang.annotation.RetentionPolicy;
  10. import java.lang.annotation.Target;
  11. @Target({ TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })
  12. @Retention(RetentionPolicy.RUNTIME)
  13. public @interface MyAnnotation {
  14. String value();
  15. }

通过反射来获取对应运行时类的完整结构