mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#729 Some clean-up
This commit is contained in:
parent
79ececf83e
commit
0a41360acf
@ -199,7 +199,8 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// then direct assignable
|
// then direct assignable
|
||||||
if ( sourceType.isAssignableTo( targetType ) || isPropertyMappable( sourceType, targetType ) ) {
|
if ( sourceType.isAssignableTo( targetType ) ||
|
||||||
|
isAssignableThroughCollectionCopyConstructor( sourceType, targetType ) ) {
|
||||||
Assignment simpleAssignment = AssignmentFactory.createDirect( sourceReference );
|
Assignment simpleAssignment = AssignmentFactory.createDirect( sourceReference );
|
||||||
return simpleAssignment;
|
return simpleAssignment;
|
||||||
}
|
}
|
||||||
@ -503,32 +504,22 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether the specified property can be mapped from source to target or not. A mapping if possible if one of
|
* Whether the given source and target type are both a collection type or both a map type and the source value
|
||||||
* the following conditions is true:
|
* can be propagated via a copy constructor.
|
||||||
* <ul>
|
|
||||||
* <li>the source type is assignable to the target type</li>
|
|
||||||
* <li>a mapping method exists</li>
|
|
||||||
* <li>a built-in conversion exists</li>
|
|
||||||
* <li>the property is of a collection or map type and the constructor of the target type (either itself or its
|
|
||||||
* implementation type) accepts the source type.</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* @return {@code true} if the specified property can be mapped, {@code false} otherwise.
|
|
||||||
*/
|
*/
|
||||||
private boolean isPropertyMappable(Type sourceType, Type targetType) {
|
private boolean isAssignableThroughCollectionCopyConstructor(Type sourceType, Type targetType) {
|
||||||
boolean collectionOrMapTargetTypeHasCompatibleConstructor = false;
|
boolean bothCollectionOrMap = false;
|
||||||
|
|
||||||
if ( sourceType.isCollectionType() || targetType.isMapType() ) {
|
if ( ( sourceType.isCollectionType() && targetType.isCollectionType() ) ||
|
||||||
collectionOrMapTargetTypeHasCompatibleConstructor = hasCompatibleCopyConstructor(
|
( sourceType.isMapType() && targetType.isMapType() ) ) {
|
||||||
sourceType,
|
bothCollectionOrMap = true;
|
||||||
targetType.getImplementationType() != null
|
|
||||||
? targetType.getImplementationType() : targetType
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ( ( targetType.isCollectionType() || targetType.isMapType() )
|
if ( bothCollectionOrMap ) {
|
||||||
&& collectionOrMapTargetTypeHasCompatibleConstructor ) ) {
|
return hasCompatibleCopyConstructor(
|
||||||
return true;
|
sourceType,
|
||||||
|
targetType.getImplementationType() != null ? targetType.getImplementationType() : targetType
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -65,7 +65,9 @@ public class ErroneousCollectionMappingTest {
|
|||||||
@Diagnostic(type = ErroneousCollectionToPrimitivePropertyMapper.class,
|
@Diagnostic(type = ErroneousCollectionToPrimitivePropertyMapper.class,
|
||||||
kind = Kind.ERROR,
|
kind = Kind.ERROR,
|
||||||
line = 26,
|
line = 26,
|
||||||
messageRegExp = "Can't map property \"java.util.List<java.lang.String> strings\" to \"int strings\". Consider to declare/implement a mapping method: \"int map\\(java.util.List<java.lang.String> value\\)\"")
|
messageRegExp = "Can't map property \"java.util.List<java.lang.String> strings\" to \"int strings\". "
|
||||||
|
+ "Consider to declare/implement a mapping method: \"int map\\(java.util.List<java.lang.String>"
|
||||||
|
+ " value\\)\"")
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public void shouldFailToGenerateImplementationBetweenCollectionAndPrimitive() {
|
public void shouldFailToGenerateImplementationBetweenCollectionAndPrimitive() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user