2014-06-09 2 views
0

"||"로 포인트 컷을 만들 수 없습니다. 연산자 및 여러 주석이 있습니다. 일부 JBehave 주석 (@Given, @Then, @When)에 대한 Pointcut을 만들려고합니다.AspectJ - 다중 @ 주석 포인트 컷

@Pointcut("@annotation(given)") 
    public void jBehaveGivenPointcut(Given given) { } 

을 내가 만들고 조언을 arount 경우, 그것은 또한 작동합니다

이 잘 작동합니다.

세 가지 주석에 대해 Pointcut을 만들기위한 구문은 무엇입니까? 나는 예외를 바인딩 inconsist를 얻을

@Pointcut("@annotation(given) || @annotation(then) || @annotation(when) ") 
    public void jBehaveGivenPointcut(Given given, Then then, When when) { } 

하지만이 작동하지 않습니다 내가 다른 포인트 컷의 논리 OR 연산자를 사용했기 때문에 나는 그것이 같은 가정 것이다. 나는 다른 조합을 시도했지만 트릭을하는 것을 찾을 수 없었다.

답변

3

오류 메시지가 말한 것처럼 주석 바인딩이 일관성이 없기 때문에 원하는대로 할 수 없습니다. 3 개 주석 모두가 (아마도 상호 배타적 인) 또는 포인트 컷의 일부이기 때문에 동시에 바인딩 할 수 없습니다 즉, 일반적으로 둘 중 하나만 바인딩 될 수 있습니다 (동일한 메소드에 여러 주석을 할당하지 않는 한). 여러분의 기대는 AspectJ가 바인딩 된 경우 다른 두 개에 null을 할당하여이 불일치를 처리 할 수 ​​있다고 생각할 수 있습니다. 그러나 이것은 현재 컴파일러가 작동하는 방식이 아닙니다.

@annotation() 바인딩 대신 리플렉션과 관련된 해결 방법을 제공 할 수 있습니다.

드라이버 응용 프로그램 :

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) {} 
} 

측면 : 상당히 일치하는 포인트 컷을 좁혀 org.jbehave.core.annotations.*에 의해 주석 당신이 볼 수 있듯이

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); 
     } 
    } 
} 

, 포인트 컷은 차단 방법 - 이상에 그냥 @Given, @When, @Then,하지만 어쩌면 그것은 무엇을 원하는 becau se 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) 
+1

왜 downvote입니까? 대답은 정확합니다. 그것이 아니더라도, downvotes는 연구 또는 노력의 뜻 깊은 부족을 보여주는 엉성한 응답을 위해 보류됩니다. – kriegaex

관련 문제