Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AspectJ plugin matching join points but not weaving them #835

Open
erichansen-nano opened this issue Jul 22, 2023 · 4 comments
Open

AspectJ plugin matching join points but not weaving them #835

erichansen-nano opened this issue Jul 22, 2023 · 4 comments

Comments

@erichansen-nano
Copy link

Hello,
I'm struggling to convert my maven project to to Gradle. The maven project compiles and weaves aspects using aspectj-maven-plugin v1.14.0 . When I add the io.freefair.aspectj plugin,

plugins {
    id 'java-library'
    id 'maven-publish'
    id 'edu.sc.seis.launch4j' version '3.0.3'
    id 'application'
    //shadow jar plugin for packaging everything into one jar
    id 'com.github.johnrengelman.shadow' version '8.1.1'

    id "io.freefair.aspectj" version "8.1.0"
}

the build output shows that the aspects are compiling but not matching anything:

[warning] advice defined in UIActivityAspect has not been applied [Xlint:adviceDidNotMatch]

when I add

compileAspectj.ajcOptions.sourceroots.from sourceSets.main.java.srcDirs

to the build file, then all the matches are found. For example,

... (TaskCellFactory.java:192) advised by after advice from 'com.nanocellect.wolfi.aop.UIActivityAspect' (UIActivityAspect.java:57)

Based on the docs, it seems that the plugin should not need to be configured with the 'sourceroots' but this is the only way I've been able to get the build to find the matching join points.

With that problem solved, my current issue is that advice is not being executed when I run my app. So it seems that the classes are not being instrumented with the advice even though the log entry implies that it is.

Here is my setup:

  • java 8
  • io.freefair.aspectj 8.1.0
  • Gradle 8.1.1
  • org.aspectj:aspectjrt:1.9.7
  • Annotated aspects are in src/main/aspectj
  • Application source is src/main/java

Do you have any ideas I can try? or let me know what additional information you need.

Thank you.

@larsgrefer
Copy link
Member

When using the io.freefair.aspectj plugin, only sources in src/*/aspectj are affected by default.

src/*/aspectj is compiled and woven by ajc, src/*/java is compiled by javac and therefore NOT woven.

When you configured the compileAspectj to also read src/*/java your classes have been compiled twice: Once by ajc (with woven aspects) and once by javac (without woven aspects). I assume you have seen the logs from the first compilation but the result of the second compilation has been used at runtime.

Just put all your sources in src/*/aspectj and you should be fine.

@erichansen-nano
Copy link
Author

Hi Lars, thank you for the explanation... it makes sense.
With the maven plugin, it worked on any sources in src/main/java. Is there way to configure the io.freefair.aspectj plugin to not require the code by in an aspectj directory?

@erichansen-nano
Copy link
Author

erichansen-nano commented Aug 9, 2023

To be clear, my aspect classes are in src/main/aspectj but I'd like the classes that I'm weaving to be in src/main/java.

edit: I'm also seeing the Lombok's @log4j2 annotation does not get processed with the ajc compiler. Is there way to configure that? and FWIW, I was able to use the post compile weaving plugin and make this work without moving my source code

Thanks

@larsgrefer
Copy link
Member

larsgrefer commented Aug 13, 2023

When using Lombok, you must use the post-compile-weaving plugin, as Lombok does not support ajc.

An example for compile-time-weaving with the io.freefair.aspectj plugin while also using src/main/java, can be found here: https://github.com/spring-projects/spring-framework/blob/2c895974b245783f187e2e332858a5f2f48fde08/spring-aspects/spring-aspects.gradle#L3C1-L9C39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants