如何使用AspectJ切入点不匹配来强制编译错误 [英] How to force compile error with aspectJ pointcut mismatch
问题描述
我有以下AspectJ切入点:
@Around(value="execution(* *(*,Map<String, Object>)) && @annotation(com.xxx.annotations.MyCustomAnnotation)")
如您所见,此切入点仅匹配以com.xxx.annotations.MyCustomAnnotation注释的方法,该方法具有2个参数-第一个参数是任意的,第二个参数必须为Map<String, Object>
类型.
如果找到了以com.xxx.annotations.MyCustomAnnotation注释但与签名* *(*,Map<String, Object>)
不匹配的方法,有没有一种配置Aspectj-Maven-plugin来强制编译错误的方法?
或者换句话说,:
@com.xxx.annotations.MyCustomAnnotation
public void test(String s, Map<String, String> m) {
...
}
->我希望这会产生编译时错误,因为Map<String, String>
!= Map<String, Object>
您可以直接在方面中进行操作,无需在AspectJ Maven插件中进行配置.这是一个小样本:
标记注释:
package de.scrum_master.app;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation {}
示例应用程序类:
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) {}
}
在方法签名不匹配方面声明编译错误:
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";
}
编译此代码时,您将在Eclipse中以代码注释和问题视图的形式看到以下错误(执行AspectJ Maven的compile
目标时,Maven控制台将显示相同的错误):
I have the following aspectJ pointcut:
@Around(value="execution(* *(*,Map<String, Object>)) && @annotation(com.xxx.annotations.MyCustomAnnotation)")
As you can see, this pointcut only matches methods, annotated with com.xxx.annotations.MyCustomAnnotation, which have 2 arguments - the first one is arbitrary and the second one must be of type Map<String, Object>
.
Is there a way to configure the aspectj-maven-plugin to force compilation errors if it find methods that are annotated with com.xxx.annotations.MyCustomAnnotation, but don't match the signature * *(*,Map<String, Object>)
?
Or in other words, :
@com.xxx.annotations.MyCustomAnnotation
public void test(String s, Map<String, String> m) {
...
}
-> I want this to produce compile time error because Map<String, String>
!= Map<String, Object>
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):
这篇关于如何使用AspectJ切入点不匹配来强制编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!