views:

43

answers:

1

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.

+1  A: 

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
seanizer
After thinking about your solution, I realized it's probably better than having an inline assignment of @nonblocking simply because it should be inherent to the object to know that is "heavy". So I am satisfied for the time being... Thanks!
Anthony Bishopric