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