Skip to content

Commit 7c45df7

Browse files
authored
Add mapstruct-processor to annotation processor paths when Lombok and MapStruct are used (#1175)
* Add mapstruct-processor to annotation processor paths for Lombok + MapStruct (#1174) * Use final fields for displayName and description * Use fluent .actual() in test assertion * Drop UpgradeToJava17Test full-chain test; covered by AddLombokMapstructBindingTest
1 parent ab370a9 commit 7c45df7

4 files changed

Lines changed: 173 additions & 5 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2026 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 lombok.EqualsAndHashCode;
19+
import lombok.Value;
20+
import org.jspecify.annotations.Nullable;
21+
import org.openrewrite.ExecutionContext;
22+
import org.openrewrite.ScanningRecipe;
23+
import org.openrewrite.TreeVisitor;
24+
import org.openrewrite.maven.AddAnnotationProcessor;
25+
import org.openrewrite.maven.MavenIsoVisitor;
26+
import org.openrewrite.maven.tree.ResolvedDependency;
27+
import org.openrewrite.xml.tree.Xml;
28+
29+
@Value
30+
@EqualsAndHashCode(callSuper = false)
31+
public class AddMapstructAnnotationProcessorPath extends ScanningRecipe<AddMapstructAnnotationProcessorPath.Accumulator> {
32+
33+
private static final String MAPSTRUCT_GROUP = "org.mapstruct";
34+
private static final String MAPSTRUCT_ARTIFACT = "mapstruct";
35+
private static final String MAPSTRUCT_PROCESSOR_ARTIFACT = "mapstruct-processor";
36+
37+
String displayName = "Add `mapstruct-processor` to the `maven-compiler-plugin` annotation processor paths";
38+
39+
String description = "Add the `mapstruct-processor` annotation processor path, matching the version of the `mapstruct` dependency, " +
40+
"so that MapStruct mappers are generated when annotation processing is configured explicitly.";
41+
42+
public static class Accumulator {
43+
@Nullable
44+
String mapstructVersion;
45+
final AddAnnotationProcessor.Scanned processorPaths = new AddAnnotationProcessor.Scanned();
46+
}
47+
48+
@Override
49+
public Accumulator getInitialValue(ExecutionContext ctx) {
50+
return new Accumulator();
51+
}
52+
53+
@Override
54+
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
55+
// The version is only consulted by the visitor; the scan phase ignores it, so any placeholder is fine here.
56+
TreeVisitor<?, ExecutionContext> processorPathScanner =
57+
new AddAnnotationProcessor(MAPSTRUCT_GROUP, MAPSTRUCT_PROCESSOR_ARTIFACT, "0").getScanner(acc.processorPaths);
58+
return new MavenIsoVisitor<ExecutionContext>() {
59+
@Override
60+
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
61+
processorPathScanner.visit(document, ctx);
62+
if (acc.mapstructVersion == null) {
63+
for (ResolvedDependency mapstruct : getResolutionResult().findDependencies(MAPSTRUCT_GROUP, MAPSTRUCT_ARTIFACT, null)) {
64+
acc.mapstructVersion = mapstruct.getVersion();
65+
break;
66+
}
67+
}
68+
return document;
69+
}
70+
};
71+
}
72+
73+
@Override
74+
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
75+
if (acc.mapstructVersion == null) {
76+
return TreeVisitor.noop();
77+
}
78+
return new AddAnnotationProcessor(MAPSTRUCT_GROUP, MAPSTRUCT_PROCESSOR_ARTIFACT, acc.mapstructVersion).getVisitor(acc.processorPaths);
79+
}
80+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ recipeList:
365365
groupId: org.projectlombok
366366
artifactId: lombok-mapstruct-binding
367367
version: 0.2.0
368+
- org.openrewrite.java.migrate.AddMapstructAnnotationProcessorPath
368369
---
369370
type: specs.openrewrite.org/v1beta/recipe
370371
name: org.openrewrite.java.migrate.AddLombokMapstructBindingMavenDependencyOnly

0 commit comments

Comments
 (0)