How to match a method with an annotated argument in AspectJ
I'd like to match a method like this:
@Foo
public void boo(@Baz Bar bar) { ... }
Basically:
- the method has a
@Foo
annotation (which I match withexecution(@Foo * *(..)) && @annotation(foo)
), - can have a variable amount of parameters,
- and one of them should have a
@Baz
annotation, - I need to further work with that annotated argument (
bar
).
If a method has a @Foo annotation but is missing a @Baz
annotati开发者_开发知识库on, I want to get an error as early as possible, if possible when weaving and not at runtime.
How can I do that?
public pointcut annArg(): execution(@Foo * *(.., @Baz (*),..));
declare error :execution(@Foo * *(..))&&!annArg() :"error";
Unfortunatly it is impossible to grab matched argument by args(..,arg,..). But you can use thisJoinPoint.getArgs() and reflection API to get the annotated argument. Or if you know the position of the argument you can use something like args(..,arg);
精彩评论