当前位置 : 主页 > 编程语言 > c++ >

通过反射打印出方法和方法注解

来源:互联网 收集:自由互联 发布时间:2021-07-03
通过反射打印出方法和方法注解 import java.lang.annotation.Annotation;import java.lang.reflect.Method;/** * Created by Doracoin on 2017/8/18. */public class ClassUtil { private static final String TAG = "ClassUtil"; public Strin
通过反射打印出方法和方法注解
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * Created by Doracoin on 2017/8/18.
 */

public class ClassUtil {
    private static final String TAG = "ClassUtil";

    public String getClassInfo(Class
  cls) {
        StringBuilder strBuid = new StringBuilder();
        Method[] ms = cls.getDeclaredMethods();
        strBuid.append(" ------------------------------------------------------------------------------------------\n");
        for (Method m : ms) {
            strBuid.append("| Method: " + m.getName()+"\n");

            Annotation[] an = m.getDeclaredAnnotations();
            strBuid.append("|     └ Annotations\n");
            for (Annotation a : an) {
                strBuid.append("|          └" + a.toString()+"\n");
            }
            strBuid.append("|\n");
        }
        strBuid.append(" ------------------------------------------------------------------------------------------\n");

        return strBuid.toString();
    }

}
网友评论