Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Please help getting started - load time weaving (LTW) doesn't work

Hi all,

My problem is that AspectJ doesn't weave my aspect, so I've reduced it
to test - still no weaving.
I have this aspect:

public privileged aspect MyAspect {
    pointcut testMethod() : execution(* *.*(..));
    pointcut getField() : get(* (@Entity *).*);

    Object around() : testMethod() {
        System.err.println("JOINPOINT testMethod() called");
        return "ASPECTVALUE";
    }
    Object around(): getField() {
        System.err.println("getField() called");
        return "ASPECTVALUE";
    }
}

which should, as I understand it, weave to all methods and these classes:
MyEntity.java

@Entity
public class MyEntity {
    public String ajField="startValue";

    public String testGet() { return ajField; }
    public void testSet(String val) { ajField=val; }

    public String ajMethod() {
        System.out.println("public ajMethod() called");
        return "ValueFromAJMethod";
    }

    public Object indirectM2() {
        return ajMethod2();
    }

    private Object ajMethod2() {
        System.out.println("private ajMethod2() called");
        return "ValueFromAJMethod";

    }
}

and Test.java:
public class Test {
    public static void main(String[] args) {
        Test t=new Test();
    }
    public Test() {
        Object result="OriginalValue";
        MyEntity e=new MyEntity();
        System.out.println("Calling ajMethod()");
        result=e.ajMethod();
        System.out.println("ajMethod() returned '"+result+"'");
        System.out.println("Calling ajMethod2() via indirectM2()");
        result=e.indirectM2();
        System.out.println("ajMethod2() via indirectM2() returned
'"+result+"'");
        System.out.println("Calling testGet()");
        result=e.testGet();
        System.out.println("testGet() returned '"+result+"'");
        System.out.println("Calling testSet(...)");
        e.testSet("newValue");
        System.out.println("Calling testGet()");
        result=e.testGet();
        System.out.println("testGet() returned '"+result+"'");
        System.out.println("Exiting");
    }
}

and this META-INF/aop.xml:
<aspectj>
    <aspects>
        <aspect name="MyAspect"/>
        <!-- no includes/excludes means "use all (declared) aspects" -->
    </aspects>

    <weaver options="-verbose -debug -showWeaveInfo">
        <!-- no includes/excludes means "weave to all classes (visible
to the weaver)" -->
    </weaver>

</aspectj>

I start the program with -javaagent:path/to/aspectjweaver.jar and
indeed, get (among others) these log messages:

[AppClassLoader@1f12c4e] info AspectJ Weaver Version 1.5.3 built on
Wednesday Nov 22, 2006 at 11:18:15 GMT
[AppClassLoader@1f12c4e] info register classloader
sun.misc.Launcher$AppClassLoader@1f12c4e
[AppClassLoader@1f12c4e] info using configuration
file:/[...]myApp.jar!/META-INF/aop.xml
[AppClassLoader@1f12c4e] info register aspect MyAspect

and even:
[AppClassLoader@1f12c4e] debug weaving 'Test'
[AppClassLoader@1f12c4e] debug weaving 'MyEntity'

yet no aspect seems to get executed (what I really want to do is
intercept field-access but tried with the method to get at least
_something_ weaved/running), no method prints anything or even returns
one of the values of my aspect.
I build with ant & iajc task and use JDK6u2 on WinXP SP2; I also tried
to run with the aj5 script with same results.
Do I have something wrong with my pointcuts? And why does AspectJ tell
me that it is weaving (among others) the classes I want it to?
Can anybody please help me to at least get started in my environment?
(Please note that I removed package-names for shortening and b/c I'm not
sure whether I may publish them, but they're correct, I'm sure).

Addendum:
I read (in the reply to "AspectJ compilation question") that "Yes, AspectJ supports weaving inpath classes and jars. This is available through the ajc command line interface, ant tools, and Eclipse plugin AJDT."

The classes Test and MyEntity are not available to the aspectj-compiler (iajc) at compile time and I get the warning "advice defined in MyAspect has not been applied [Xlint:adviceDidNotMatch]". Does that mean that all classes to be woven, even with the load-time-weaver, must be kind of "prepared" by the AspectJ compiler? If yes, is there any way around this, as it would make AspectJ useless for my case (thus I cannot imagine that this is required). If the to-be-weaved classes (Test, MyEntity) are available to the AspectJ compiler, it seems to work; the result is also different when using JDK5 instead of JDK6, I then get some exceptions - Searching in Google, it seems AspectJ 1.5.3 supports (or at least "works on") JDK6, even improving performance quite a bit.

So, can anybody help? Of course, I'll give further info if needed.

Thanks in advance & kind regards,

Messi



Back to the top