Reputation: 1296
I want to use local variable annotations to do better AOP. One idea is to implement the Future<T> concept with a proxy using an annotation.
@NonBlocking ExpensiveObject exp = new ExpensiveObject();
//returns immediately, but has threaded out instantiation of the ExpensiveObject.
exp.doStuff();
//okay, now it blocks until it's finished instantiating and then executes #doStuff
Can I sick AspectJ on this somehow and get what I want done with local variable annotations? I know other threads have indicated that Java doesn't really support them but it would be magical. I really don't want to pass around a Future and break encapsulation.
Upvotes: 2
Views: 2742
Reputation: 299218
You can not do this with a proxy, but real aspectj bytecode weaving will get you there if you annotate the type instead of the local variable. (I don't think local variable access is supported as a pointcut). Anyway, here's some code.
An annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Later {}
A class marked with this annotation:
package com.dummy.aspectj;
@Later
public class HeavyObject{
public HeavyObject(){
System.out.println("Boy, I am 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 with main");
}
}
and an 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... this is too heavy");
try{
Thread.sleep(2000);
} catch(final InterruptedException e){
throw new IllegalStateException(e);
}
System.out.println("OK, now I am up to the task");
proceed();
}
}).start();
}
}
Here is the output from HeavyLifter when you run it as an AspectJ/Java Application from eclipse:
Finished with main
Wait... this is too heavy
OK, now I am up to the task
Boy, I am heavy
Upvotes: 2