eclipse-aspectj / aspectj

Other
289 stars 83 forks source link

Bug? NoClassDefFoundError when using around advice and LTW cache #314

Open guan-kevin opened 1 month ago

guan-kevin commented 1 month ago

in src/MyAspect.aj

public aspect MyAspect {
    before() : call(void Foo.bar()) {
        System.out.println("before bar");
    }

    void around(): call(void Foo.bar()) {
        System.out.println("around bar");
        proceed();
    }
}

In src/foo.java

class Foo {
    public void bar() {
        System.out.println("inside bar");
    }
    public static void main(String[] args) {
        Foo f = new Foo();
        f.bar();
    }
}

In classes/META-INF/aop.xml

<aspectj>
    <aspects>
        <aspect name="MyAspect"/>
    </aspects>
    <weaver options="-verbose">
    </weaver>
</aspectj>

Running the command

java -javaagent:path/to/aspectjweaver.jar -Dorg.aspectj.weaver.showWeaveInfo=true -Daj.weaving.cache.enabled=true -Daj.weaving.cache.dir=/tmp/aspectj-cache/ -cp classes Foo

twice will lead to the following error

Error: Unable to initialize main class Foo
Caused by: java.lang.NoClassDefFoundError: Foo$AjcClosure1

However, if I set cache implementation to shared, then it will work as expected:

java -javaagent:path/to/aspectjweaver.jar -Dorg.aspectj.weaver.showWeaveInfo=true -Daj.weaving.cache.enabled=true -Daj.weaving.cache.dir=/tmp/aspectj-cache/ -Daj.weaving.cache.impl=shared -cp classes Foo

Is this a bug? And what does this -Daj.weaving.cache.impl=shared shared implementation option do?

kriegaex commented 1 month ago

Maybe this is somehow similar or related to #285. I can reproduce this behaviour back until at least 1.8.13, i.e. it is not a new thing and dates back to when I was just a simple AspectJ user, not a contributor to the project yet.

Judging from this test configuration file, not using aj.weaving.cache.impl at all defaults to aj.weaving.cache.impl=perloader, which seems to be correct according to my quick experiments. If the cache ever worked for that case when used repeatedly and "just" broke a long time ago or if it never worked to begin with, is yet to be established. I admit that I never use the weaving cache myself, so I need to dig into this at some point. This is just my first feedback.

kriegaex commented 1 month ago

Please note my recent message to the aspectj-users and aspectj-announce mailing lists regarding my situation as an AspectJ maintainer.