Intellij + Ajc + Lombok/Mapstruct
Asked Answered
T

3

9

I want to enable AspectJ compiler in my IntelliJ Idea as I have several aspects that I'd like to be woven during compile time.

At the same time, I am using Lombok and Mapstruct in my codebase.

Those 2 require additional annotations processing, that must happen before ajc kicks in. I have both plugins installed for Lombok and Mapstruct. Independently they work fine, source code is being generated. But when I enable ajc and tick Enable annotation processing options, and then build a project, I get:

Error:(9, 0) ajc: Internal error in the mapping processor: java.lang.NullPointerException   at org.aspectj.org.eclipse.jdt.internal.compiler.lookup.FieldBinding.sourceField(FieldBinding.java:425)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl$SourceLocationComparator.determineSourceStart(TypeElementImpl.java:108)      at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl$SourceLocationComparator.getSourceStart(TypeElementImpl.java:72)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl$SourceLocationComparator.compare(TypeElementImpl.java:65)    at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl$SourceLocationComparator.compare(TypeElementImpl.java:1)     at java.util.TimSort.countRunAndMakeAscending(TimSort.java:360)     at java.util.TimSort.sort(TimSort.java:234)     at java.util.Arrays.sort(Arrays.java:1512)      at java.util.ArrayList.sort(ArrayList.java:1462)    at java.util.Collections.sort(Collections.java:175)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl.getEnclosedElements(TypeElementImpl.java:166)    at org.mapstruct.ap.internal.util.workarounds.SpecificCompilerWorkarounds.replaceTypeElementIfNecessary(SpecificCompilerWorkarounds.java:99)    at org.mapstruct.ap.internal.util.Executables.getAllEnclosedExecutableElements(Executables.java:99)     at org.mapstruct.ap.internal.model.common.Type.getAllMethods(Type.java:633)     at org.mapstruct.ap.internal.model.common.Type.getPropertyReadAccessors(Type.java:496)      at org.mapstruct.ap.internal.model.BeanMappingMethod$Builder.build(BeanMappingMethod.java:168)      at org.mapstruct.ap.internal.processor.MapperCreationProcessor.getMappingMethods(MapperCreationProcessor.java:376)      at org.mapstruct.ap.internal.processor.MapperCreationProcessor.getMapper(MapperCreationProcessor.java:151)      at org.mapstruct.ap.internal.processor.MapperCreationProcessor.process(MapperCreationProcessor.java:122)    at org.mapstruct.ap.internal.processor.MapperCreationProcessor.process(MapperCreationProcessor.java:76)     at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:283)     at org.mapstruct.ap.MappingProcessor.processMapperTypeElement(MappingProcessor.java:263)    at org.mapstruct.ap.MappingProcessor.processMapperElements(MappingProcessor.java:221)   at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:156)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:139)     at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.round(RoundDispatcher.java:121)   at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager.processAnnotations(BaseAnnotationProcessorManager.java:159)    at org.aspectj.org.eclipse.jdt.internal.compiler.Compiler.processAnnotations(Compiler.java:931)     at org.aspectj.org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:437)    at org.aspectj.org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:417)    at org.aspectj.ajdt.internal.core.builder.AjBuildManager.performCompilation(AjBuildManager.java:1036)   at org.aspectj.ajdt.internal.core.builder.AjBuildManager.performBuild(AjBuildManager.java:272)      at org.aspectj.ajdt.internal.core.builder.AjBuildManager.batchBuild(AjBuildManager.java:185)    at com.intellij.lang.aspectj.build.AjJpsCompiler.doBuild(AjJpsCompiler.java:249)    at com.intellij.lang.aspectj.build.AjJpsCompiler.build(AjJpsCompiler.java:119)      at com.intellij.lang.aspectj.build.AjTranslatingBuilder.doBuild(AjTranslatingBuilder.java:114)      at com.intellij.lang.aspectj.build.AjBuilderBase.build(AjBuilderBase.java:74)   at org.jetbrains.jps.incremental.IncProjectBuilder.runModuleLevelBuilders(IncProjectBuilder.java:1328)      at org.jetbrains.jps.incremental.IncProjectBuilder.runBuildersForChunk(IncProjectBuilder.java:1008)     at org.jetbrains.jps.incremental.IncProjectBuilder.buildTargetsChunk(IncProjectBuilder.java:1075)   at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunkIfAffected(IncProjectBuilder.java:969)     at org.jetbrains.jps.incremental.IncProjectBuilder.buildChunks(IncProjectBuilder.java:798)      at org.jetbrains.jps.incremental.IncProjectBuilder.runBuild(IncProjectBuilder.java:380)     at org.jetbrains.jps.incremental.IncProjectBuilder.build(IncProjectBuilder.java:178)    at org.jetbrains.jps.cmdline.BuildRunner.runBuild(BuildRunner.java:139)     at org.jetbrains.jps.cmdline.BuildSession.runBuild(BuildSession.java:302)   at org.jetbrains.jps.cmdline.BuildSession.run(BuildSession.java:135)    at org.jetbrains.jps.cmdline.BuildMain$MyMessageHandler.lambda$channelRead0$0(BuildMain.java:228)   at org.jetbrains.jps.service.impl.SharedThreadPoolImpl.lambda$executeOnPooledThread$0(SharedThreadPoolImpl.java:42)     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)      at java.util.concurrent.FutureTask.run(FutureTask.java:266)     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)      at java.lang.Thread.run(Thread.java:748)  

Also, I see a bunch of other ajc compilation errors as it is not able to locate methods that should've been generated by the Lombok.

How do I solve these issues?

Thunell answered 9/10, 2019 at 19:28 Comment(2)
Which version of acj are you using? Do you have the same problem with your build tool?Reginiaregiomontanus
/aspectj/aspectjtools/1.8.13/aspectjtools-1.8.13.jarThunell
C
4

Enabling Post-compile weave mode in IntelliJ solved it for me (https://www.jetbrains.com/help/idea/using-the-aspectj-ajc-compiler.html).

Chaperone answered 2/2, 2021 at 8:13 Comment(0)
S
5

Hi I had similar issue.

The fix was to let aspectj-maven-plugin to only start weaving after classes are compiled.

It describes in more detail here https://www.mojohaus.org/aspectj-maven-plugin/examples/weaveDirectories.html

I needed to:

(1) Specify folder where already compiled classes will be via <weaveDirectories >

(2) Change the phase of maven-compiler-plugin lifecycle to be run "process-classes" or later.

This is how my pom looked like.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${org.projectlombok.version}</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <compilerArg>-Amapstruct.defaultComponentModel="spring"</compilerArg>
                    <compilerArg>-Amapstruct.unmappedTargetPolicy=ERROR</compilerArg>
                    <compilerArg>-Amapstruct.suppressGeneratorTimestamp=true</compilerArg>
                    <compilerArg>-Amapstruct.suppressGeneratorVersionInfoComment=true</compilerArg>
                </compilerArgs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <showWeaveInfo>${showWeaveInfo}</showWeaveInfo>
                <showWeaveInfo>true</showWeaveInfo>
                <Xlint>ignore</Xlint>
                <complianceLevel>${java.compiler.version}</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>false</verbose>
                <verbose>true</verbose>
                <forceAjcCompile>true</forceAjcCompile>
                <sources/>
                <weaveDirectories>
                    <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                </weaveDirectories>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>com.lib.test</groupId>
                        <artifactId>test1</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>com.lib.test2</groupId>
                        <artifactId>test2</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
Sorilda answered 10/10, 2019 at 6:58 Comment(6)
For some reason, tests are not being properly compiled with the setup that you provided. [ERROR] The method getAList() is undefined for the type TransactionalIT.ObjectGraph ...transactional/NestedTransactionsJdbcDao.java:26 createEntitiesA(og.getAList());Thunell
any idea what part is the culprit here?Thunell
Here is the error that I see: Error:(18, 0) ajc: No property named "contentType" exists in source parameter(s). Did you mean "createdTime"? This is caused by lombok processor not being invoked. I had tried it to configure but no luck.Thunell
Here is the warning that precedes that error: Warning:ajc: 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: sun/apple javac 1.6, ECJThunell
I think that it would make sense to publish an MCVE on GitHub in order to enable us to reproduce and analyse your problem.Dunlop
Good example with 3 annotation processors: lombok, mapstruct and aspectj. I followed the indications here, that compile information from aspectj plugin and other similar issues, and it works. The key here is let aspectj weave generated classes and not interfere with mapstruct and aspectj processors for code generation.Towney
C
4

Enabling Post-compile weave mode in IntelliJ solved it for me (https://www.jetbrains.com/help/idea/using-the-aspectj-ajc-compiler.html).

Chaperone answered 2/2, 2021 at 8:13 Comment(0)
M
0

if you have aspect sources with the same project then you should add a few line codes to the @jun-huh answers and delete <aspectLibraries>. The below pom.xml is for if you use your aspects with the same project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>az.sanco</groupId>
    <artifactId>aspect</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.7</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.14.0</version>
                <configuration>
                    <forceAjcCompile>true</forceAjcCompile>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                    </weaveDirectories>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                    <proc>none</proc><!-- annotation process already finished, so we don't need to run again-->
                </configuration>
                <executions>
                    <execution>
                        <id>copy-only-aspect-files</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <!--when `javac` compile works it does not copy *.aj files to the 
                            output directory. We need to run `ajc` compile during `compile` phase and 
                            copy only *.aj files to weave our classes-->
                            <includes>
                                <include>**/*.aj</include><!--IMPORTANT-->
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Market answered 26/12, 2021 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.