From 5e59f3484c65f63d6f6f7d314e78564a5e7237ae Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Tue, 31 Jan 2017 22:02:29 +0100 Subject: [PATCH] #510 Adding experimental SPI for letting AST-modifying annotation processors such as Lombok tell us about future modifications --- .../ap/internal/model/common/TypeFactory.java | 16 +++++++++++----- .../mapstruct/ap/internal/util/RoundContext.java | 8 ++++---- .../ap/spi/AstModifyingAnnotationProcessor.java | 3 ++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java index d6ded05fd..b99242b23 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java @@ -70,7 +70,7 @@ public class TypeFactory { private final Elements elementUtils; private final Types typeUtils; - private RoundContext roundContext; + private final RoundContext roundContext; private final TypeMirror iterableType; private final TypeMirror collectionType; @@ -147,11 +147,12 @@ public class TypeFactory { } public Type getType(TypeMirror mirror) { + if ( mirror.getKind() == TypeKind.ERROR ) { throw new AnnotationProcessingException( "Encountered erroneous type " + mirror ); } - if ( !isCleared( mirror ) ) { + if ( !canBeProcessed( mirror ) ) { throw new TypeHierarchyErroneousException( mirror ); } @@ -573,12 +574,17 @@ public class TypeFactory { return trimmedClassName; } - private boolean isCleared(TypeMirror type) { + /** + * Whether the given type is ready to be processed or not. It can be processed if it is not of kind + * {@link TypeKind#ERROR} and all {@link AstModifyingAnnotationProcessor}s (if any) indicated that they've fully + * processed the type. + */ + private boolean canBeProcessed(TypeMirror type) { if ( type.getKind() != TypeKind.DECLARED ) { return true; } - if ( roundContext.isCleared( type ) ) { + if ( roundContext.isReadyForProcessing( type ) ) { return true; } @@ -592,7 +598,7 @@ public class TypeFactory { } } - roundContext.addClearedType( type ); + roundContext.addTypeReadyForProcessing( type ); return true; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/RoundContext.java b/processor/src/main/java/org/mapstruct/ap/internal/util/RoundContext.java index 253af596c..23182100e 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/RoundContext.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/RoundContext.java @@ -47,18 +47,18 @@ public class RoundContext { /** * Marks the given type as being ready for further processing. */ - public void addClearedType(TypeMirror type) { + public void addTypeReadyForProcessing(TypeMirror type) { clearedTypes.add( type ); } /** * Whether the given type has been found to be ready for further processing or not. This is the case if the type's - * hierarchy is complete (no super-types need to be generated by other procesors) an no processors have signaled the - * intention to amend the given type. + * hierarchy is complete (no super-types need to be generated by other processors) an no processors have signaled + * the intention to amend the given type. * * @see AstModifyingAnnotationProcessor */ - public boolean isCleared(TypeMirror type) { + public boolean isReadyForProcessing(TypeMirror type) { return clearedTypes.contains( type ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/spi/AstModifyingAnnotationProcessor.java b/processor/src/main/java/org/mapstruct/ap/spi/AstModifyingAnnotationProcessor.java index 524bf43a2..aa263ab12 100644 --- a/processor/src/main/java/org/mapstruct/ap/spi/AstModifyingAnnotationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/spi/AstModifyingAnnotationProcessor.java @@ -44,7 +44,8 @@ public interface AstModifyingAnnotationProcessor { * given type's structure after this invocation). * * @param type The type of interest - * @return {@code true} if this processor has fully processed the given type, {@code false} otherwise. + * @return {@code true} if this processor has fully processed the given type (or has no interest in processing this + * type altogether), {@code false} otherwise. */ boolean isTypeComplete(TypeMirror type); }