package com.slliver.reflection;
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier;
/** * 利用反射打印一个类的全部信息,构造方法、方法、属性 * * @author slliver * @date 2017-04-13 21:12 */ public class ReflectionTest {
1public static void main(String\[\] args) { 2 3 String name = "com.slliver.reflection.Student"; 4 5 try { 6 Class classzz = Class.forName(name); 7 // 当前类的父类 8 Class superClass = classzz.getSuperclass(); 9 // 获取当前类的修饰符 10 String modifiers = Modifier.toString(classzz.getModifiers()); 11 if (modifiers.length() > 0) { 12 System.out.println("当前类修饰符 == >>> " + modifiers); 13 } 14 if (superClass != null && superClass != Object.class) { 15 System.out.println("当前类的父类 == >>> " + superClass.getName()); 16 } 17 18 System.out.print("\\n{\\n"); 19 System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 类构造方法的信息 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); 20 printConstructors(classzz); 21 System.out.println(); 22 System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 类中方法的信息 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); 23 printMethods(classzz); 24 System.out.println(); 25 System.out.println("\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 类中属性的信息 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*"); 26 printFields(classzz); 27 System.out.print("}"); 28 } catch (ClassNotFoundException e) { 29 e.printStackTrace(); 30 } 31 32} 33 34/\*\* 35 \* 当前类构造方法信息,构造方法的修饰符,构造方法参数类型和参数名称 36 \*/ 37private static void printConstructors(Class classzz) { 38 // 返回所有构造器 39 Constructor\[\] constructors = classzz.getDeclaredConstructors(); 40 // 当前类所有构造方法 41 Constructor\[\] currentConstructors = classzz.getConstructors(); 42 for (Constructor constructor : currentConstructors) { 43 String name = constructor.getName(); 44 System.out.print(" "); 45 String modifiers = Modifier.toString(constructor.getModifiers()); 46 if (modifiers.length() > 0) { 47 System.out.print(modifiers + " "); 48 } 49 50 System.out.print(name + "("); 51 // 构造方法参数类型 52 Class\[\] paramTypes = constructor.getParameterTypes(); 53 for (int j = 0; j < paramTypes.length; j++) { 54 if (j > 0) { 55 System.out.print(", "); 56 } 57 // 构造方法参数类型和参数名字 58 System.out.print(paramTypes\[j\].getName()); 59 } 60 System.out.println(");"); 61 } 62} 63 64/\*\* 65 \* 当前类中方法信息 66 \*/ 67private static void printMethods(Class classzz) { 68 // 返回所有的共有方法,包括从超类(父类)继承的共有方法 69 Method\[\] methods = classzz.getMethods(); 70 // 返回当前类的全部方法 71 Method\[\] currentMethods = classzz.getDeclaredMethods(); 72 for (Method method : currentMethods) { 73 // 方法返回值 74 Class returnType = method.getReturnType(); 75 // 方法名 76 String methodName = method.getName(); 77 System.out.print(" "); 78 // 方法的修饰符 79 String modifiers = Modifier.toString(method.getModifiers()); 80 if (modifiers.length() > 0) { 81 System.out.print(modifiers + " "); 82 } 83 System.out.print(returnType.getName() + " " + methodName + " ("); 84 // 方法参数类型 85 Class\[\] paramTypes = method.getParameterTypes(); 86 87 for (int j = 0; j < paramTypes.length; j++) { 88 if (j > 0) { 89 System.out.print(", "); 90 } 91 // 构造方法参数类型和参数名字 92 System.out.print(paramTypes\[j\].getName()); 93 } 94 System.out.println(");"); 95 } 96} 97 98 99/\*\* 100 \* 当前类中属性信息 101 \*/ 102private static void printFields(Class classzz) { 103 // 返回当前类所有公有域(public修饰的属性) 104 Field\[\] publicFields = classzz.getFields(); 105 // 返回当前类所有属性 106 Field\[\] fields = classzz.getDeclaredFields(); 107 for (Field field : fields) { 108 // 属性类型 109 Class type = field.getType(); 110 // 属性名 111 String name = field.getName(); 112 System.out.print(" "); 113 // 属性修饰符 114 String modifiers = Modifier.toString(field.getModifiers()); 115 if (modifiers.length() > 0) { 116 System.out.print(modifiers + " "); 117 } 118 System.out.print(type.getName() + " " + name + ";\\n"); 119 } 120}
}