1 package com.example.scansell;2
3 import android.util.Log;4
5 import java.lang.reflect.Constructor;6 import java.lang.reflect.Field;7 import java.lang.reflect.Method;8 import java.lang.reflect.Parameter;9
10 public classReflectUtil {11
12 private static String getModifier(inti){13 switch(i){14 case 1:15 case 2 : return "public";16 case 3 : return "private";17 case 9 : return "public static";18 case 10 : return "private static";19
20 case 18 : return "private final";21 case 25 : return "public static final";22 }23 return "" +i;24 }25
26 public static voidprintClass(Class clazz){27 Log.e("ReflectUtil", "printAllMethods");28
29 Class supper =clazz.getSuperclass();30 Class interfaces[] =clazz.getInterfaces();31 String data = "\n\n\n---------------------class.info -------------------\n\n";32 data += "package =" +clazz.getPackage()33 + "\nname =" +clazz.getName()34 + "\ntypeName =" +clazz.getTypeName()35 + "\nsimpleName =" +clazz.getSimpleName()36 + "\nCanonicalName =" +clazz.getCanonicalName()37 + "\nextends =" +supper.getCanonicalName()38 + "\nimplements [" + interfaces.length + "] =";39 for (int i = 0; i < interfaces.length; ++i){40 Class intfc =interfaces[i];41 data += intfc.getCanonicalName() + ( i < interfaces.length - 1 ? "," : "") ;42 }43 Log.e("ReflectUtil", data + "\n\n\n");44
45
46 Constructor[] cons =clazz.getDeclaredConstructors();47 data = "\n\n\n---------------------所有构造方法 [" + cons.length + "]-------------------\n\n";48
49 for(Constructor constructor : cons){50 data += getModifier(constructor.getModifiers()) + "\t" + constructor.getName() + "(";51 Parameter parameters[] =constructor.getParameters();52 for (int i = 0 ;i < parameters.length;++i){53 Parameter parameter =parameters[i];54 data += parameter.getType().getSimpleName() + (i < parameters.length - 1 ? "," : "");55 }56 data += ")\n";57 }58 Log.e("ReflectUtil", data + "\n\n\n");59
60
61 cons =clazz.getConstructors();62 data = "\n\n\n---------------------公开构造方法 [" + cons.length + "]-------------------\n\n";63 for(Constructor constructor : cons){64 data += getModifier(constructor.getModifiers()) + "\t" + constructor.getName() + "(";65 Parameter parameters[] =constructor.getParameters();66 for (int i = 0 ;i < parameters.length;++i){67 Parameter parameter =parameters[i];68 data += parameter.getType().getSimpleName() + (i < parameters.length - 1 ? "," : "");69 }70 data += ")\n";71 }72 Log.e("ReflectUtil", data + "\n\n\n");73
74
75 Method[] methods =clazz.getDeclaredMethods();76 data = "\n\n\n---------------------类方法(不含基类) [" + methods.length + "]-------------------\n\n";77 for(Method method : methods){78 data += getModifier(method.getModifiers()) + "\t" + method.getName() + "(";79 Parameter parameters[] =method.getParameters();80 for (int i = 0 ;i < parameters.length;++i){81 Parameter parameter =parameters[i];82 data += parameter.getType().getSimpleName() + (i < parameters.length - 1 ? "," : "");83 }84 data += ")\n";85 }86 Log.e("ReflectUtil", data + "\n\n\n");87
88
89 methods =clazz.getMethods();90 data = "\n\n\n---------------------类方法(含基类) [" + methods.length + "]-------------------\n\n";91 for(Method method : methods){92 data += getModifier(method.getModifiers()) + "\t" + method.getName() + "(";93 Parameter parameters[] =method.getParameters();94 for (int i = 0 ;i < parameters.length;++i){95 Parameter parameter =parameters[i];96 data += parameter.getType().getSimpleName() + (i < parameters.length - 1 ? "," : "");97 }98 data += ")\n";99 }100 Log.e("ReflectUtil", data + "\n\n\n");101
102
103 Field[] fields =clazz.getFields();104 data = "\n\n\n---------------------类公开属性(含基类) [" + fields.length + "]-------------------\n\n";105 for(Field field : fields){106 data += getModifier(field.getModifiers()) + "\t" + field.getType().getSimpleName() + "\t" +field.getName() ;107 data += "\n";108 }109 Log.e("ReflectUtil", data + "\n\n\n");110
111
112 fields =clazz.getDeclaredFields();113 data = "\n\n\n---------------------类定义的属性(不含基类) [" + fields.length + "]-------------------\n\n";114 for(Field field : fields){115 data += getModifier(field.getModifiers()) + "\t" + field.getType().getSimpleName() + "\t" +field.getName() ;116 data += "\n";117 }118 Log.e("ReflectUtil", data + "\n\n\n");119
120
121 Log.e("ReflectUtil", "printAllMethods : ==========-------=========== =======\n\n\n");122 }123 }