java - Pointcuts on inherited methods (in a class design-agnostic context) -
i fiddling around aspectj , came idea don't seem able implement (story of life).
i have defined aspect :
package my.package; import org.aspectj.lang.annotation.*; import org.aspectj.lang.proceedingjoinpoint; @aspect public class myaspect { @pointcut("execution(* *(..)) && this(o)") public void instancemethod(object o) {} @pointcut("within(@marker *)") public void methodsfrommarkedclasses() {} @around("methodsfrommarkedclasses() && instancemethod(o)") public object markedmethodsadvice(proceedingjoinpoint joinpoint, object o) throws throwable { // awesome stuff return null; //<- not actual return, added head wouldn't hurt } } i have defined @marker annotation empty.
the idea have advice markedmethodsadvice execute time method called on object of class marked @marker. (and here're tricky parts) :
case 1
if said method inherited class not marked see example :
given
package my.package; public class alpha { public void methoda() {} } when
package my.package; @marked public class beta extends alpha {} then
/* should trigger advice */ new beta().methoda(); case 2
if said method called on object of subclass (liskov)
given
@marked public class beta extends alpha { //still extends coherent previous example public void methodb() {} } when
package my.package; public class gamma extends beta { } then
/* should trigger advice */ new gamma().methodb(); (and since i'm greedy ask bonus one)
case 3
if said method declared on subclass
given
@marked public class beta extends alpha {} //still extends coherent previous example when
package my.package; public class gamma extends beta { public void methodc() {} } then
/* should trigger advice */ new gamma().methodc(); the aspectj documentation seems state possible
execution(public void middle.*()) picks out method executions public methods returning void , having no arguments either declared in, or inherited by, middle, if methods overridden in subclass of middle
so did wrong ?
you try annotate @marker annotation @inherited annotation. subclasses should inherit @marker annotation.
java.lang.annotation.inherited @inherited public @interface marked{ }
Comments
Post a Comment