soc
soc

Reputation: 28423

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:

If a method has a @Foo annotation but is missing a @Baz annotation, I want to get an error as early as possible, if possible when weaving and not at runtime.

How can I do that?

Upvotes: 2

Views: 846

Answers (1)

alehro
alehro

Reputation: 2208

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);

Upvotes: 2

Related Questions