Skip to content

Commit a7960ef

Browse files
committed
Add mapstruct-processor to annotation processor paths for Lombok + MapStruct (#1174)
1 parent ab370a9 commit a7960ef

5 files changed

Lines changed: 217 additions & 5 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
@Override
38+
public String getDisplayName() {
39+
return "Add `mapstruct-processor` to the `maven-compiler-plugin` annotation processor paths";
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Add the `mapstruct-processor` annotation processor path, matching the version of the `mapstruct` dependency, " +
45+
"so that MapStruct mappers are generated when annotation processing is configured explicitly.";
46+
}
47+
48+
public static class Accumulator {
49+
@Nullable
50+
String mapstructVersion;
51+
final AddAnnotationProcessor.Scanned processorPaths = new AddAnnotationProcessor.Scanned();
52+
}
53+
54+
@Override
55+
public Accumulator getInitialValue(ExecutionContext ctx) {
56+
return new Accumulator();
57+
}
58+
59+
@Override
60+
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
61+
// The version is only consulted by the visitor; the scan phase ignores it, so any placeholder is fine here.
62+
TreeVisitor<?, ExecutionContext> processorPathScanner =
63+
new AddAnnotationProcessor(MAPSTRUCT_GROUP, MAPSTRUCT_PROCESSOR_ARTIFACT, "0").getScanner(acc.processorPaths);
64+
return new MavenIsoVisitor<ExecutionContext>() {
65+
@Override
66+
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
67+
processorPathScanner.visit(document, ctx);
68+
if (acc.mapstructVersion == null) {
69+
for (ResolvedDependency mapstruct : getResolutionResult().findDependencies(MAPSTRUCT_GROUP, MAPSTRUCT_ARTIFACT, null)) {
70+
acc.mapstructVersion = mapstruct.getVersion();
71+
break;
72+
}
73+
}
74+
return document;
75+
}
76+
};
77+
}
78+
79+
@Override
80+
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
81+
if (acc.mapstructVersion == null) {
82+
return TreeVisitor.noop();
83+
}
84+
return new AddAnnotationProcessor(MAPSTRUCT_GROUP, MAPSTRUCT_PROCESSOR_ARTIFACT, acc.mapstructVersion).getVisitor(acc.processorPaths);
85+
}
86+
}

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)