Skip to content

Commit 2a849dc

Browse files
sjaakdfiliphr
authored andcommitted
mapstruct#2145 fixing 2 step mapping methods (refactoring) (mapstruct#2146)
1 parent 74f281f commit 2a849dc

16 files changed

Lines changed: 946 additions & 294 deletions

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,24 @@ public boolean isAssignableTo(Type other) {
482482
return typeUtils.isAssignable( typeMirrorToMatch, other.typeMirror );
483483
}
484484

485+
/**
486+
* Whether this type is raw assignable to the given other type. We can't make a verdict on typevars,
487+
* they need to be resolved first.
488+
*
489+
* @param other The other type.
490+
*
491+
* @return {@code true} if and only if this type is assignable to the given other type.
492+
*/
493+
public boolean isRawAssignableTo(Type other) {
494+
if ( isTypeVar() || other.isTypeVar() ) {
495+
return true;
496+
}
497+
if ( equals( other ) ) {
498+
return true;
499+
}
500+
return typeUtils.isAssignable( typeUtils.erasure( typeMirror ), typeUtils.erasure( other.typeMirror ) );
501+
}
502+
485503
/**
486504
* getPropertyReadAccessors
487505
*

processor/src/main/java/org/mapstruct/ap/internal/model/source/Method.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,18 @@ public interface Method {
187187
default boolean isMappingTargetAssignableToReturnType() {
188188
return isUpdateMethod() && getResultType().isAssignableTo( getReturnType() );
189189
}
190+
191+
/**
192+
* @return the first source type, intended for mapping methods from single source to target
193+
*/
194+
default Type getMappingSourceType() {
195+
return getSourceParameters().get( 0 ).getType();
196+
}
197+
198+
/**
199+
* @return the short name for error messages
200+
*/
201+
default String shortName() {
202+
return getResultType().getName() + ":" + getName() + "(" + getMappingSourceType().getName() + ")";
203+
}
190204
}

processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceMethod.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public boolean inverses(SourceMethod method) {
294294
return method.getDeclaringMapper() == null
295295
&& method.isAbstract()
296296
&& getSourceParameters().size() == 1 && method.getSourceParameters().size() == 1
297-
&& first( getSourceParameters() ).getType().isAssignableTo( method.getResultType() )
297+
&& getMappingSourceType().isAssignableTo( method.getResultType() )
298298
&& getResultType().isAssignableTo( first( method.getSourceParameters() ).getType() );
299299
}
300300

@@ -326,7 +326,7 @@ public Parameter getTargetTypeParameter() {
326326
public boolean isIterableMapping() {
327327
if ( isIterableMapping == null ) {
328328
isIterableMapping = getSourceParameters().size() == 1
329-
&& first( getSourceParameters() ).getType().isIterableType()
329+
&& getMappingSourceType().isIterableType()
330330
&& getResultType().isIterableType();
331331
}
332332
return isIterableMapping;
@@ -335,17 +335,17 @@ && first( getSourceParameters() ).getType().isIterableType()
335335
public boolean isStreamMapping() {
336336
if ( isStreamMapping == null ) {
337337
isStreamMapping = getSourceParameters().size() == 1
338-
&& ( first( getSourceParameters() ).getType().isIterableType() && getResultType().isStreamType()
339-
|| first( getSourceParameters() ).getType().isStreamType() && getResultType().isIterableType()
340-
|| first( getSourceParameters() ).getType().isStreamType() && getResultType().isStreamType() );
338+
&& ( getMappingSourceType().isIterableType() && getResultType().isStreamType()
339+
|| getMappingSourceType().isStreamType() && getResultType().isIterableType()
340+
|| getMappingSourceType().isStreamType() && getResultType().isStreamType() );
341341
}
342342
return isStreamMapping;
343343
}
344344

345345
public boolean isMapMapping() {
346346
if ( isMapMapping == null ) {
347347
isMapMapping = getSourceParameters().size() == 1
348-
&& first( getSourceParameters() ).getType().isMapType()
348+
&& getMappingSourceType().isMapType()
349349
&& getResultType().isMapType();
350350
}
351351
return isMapMapping;

processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMethod.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,13 @@ public boolean matches(List<Type> sourceTypes, Type targetType) {
6767

6868
Type sourceType = first( sourceTypes );
6969

70-
if ( getReturnType().isAssignableTo( targetType.erasure() )
71-
&& sourceType.erasure().isAssignableTo( getParameter().getType() ) ) {
72-
return doTypeVarsMatch( sourceType, targetType );
73-
}
74-
if ( getReturnType().getFullyQualifiedName().equals( "java.lang.Object" )
75-
&& sourceType.erasure().isAssignableTo( getParameter().getType() ) ) {
76-
// return type could be a type parameter T
77-
return doTypeVarsMatch( sourceType, targetType );
78-
}
79-
if ( getReturnType().isAssignableTo( targetType.erasure() )
80-
&& getParameter().getType().getFullyQualifiedName().equals( "java.lang.Object" ) ) {
81-
// parameter type could be a type parameter T
82-
return doTypeVarsMatch( sourceType, targetType );
70+
Type returnType = getReturnType().resolveTypeVarToType( sourceType, getParameter().getType() );
71+
if ( returnType == null ) {
72+
return false;
8373
}
84-
return false;
74+
75+
return returnType.isAssignableTo( targetType )
76+
&& sourceType.erasure().isAssignableTo( getParameter().getType() );
8577
}
8678

8779
@Override

processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
*/
66
package org.mapstruct.ap.internal.model.source.builtin;
77

8+
import static org.mapstruct.ap.internal.util.Collections.asSet;
9+
810
import java.util.Set;
11+
912
import javax.xml.bind.JAXBElement;
1013

1114
import org.mapstruct.ap.internal.model.common.Parameter;
1215
import org.mapstruct.ap.internal.model.common.Type;
1316
import org.mapstruct.ap.internal.model.common.TypeFactory;
1417

15-
import static org.mapstruct.ap.internal.util.Collections.asSet;
16-
1718
/**
1819
* @author Sjaak Derksen
1920
*/
@@ -24,8 +25,9 @@ public class JaxbElemToValue extends BuiltInMethod {
2425
private final Set<Type> importTypes;
2526

2627
public JaxbElemToValue(TypeFactory typeFactory) {
27-
this.parameter = new Parameter( "element", typeFactory.getType( JAXBElement.class ) );
28-
this.returnType = typeFactory.getType( Object.class );
28+
Type type = typeFactory.getType( JAXBElement.class );
29+
this.parameter = new Parameter( "element", type );
30+
this.returnType = type.getTypeParameters().get( 0 );
2931
this.importTypes = asSet( parameter.getType() );
3032
}
3133

processor/src/main/java/org/mapstruct/ap/internal/model/source/selector/SelectedMethod.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.mapstruct.ap.internal.model.source.selector;
77

88
import java.util.List;
9+
import java.util.Objects;
910

1011
import org.mapstruct.ap.internal.model.common.ParameterBinding;
1112
import org.mapstruct.ap.internal.model.source.Method;
@@ -39,4 +40,21 @@ public void setParameterBindings(List<ParameterBinding> parameterBindings) {
3940
public String toString() {
4041
return method.toString();
4142
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if ( this == o ) {
47+
return true;
48+
}
49+
if ( o == null || getClass() != o.getClass() ) {
50+
return false;
51+
}
52+
SelectedMethod<?> that = (SelectedMethod<?>) o;
53+
return method.equals( that.method );
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
return Objects.hash( method );
59+
}
4260
}

processor/src/main/java/org/mapstruct/ap/internal/model/source/selector/XmlElementDeclSelector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mapp
6969
}
7070

7171
String name = xmlElementDecl.name().get();
72-
TypeMirror scope = xmlElementDecl.scope().get();
72+
TypeMirror scope = xmlElementDecl.scope().getValue();
7373

7474
boolean nameIsSetAndMatches = name != null && name.equals( xmlElementRefInfo.nameValue() );
7575
boolean scopeIsSetAndMatches =

0 commit comments

Comments
 (0)