Reputation: 1050
What I'm looking for is a way to specify a pointcut around a class level variable. Something like:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.FIELD)
@interface MyPointCut
{
}
public class Foo
{
@MyPointCut
BarAPI api = new BarAPI();
}
@Aspect
public class MyAspect
{
@Around(value="execution(* *(..)) && @annotation(MyPointCut)")
public Object doSomethingBeforeAndAfterEachMethodCall()
{
...
}
}
I would then like to have an aspect that performs some work before and after each method invocation of the api field. Is this doable? Could you please point me to some documentation where I can read how to do it?
Upvotes: 0
Views: 367
Reputation: 2560
It is a bit like simply putting execution advice on all the methods in type BarAPI, but your difference is that you only care about a specific instance of BarAPI and not all of them.
// Execution of any BarAPI method running in the control flow of a Foo method
Object around(): execution(* BarAPI.*(..)) && cflow(within(Foo)) {...}
cflow is a bit 'heavy' for this though, can we do something lighter:
// Call to any BarAPI method from the type Foo
Object around(): call(* BarAPI.*(..)) && within(Foo) { ... }
And what about something like that but more generally applicable:
// Assume Foo has an annotation on it so it is more general than type Foo.
@HasInterestingBarAPIField
public class Foo { ... }
Object around(): call(* BarAPI.*(..)) && @within(HasInterestingBarAPIField) { ... }
Upvotes: 2