package cn.aofeng.demo.java.lang.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * 通过反射获取类的构造方法、字段、方法和注解等信息。
 * 
 * @author <a href="mailto:aofengblog@163.com">聂勇</a>
 */
public class ClassAnalyze {

    final static String PREFIX = "==========";
    final static String SUFFIX = PREFIX;
    
    private static void parseClass(Class<?> claz) {
        // 注解
        parseAnnotation(claz);
        
        // 类
        StringBuilder buffer = new StringBuilder(32)
            .append( Modifier.toString(claz.getModifiers()) )
            .append(' ')
            .append(claz.getName());
        log(buffer.toString());
    }
    
    private static void parseConstructor(Constructor<?> c) {
        // 注解
        parseAnnotation(c);
        
        // 构造方法
        StringBuilder buffer = new StringBuilder(32)
            .append( Modifier.toString(c.getModifiers()) )
            .append(' ')
            .append(c.getName())
            .append('(');
        
        // 参数
        Class<?>[] params = c.getParameterTypes();
        for (int index = 0; index < params.length; index++) {
            buffer.append(params[index].getName());
            if (index!=params.length-1) {
                buffer.append(", ");
            }
        }
        buffer.append(')');
        
        log(buffer.toString());
    }
    
    private static void parseMethod(Method method) {
        // 注解
        parseAnnotation(method);
        
        // 方法
        StringBuilder buffer = new StringBuilder(32)
            .append( Modifier.toString(method.getModifiers()) )
            .append(' ')
            .append(method.getName())
            .append('(');
        
        // 参数
        Class<?>[] params = method.getParameterTypes();
        for (int index = 0; index < params.length; index++) {
            buffer.append(params[index].getName());
            if (index!=params.length-1) {
                buffer.append(", ");
            }
        }
        buffer.append(')');
        
        log(buffer.toString());
    }
    
    private static void parseField(Field field) {
        // 注解
        parseAnnotation(field);
        
        // 字段
        StringBuilder buffer = new StringBuilder()
            .append(Modifier.toString(field.getModifiers()))
            .append(' ')
            .append(field.getName());
        log(buffer.toString());
    }
    
    /**
     * 解析注解信息。
     */
    private static void parseAnnotation(AnnotatedElement ae) {
        Annotation[] ans = ae.getDeclaredAnnotations();
        for (Annotation annotation : ans) {
            log(annotation.toString());
        }
    }
    
    public static void log(String msg, Object... param) {
        System.out.println( String.format(msg, param) );
    }
    
    public static void main(String[] args) throws ClassNotFoundException {
        if (args.length != 1) {
            log("无效的输入参数！");
            log("示例：");
            log("java cn.aofeng.demo.java.lang.reflect.ClassAnalyze java.util.HashMap");
        }
        Class<?> claz = Class.forName(args[0]);
        
        log("%s类%s", PREFIX, SUFFIX);
        parseClass(claz);
        
        log("%s构造方法%s", PREFIX, SUFFIX);
        Constructor<?>[] cs = claz.getDeclaredConstructors();
        for (Constructor<?> constructor : cs) {
            parseConstructor(constructor);
        }
        
        log("%s字段%s", PREFIX, SUFFIX);
        Field[] fields = claz.getDeclaredFields();
        for (Field field : fields) {
            parseField(field);
        }
        
        log("%s方法%s", PREFIX, SUFFIX);
        Method[] methods = claz.getDeclaredMethods();
        for (Method method : methods) {
            parseMethod(method);
        }
    }

}

@@ -189,16 +189,49 @@
 .Field;%0A
+import java.lang.reflect.Member;%0A
 import j
@@ -1053,102 +1053,23 @@
 nd( 
-Modifier.toString(c.getModifiers()) )%0A            .append(' ')%0A            .append(c.getName()
+parseMember(c) 
 )%0A  
@@ -1664,112 +1664,28 @@
 nd( 
-Modifier.toString(method.getModifiers()) )%0A            .append(' ')%0A            .append(method.getName()
+parseMember(method) 
 )%0A  
@@ -2203,16 +2203,273 @@
   // %E5%AD%97%E6%AE%B5%0A
+        StringBuilder buffer = parseMember(field);%0A        log(buffer.toString());%0A    %7D%0A    %0A    /**%0A     * %E8%A7%A3%E6%9E%90%E6%96%B9%E6%B3%95%E3%80%81%E5%AD%97%E6%AE%B5%E6%88%96%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%E7%9A%84%E4%BF%A1%E6%81%AF%E3%80%82%0A     * @param member %E6%96%B9%E6%B3%95%E3%80%81%E5%AD%97%E6%AE%B5%E6%88%96%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%0A     * @return %E4%BF%AE%E9%A5%B0%E7%AC%A6%E5%92%8C%E5%90%8D%E7%A7%B0%E7%BB%84%E6%88%90%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E3%80%82%0A     */%0A    private static StringBuilder parseMember(Member member) %7B%0A
         
@@ -2549,21 +2549,22 @@
 oString(
-field
+member
 .getModi
@@ -2618,21 +2618,22 @@
 .append(
-field
+member
 .getName
@@ -2645,38 +2645,29 @@
         
-log(buffer.toString())
+return buffer
 ;%0A    %7D%0A

重构：抽取公共方法复用代码