AspectJ Pointcut pour introduire un code de méthode local et imprimer une variable à l'intérieur de la méthode locale

StackOverflow https://stackoverflow.com/questions/5902274

  •  29-10-2019
  •  | 
  •  

Question

J'essaie d'écrire une coupe de points et des conseils qui pourraient imprimer une chaîne à partir de la méthode suivante -

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

Je ne suis pas en mesure de trouver un moyen par lequel un point de point regarde à String EmailID, puis d'imprimer la valeur de la même chose dans un conseil.

Était-ce utile?

La solution

Peut-être que vous avez besoin de quelque chose comme ce qui suit:

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 est une classe contenant getCustomer() et getEmailAddress() Méthodes.

Ou le même en utilisant @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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top