#3202 Improve line location report for invalid qualifier for SubclassMapping

This commit is contained in:
Claudio Nave 2023-03-18 13:01:49 +01:00 committed by Filip Hrisafov
parent 9adcb06c34
commit b1034e6703
4 changed files with 27 additions and 21 deletions

View File

@ -420,7 +420,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
FormattingParameters.EMPTY, FormattingParameters.EMPTY,
criteria, criteria,
rightHandSide, rightHandSide,
null, subclassMappingOptions.getMirror(),
() -> forgeSubclassMapping( () -> forgeSubclassMapping(
rightHandSide, rightHandSide,
sourceType, sourceType,

View File

@ -7,7 +7,10 @@ package org.mapstruct.ap.internal.model.source;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
@ -34,14 +37,16 @@ public class SubclassMappingOptions extends DelegatingOptions {
private final TypeMirror target; private final TypeMirror target;
private final TypeUtils typeUtils; private final TypeUtils typeUtils;
private final SelectionParameters selectionParameters; private final SelectionParameters selectionParameters;
private final SubclassMappingGem subclassMapping;
public SubclassMappingOptions(TypeMirror source, TypeMirror target, TypeUtils typeUtils, DelegatingOptions next, public SubclassMappingOptions(TypeMirror source, TypeMirror target, TypeUtils typeUtils, DelegatingOptions next,
SelectionParameters selectionParameters) { SelectionParameters selectionParameters, SubclassMappingGem subclassMapping) {
super( next ); super( next );
this.source = source; this.source = source;
this.target = target; this.target = target;
this.typeUtils = typeUtils; this.typeUtils = typeUtils;
this.selectionParameters = selectionParameters; this.selectionParameters = selectionParameters;
this.subclassMapping = subclassMapping;
} }
@Override @Override
@ -124,14 +129,18 @@ public class SubclassMappingOptions extends DelegatingOptions {
return selectionParameters; return selectionParameters;
} }
public AnnotationMirror getMirror() {
return Optional.ofNullable( subclassMapping ).map( SubclassMappingGem::mirror ).orElse( null );
}
public static void addInstances(SubclassMappingsGem gem, ExecutableElement method, public static void addInstances(SubclassMappingsGem gem, ExecutableElement method,
BeanMappingOptions beanMappingOptions, FormattingMessager messager, BeanMappingOptions beanMappingOptions, FormattingMessager messager,
TypeUtils typeUtils, Set<SubclassMappingOptions> mappings, TypeUtils typeUtils, Set<SubclassMappingOptions> mappings,
List<Parameter> sourceParameters, Type resultType, List<Parameter> sourceParameters, Type resultType,
SubclassValidator subclassValidator) { SubclassValidator subclassValidator) {
for ( SubclassMappingGem subclassMappingGem : gem.value().get() ) { for ( SubclassMappingGem subclassMapping : gem.value().get() ) {
addInstance( addInstance(
subclassMappingGem, subclassMapping,
method, method,
beanMappingOptions, beanMappingOptions,
messager, messager,
@ -175,25 +184,22 @@ public class SubclassMappingOptions extends DelegatingOptions {
targetSubclass, targetSubclass,
typeUtils, typeUtils,
beanMappingOptions, beanMappingOptions,
selectionParameters selectionParameters,
subclassMapping
) ); ) );
} }
public static List<SubclassMappingOptions> copyForInverseInheritance(Set<SubclassMappingOptions> subclassMappings, public static List<SubclassMappingOptions> copyForInverseInheritance(Set<SubclassMappingOptions> mappings,
BeanMappingOptions beanMappingOptions) { BeanMappingOptions beanMappingOptions) {
// we are not interested in keeping it unique at this point. // we are not interested in keeping it unique at this point.
List<SubclassMappingOptions> mappings = new ArrayList<>(); return mappings.stream().map( mapping -> new SubclassMappingOptions(
for ( SubclassMappingOptions subclassMapping : subclassMappings ) { mapping.target,
mappings.add( mapping.source,
new SubclassMappingOptions( mapping.typeUtils,
subclassMapping.target,
subclassMapping.source,
subclassMapping.typeUtils,
beanMappingOptions, beanMappingOptions,
subclassMapping.selectionParameters mapping.selectionParameters,
) ); mapping.subclassMapping
} ) ).collect( Collectors.toCollection( ArrayList::new ) );
return mappings;
} }
@Override @Override

View File

@ -11,6 +11,5 @@ import org.mapstruct.SubclassMapping;
@Mapper(uses = { RossiniMapper.class, VivaldiMapper.class }) @Mapper(uses = { RossiniMapper.class, VivaldiMapper.class })
public interface ErroneousSubclassQualifiedByMapper { public interface ErroneousSubclassQualifiedByMapper {
@SubclassMapping(source = Rossini.class, target = RossiniDto.class, qualifiedBy = NonExistent.class) @SubclassMapping(source = Rossini.class, target = RossiniDto.class, qualifiedBy = NonExistent.class)
@SubclassMapping(source = Vivaldi.class, target = VivaldiDto.class)
ComposerDto toDto(Composer composer); ComposerDto toDto(Composer composer);
} }

View File

@ -230,7 +230,7 @@ public class SubclassQualifierMapperTest {
diagnostics = @Diagnostic( diagnostics = @Diagnostic(
type = ErroneousSubclassQualifiedByMapper.class, type = ErroneousSubclassQualifiedByMapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR, kind = javax.tools.Diagnostic.Kind.ERROR,
line = 15, line = 13,
message = "Qualifier error. No method found annotated with: [ @NonExistent ]. " + message = "Qualifier error. No method found annotated with: [ @NonExistent ]. " +
"See https://mapstruct.org/faq/#qualifier for more info." "See https://mapstruct.org/faq/#qualifier for more info."
) )
@ -245,7 +245,8 @@ public class SubclassQualifierMapperTest {
diagnostics = @Diagnostic( diagnostics = @Diagnostic(
type = ErroneousSubclassQualifiedByNameMapper.class, type = ErroneousSubclassQualifiedByNameMapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR, kind = javax.tools.Diagnostic.Kind.ERROR,
line = 15, line = 13,
alternativeLine = 15,
message = "Qualifier error. No method found annotated with @Named#value: [ non-existent ]. " + message = "Qualifier error. No method found annotated with @Named#value: [ non-existent ]. " +
"See https://mapstruct.org/faq/#qualifier for more info." "See https://mapstruct.org/faq/#qualifier for more info."
) )