java - AOP with Local Variable Annotations -
i want use local variable annotations better aop. 1 idea implement future<t> concept proxy using annotation.
@nonblocking expensiveobject exp = new expensiveobject(); //returns immediately, has threaded out instantiation of expensiveobject. exp.dostuff(); //okay, blocks until it's finished instantiating , executes #dostuff
can sick aspectj on somehow , want done local variable annotations? know other threads have indicated java doesn't support them magical. don't want pass around future , break encapsulation.
you can not proxy, real aspectj bytecode weaving there if annotate type instead of local variable. (i don't think local variable access supported pointcut). anyway, here's code.
an annotation:
@retention(retentionpolicy.runtime) @target(elementtype.type) public @interface later {}
a class marked annotation:
package com.dummy.aspectj; @later public class heavyobject{ public heavyobject(){ system.out.println("boy, heavy"); } }
a main class:
package com.dummy.aspectj; public class heavylifter{ public static void main(final string[] args){ final heavyobject fatman = new heavyobject(); system.out.println("finished main"); } }
and aspect:
package com.dummy.aspectj; public aspect lateraspect{ pointcut laterinstantiation() : execution(@later *.new(..)) ; void around() : laterinstantiation() { new thread(new runnable(){ @override public void run(){ system.out.println("wait... heavy"); try{ thread.sleep(2000); } catch(final interruptedexception e){ throw new illegalstateexception(e); } system.out.println("ok, task"); proceed(); } }).start(); } }
here output heavylifter when run aspectj/java application eclipse:
finished main wait... heavy ok, task boy, heavy
Comments
Post a Comment