mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#34 Adding concerned method to reported diagnostic in case a mapping is ambiguous
This commit is contained in:
parent
785ccece90
commit
cce558827d
@ -26,6 +26,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.processing.Messager;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
@ -242,7 +243,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
messager.printMessage(
|
||||
Kind.ERROR,
|
||||
String.format(
|
||||
"Ambigious factory methods: \"%s\" conflicts with \"%s\".",
|
||||
"Ambiguous factory methods: \"%s\" conflicts with \"%s\".",
|
||||
result,
|
||||
method
|
||||
),
|
||||
@ -557,6 +558,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
}
|
||||
|
||||
MethodReference propertyMappingMethod = getMappingMethodReference(
|
||||
method,
|
||||
"property '" + Executables.getPropertyName( sourceAccessor ) + "'",
|
||||
mapperReferences,
|
||||
methods,
|
||||
sourceType,
|
||||
@ -605,7 +608,14 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
);
|
||||
|
||||
MethodReference elementMappingMethod =
|
||||
getMappingMethodReference( mapperReferences, methods, sourceElementType, targetElementType );
|
||||
getMappingMethodReference(
|
||||
method,
|
||||
"collection element",
|
||||
mapperReferences,
|
||||
methods,
|
||||
sourceElementType,
|
||||
targetElementType
|
||||
);
|
||||
|
||||
if ( !sourceElementType.isAssignableTo( targetElementType ) && conversion == null &&
|
||||
elementMappingMethod == null ) {
|
||||
@ -651,12 +661,16 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
);
|
||||
|
||||
MethodReference keyMappingMethod = getMappingMethodReference(
|
||||
method,
|
||||
"map key",
|
||||
mapperReferences,
|
||||
methods,
|
||||
sourceKeyType,
|
||||
targetKeyType
|
||||
);
|
||||
MethodReference valueMappingMethod = getMappingMethodReference(
|
||||
method,
|
||||
"map value",
|
||||
mapperReferences,
|
||||
methods,
|
||||
sourceValueType,
|
||||
@ -708,7 +722,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
);
|
||||
}
|
||||
|
||||
private MethodReference getMappingMethodReference(List<MapperReference> mapperReferences,
|
||||
private MethodReference getMappingMethodReference(Method mappingMethod, String mappedElement,
|
||||
List<MapperReference> mapperReferences,
|
||||
Iterable<Method> methods, Type parameterType,
|
||||
Type returnType) {
|
||||
List<Method> candidatesWithMathingTargetType = new ArrayList<Method>();
|
||||
@ -750,11 +765,12 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
messager.printMessage(
|
||||
Kind.ERROR,
|
||||
String.format(
|
||||
"Ambiguous mapping methods found for mapping from %s to %s: %s.",
|
||||
"Ambiguous mapping methods found for mapping " + mappedElement + " from %s to %s: %s.",
|
||||
parameterType,
|
||||
returnType,
|
||||
candidatesWithBestMatchingSourceType
|
||||
)
|
||||
Strings.join( candidatesWithBestMatchingSourceType, ", " )
|
||||
),
|
||||
mappingMethod.getExecutable()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,6 @@ import org.testng.annotations.Test;
|
||||
})
|
||||
public class FactoryTest extends MapperTestBase {
|
||||
|
||||
|
||||
@Test
|
||||
@IssueKey("81")
|
||||
@ExpectedCompilationOutcome(
|
||||
@ -46,7 +45,7 @@ public class FactoryTest extends MapperTestBase {
|
||||
@Diagnostic(type = BarFactory.class,
|
||||
kind = javax.tools.Diagnostic.Kind.ERROR,
|
||||
line = 29,
|
||||
messageRegExp = "^Ambigious factory methods: \"org\\.mapstruct\\.ap\\.test\\.erroneous\\."
|
||||
messageRegExp = "^Ambiguous factory methods: \"org\\.mapstruct\\.ap\\.test\\.erroneous\\."
|
||||
+ "ambiguousfactorymethod\\.Bar createBar\\(\\)\" conflicts with "
|
||||
+ "\"org\\.mapstruct\\.ap\\.test\\.erroneous\\.ambiguousfactorymethod\\.Bar "
|
||||
+ "org\\.mapstruct\\.ap\\.test\\.erroneous\\.ambiguousfactorymethod"
|
||||
|
@ -18,10 +18,7 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.inheritance.complex;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
@ -32,6 +29,8 @@ import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
|
||||
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for propagation of attributes inherited from super types.
|
||||
*
|
||||
@ -78,9 +77,12 @@ public class ComplexInheritanceTest extends MapperTestBase {
|
||||
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
|
||||
diagnostics = @Diagnostic(
|
||||
kind = Kind.ERROR,
|
||||
messageRegExp = "Ambiguous mapping methods found for mapping from .*SourceExt to .*Reference: \\["
|
||||
type = ErroneousSourceCompositeTargetCompositeMapper.class,
|
||||
line = 32,
|
||||
messageRegExp =
|
||||
"Ambiguous mapping methods found for mapping property 'prop1' from .*SourceExt to .*Reference: "
|
||||
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*SourceBase source\\), "
|
||||
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*AdditionalFooSource source\\)\\]"))
|
||||
+ ".*Reference .*AdditionalMappingHelper\\.asReference\\(.*AdditionalFooSource source\\)"))
|
||||
public void ambiguousMappingMethodsReportError() {
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user