美文网首页
AOP 获取对象、方法、属性、方法上参数的注解信息

AOP 获取对象、方法、属性、方法上参数的注解信息

作者: knock | 来源:发表于2020-07-25 17:58 被阅读0次
 public void getAspectLogDescription(ProceedingJoinPoint joinPoint) throws Exception {
        Class<?> targetClass = joinPoint.getTarget().getClass();

        //对象上的注解
        String targetName = joinPoint.getTarget().getClass().getName();
        System.out.println("joinPoint.getTarget().getClass().getName():      " + targetName);
        if (targetClass.isAnnotationPresent(WebLog.class)) {
            WebLog webLog = targetClass.getAnnotation(WebLog.class);
            System.out.println("对象上的注解:" + webLog.description());
        }

        System.out.println();

//        //方法上的注解
//        String methodName = joinPoint.getSignature().getName();
//        System.out.println("joinPoint.getSignature().getName():      "+methodName);
//        Method[] methods = targetClass.getMethods();
//        for (Method method : methods) {
//            if (method.getName().equals(methodName)) {
//                if(method.isAnnotationPresent(WebLog.class)){
//                    WebLog webLog=method.getAnnotation(WebLog.class);
//                    System.out.println("方法上的注解:" + webLog.description());
//                }
//            }
//        }

        //获取方法上的注解
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        if (method.isAnnotationPresent(WebLog.class)) {
            WebLog webLog = method.getAnnotation(WebLog.class);
            System.out.println("方法上的注解:" + webLog.description());
        }

        System.out.println();

        //属性上的注解
        Field[] fields = targetClass.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(WebLog.class)) {
                field.setAccessible(true);
                WebLog webLog = field.getAnnotation(WebLog.class);
                System.out.println("属性上的注解:" + field.getName() + ":" + field.get(joinPoint.getTarget()) + ":" + webLog.description());
            }
        }

        System.out.println();

        //方法参数上的注解
        Object[] arguments = joinPoint.getArgs();
        System.out.println("arguments:      " + JSON.toJSONString(arguments));
        Annotation[][] annotations = method.getParameterAnnotations();// 一个参数可以能有多个注解
        for (int i = 0; i < arguments.length; i++) {
            Object param = arguments[i];
            Annotation[] paramAnn = annotations[i];
            //参数为空,直接下一个参数
            if (param == null || paramAnn.length == 0) {
                continue;
            }
            for (Annotation annotation : paramAnn) {
                if (annotation.annotationType().equals(WebLog.class)) {
                    WebLog webLog = (WebLog) annotation;
                    System.out.println("方法参数上的注解:    " + param + ":" + webLog.description());
                }
            }
        }

    }

相关文章

网友评论

      本文标题:AOP 获取对象、方法、属性、方法上参数的注解信息

      本文链接:https://www.haomeiwen.com/subject/ljemlktx.html