Eric B.
Eric B.

Reputation: 24481

Is there a way to write a pointcut for a getter to an annotated field?

I'm trying to write a pointcut which will intercept getters for annotated members.

public class MyClass {

    private String volume;

    @MyAttribute
    private Validity validity;


    public void setValidity( Validity obj ){
        validity = obj;
    }

    public Validity getValidity(){
        return validity;
    }
}

Is there a way to write a pointcut that will intercept all calls to getValidity() based on validity being annotated with @MyAttribute? Written differently, I'm looking to create a pointcut for any getter of a member field that is annotated with @MyAttribute.

A simple getter pointcut can advise any getter method:

pointcut embeddedGetter() : execution( public * com.ia.domain..get*());

but that won't specify that the field it is getting has to be annotated. And if I put a modifier in front of public that would specify that the getter method has to be annotated, which isn't the case.

Is this even feasible?

Upvotes: 1

Views: 1083

Answers (1)

Eric B.
Eric B.

Reputation: 24481

After playing around with AspectJ, I finally rediscovered the join point I was looking for:

pointcut embeddedGetter() : get( @MyAnnotation Validity *..* );

The key is not to use the execution pointcut but rather the get.

Upvotes: 4

Related Questions