- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不能用“||”做一个切入点运算符和多个注释。我正在尝试为一些 JBehave 注释(@Given、@Then、@When)创建切入点。
这很好用:
@Pointcut("@annotation(given)")
public void jBehaveGivenPointcut(Given given) { }
如果我围绕它创建并提出建议,它也会起作用。
为三个注释制作切入点的语法是什么?因为我在其他切入点中使用了逻辑 OR 运算符,所以我假设它是这样的:
@Pointcut("@annotation(given) || @annotation(then) || @annotation(when) ")
public void jBehaveGivenPointcut(Given given, Then then, When when) { }
但它不起作用,我得到一个不一致的绑定(bind)异常。我尝试了其他组合,但找不到能解决问题的组合。
最佳答案
你想要的不能以这种方式完成,因为正如错误消息所说,注解绑定(bind)不一致:你不能同时绑定(bind)所有三个注解,因为它们位于(可能互斥)或切入点的部分,也就是说,通常只有其中一个可以绑定(bind)(除非您将多个注释分配给同一方法)。您的期望可能是 AspectJ 可以通过将 null
分配给其他两个绑定(bind)来处理这种不一致,但这不是编译器现在的工作方式。
我可以提供一个涉及反射而不是使用 @annotation()
绑定(bind)的解决方法。
司机申请:
package de.scrum_master.app;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
public class Application {
public static void main(String[] args) {
doGiven("foo");
doSomething("bar");
doWhen(11);
doSomethingElse(22);
doThen();
}
@Given("an input value") public static void doGiven(String string) {}
@When("I do something") public static void doWhen(int i) {}
@Then("I should obtain a result") public static boolean doThen() { return true; }
public static void doSomething(String string) {}
public static void doSomethingElse(int i) {}
}
看点:
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class JBehaveInterceptor {
@Pointcut("execution(@org.jbehave.core.annotations.* * *(..))")
public void jBehavePointcut() {}
@Before("jBehavePointcut()")
public void jBehaveAdvice(JoinPoint.StaticPart thisJoinPointStaticPart) {
Method method = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod();
for (Annotation jBehaveAnnotation : method.getAnnotations()) {
if (jBehaveAnnotation.annotationType().getPackage().getName().equals("org.jbehave.core.annotations"))
System.out.println(thisJoinPointStaticPart + " -> " + jBehaveAnnotation);
}
}
}
如您所见,切入点仅拦截由 org.jbehave.core.annotations.*
注释的方法,这大大缩小了切入点匹配的范围 - 不仅仅是 @Given
、@When
、@Then
,但也许这就是您想要的,因为 JBehave 提供的注解远不止这些。
在建议中,我们遍历所有方法注释,因为可能不仅仅是 JBehave 注释。如果任何注释包名称与相应的 JBehave 包匹配,我们就会做一些事情(在这种情况下将注释打印到标准输出)。
希望这能解决您的问题。我无法为您扩展 AspectJ 语言,这是我能想到的最好的。无论如何,这会产生以下输出:
execution(void de.scrum_master.app.Application.doGiven(String)) -> @org.jbehave.core.annotations.Given(priority=0, value=an input value)
execution(void de.scrum_master.app.Application.doWhen(int)) -> @org.jbehave.core.annotations.When(priority=0, value=I do something)
execution(boolean de.scrum_master.app.Application.doThen()) -> @org.jbehave.core.annotations.Then(priority=0, value=I should obtain a result)
关于java - AspectJ - 多个@annotation切入点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24127169/
我正在使用 aspectj 来拦截用 @Profile(description="something") 注释的方法 @Retention(RetentionPolicy.RUNTIME) @Targ
我对 Spring AOP 中的所有类都有一个切入点,例如 @Pointcut("执行(* com.company.app..*(..))") 现在我需要排除一个类 com.company.app.I
我对 Spring AOP 中的所有类都有一个切入点,例如 @Pointcut("执行(* com.company.app..*(..))") 现在我需要排除一个类 com.company.app.I
我想知道以下内容在切入点中意味着什么 after(FigureElement fe, int x, int y) returning: call(void FigureElement.
在开始之前,我想澄清一下,我目前对 AOP 术语的理解如下...... 方面是 AOP 等同于 OOP 中的类。 通知是 AOP 等价于 OOP 中的方法。 切入点是 AOP 等价于 OOP 中的“使
我将参与 android 设备驱动程序的开发。在准备过程中,我想掌握基础知识并为此提高我的技能。 过去 2 年我一直在使用 VB 和 JAVA,所以我对自己的 C 编程没有信心。 Linux 设备驱动
我想记录类列表(可能属于不同的包)中所有方法的条目。请注意,这些方法应该只属于指定的类。 我试过以下方法,但这些都不起作用 (1) 使用 if() 切入点这里报错 "incompatible numb
我正在使用 @AspectJ 样式来编写方面,以处理我们应用程序中的日志记录。基本上我有一个像这样设置的切入点: @Pointcut("call(public * com.example..*(..)
例如我有以下方法: public void method1(@MyAnnotation Object a, Object b..) { ... } public void method1(Obj
我正在寻找一种围绕类级别变量指定切入点的方法。像这样的东西: @Target(ElementType.METHOD) @Retention(RetentionPolicy.FIELD) @interf
如何编写一个切入点来触发方法,例如MyClass 上的所有 setter 都被执行,但该方法缺少一些特定的注释,例如@Ann1 和 @Ann2 最佳答案 import java.lang.annota
我在让各个方面正常工作时遇到一些问题。由于某种原因,它们似乎只有在我真正了解该方面的应用位置时才起作用。在四个切入点/建议中,只有最后一个匹配并输出任何内容。我在这里缺少什么? public aspe
我是 Spring AOP 触发器的新手。我的代码中有以下切入点触发器。当定义了所有四个切入点时,只有第一个切入点触发器 (com.src.a()) 正在工作,下面的其余切入点不会触发。但是当我在四个
使用加载时编织,纯 AspectJ。 我们有 2 个注释 @Time 和 @Count,以及一些带注释的方法。 @Time (name="myMethod1Time") @Count (name="m
这个问题已经有答案了: Java Aspect returned value to be used in the method (1 个回答) 已关闭 6 年前。 我想要 AOP 记录方法的返回值
我正在尝试围绕使用自定义注释注释的方法定义切入点。注释有一个参数,我想在切入点定义中包含一个检查。 这是注释: public @interface MyAnno { String[] types;
我正在使用 Spring AOP 进行日志记录。我想创建一个适用于除具有特定注释的方法之外的所有方法的切入点,但我不知道如何去做。我所发现的只是如何包含带有注释的方法。 最佳答案 示例注释: pack
/* 0 */ pointcut services(Server s): target(s) && call(public * *(..)) This pointcut, named services
我需要使用 AspectJ 向每个初始化对象注入(inject)一些方法。 我想用这个: pointcut vistaInjection(Object o) : initialization(
假设我有这样的方法: public void method(@CustomAnnotation("value") String argument) 是否有一个切入点表达式可以选择所有带有 @Custo
我是一名优秀的程序员,十分优秀!