java - How to get the pointcut details in the aspect constructor -
i have annotation style aspect that:
//.... //somewhere in class // @myannotation public void foo(){ \* stuff *\} ///////////////////// // in aspect file @aspect("percflow(execution(@com.bla.myannotation * *(..)))") public class myaspect { public myaspect(){ /* here i'd access name of annotated function e.g: foo*/ } /* more pointcuts , advice*/ }
i've tried capturing object this(object)
didn't good. i've tried introducing parameter constructor caused error.
well, not necessary in constructor. can declare pointcut within percflow()
stand-alone object , use within percflow()
in @before
advice which, instantiation model implies, executed once , gives necessary information via corresponding joinpoint
object. can log or store information fancy.
btw, idea of using constructor not nice because within constructor aspect instance not initialised yet (hen vs. egg problem) , cause exceptions when trying access it.
package de.scrum_master.app; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; @retention(retentionpolicy.runtime) @target(elementtype.method) public @interface myannotation {}
package de.scrum_master.app; public class application { public void dosomething() {} @myannotation public string repeattext(int times, string text) { string result = ""; (int = 0; < times; i++) result += text; return result; } public static void main(string[] args) { application application = new application(); application.dosomething(); application.repeattext(3, "andale "); } }
package de.scrum_master.aspect; import org.aspectj.lang.joinpoint; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.before; import org.aspectj.lang.annotation.pointcut; @aspect("percflow(mypointcut())") public class myaspect { @pointcut("execution(@de.scrum_master.app.myannotation * *(..))") private static void mypointcut() {} @before("mypointcut()") public void myadvice(joinpoint thisjoinpoint) { system.out.println(thisjoinpoint); system.out.println(" " + thisjoinpoint.getsignature().getname()); (object arg : thisjoinpoint.getargs()) system.out.println(" " + arg); } }
console output:
execution(string de.scrum_master.app.application.repeattext(int, string)) repeattext 3 andale
Comments
Post a Comment