Skip to content

Commit

Permalink
[fixes #3116] Add multi round support for mapstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
kanchev1 committed Sep 11, 2024
1 parent 2773ed5 commit 61ea0de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
44 changes: 23 additions & 21 deletions src/bindings/mapstruct/lombok/mapstruct/NotifierHider.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package lombok.mapstruct;

import java.lang.reflect.Field;
import org.mapstruct.ap.spi.AstModifyingAnnotationProcessor;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.type.TypeMirror;
import java.util.List;

import org.mapstruct.ap.spi.AstModifyingAnnotationProcessor;

/**
* 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<? extends AnnotationMirror> 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;

}
}
}
5 changes: 0 additions & 5 deletions src/launch/lombok/launch/AnnotationProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@

class AnnotationProcessorHider {

public static class AstModificationNotifierData {
public volatile static boolean lombokInvoked = false;
}

public static class AnnotationProcessor extends AbstractProcessor {
private final AbstractProcessor instance = createWrappedInstance();

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

@Override public void init(ProcessingEnvironment processingEnv) {
disableJava9SillyWarning();
AstModificationNotifierData.lombokInvoked = true;
instance.init(processingEnv);
super.init(processingEnv);
}
Expand Down

0 comments on commit 61ea0de

Please sign in to comment.