Skip to content

Commit 317ca42

Browse files
authored
Only add lombok annotation processor to modules that use lombok (#1031)
* Add test reproducing lombok annotation processor bug in multi-module projects RepositoryHasDependency marks all files if any module has lombok, causing AddAnnotationProcessor to run on modules without lombok. * Use per-module DependencyInsight precondition for EnableLombokAnnotationProcessor RepositoryHasDependency checks the whole repo and marks ALL files when any module has lombok, causing AddAnnotationProcessor to run on modules without lombok. Switch to DependencyInsight which checks per-module, matching the pattern used by AddLombokMapstructBinding. Fixes moderneinc/customer-requests#2110 * Remove nested parent/child test that can't work with DependencyInsight DependencyInsight doesn't resolve dependencies in nested mavenProject test structures (test infrastructure limitation). The sibling mavenProject test already proves the per-module behavior correctly. * Use ModuleHasDependency instead of DependencyInsight for per-module precision Switch to ModuleHasDependency which is designed as a per-module precondition using ScanningRecipe to collect JavaProject markers. This is the proper counterpart to RepositoryHasDependency for module-scoped checks, and removes the need for the Singleton wrapper. * Assert annotation processor is added in multi-module test
1 parent 4970f3f commit 317ca42

2 files changed

Lines changed: 155 additions & 3 deletions

File tree

src/main/resources/META-INF/rewrite/java-version-25.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,9 @@ description: >-
170170
With Java 23 the encapsulation of JDK internals made it necessary to configure annotation processors like Lombok explicitly.
171171
The change is valid for older versions as well.
172172
preconditions:
173-
# we have to check the whole repo here as we might do a change parent pom not defining lombok as dependency
174-
- org.openrewrite.java.dependencies.search.RepositoryHasDependency:
173+
- org.openrewrite.java.dependencies.search.ModuleHasDependency:
175174
groupIdPattern: org.projectlombok
176175
artifactIdPattern: lombok
177-
- org.openrewrite.Singleton
178176
recipeList:
179177
- org.openrewrite.maven.AddAnnotationProcessor:
180178
groupId: org.projectlombok
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.test.RecipeSpec;
20+
import org.openrewrite.test.RewriteTest;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.openrewrite.java.Assertions.mavenProject;
24+
import static org.openrewrite.maven.Assertions.pomXml;
25+
26+
class EnableLombokAnnotationProcessorTest implements RewriteTest {
27+
28+
@Override
29+
public void defaults(RecipeSpec spec) {
30+
spec.recipeFromResources("org.openrewrite.java.migrate.EnableLombokAnnotationProcessor");
31+
}
32+
33+
@Test
34+
void doesNotAddLombokWhenNotPresentViaUpgradeToJava25() {
35+
rewriteRun(
36+
spec -> spec.recipeFromResources("org.openrewrite.java.migrate.UpgradeToJava25"),
37+
mavenProject("project",
38+
pomXml(
39+
//language=xml
40+
"""
41+
<project>
42+
<groupId>com.mycompany.app</groupId>
43+
<artifactId>my-app</artifactId>
44+
<version>1</version>
45+
<properties>
46+
<maven.compiler.release>17</maven.compiler.release>
47+
</properties>
48+
<dependencies>
49+
<dependency>
50+
<groupId>com.google.guava</groupId>
51+
<artifactId>guava</artifactId>
52+
<version>33.0.0-jre</version>
53+
</dependency>
54+
</dependencies>
55+
</project>
56+
""",
57+
//language=xml
58+
"""
59+
<project>
60+
<groupId>com.mycompany.app</groupId>
61+
<artifactId>my-app</artifactId>
62+
<version>1</version>
63+
<properties>
64+
<maven.compiler.release>25</maven.compiler.release>
65+
</properties>
66+
<dependencies>
67+
<dependency>
68+
<groupId>com.google.guava</groupId>
69+
<artifactId>guava</artifactId>
70+
<version>33.0.0-jre</version>
71+
</dependency>
72+
</dependencies>
73+
</project>
74+
"""
75+
)
76+
)
77+
);
78+
}
79+
80+
@Test
81+
void doesNotAddLombokToModuleWithoutLombok() {
82+
rewriteRun(
83+
mavenProject("module-with-lombok",
84+
pomXml(
85+
//language=xml
86+
"""
87+
<project>
88+
<groupId>com.mycompany.app</groupId>
89+
<artifactId>module-with-lombok</artifactId>
90+
<version>1</version>
91+
<dependencies>
92+
<dependency>
93+
<groupId>org.projectlombok</groupId>
94+
<artifactId>lombok</artifactId>
95+
<version>1.18.40</version>
96+
</dependency>
97+
</dependencies>
98+
</project>
99+
""",
100+
spec -> spec.after(actual ->
101+
assertThat(actual)
102+
.containsPattern("<annotationProcessorPaths>(.|\\n)*<path>(.|\\n)*<groupId>org.projectlombok")
103+
.containsPattern("<annotationProcessorPaths>(.|\\n)*<path>(.|\\n)*<artifactId>lombok")
104+
.actual()
105+
)
106+
)
107+
),
108+
mavenProject("module-without-lombok",
109+
pomXml(
110+
//language=xml
111+
"""
112+
<project>
113+
<groupId>com.mycompany.app</groupId>
114+
<artifactId>module-without-lombok</artifactId>
115+
<version>1</version>
116+
<dependencies>
117+
<dependency>
118+
<groupId>com.google.guava</groupId>
119+
<artifactId>guava</artifactId>
120+
<version>33.0.0-jre</version>
121+
</dependency>
122+
</dependencies>
123+
</project>
124+
"""
125+
)
126+
)
127+
);
128+
}
129+
130+
@Test
131+
void doesNotAddLombokWhenNotPresent() {
132+
rewriteRun(
133+
mavenProject("project",
134+
pomXml(
135+
//language=xml
136+
"""
137+
<project>
138+
<groupId>com.mycompany.app</groupId>
139+
<artifactId>my-app</artifactId>
140+
<version>1</version>
141+
<dependencies>
142+
<dependency>
143+
<groupId>com.google.guava</groupId>
144+
<artifactId>guava</artifactId>
145+
<version>33.0.0-jre</version>
146+
</dependency>
147+
</dependencies>
148+
</project>
149+
"""
150+
)
151+
)
152+
);
153+
}
154+
}

0 commit comments

Comments
 (0)