How to force compile error with aspectJ pointcut mismatch

后端 未结 1 1002
孤城傲影
孤城傲影 2021-01-15 02:17

I have the following aspectJ pointcut:

@Around(value=\"execution(* *(*,Map)) && @annotation(com.xxx.annotations.MyCustomAnnotat         


        
相关标签:
1条回答
  • 2021-01-15 03:04

    You do it directly within an aspect, no need to configure it in AspectJ Maven plugin. Here is a little sample:

    Marker annotation:

    package de.scrum_master.app;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyCustomAnnotation {}
    

    Sample application class:

    package de.scrum_master.app;
    
    import java.util.Map;
    
    public class Application {
        public void notAnnotated(String s, Map<String, Object> m) {}
    
        @MyCustomAnnotation
        public void correctSignature(String s, Map<String, Object> m) {}
    
        @MyCustomAnnotation
        public void wrongSignature(String s, Map<String, String> m) {}
    }
    

    Aspect declaring compile error on method signature mismatch:

    package de.scrum_master.aspect;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.DeclareError;
    
    @Aspect
    public class PointcutChecker {
        @DeclareError(
            "execution(* *(..)) && " +
            "@annotation(de.scrum_master.app.MyCustomAnnotation) && " +
            "!execution(* *(*, java.util.Map<String, Object>))"
        )
        static final String wrongSignatureError =
            "wrong method signature for @MyCustomAnnotation";
    }
    

    When compiling this code you will see the following error in Eclipse as a code annotation and in the problem view (Maven console would show the same error when performing AspectJ Maven's compile goal):

    0 讨论(0)
提交回复
热议问题