public class MethodDictionaryUtil {
public static void main(String[] args) {
UserController userController = new UserController();
getMethodDictionary(userController.getClass());
}
public static void getMethodDictionary(Class<?> cls) {
String controllerName = cls.getSimpleName();
System.err.println( "controllerName -->" + controllerName);
// 获取普通方法
// Method[] methods = cls.getDeclaredMethods(); // 返回public and private方法
Method[] methods = cls.getMethods(); // 返回public方法
for (Method method:methods) {
String methodName = method.getName();
System.err.println("methodName --->" + methodName);
//获取类方法注解
Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
for (Annotation annotation : declaredAnnotations) {
//获取注解名
// String name = annotation.annotationType().getSimpleName();
if (annotation instanceof SystemControllerLog) {
SystemControllerLog param = (SystemControllerLog) annotation;
System.out.println("获取SystemControllerLog注解的值------"+param.description());
}
}
//获取方法参数注解
/*Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (Annotation[] annotations : parameterAnnotations) {
for (Annotation annotation : annotations) {
//获取注解名
String name = annotation.annotationType().getSimpleName();
if (annotation instanceof SystemControllerLog) {
SystemControllerLog param = (SystemControllerLog) annotation;
System.out.println("获取SystemControllerLog注解的值------"+param.description());
}
}
}*/
}
}
}
Java反射获取类的方法和注解
最新推荐文章于 2024-07-27 17:56:15 发布