diff --git a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java index 50831a8f42..0282ccf206 100644 --- a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java @@ -14,6 +14,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.ServiceLoader; import java.util.Set; import java.util.stream.Collectors; @@ -254,33 +255,26 @@ private Set getAndResetDeferredMappers() { private Set getMappers(final Set annotations, final RoundEnvironment roundEnvironment) { - Set mapperTypes = new HashSet<>(); - - for ( TypeElement annotation : annotations ) { - //Indicates that the annotation's type isn't on the class path of the compiled - //project. Let the compiler deal with that and print an appropriate error. - if ( annotation.getKind() != ElementKind.ANNOTATION_TYPE ) { - continue; - } + return annotations.stream() + .filter( a -> a.getKind().equals( ElementKind.ANNOTATION_TYPE ) ) + .map( a -> mapperTypeElements( roundEnvironment, a ) ) + .flatMap( Set::stream ) + .collect( Collectors.toSet() ); + } - try { - Set annotatedMappers = roundEnvironment.getElementsAnnotatedWith( annotation ); - for ( Element mapperElement : annotatedMappers ) { - TypeElement mapperTypeElement = asTypeElement( mapperElement ); - - // on some JDKs, RoundEnvironment.getElementsAnnotatedWith( ... ) returns types with - // annotations unknown to the compiler, even though they are not declared Mappers - if ( mapperTypeElement != null && MapperGem.instanceOn( mapperTypeElement ) != null ) { - mapperTypes.add( mapperTypeElement ); - } - } - } - catch ( Throwable t ) { // whenever that may happen, but just to stay on the save side - handleUncaughtError( annotation, t ); - continue; - } + private Set mapperTypeElements(final RoundEnvironment roundEnvironment, final TypeElement annotation) { + try { + return roundEnvironment.getElementsAnnotatedWith( annotation ) + .stream() + .map( this::asTypeElement ) + .filter( Objects::nonNull ) + .filter( mapper -> Objects.nonNull( MapperGem.instanceOn( mapper ) ) ) + .collect( Collectors.toSet() ); + } + catch ( Throwable t ) { // whenever that may happen, but just to stay on the save side + handleUncaughtError( annotation, t ); + return new HashSet<>(); } - return mapperTypes; } private void processMapperElements(Set mapperElements, RoundContext roundContext) {