Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Compile Time Weaving a Constructor

I am looking for a way to weave a constructor statically so that it calls some piece of code at the end of the instantiation process.

I am using AspectJ 1.6.5 .

I looked at the FAQ here at -
http://www.eclipse.org/aspectj/doc/released/faq.html#q:adviseconstructors .

I need to get the second option with the join point inside the constructor .


import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

@Aspect
class ToInject {

    @AfterReturning(value = "execution(WeaveMe.new(..))", returning = "obj")
    public void aop(WeaveMe obj) {
        // Warning: advice defined in poc.ToInject has not been applied.
        obj.setA(100);
    }
}

class WeaveMe {

    private int a;

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

}

public class App {

    public static void main(String[] args) {
        WeaveMe weave = new WeaveMe();
        System.out.println(weave.getA());
        // Error: I am expecting 100 but am getting 0.
    }
}

The advice does not seem to be working but I get a compiler error in the IDE (AJDT 2.0.0 on Eclipse).


What I prefer from the weaving is to see the bytecode of WeaveMe constructor changed. ( and not that of App , where it is getting called ) since I need to use this in a larger context of Spring injection.

Can somebody help with the same, as to if the advice is correct.  Thanks.

Back to the top