我有两个注释@LookAtThisMethod@LookAtThisParameter,如果我在使用@LookAtThisMethod的方法周围有一个切入点,如何提取用@LookAtThisParameter注释的所述方法的参数?

例如:

@Aspect
public class LookAdvisor {

    @Pointcut("@annotation(lookAtThisMethod)")
    public void lookAtThisMethodPointcut(LookAtThisMethod lookAtThisMethod){}

    @Around("lookAtThisMethodPointcut(lookAtThisMethod)")
    public void lookAtThisMethod(ProceedingJoinPoint joinPoint, LookAtThisMethod lookAtThisMethod) throws Throwable {
        for(Object argument : joinPoint.getArgs()) {
            //I can get the parameter values here
        }

        //I can get the method signature with:
        joinPoint.getSignature.toString();


        //How do I get which parameters  are annotated with @LookAtThisParameter?
    }

}

最佳答案

我围绕这个other answer将解决方案建模为一个不同但相似的问题。

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

我必须遍历目标类的原因是因为被注释的类是接口(interface)的实现,因此signature.getMethod().getParameterAnnotations()返回null。

10-01 19:29