1

There are two different projects in which we need to use AspectJ.

Plugin in pom.xml:

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.10</version>
            <configuration>
                <verbose>true</verbose>
                <showWeaveInfo>true</showWeaveInfo>
                <source>1.8</source>
                <target>1.8</target>
                <complianceLevel>1.8</complianceLevel>
                <!-- <encoding>UTF-8</encoding> -->
                <verbose>false</verbose>
                <Xlint>ignore</Xlint>
                <outxml>true</outxml>
                <forceAjcCompile>true</forceAjcCompile>
                <reweavable>false</reweavable>
                <!-- this is important: start-->
                <sources/>
                <weaveDirectories>
                    <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                </weaveDirectories>
                <!-- this is important: end-->
            </configuration>
            <executions>
                <execution>
                    <!-- The right phase is very important! Compile and weave aspects after all classes compiled by javac -->
                    <phase>process-classes</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>1.8.9</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.8.9</version>
                </dependency>
            </dependencies>
        </plugin>*

Dependencies:

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency>

Exception: Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression 'handler(*) && args(e)' contains unsupported pointcut primitive 'handler'

The first project does not contain a Lombok, so the "Build Project" or "Rebuild Project" helps in this situation. But there is weaving during compilation, and everything works out correctly.

But the second project uses Lombok, and the solution with "Build/Rebuild" does not help, because build means weaving at compile time and => AspectJ does not see the functionality of Lombok (For example, getters).

At the same time, a setting has been introduced in the plugin so that weaving works on the spot, and not during compilation: <forceAjcCompile>true</forceAjcCompile> and empty <sources/>.

The combination of AspectJ and Lombok is mentioned in the news documents for frequently asked questions on Lambda power tools: https://awslabs.github.io/aws-lambda-powertools-java/FAQs/

Poweretools uses aspectj-maven-plugin to compile-time weave (CTW) aspects into the project. In case you want to use Lombok or other compile-time preprocessor for your project, it is required to change aspectj-maven-plugin configuration to enable in-place weaving feature. Otherwise the plugin will ignore changes introduced by Lombok and will use .java files as a source. To enable in-place weaving feature you need to use following aspectj-maven-plugin configuration:

<configuration>
    <forceAjcCompile>true</forceAjcCompile> 
    <sources/>
    <weaveDirectories>
        <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
    </weaveDirectories>
    ...
    <aspectLibraries>
        <aspectLibrary>
            <groupId>software.amazon.lambda</groupId>
            <artifactId>powertools-logging</artifactId>
        </aspectLibrary>
    </aspectLibraries>
</configuration>

A source with the same information, the comments helped a lot: https://palesz.wordpress.com/2011/12/03/howto-maven-lombok-and-aspectj-together/

The key of the solution is the empty sources tag in the configuration and the forceAjcCompile=true setting.

Please tell me why only "Build" (i.e. weaving during compilation) can help solve this error? What is missing (what is there when starting "build") to start weaving in place? How is it possible to resolve this situation?

Mary
  • 35
  • 5
  • I do not know, but there is a detail in the quoted text: "*Otherwise the plugin will ignore changes introduced by Lombok and will use .java files as a source*". I thought AspectJ works directly on the compiled bytecode. If this quote is correct, it seems that AspectJ works by default with the source code, and yeah, it cannot find the artifacts introduced by Lombok (getters, setters, etc)! – Nikos Paraskevopoulos Jun 22 '22 at 07:57
  • Mary, you have been asking several questions here as a newbie lately, but everywhere you have failed to provide feedback to comments, suggestions, answers. Neither is that polite nor does it help you to solve your problem. Next time when asking questions, only do so if you are willing to (a) make your problem reproducible and (b) spend a little bit of time to give feedback to the community trying to help you. Like this, you are not going to get many friends here, and people will refrain from trying to help you again in the future, because their efforts are futile anyway. – kriegaex Jul 09 '22 at 08:55

1 Answers1

1

The UnsupportedPointcutPrimitiveException message tells you that you seem to be mixing native AspectJ aspects with Spring AOP ones, or maybe you forgot to configure Spring in a way that makes it stop from trying to wire AspectJ aspects redundantly as Spring AOP ones again.

How can I know that without having seen your aspect code or Spring config? Because native AspectJ knows handler() pointcuts, but Spring AOP does not. So it must be Spring picking it up while wiring the application. This problem is completely unrelated to Lombok.

As you might have noticed, I cannot answer more precisely, because your question is so generic and is specifically lacking a sample project or any code, for that matter. If you would please be so kind to post an MCVE on GitHub, I can help you fix your project, in case my general explanation what went wrong is not comprehensive enough for you.

kriegaex
  • 63,017
  • 15
  • 111
  • 202