0

I'm using Gradle 7.4 and applying the io.freefair.aspectj.post-compile-weaving plugin. I want to test my aspect so I wrote a test class including an static inner class which should be advised by my aspect but my aspect does not advise it. I also wrote another class which should be advised in src/main/java directory and my aspect properly advise it.

For example I have following aspect, classes and tests in package com.example.

  • aspect class.
@Aspect
public class MyAspect {
  @AfterReturning("@within(com.example.ShouldBeWoven) && execution(* .new(..))")
  public void myAdvice(JoinPoint joinPoint) {
    System.out.println("******* MyAspect advised: " + joinPoint.getSignature().getDeclaringTypeName());
  }
}
  • marker annotation
@Documented
@Target({TYPE})
@Retention(RUNTIME)
public @interface ShouldBeWoven {
}
  • target static inner class in main source set
class MyClass {

  @ShouldBeWoven
  static class WovenClassInMain {
  }

}
  • target static inner class and test code in test source set
class MyAspectTest {

  @ShouldBeWoven
  static class WovenClassInTest {
  }

  @Test
  void instantiateWovenClassInMain() {
    new WovenClassInMain();
  }
  
  @Test
  void instantiateWovenClassInTest() {
    new WovenClassInTest();
  }

}

The result was that instantiateWovenClassInMain showed message in the advice and instantiateWovenClassInTest did not. How can I weave my aspects to classes in src/test/java directory?

I could achieve this when I was using Load Time Weaving by the way.

Here is an MCVE.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
avenue68
  • 43
  • 7
  • 1
    What is the point of doing post-compile weaving within a single module? Usually, you would use that technique when weaving into existing libraries (possible third-party) which you do not have source code access to. You have a single module. There you should use the `io.freefair.aspectj` plugin rather than `io.freefair.aspectj.post-compile-weaving`. – kriegaex Jan 12 '23 at 09:12
  • 1
    @kriegaex I think it's required to do post-compile weaving to use AspectJ with Lombok. And of course my production code consists of multiple modules but it seems that this problem is not related to the module is single or multi so my example is a single module. – avenue68 Jan 12 '23 at 13:09

1 Answers1

3

You need to add this to your Gradle build config:

compileTestJava.ajc.options.aspectpath.from sourceSets.main.output 

See also here.

I tried with your sample project, and it works for me with these properties set:

compileJava.ajc.options.compilerArgs << "-showWeaveInfo -verbose"
compileTestJava.ajc.options.compilerArgs << "-showWeaveInfo -verbose"
compileTestJava.ajc.options.aspectpath.from sourceSets.main.output

I also had to rename your class PluginTest to PostCompileWeavingPluginTest in order to avoid a build error, because the file name was different from the class name.

The clean test build log now says:

> Task :clean
> Task :compileJava
Join point 'constructor-execution(void com.example.MyClass$WovenClassInMain.<init>())' in Type 'com.example.MyClass$WovenClassInMain' (WovenClassInMain.java:6) advised by afterReturning advice from 'com.example.MyAspect' (MyAspect.class(from MyAspect.java))
    
(...)

> Task :compileTestJava
Join point 'constructor-execution(void com.example.MyAspectTest$WovenClassInTest.<init>())' in Type 'com.example.MyAspectTest$WovenClassInTest' (MyAspectTest.java:9) advised by afterReturning advice from 'com.example.MyAspect' (MyAspect.class(from MyAspect.java))
    
(...)

> Task :test
******* MyAspect wove: com.example.MyClass$WovenClassInMain
******* MyAspect wove: com.example.MyAspectTest$WovenClassInTest
root project 'test'
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Your answer is completely what I needed!! Thank you for quick answering and I'm sorry for lefting some trash codes, I'll organize them later. – avenue68 Jan 12 '23 at 14:57
  • Excuse me, can you please add this code but for .kts? I cannot understand how just it transform :( – S-Kerrigan Mar 31 '23 at 06:59
  • It would be better to ask a new question, providing an [MCVE](https://stackoverflow.com/help/mcve). I do not speak Gradle, only Maven. I helped as much as I can here, but whatever the meaning of "kts" may be (Kotlin Script?), I do not know. If you, just like me, do not speak Kotlin either, maybe you want to stick with Groovy syntax. – kriegaex Mar 31 '23 at 09:59