0

This problem has been solved partly before,The question I asked builds on the answer to this question reference: How to intercept meta annotations (annotated annotations) in Spring AOP

related codes above

package de.scrum_master.app;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target({ TYPE })
public @interface MetaAnnotation {}
package de.scrum_master.app;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target({ TYPE })
@MetaAnnotation
public @interface MyAnnotation {}

but only when the meta-annotation is added to the type,it works. Now I hope that when the annotation is used on the method, it can also be intercepted by asepct.

The following expression can intercept all MetaAnnotation used on type, but it cannot be applied to method

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MetaAnnotationInterceptor {
  @Before(
    "execution(* *(..)) && (" +
      "within(@de.scrum_master.app.MetaAnnotation *) || " +
      "within(@(@de.scrum_master.app.MetaAnnotation *) *)" +
    ")"
  )
  public void myAdvice(JoinPoint thisJoinPoint){
    System.out.println(thisJoinPoint);
  }
}

I tried

"annotation(@de.scrum_master.app.MetaAnnotation *) || " + "annotation(@(@de.scrum_master.app.MetaAnnotation *) *)" +

and

"@annotation(de.scrum_master.app.MetaAnnotation) || " + "@annotation(@annotation(de.scrum_master.app.MetaAnnotation))" +

And some similar combinations, but they will all report an error: the aspect expression cannot be parsed

I want to know what kind of expression can intercept meta annotations annotated on method

White
  • 1
  • 2
  • It only works for type as that is what you defined. You can take a look at the [`AnnotationTransactionAspect`](https://github.com/spring-projects/spring-framework/blob/main/spring-aspects/src/main/java/org/springframework/transaction/aspectj/AnnotationTransactionAspect.aj). Which does the same for `@Transactional` (although not a meta-annotation). It defines 2 pointcuts, 1 for the type (what you have now) and one for methods (the `execution` one), then it adds a third pointcut which combines the 2 for easier reference. – M. Deinum Apr 06 '23 at 06:27
  • Maybe my problem description is not clear enough, I know how to intercept annotation on method and type. And I also know how to intercept meta annotation on type, but I don't know how to intercept meta annotation on method. The aspect in this example only intercept the @Transcational annotation. If I want to intercept its sub-annotation in the method, can it be realized and how should the expression be written? for example ``` – White Apr 06 '23 at 06:56
  • ``` @Retention(RUNTIME) @Target({ TYPE }) @Transcational public @interface SubAnnotation {} ``` – White Apr 06 '23 at 07:04
  • You already intercepted the meta on the type for the execution on the method it is the same but using `@annotation`. ANd please don't add code as comments as that is totally unreadable. You already now how to match a subtype/metatytpe for the type expression it is no different for an execution pointcut. As I stated you already have the type one, apply the same principle to the method one. – M. Deinum Apr 06 '23 at 07:40
  • Thanks for the tip, I just tried a few times myself and finally figured it out – White Apr 06 '23 at 08:21

1 Answers1

0

execution(@(@de.scrum_master.app.MetaAnnotation *) * *(..))

After many tries, the above function can be accomplished using this expression

White
  • 1
  • 2
  • Thanks for quoting from [my answer](https://stackoverflow.com/a/67005346/1082681) without giving me credit. Please behave better next time. Thank you. – kriegaex Apr 06 '23 at 09:08