import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; /** *//** * This class is used to find the Fields and Methods declared in * the underline class. * * @author Administrator * */ publicclass FindClassInfo ...{ //store the outPut info private StringBuffer strInfo =null; //store the class name private String strClassName =null; private String strSpace =""; private String strNextLine =""; /** *//** * Set the name of class,which will be showed datail info. * * @param str */ publicvoid setClassName(String str)...{ strClassName = str; } public String getDatail()...{ if(strClassName ==null)...{ return"Please set the Name of Class!"; } Class c =null; try...{ c = Class.forName(strClassName); }catch (ClassNotFoundException e) ...{ return"The Class can`t been find by the name!"; } strInfo =new StringBuffer(); strInfo.append("----------------------detail of Class:"+strClassName +"----------------------"); strInfo.append(strNextLine); getFields(c); getConstructor(c); getMethods(c); getSuperClssInfo(c); return strInfo.toString(); } /** *//** * Get public Fields of Class. * @param c */ privatevoid getFields(Class c)...{ Field[] arrFields = c.getDeclaredFields(); for(Field tempField:arrFields)...{ //Scape the Private,Default and Protected Field if(!Modifier.isPublic(tempField.getModifiers()))...{ continue; } strInfo.append(Modifier.toString(tempField.getModifiers())); strInfo.append(strSpace); strInfo.append(forTypeName(tempField.getType().getName())); strInfo.append(strSpace); strInfo.append(forName(tempField.getName())); strInfo.append(strNextLine); } } /** *//** * Get public Constructor. * @param c */ privatevoid getConstructor(Class c)...{ Constructor[] arrConstructor = c.getDeclaredConstructors(); for(Constructor tempCons : arrConstructor)...{ //Scape the Private,Default and Protected Constructor if(!Modifier.isPublic(tempCons.getModifiers()))...{ continue; } strInfo.append(Modifier.toString(tempCons.getModifiers())); strInfo.append(strSpace); strInfo.append(forName(tempCons.getName())); strInfo.append("("); Class[] arrParaTypes = tempCons.getParameterTypes(); for(int i=0;i<arrParaTypes.length;i++)...{ strInfo.append(forTypeName(arrParaTypes[i].getName())); if(i<arrParaTypes.length -1)...{ strInfo.append(","); } } strInfo.append(")"); strInfo.append(strNextLine); } } /** *//** * Get public method of Class * @param c */ privatevoid getMethods(Class c)...{ Method[] arrMethod = c.getDeclaredMethods(); for(Method tempMethod:arrMethod)...{ //Scape the Private,Default and Protected Method if(!Modifier.isPublic(tempMethod.getModifiers()))...{ continue; } strInfo.append(Modifier.toString(tempMethod.getModifiers())); strInfo.append(strSpace); strInfo.append(forTypeName(tempMethod.getReturnType().getName())); strInfo.append(strSpace); strInfo.append(forName(tempMethod.getName())); strInfo.append("("); Class[] arrParaTypes = tempMethod.getParameterTypes(); for(int i=0;i<arrParaTypes.length;i++)...{ strInfo.append(forTypeName(arrParaTypes[i].getName())); if(i<arrParaTypes.length -1)...{ strInfo.append(","); } } strInfo.append(")"); strInfo.append(strNextLine); } } privatevoid getSuperClssInfo(Class c)...{ if(c.getSuperclass().getName().equals(Object.class.getName()))...{ return; } Class superClass = c.getSuperclass(); getSuperClssInfo(superClass); strInfo.append("------------Fields inherited from "+superClass.getName()+"----------------"); strInfo.append(strNextLine); getFields(superClass); strInfo.append(strNextLine); strInfo.append("------------Methods inherited from "+superClass.getName()+"----------------"); strInfo.append(strNextLine); getMethods(superClass); } /** *//** * Get name from InputString. * This method is used to Get name of Class except TypeClass. * * @param str .class Name or method name or Field name. * @return */ private String forName(String str)...{ return str.substring(str.lastIndexOf(".")+1); } /** *//** * Get name from InputString,and the inputString is the original name of * Clss which refered to Type. * The name of ReturnType have three kinds. * one matches "[*[B,C,S,I,D,J,F]",this is primitive type array. * one mathes "[*S+;",This is Object type array. * one is not array. * * @param str * @return the String imply Type. */ private String forTypeName(String str)...{ String tempStr =""; //return type is array if(str.startsWith("["))...{ switch(str.charAt(str.lastIndexOf("[")+1))...{ case'B': tempStr ="byte"; break; case'C': tempStr ="char"; break; case'S': tempStr ="short"; break; case'I': tempStr ="int"; break; case'D': tempStr ="double"; break; case'J': tempStr ="long"; break; case'F': tempStr ="float"; break; //Return-Type is array of Object case'L': tempStr = str.substring(str.lastIndexOf(".")+1,str.length()-1); default: break; } //add "[]" int arrCount = str.lastIndexOf("[")+1; for(int i=0;i<arrCount;i++)...{ tempStr +="[]"; } }else...{ tempStr = str.substring(str.lastIndexOf(".")+1); } return tempStr; } }