java - AspectJ - Pointcut at specified method with a param annotated with class level annotation -
in aspect, i'd stop @ specified method. method has 1 parameter annotated class level annotation:
the annotation is:
@retention(retentionpolicy.runtime) @target(elementtype.type) public @interface auditable {} the parameter object of class annotated like:
@auditable public class user {} the method inspect:
public object findsingleresultbyexample(final object entity) {} this aspect not working:
@afterreturning(value="execution(* org.wtp.repository.genericdao.find*(@org.wtp.aspects.auditable (*)))", argnames = "joinpoint, result", returning = "result") private void auditfindannotation(final joinpoint joinpoint, final object result) {}
first of all, advice method must public, not private. please change into
public void auditfindannotation(...) it not working because pointcut intercepts methods @auditable parameter annotation. sample method not have such annotation, though. work if method signature this:
public object findsingleresultbyexample(final @auditable object entity) {} btw, @target(elementtype.type) restriction must removed or extended in order code still compile.
but guess want not match on parameter annotations, on type annotations. pointcut (no parentheses around * time):
execution(* org.wtp.repository.genericdao.find*(@org.wtp.aspects.auditable *)) but again, not match sample method because parameter type not user or auditable object , latter not carry annotation. can see difference if overload find* method , this:
package de.scrum_master.app; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; @retention(retentionpolicy.runtime) public @interface auditable {} package de.scrum_master.app; @auditable public class user {} package de.scrum_master.app; import java.util.arraylist; public class application { public object findsingleresultbyexample(final object entity) { return entity; } public object findsingleresultbyexample(final user entity) { return entity; } public static void main(string[] args) { application application = new application(); application.findsingleresultbyexample("foo"); application.findsingleresultbyexample(new user()); application.findsingleresultbyexample(new arraylist<string>()); } } package de.scrum_master.aspect; import org.aspectj.lang.joinpoint; import org.aspectj.lang.annotation.afterreturning; import org.aspectj.lang.annotation.aspect; @aspect public class auditaspect { @afterreturning( value = "execution(* de.scrum_master.app..find*(@de.scrum_master.app.auditable *))", argnames = "thisjoinpoint, result", returning = "result" ) public void auditfindannotation(final joinpoint thisjoinpoint, final object result) { system.out.println(thisjoinpoint + " -> " + result); } } the console log looks this:
execution(object de.scrum_master.app.application.findsingleresultbyexample(user)) -> de.scrum_master.app.user@4a574795 update: in order whole thing working without changing or overloading method signatures, have make pointcut match calls , dynamically determine type , annotations withing aspect via reflection (not nice, possible). feel free ask questions if not understand idea.
Comments
Post a Comment