I have a method annotation (@MethodAnno) and a parameter annotation (@P). I need to create an aspect to capture invocations of methods annotated with @MethodAnno and look up method arguments annotated with @P. However while I am able to obtain the method annotation in my aspect, parameter annotations are not returned in MethodSignature. Below is what I have.
Annotations
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MethodAnno {
Foo[] foo();
}
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface P {
Bar value();
}
Aspect
@Aspect
@Component
public class MyAspect {
@Before("@annotation(methodAnnotation)")
public void methodsWithMethodLevelAnnotation(final JoinPoint pjp, MethodAnno methodAnnotation) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations();
// parameterAnnotations is always a single element array and the element is a zero-length array
}
}
Usage
public interface Service {
public void execute(String input);
}
@Service
public class ServiceImpl implements Service {
@Override
@MethodAnno
public void execute(@P String input) {
....
}
}