package com.pa.reflection; import java.io.*; import java.lang.reflect.*; @SuppressWarnings("rawtypes") public class ClassReflection { private static final String BLANK = " "; private static final String LEFT_BRACKET = "("; private static final String RIGHT_BRACKET = ")"; private static final String THROWS = "throws"; private static final String SEPARATE = ";"; private static final String NEW_LINE = "\r\n"; private static final String COMMA = ","; private static final String ARG = "arg"; public static void getClassInfo(Class> clazz, Writer writer) { try { //获取构造函数信息 StringBuffer constructorsResult = getConstructors(clazz); //获取字段信息 StringBuffer fieldsResult = getFields(clazz); //获取方法信息 StringBuffer methodsResult = getMethods(clazz); //最终结果 StringBuffer finalResult = new StringBuffer(); finalResult.append(constructorsResult).append(NEW_LINE); finalResult.append(fieldsResult).append(NEW_LINE); finalResult.append(methodsResult).append(NEW_LINE); //输出结果 writer.write(finalResult.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) try { writer.close(); } catch (Exception e) { e.printStackTrace(); } } } private static StringBuffer getMethods(Class> clazz) { Method[] methods = clazz.getDeclaredMethods(); beforeGetClassMethods(clazz,methods); StringBuffer methodsResult = new StringBuffer(); if (methods != null && methods.length > 0) { for (Method method : methods) { methodsResult.append(addSuffixBlank(Modifier.toString(method.getModifiers()))); methodsResult.append(addSuffixBlank(method.getReturnType().getCanonicalName())); methodsResult.append(method.getName()); Class[] parameterTypes = method.getParameterTypes(); getParameters(methodsResult, parameterTypes); Class[] exceptionTypes = method.getExceptionTypes(); getExceptions(methodsResult, exceptionTypes); methodsResult.append(SEPARATE); methodsResult.append(NEW_LINE); } } afterGetClassMethods(clazz,methods); return methodsResult; } private static void afterGetClassMethods(Class> clazz,Method[] methods) { } private static void beforeGetClassMethods(Class> clazz,Method[] methods) { System.out.println("get "+clazz.getCanonicalName()+" methods,totoal size: "+methods.length); } /** * 获取方法异常信息 * @param methodsResult * @param exceptionTypes */ private static void getExceptions(StringBuffer methodsResult,Class[] exceptionTypes) { if (exceptionTypes != null && exceptionTypes.length > 0) { methodsResult.append(addSuffixBlank(THROWS)); for (int j = 0; j < exceptionTypes.length; j++) { Class exceptionType = exceptionTypes[j]; methodsResult.append(exceptionType.getCanonicalName()); if (j != exceptionTypes.length - 1) { methodsResult.append(COMMA); } } } } /** * 获取方法参数信息 * @param methodsResult * @param parameterTypes */ private static void getParameters(StringBuffer methodsResult,Class[] parameterTypes) { methodsResult.append(LEFT_BRACKET); if (parameterTypes != null && parameterTypes.length > 0) { for (int i = 0; i < parameterTypes.length; i++) { Class parameterType = parameterTypes[i]; methodsResult.append(addSuffixBlank(parameterType.getCanonicalName())).append(ARG + (i + 1)); if (i != parameterTypes.length - 1) { methodsResult.append(COMMA); } } } methodsResult.append(RIGHT_BRACKET); } private static StringBuffer getFields(Class> clazz) { Field[] fields = clazz.getDeclaredFields(); beforeGetClassFields(clazz,fields); StringBuffer fieldsResult = new StringBuffer(); if (fields != null && fields.length > 0) { for (Field field : fields) { fieldsResult.append(addSuffixBlank(Modifier.toString((field.getModifiers())))); fieldsResult.append(addSuffixBlank(field.getType().getCanonicalName())); fieldsResult.append(field.getName()); fieldsResult.append(SEPARATE); fieldsResult.append(NEW_LINE); } } afterGetClassFields(clazz,fields); return fieldsResult; } private static void afterGetClassFields(Class> clazz,Field[] fields) { } private static void beforeGetClassFields(Class> clazz,Field[] fields) { System.out.println("get "+clazz.getCanonicalName()+" fieds,totoal size: "+fields.length); } private static StringBuffer getConstructors(Class> clazz) { Constructor[] constructors = clazz.getDeclaredConstructors(); beforeGetClassContructors(clazz,constructors); StringBuffer constructorsResult = new StringBuffer(); if (constructors != null && constructors.length > 0) { for (Constructor constructor : constructors) { constructorsResult.append(addSuffixBlank(Modifier.toString(constructor.getModifiers()))); constructorsResult.append(addSuffixBlank(constructor.getName())); Class[] parameterTypes = constructor.getParameterTypes(); getParameters(constructorsResult, parameterTypes); Class[] exceptionTypes = constructor.getExceptionTypes(); getExceptions(constructorsResult, exceptionTypes); constructorsResult.append(SEPARATE); constructorsResult.append(NEW_LINE); } } afterGetClassContructors(clazz,constructors); return constructorsResult; } private static void afterGetClassContructors(Class> clazz,Constructor[] constructors) { } private static void beforeGetClassContructors(Class> clazz,Constructor[] constructors) { System.out.println("get "+clazz.getCanonicalName()+" constructors,totoal size: "+constructors.length); } public static String addPrefix(String str, String prefix) { return prefix + str; } public static String addSuffix(String str, String suffix) { return str + suffix; } public static String addPrefixAndSuffix(String str, String prefix, String suffix) { return prefix + str + suffix; } public static String addSuffixBlank(String str) { return addSuffix(str, BLANK); } public static void main(String[] args) throws IOException { Writer writer = new PrintWriter(System.out); System.out.println("请输入你要反射的类(输入'exit'则退出):"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String destClass = null; while(true){ destClass = reader.readLine(); if(destClass.equalsIgnoreCase("exit")){ break; } try { Class clazz = Class.forName(destClass); getClassInfo(clazz, writer); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } }
输出结果如下:
get javax.mail.Part constructors,totoal size: 0 get javax.mail.Part fieds,totoal size: 2 get javax.mail.Part methods,totoal size: 25 public static final java.lang.String ATTACHMENT; public static final java.lang.String INLINE; public abstract int getSize()throws javax.mail.MessagingException; public abstract java.lang.String getFileName()throws javax.mail.MessagingException; public abstract java.lang.Object getContent()throws java.io.IOException,javax.mail.MessagingException; public abstract java.io.InputStream getInputStream()throws java.io.IOException,javax.mail.MessagingException; public abstract java.lang.String getContentType()throws javax.mail.MessagingException; public abstract void writeTo(java.io.OutputStream arg1)throws java.io.IOException,javax.mail.MessagingException; public abstract void setText(java.lang.String arg1)throws javax.mail.MessagingException; public abstract int getLineCount()throws javax.mail.MessagingException; public abstract boolean isMimeType(java.lang.String arg1)throws javax.mail.MessagingException; public abstract java.lang.String getDisposition()throws javax.mail.MessagingException; public abstract void setDisposition(java.lang.String arg1)throws javax.mail.MessagingException; public abstract java.lang.String getDescription()throws javax.mail.MessagingException; public abstract void setDescription(java.lang.String arg1)throws javax.mail.MessagingException; public abstract void setFileName(java.lang.String arg1)throws javax.mail.MessagingException; public abstract javax.activation.DataHandler getDataHandler()throws javax.mail.MessagingException; public abstract void setDataHandler(javax.activation.DataHandler arg1)throws javax.mail.MessagingException; public abstract void setContent(javax.mail.Multipart arg1)throws javax.mail.MessagingException; public abstract void setContent(java.lang.Object arg1,java.lang.String arg2)throws javax.mail.MessagingException; public abstract java.lang.String[] getHeader(java.lang.String arg1)throws javax.mail.MessagingException; public abstract void setHeader(java.lang.String arg1,java.lang.String arg2)throws javax.mail.MessagingException; public abstract void addHeader(java.lang.String arg1,java.lang.String arg2)throws javax.mail.MessagingException; public abstract void removeHeader(java.lang.String arg1)throws javax.mail.MessagingException; public abstract java.util.Enumeration getAllHeaders()throws javax.mail.MessagingException; public abstract java.util.Enumeration getMatchingHeaders(java.lang.String[] arg1)throws javax.mail.MessagingException; public abstract java.util.Enumeration getNonMatchingHeaders(java.lang.String[] arg1)throws javax.mail.MessagingException;