java - AspectJ - Multiple @annotation Pointcut -


i can't make pointcut "||" operator , multiple annotations. i'm trying create pointcut jbehave annotations (@given, @then, @when).

this works fine:

    @pointcut("@annotation(given)")     public void jbehavegivenpointcut(given given) { } 

and if create , advice arount it, works.

what syntax making pointcut 3 annotations? since had used logical or operator in other pointcuts assume like:

    @pointcut("@annotation(given) || @annotation(then) || @annotation(when) ")     public void jbehavegivenpointcut(given given, then, when when) { } 

but doesn't work, inconsist binding exception. tried other combinations couldn't find 1 trick.

what want cannot done way because, error message says, annotation binding inconsistent: cannot bind 3 annotations @ same time because in (possibly mutually exclusive) or parts of pointcut, i.e. of of them can bound (unless assign multiple annotations same method). expectation might aspectj can deal inconsistency assigning null other 2 if 1 bound, not how compiler works right now.

i can offer workaround involves reflection instead of using @annotation() binding.

driver application:

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 something") public static void dowhen(int i) {}     @then("i should obtain result") public static boolean dothen() { return true; }     public static void dosomething(string string) {}     public static void dosomethingelse(int i) {} } 

aspect:

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();         (annotation jbehaveannotation : method.getannotations()) {             if (jbehaveannotation.annotationtype().getpackage().getname().equals("org.jbehave.core.annotations"))                 system.out.println(thisjoinpointstaticpart + " -> " + jbehaveannotation);         }     } } 

as can see, pointcut intercepts methods annotated org.jbehave.core.annotations.* narrows down pointcut matching considerably - more @given, @when, @then, maybe want because jbehave offers more annotations those.

in advice loop on method annotations because there might more jbehave ones. if annotation package name matches corresponding jbehave package, (in case print annotation standard output).

i hope solves problem. cannot extend aspectj language you, best can think of. anyway, yields following output:

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 something) execution(boolean de.scrum_master.app.application.dothen()) -> @org.jbehave.core.annotations.then(priority=0, value=i should obtain result) 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -