简体   繁体   中英

How can I pass in operations to make on a method parameter via a custom aspectJ annotation?

It is non-spring and operationsToDo needs to be able to handle any operations on the first parameter. I need to be able to do operationsToDo from the annotation on param. See below code example

Annotation example A:

@MyCustomAnnotation(operationsToDo=".getIdentity().getUsername()")
public Int getHighScore(User user, Game game) {
    ...
}

Annotation example B:

@MyCustomAnnotation(operationsToDo=".getEmail().get(0).getSubject()")
public List<Cat> getUsersCats(User user) {
    ...
}

Aspect File:

@Aspect
class MyCustomAspect {
    @Around(value = "@annotation(myCustomAnnotation)", argNames = "joinPoint,myCustomAnnotation")
    public Object getData(ProceedingJoinPoint joinPoint, MyCustomAnnotation myCustomAnnotation) throws Throwable {
        Object param = joinPoint.getArgs()[0];
        String something = *Do operationsToDo on param*;
        System.out.println(something);
        joinPoint.proceed();
    }

}

You cannot just write code into a text constant as an annotation parameter and expect it to magically get compiled and executed in a language like Java. This just is not how it works. Please change your application design and use a design pattern better suited to solving your problem.

It would be easy to get a reference to the annotation and its parameter value in AspectJ, but that does not give you compiled and executable code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM