java 反射工具类
1.ReflectUtil.java
package com.example.sjjg.java;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class ReflectUtil {
private static String getModifier(int i){
switch (i){
case 1 :
case 2 : return "public";
case 3 : return "private";
case 9 : return "public static";
case 10 : return "private static";
case 18 : return "private final";
case 25 : return "public static final";
}
return "" + i;
}
public static void printClass(Class clazz){
System.out.println("printAllMethods");
Class supper = clazz.getSuperclass();
Class interfaces[] = clazz.getInterfaces();
StringBuilder data = new StringBuilder();
data.append(" \n---------------------class.info -------------------\n");
data.append("package = " + clazz.getPackage());
data.append("\nname = " + clazz.getName());
data.append("\ntypeName = " + clazz.getTypeName());
data.append("\nsimpleName = " + clazz.getSimpleName());
data.append("\nCanonicalName = " + clazz.getCanonicalName());
data.append("\nextends = " + supper.getCanonicalName());
data.append("\nimplements [ " + interfaces.length + " ] = ");
for (int i = 0; i < interfaces.length; ++i){
Class intfc = interfaces[i];
data.append(intfc.getCanonicalName() + ( i < interfaces.length - 1 ? "," : ""));
}
System.out.println(data);
Constructor[] cons = clazz.getDeclaredConstructors();
data.delete(0,data.length());
data.append(" \n---------------------所有构造方法 [" + cons.length + "]-------------------\n");
for (Constructor constructor : cons){
data.append(getModifier(constructor.getModifiers()) + "\t" + constructor.getName() + "( ");
Parameter parameters[] = constructor.getParameters();
for (int i = 0 ;i < parameters.length;++i){
Parameter parameter = parameters[i];
data.append(parameter.getType().getSimpleName() + (i < parameters.length - 1 ? "," : ""));
}
data.append(" )\n");
}
System.out.println(data);
cons = clazz.getConstructors();
data.delete(0,data.length());
data.append(" \n---------------------公开构造方法 [" + cons.length + "]-------------------\n");
for (Constructor constructor : cons){
data.append(getModifier(constructor.getModifiers()) + "\t" + constructor.getName() + "( ");
Parameter parameters[] = constructor.getParameters();
for (int i = 0 ;i < parameters.length;++i){
Parameter parameter = parameters[i];
data.append(parameter.getType().getSimpleName() + (i < parameters.length - 1 ? "," : ""));
}
data.append(" )\n");
}
System.out.println(data);
Method[] methods = clazz.getDeclaredMethods();
data.delete(0,data.length());
data.append(" \n---------------------类方法(不含基类) [" + methods.length + "]-------------------\n");
for (Method method : methods){
data.append(getModifier(method.getModifiers()) + "\t" + method.getName() + "( " );
Parameter parameters[] = method.getParameters();
for (int i = 0 ;i < parameters.length;++i){
Parameter parameter = parameters[i];
data.append(parameter.getType().getSimpleName() + (i < parameters.length - 1 ? "," : ""));
}
data.append(" )\n");
}
System.out.println(data);
methods = clazz.getMethods();
data.delete(0,data.length());
data.append(" \n---------------------类方法(含基类) [" + methods.length + "]-------------------\n");
for (Method method : methods){
data.append(getModifier(method.getModifiers()) + "\t" + method.getName() + "( ");
Parameter parameters[] = method.getParameters();
for (int i = 0 ;i < parameters.length;++i){
Parameter parameter = parameters[i];
data.append(parameter.getType().getSimpleName() + (i < parameters.length - 1 ? "," : ""));
}
data.append(" )\n");
}
System.out.println(data);
Field[] fields = clazz.getFields();
data.delete(0,data.length());
data.append(" \n---------------------类公开属性(含基类) [" + fields.length + "]-------------------\n");
for (Field field : fields){
data.append( getModifier(field.getModifiers()) + "\t" + field.getType().getSimpleName() + "\t" + field.getName() );
data.append( "\n");
}
System.out.println(data);
fields = clazz.getDeclaredFields();
data.delete(0,data.length());
data.append(" \n---------------------类定义的属性(不含基类) [" + fields.length + "]-------------------\n");
for (Field field : fields){
data.append( getModifier(field.getModifiers()) + "\t" + field.getType().getSimpleName() + "\t" + field.getName() );
data.append( "\n");
}
System.out.println(data);
System.out.println(" printAllMethods : ==========-------=========== =======\n" );
}
}