简介
反射是一个很强大的工具,能够在程序运行时动态的获取这个类中我们想获取到的信息,包括所有的构造器、方法、字段;
这里先写一个上述功能的一个小工具类,输入一个类名,然后输出类中所有的方法和构造器的签名,以及域,包括修饰关键字;
反射的功能当然不止这些,后面会继续完善;
用到的方法
Class.forName("class name");
返回类名的class对象;
class.getSuperClass();
返回class所继承的父类, 如果是Object则返回null;
class.getDeclaredConstructors();
返回这个对象的所有构造器, 是一个Construtor数组;
class.getDeclaredMethods();
返回这个对象所有方法, 是一个Method数组, 但是不包括由父类继承的方法;
class.getDeclaredFields();
返回一个Field数组, 数组包含这个类全部的域, 如果这个Class对象描述的是基本类型那么数组长度是0;
class.getModifiers();
返回这个对象的修饰符属性值;
代码示例
public class ReflectionTest {
public static void main(String[] args) {
try {
// todo 这里的className为了方便展示,所以写死
String className ;
className = "java.util.Date";
Class cl = Class.forName(className);
Class superclass = cl.getSuperclass();
String clModifier = Modifier.toString(cl.getModifiers());
if (clModifier.length() > 0) {
System.out.print(clModifier + " ");
}
System.out.print("class " + className);
if (superclass != null && superclass != Object.class) {
System.out.print(" extends " + superclass.getName());
}
System.out.print("\n{\n");
printConstrutors(cl);
System.out.println();
printMethods(cl);
System.out.println();
printFields(cl);
System.out.println("}");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void printConstrutors(Class cl) {
Constructor[] declaredConstructors = cl.getDeclaredConstructors();
for(Constructor c : declaredConstructors) {
String name = c.getName();
System.out.print(" ");
String modifiers = Modifier.toString(c.getModifiers());
if (modifiers.length() > 0 ) {
System.out.print(modifiers + " ");
}
System.out.print(name + "(");
Class[] types = c.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (i > 0) {
System.out.print(", ");
}
System.out.print(types[i].getName());
}
System.out.println(");");
}
}
public static void printMethods(Class cl) {
Method[] methods = cl.getDeclaredMethods();
for(Method m : methods){
Class> returnType = m.getReturnType();
String name = m.getName();
System.out.print(" ");
String modiferString = Modifier.toString(m.getModifiers());
if (modiferString.length() > 0) {
System.out.print(modiferString + " ");
}
System.out.print(returnType.getName() + " " + name + "(");
Class>[] parameterTypes = m.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
if (i > 0) {
System.out.print(", ");
}
System.out.print(parameterTypes[i].getName());
}
System.out.println(");");
}
}
public static void printFields(Class cl) {
Field[] declaredFields = cl.getDeclaredFields();
for (Field f : declaredFields) {
Class> type = f.getType();
String name = f.getName();
System.out.print(" ");
String modifiers = Modifier.toString(f.getModifiers());
if (modifiers.length() > 0) {
System.out.print(modifiers + " ");
}
System.out.println(type.getName() + " " + name + ";");
}
}
}
输出结果
public class java.util.Date
{
public java.util.Date(int, int, int, int, int, int);
public java.util.Date(java.lang.String);
public java.util.Date();
public java.util.Date(long);
public java.util.Date(int, int, int);
public java.util.Date(int, int, int, int, int);
public boolean after(java.util.Date);
public boolean before(java.util.Date);
public boolean equals(java.lang.Object);
public java.lang.String toString();
public int hashCode();
public java.lang.Object clone();
public int compareTo(java.util.Date);
public volatile int compareTo(java.lang.Object);
private void readObject(java.io.ObjectInputStream);
private void writeObject(java.io.ObjectOutputStream);
private final sun.util.calendar.BaseCalendar$Date normalize();
private final sun.util.calendar.BaseCalendar$Date normalize(sun.util.calendar.BaseCalendar$Date);
public static long parse(java.lang.String);
public int getDate();
public static java.util.Date from(java.time.Instant);
public long getTime();
public void setTime(long);
public static long UTC(int, int, int, int, int, int);
private static final java.lang.StringBuilder convertToAbbr(java.lang.StringBuilder, java.lang.String);
private final sun.util.calendar.BaseCalendar$Date getCalendarDate();
private static final sun.util.calendar.BaseCalendar getCalendarSystem(long);
private static final sun.util.calendar.BaseCalendar getCalendarSystem(sun.util.calendar.BaseCalendar$Date);
private static final sun.util.calendar.BaseCalendar getCalendarSystem(int);
public int getDay();
public int getHours();
private static final synchronized sun.util.calendar.BaseCalendar getJulianCalendar();
static final long getMillisOf(java.util.Date);
public int getMinutes();
public int getMonth();
public int getSeconds();
private final long getTimeImpl();
public int getTimezoneOffset();
public int getYear();
public void setDate(int);
public void setHours(int);
public void setMinutes(int);
public void setMonth(int);
public void setSeconds(int);
public void setYear(int);
public java.lang.String toGMTString();
public java.time.Instant toInstant();
public java.lang.String toLocaleString();
private static final sun.util.calendar.BaseCalendar gcal;
private static sun.util.calendar.BaseCalendar jcal;
private transient long fastTime;
private transient sun.util.calendar.BaseCalendar$Date cdate;
private static int defaultCenturyStart;
private static final long serialVersionUID;
private static final [Ljava.lang.String; wtb;
private static final [I ttb;
}
Process finished with exit code 0
作用
得到这个类的所有构造器、方法、域,方便进行后续的逻辑处理;
反射的功能当然远远不止如此, 后面会继续完善;
欢迎大佬们给出不足或者更好的建议;