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