0

I am trying to write a pointcut and advice which could print a string from following method -

public CustomerDto getCustomer(Integer customerCode){           
           CustomerDto customerDto = new CustomerDto();           
           String emailID =getEmailAddress();
           customerDto.setEmailAddress(emailID);             
           customerDto.setEmployer(getEmployer());
           customerDto.setSpouseName(getSpouse());
           return customerDto;      
}

I am unable to figure out a way by which a pointcut look at String emailID and then print the value of the same in an advice.

Constantiner
  • 14,231
  • 4
  • 27
  • 34

1 Answers1

4

Maybe you need something like the following:

public privileged aspect LocalMethodCallAspect {
    private pointcut localMethodExecution() : withincode(public CustomerDto TargetClass.getCustomer(Integer)) && 
        call(private String TargetClass.getEmailAddress());

    after() returning(String email) : localMethodExecution()
    {
        System.out.println(email);
    }
}

Where TargetClass is a class containing getCustomer() and getEmailAddress() methods.

Or the same using @AspectJ:

@Aspect
public class LocalMethodCallAnnotationDrivenAspect {
    @Pointcut("withincode(public CustomerDto TargetClass.getCustomer(Integer)) && " +
            "call(private String TargetClass.getEmailAddress())")
    private void localMethodExecution() {

    }

    @AfterReturning(pointcut="localMethodExecution()",returning="email")
    public void printingEmail(String email) {
        System.out.println(email);
    }
}
Constantiner
  • 14,231
  • 4
  • 27
  • 34
  • I know this is an old question, but I can't get the @AspectJ part to work. When I use `withincode` I get the error `Pointcut expression 'localMethodExecution()' contains unsupported pointcut primitive 'withincode'` When I try `@withincode` I get the error `Pointcut is not well-formed: expecting ')' at character position 19 @withincode(public int com.....` I am using AspectJ annotations, though in the Spring documentation it says that _Spring_ does not support `withincode` and `@withincode`... is that the reason it won't work? – Roger Jun 04 '13 at 09:17
  • @Roger Ask a new question. – Mario Rossi Mar 05 '15 at 06:07