Get annotated parameters inside a pointcut

后端 未结 2 1040
抹茶落季
抹茶落季 2020-12-13 20:10

I have two annotation @LookAtThisMethod and @LookAtThisParameter, if I have a pointcut around the methods with @LookAtThisMethod how c

相关标签:
2条回答
  • 2020-12-13 20:40
    final String methodName = joinPoint.getSignature().getName();
        final MethodSignature methodSignature = (MethodSignature) joinPoint
                .getSignature();
        Method method = methodSignature.getMethod();
        GuiAudit annotation = null;
        if (method.getDeclaringClass().isInterface()) {
            method = joinPoint.getTarget().getClass()
                    .getDeclaredMethod(methodName, method.getParameterTypes());
            annotation = method.getAnnotation(GuiAudit.class);
        }
    

    This code covers the case where the Method belongs to the interface

    0 讨论(0)
  • 2020-12-13 20:43

    I modeled my solution around this other answer to a different but similar question.

    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    String methodName = signature.getMethod().getName();
    Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
    Annotation[][] annotations = joinPoint.getTarget().getClass().getMethod(methodName,parameterTypes).getParameterAnnotations();
    

    The reason that I had to go through the target class was because the class that was annotated was an implementation of an interface and thusly signature.getMethod().getParameterAnnotations() returned null.

    0 讨论(0)
提交回复
热议问题