Skip to content

Commit 04c9755

Browse files
committed
[fixes #3116] Add multi round support for mapstruct
1 parent 2773ed5 commit 04c9755

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Lombok contributors in alphabetical order:
22

33
Adam Juraszek <juriad@gmail.com>
4+
Aleksandar Kanchev <136312841+kanchev1@users.noreply.github.com>
45
Aleksandr Zhelezniak <lekan1992@gmail.com>
56
Amine Touzani <ttzn.dev@gmail.com>
67
Andre Brait <andrebrait@gmail.com>
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
package lombok.mapstruct;
22

3-
import java.lang.reflect.Field;
3+
import org.mapstruct.ap.spi.AstModifyingAnnotationProcessor;
44

5+
import javax.lang.model.element.AnnotationMirror;
56
import javax.lang.model.type.TypeMirror;
7+
import java.util.List;
68

7-
import org.mapstruct.ap.spi.AstModifyingAnnotationProcessor;
8-
9+
/**
10+
* Report to MapStruct that a type is completed when there aren't any Lombok annotations left on it. Lombok annotations
11+
* are removed whenever a class is processed. This way, annotations which require multiple rounds to process are also
12+
* correctly handled, and MapStruct processing will be delayed until Lombok completely finishes processing required types.
13+
*/
914
class NotifierHider {
1015

1116
public static class AstModificationNotifier implements AstModifyingAnnotationProcessor {
12-
private static Field lombokInvoked;
13-
14-
@Override public boolean isTypeComplete(TypeMirror type) {
15-
if (System.getProperty("lombok.disable") != null) return true;
16-
return isLombokInvoked();
17-
}
18-
19-
private static boolean isLombokInvoked() {
20-
if (lombokInvoked != null) {
21-
try {
22-
return lombokInvoked.getBoolean(null);
23-
} catch (Exception e) {}
17+
18+
@Override
19+
public boolean isTypeComplete(final TypeMirror typeMirror) {
20+
final List<? extends AnnotationMirror> annotationMirrors = typeMirror.getAnnotationMirrors();
21+
if (annotationMirrors == null || annotationMirrors.isEmpty()) {
2422
return true;
2523
}
26-
27-
try {
28-
Class<?> data = Class.forName("lombok.launch.AnnotationProcessorHider$AstModificationNotifierData");
29-
lombokInvoked = data.getField("lombokInvoked");
30-
return lombokInvoked.getBoolean(null);
31-
} catch (Exception e) {}
24+
25+
for (final AnnotationMirror annotationMirror : annotationMirrors) {
26+
final String annotationName = String.valueOf(annotationMirror);
27+
// check for ClaimingProcessor's SupportedAnnotationTypes
28+
if (annotationName.startsWith("@lombok.")) {
29+
return false;
30+
}
31+
}
32+
3233
return true;
34+
3335
}
3436
}
3537
}

src/launch/lombok/launch/AnnotationProcessor.java

-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@
3939

4040
class AnnotationProcessorHider {
4141

42-
public static class AstModificationNotifierData {
43-
public volatile static boolean lombokInvoked = false;
44-
}
45-
4642
public static class AnnotationProcessor extends AbstractProcessor {
4743
private final AbstractProcessor instance = createWrappedInstance();
4844

@@ -60,7 +56,6 @@ public static class AnnotationProcessor extends AbstractProcessor {
6056

6157
@Override public void init(ProcessingEnvironment processingEnv) {
6258
disableJava9SillyWarning();
63-
AstModificationNotifierData.lombokInvoked = true;
6459
instance.init(processingEnv);
6560
super.init(processingEnv);
6661
}

0 commit comments

Comments
 (0)