From 04c9755b39617a0e222a8e88ef44677b34044ab2 Mon Sep 17 00:00:00 2001 From: Aleksandar Kanchev <136312841+kanchev1@users.noreply.github.com> Date: Wed, 11 Sep 2024 06:45:09 +0200 Subject: [PATCH 1/3] [fixes #3116] Add multi round support for mapstruct --- AUTHORS | 1 + .../lombok/mapstruct/NotifierHider.java | 46 ++++++++++--------- .../lombok/launch/AnnotationProcessor.java | 5 -- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/AUTHORS b/AUTHORS index b291072f..b39b8450 100755 --- a/AUTHORS +++ b/AUTHORS @@ -1,6 +1,7 @@ Lombok contributors in alphabetical order: Adam Juraszek +Aleksandar Kanchev <136312841+kanchev1@users.noreply.github.com> Aleksandr Zhelezniak Amine Touzani Andre Brait diff --git a/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java b/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java index a8e92b4b..bed6c714 100644 --- a/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java +++ b/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java @@ -1,35 +1,37 @@ package lombok.mapstruct; -import java.lang.reflect.Field; - -import javax.lang.model.type.TypeMirror; - import org.mapstruct.ap.spi.AstModifyingAnnotationProcessor; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.type.TypeMirror; +import java.util.List; + +/** + * Report to MapStruct that a type is completed when there aren't any Lombok annotations left on it. Lombok annotations + * are removed whenever a class is processed. This way, annotations which require multiple rounds to process are also + * correctly handled, and MapStruct processing will be delayed until Lombok completely finishes processing required types. + */ class NotifierHider { public static class AstModificationNotifier implements AstModifyingAnnotationProcessor { - private static Field lombokInvoked; - - @Override public boolean isTypeComplete(TypeMirror type) { - if (System.getProperty("lombok.disable") != null) return true; - return isLombokInvoked(); - } - - private static boolean isLombokInvoked() { - if (lombokInvoked != null) { - try { - return lombokInvoked.getBoolean(null); - } catch (Exception e) {} + + @Override + public boolean isTypeComplete(final TypeMirror typeMirror) { + final List annotationMirrors = typeMirror.getAnnotationMirrors(); + if (annotationMirrors == null || annotationMirrors.isEmpty()) { return true; } - - try { - Class data = Class.forName("lombok.launch.AnnotationProcessorHider$AstModificationNotifierData"); - lombokInvoked = data.getField("lombokInvoked"); - return lombokInvoked.getBoolean(null); - } catch (Exception e) {} + + for (final AnnotationMirror annotationMirror : annotationMirrors) { + final String annotationName = String.valueOf(annotationMirror); + // check for ClaimingProcessor's SupportedAnnotationTypes + if (annotationName.startsWith("@lombok.")) { + return false; + } + } + return true; + } } } diff --git a/src/launch/lombok/launch/AnnotationProcessor.java b/src/launch/lombok/launch/AnnotationProcessor.java index 456a8cef..28a28d72 100644 --- a/src/launch/lombok/launch/AnnotationProcessor.java +++ b/src/launch/lombok/launch/AnnotationProcessor.java @@ -39,10 +39,6 @@ import sun.misc.Unsafe; class AnnotationProcessorHider { - public static class AstModificationNotifierData { - public volatile static boolean lombokInvoked = false; - } - public static class AnnotationProcessor extends AbstractProcessor { private final AbstractProcessor instance = createWrappedInstance(); @@ -60,7 +56,6 @@ class AnnotationProcessorHider { @Override public void init(ProcessingEnvironment processingEnv) { disableJava9SillyWarning(); - AstModificationNotifierData.lombokInvoked = true; instance.init(processingEnv); super.init(processingEnv); } From 081e8aa961994c84d193733c587996e4669bef1f Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 17 Oct 2024 23:50:26 +0200 Subject: [PATCH 2/3] [trivial] [style] --- .../lombok/mapstruct/NotifierHider.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java b/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java index bed6c714..3e0975f8 100644 --- a/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java +++ b/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java @@ -12,26 +12,18 @@ import java.util.List; * correctly handled, and MapStruct processing will be delayed until Lombok completely finishes processing required types. */ class NotifierHider { - public static class AstModificationNotifier implements AstModifyingAnnotationProcessor { - @Override - public boolean isTypeComplete(final TypeMirror typeMirror) { - final List annotationMirrors = typeMirror.getAnnotationMirrors(); - if (annotationMirrors == null || annotationMirrors.isEmpty()) { - return true; + public boolean isTypeComplete(TypeMirror typeMirror) { + List annotationMirrors = typeMirror.getAnnotationMirrors(); + if (annotationMirrors == null || annotationMirrors.isEmpty()) return true; + + for (AnnotationMirror annotationMirror : annotationMirrors) { + String annotationName = String.valueOf(annotationMirror); + if (annotationName.startsWith("@lombok.")) return false; } - - for (final AnnotationMirror annotationMirror : annotationMirrors) { - final String annotationName = String.valueOf(annotationMirror); - // check for ClaimingProcessor's SupportedAnnotationTypes - if (annotationName.startsWith("@lombok.")) { - return false; - } - } - + return true; - } } } From dc57606f3480626a5b0940989b7c292f14c07b6d Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Thu, 17 Oct 2024 23:50:47 +0200 Subject: [PATCH 3/3] MapStruct notifier: Ensure we always report lombok will not do anything further if lombok is entirely disabled. --- src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java b/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java index 3e0975f8..834b5bf2 100644 --- a/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java +++ b/src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java @@ -15,6 +15,7 @@ class NotifierHider { public static class AstModificationNotifier implements AstModifyingAnnotationProcessor { @Override public boolean isTypeComplete(TypeMirror typeMirror) { + if (System.getProperty("lombok.disable") != null) return true; List annotationMirrors = typeMirror.getAnnotationMirrors(); if (annotationMirrors == null || annotationMirrors.isEmpty()) return true;