Search code examples
javaaspectj

AspectJ configuration / setup - pointcuts not firing


I am trying to get my first AspectJ aspect running. Setup looks ok to me, following the documentation I can find. e.g.

But no aspect will fire, no matter what options I have tried during all today.

I believe the pointcuts is correctly pointing to my method call because I was getting the error about pointcuts not being used when I changed it. (Thats not happening now - dont know what I did :-())

So is the problem in the setup? The pointcuts? How do you go about de-bugging such a situation? Am I missing something in the docs?

POM

<packaging>jar</packaging>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.14.0</version>
            <!--
            <groupId>dev.aspectj</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.13.2-SNAPSHOT</version>
            -->
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
                        <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.9.7</version>
                </dependency>
            </dependencies>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <forceAjcCompile>true</forceAjcCompile>
                <sources/>
                <weaveDirectories>
                    <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                </weaveDirectories>
                <complianceLevel>11</complianceLevel>
                <source>11</source>
                <target>11</target>
                <verbose>true</verbose>
                <Xlint>warning</Xlint>
            </configuration>
        </plugin>
    </plugins>
</build>
   

<dependencies>
.....
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.22</version>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.6.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.6.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.6.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jetbrains</groupId>
        <artifactId>annotations</artifactId>
        <version>22.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/dev.aspectj/aspectj-maven-plugin -->
    <dependency>
        <groupId>dev.aspectj</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.13.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.7</version>
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.9.7</version>
    </dependency>

</dependencies>

ASPECT

public aspect   MdDOMAINChildAspect {

    pointcut myExceptionHandlerPointcut(): execution(void mdDOMAIN.MdDOMAINInstance.testCall() );

    before(): myExceptionHandlerPointcut() {
        System.out.println("-------------- Aspect Advice Logic ---------------");
    }

CLASS

@Data
public class MdDOMAINInstance {


    @MdDOMAINChildAnnotation(selectName = "Indicators")
    public MdDOMAINInstance selectFilingIndicators() throws NoSuchMethodException {

        testCall();

        Method m=this.getClass().getMethod("selectFilingIndicators");
        MdDOMAINChildAnnotation metaidAnnotation=m.getAnnotation(MdDOMAINChildAnnotation.class);
        System.out.println("Annotation: " + metaidAnnotation.selectName());

        return this;
    }

    public void testCall()
    {
        System.out.println("In testCall()");
        return ;
    }
}

TEST

class MdDOMAINInstanceTest {

    @Test
    void selectFilingIndicatorsTest() throws NoSuchMethodException {
        MdDOMAINInstance mdDOMAINInstance = new MdDOMAINInstance().selectFilingIndicators();
    }
}

MAVEN TEST RUN

clean test -Dtest=MdDOMAINInstanceTest -f pom.xml

MAVEN TEST RUN OUTCOME

"C:\Program Files\Java\jdk-11.0.12\bin\java.exe" -Dmaven.multiModuleProjectDirectory=....IdeaProjects\us-MYROOT-DOMAIN-004 "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\plugins\maven\lib\maven-event-listener.jar" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\lib\idea_rt.jar=52349:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2021.3.2 clean test -Dtest=MdDOMAINInstanceTest
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for us-MYROOT:us-MYROOT-DOMAIN-004:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.version' for org.junit.jupiter:junit-jupiter:jar is either LATEST or RELEASE (both of them are being deprecated) @ line 117, column 22
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 20, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] --------------< us-MYROOT:us-MYROOT-DOMAIN-004 >--------------
[INFO] Building us-MYROOT-DOMAIN-004 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ us-MYROOT-DOMAIN-004 ---
[INFO] Deleting .....IdeaProjects\us-MYROOT-DOMAIN-004\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ us-MYROOT-DOMAIN-004 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ us-MYROOT-DOMAIN-004 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to .....IdeaProjects\us-MYROOT-DOMAIN-004\target\classes
[INFO] 
[INFO] --- aspectj-maven-plugin:1.14.0:compile (default) @ us-MYROOT-DOMAIN-004 ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ us-MYROOT-DOMAIN-004 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ us-MYROOT-DOMAIN-004 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to .....IdeaProjects\us-MYROOT-DOMAIN-004\target\test-classes
[INFO] 
[INFO] --- aspectj-maven-plugin:1.14.0:test-compile (default) @ us-MYROOT-DOMAIN-004 ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[WARNING] You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.
Your processor is: org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl
Lombok supports: OpenJDK javac, ECJ
    <unknown source file>:<no line information>

[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ us-MYROOT-DOMAIN-004 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running mdDOMAIN.MdDOMAINInstanceTest
In testCall()
Annotation: Indicators
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 s - in mdDOMAIN.MdDOMAINInstanceTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  22.238 s
[INFO] Finished at: 2022-10-21T20:14:46+11:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

Solution

  • Have you tried without Lombok in order to avoid this problem? Try doing manually in your test class what the Lombok annotation does instead.

    After we have established that this solves the problem, we can start looking into possible workarounds. If this really is your first AspectJ problem, you should not start with a difficult mix like AspectJ + Lombok.

    Besides, if your @Data class is immutable, you might not need Lombok at all and could use a recent Java version and native Java records for that.