mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#775 Fix mapping method type matching for lower-bounded target type.
This commit is contained in:
parent
4c1d7a7a19
commit
b7450da585
@ -224,17 +224,32 @@ public class MethodMatcher {
|
||||
}
|
||||
|
||||
private enum Assignability {
|
||||
VISITED_ASSIGNABLE_FROM, VISITED_ASSIGNABLE_TO
|
||||
VISITED_ASSIGNABLE_FROM, VISITED_ASSIGNABLE_TO;
|
||||
|
||||
Assignability invert() {
|
||||
return this == VISITED_ASSIGNABLE_FROM
|
||||
? VISITED_ASSIGNABLE_TO
|
||||
: VISITED_ASSIGNABLE_FROM;
|
||||
}
|
||||
}
|
||||
|
||||
private class TypeMatcher extends SimpleTypeVisitor6<Boolean, TypeMirror> {
|
||||
private final Assignability assignability;
|
||||
private final Map<TypeVariable, TypeMirror> genericTypesMap;
|
||||
private final TypeMatcher inverse;
|
||||
|
||||
TypeMatcher(Assignability assignability, Map<TypeVariable, TypeMirror> genericTypesMap) {
|
||||
super( Boolean.FALSE ); // default value
|
||||
this.assignability = assignability;
|
||||
this.genericTypesMap = genericTypesMap;
|
||||
this.inverse = new TypeMatcher( this, genericTypesMap );
|
||||
}
|
||||
|
||||
TypeMatcher(TypeMatcher inverse, Map<TypeVariable, TypeMirror> genericTypesMap) {
|
||||
super( Boolean.FALSE ); // default value
|
||||
this.assignability = inverse.assignability.invert();
|
||||
this.genericTypesMap = genericTypesMap;
|
||||
this.inverse = inverse;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -262,7 +277,7 @@ public class MethodMatcher {
|
||||
if ( assignabilityMatches( t, t1 )
|
||||
&& t.getTypeArguments().size() == t1.getTypeArguments().size() ) {
|
||||
for ( int i = 0; i < t.getTypeArguments().size(); i++ ) {
|
||||
if ( !t.getTypeArguments().get( i ).accept( this, t1.getTypeArguments().get( i ) ) ) {
|
||||
if ( !visit( t.getTypeArguments().get( i ), t1.getTypeArguments().get( i ) ) ) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
@ -272,6 +287,9 @@ public class MethodMatcher {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
else if ( p.getKind() == TypeKind.WILDCARD ) {
|
||||
return inverse.visit( p, t ); // inverse, as we switch the params
|
||||
}
|
||||
else {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
@ -326,7 +344,7 @@ public class MethodMatcher {
|
||||
case DECLARED:
|
||||
// for example method: String method(? extends String)
|
||||
// isSubType checks range [subtype, type], e.g. isSubtype [Object, String]==true
|
||||
return typeUtils.isSubtype( p, extendsBound );
|
||||
return visit( extendsBound, p );
|
||||
|
||||
case TYPEVAR:
|
||||
// for example method: <T extends String & Serializable> T method(? extends T)
|
||||
@ -401,7 +419,7 @@ public class MethodMatcher {
|
||||
* @return true if within bounds
|
||||
*/
|
||||
private boolean isWithinBounds(TypeMirror t, TypeParameterElement tpe) {
|
||||
List<? extends TypeMirror> bounds = tpe.getBounds();
|
||||
List<? extends TypeMirror> bounds = tpe != null ? tpe.getBounds() : null;
|
||||
if ( t != null && bounds != null ) {
|
||||
for ( TypeMirror bound : bounds ) {
|
||||
if ( !( bound.getKind() == TypeKind.DECLARED && typeUtils.isSubtype( t, bound ) ) ) {
|
||||
|
@ -32,6 +32,8 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
* Verifies:
|
||||
* <ul>
|
||||
* <li>For target properties of type {@link Iterable}, a forged method can be created.
|
||||
* <li>For target properties of type {@code Iterable<? extends Integer>}, a custom mapping method that returns
|
||||
* {@code List<Integer>} is chosen as property mapping method.
|
||||
* </ul>
|
||||
*
|
||||
* @author Andreas Gudian
|
||||
@ -40,6 +42,7 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
@IssueKey("775")
|
||||
@WithClasses({
|
||||
MapperWithForgedIterableMapping.class,
|
||||
MapperWithCustomListMapping.class,
|
||||
ListContainer.class,
|
||||
IterableContainer.class
|
||||
})
|
||||
@ -54,4 +57,14 @@ public class IterableWithBoundedElementTypeTest {
|
||||
|
||||
assertThat( result.getValues() ).contains( Integer.valueOf( 42 ), Integer.valueOf( 47 ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesListIntegerMethodForIterableLowerBoundInteger() {
|
||||
ListContainer source = new ListContainer();
|
||||
|
||||
source.setValues( Arrays.asList( "42", "47" ) );
|
||||
IterableContainer result = MapperWithCustomListMapping.INSTANCE.toContainerWithIterable( source );
|
||||
|
||||
assertThat( result.getValues() ).contains( Integer.valueOf( 66 ), Integer.valueOf( 71 ) );
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._775;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Andreas Gudian
|
||||
*/
|
||||
@Mapper
|
||||
public abstract class MapperWithForgedIterableMapping {
|
||||
public static final MapperWithForgedIterableMapping INSTANCE =
|
||||
Mappers.getMapper( MapperWithForgedIterableMapping.class );
|
||||
|
||||
public abstract IterableContainer toContainerWithIterable(ListContainer source);
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user