AspectJ Pointcut для внутреннего анализа кода локального метода и печати переменной внутри локального метода - PullRequest
0 голосов
/ 05 мая 2011

Я пытаюсь написать pointcut и совет, который мог бы напечатать строку из следующего метода -

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

Я не могу выяснить, каким образом pointcut смотрит на электронный идентификатор String, а затем печатаетзначение одинаковое в совете.

1 Ответ

4 голосов
/ 05 мая 2011

Может быть, вам нужно что-то вроде следующего:

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

Где TargetClass - класс, содержащий методы getCustomer() и getEmailAddress().

Или то же самое, используя @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);
    }
}
...