diff --git a/core-jdk8/src/main/java/org/mapstruct/Mapping.java b/core-jdk8/src/main/java/org/mapstruct/Mapping.java index 0794e4520..2e0ab0e52 100644 --- a/core-jdk8/src/main/java/org/mapstruct/Mapping.java +++ b/core-jdk8/src/main/java/org/mapstruct/Mapping.java @@ -136,4 +136,11 @@ public @interface Mapping { * @return the qualifiers */ Class[] qualifiedBy() default { }; + + /** + * Specifies the result type of the mapping method to be used in case multiple mapping methods qualify. + * + * @return the resultType to select + */ + Class resultType() default void.class; } diff --git a/core/src/main/java/org/mapstruct/Mapping.java b/core/src/main/java/org/mapstruct/Mapping.java index c4226d406..4415bb059 100644 --- a/core/src/main/java/org/mapstruct/Mapping.java +++ b/core/src/main/java/org/mapstruct/Mapping.java @@ -134,4 +134,11 @@ public @interface Mapping { * @return the qualifiers */ Class[] qualifiedBy() default { }; + + /** + * Specifies the result type of the mapping method to be used in case multiple mapping methods qualify. + * + * @return the resultType to select + */ + Class resultType() default void.class; } diff --git a/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java index d36902da7..ed46f294e 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java @@ -170,6 +170,7 @@ public class BeanMappingMethod extends MappingMethod { .targetPropertyName( mapping.getTargetName() ) .sourceReference( sourceRef ) .qualifiers( mapping.getQualifiers() ) + .resultType( mapping.getResultType() ) .dateFormat( mapping.getDateFormat() ) .build(); handledTargets.add( mapping.getTargetName() ); @@ -191,6 +192,7 @@ public class BeanMappingMethod extends MappingMethod { .targetAccessor( targetProperty ) .dateFormat( mapping.getDateFormat() ) .qualifiers( mapping.getQualifiers() ) + .resultType( mapping.getResultType() ) .build(); handledTargets.add( mapping.getTargetName() ); } @@ -277,6 +279,7 @@ public class BeanMappingMethod extends MappingMethod { .targetPropertyName( targetProperty.getKey() ) .sourceReference( sourceRef ) .qualifiers( mapping != null ? mapping.getQualifiers() : null ) + .resultType( mapping != null ? mapping.getResultType() : null ) .dateFormat( mapping != null ? mapping.getDateFormat() : null ) .build(); @@ -339,6 +342,7 @@ public class BeanMappingMethod extends MappingMethod { .targetPropertyName( targetProperty.getKey() ) .sourceReference( sourceRef ) .qualifiers( mapping != null ? mapping.getQualifiers() : null ) + .resultType( mapping != null ? mapping.getResultType() : null ) .dateFormat( mapping != null ? mapping.getDateFormat() : null ) .build(); diff --git a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java index 08bbb1d21..7022af4ea 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java @@ -100,6 +100,7 @@ public class IterableMappingMethod extends MappingMethod { null, // there is no targetPropertyName dateFormat, qualifiers, + null, // resulttype does not seem to make sense loopVariableName ); diff --git a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java index 12dc8650d..4a1bf9ab5 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java @@ -102,6 +102,7 @@ public class MapMappingMethod extends MappingMethod { null, // there is no targetPropertyName keyDateFormat, keyQualifiers, + null, // resulttype does not seem to make sense "entry.getKey()" ); @@ -125,6 +126,7 @@ public class MapMappingMethod extends MappingMethod { null, // there is no targetPropertyName valueDateFormat, valueQualifiers, + null, // resulttype does not seem to make sense "entry.getValue()" ); diff --git a/processor/src/main/java/org/mapstruct/ap/model/MappingBuilderContext.java b/processor/src/main/java/org/mapstruct/ap/model/MappingBuilderContext.java index 40b6b80ee..c3e4ab2d3 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MappingBuilderContext.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MappingBuilderContext.java @@ -80,6 +80,7 @@ public class MappingBuilderContext { * @param targetPropertyName name of the target property * @param dateFormat used for formatting dates in build in methods that need context information * @param qualifiers used for further select the appropriate mapping method based on class and name + * @param resultType used for further select the appropriate mapping method based on class and name * @param sourceReference call to source type as string * * @return an assignment to a method parameter, which can either be: @@ -92,7 +93,7 @@ public class MappingBuilderContext { */ Assignment getTargetAssignment(Method mappingMethod, String mappedElement, Type sourceType, Type targetType, String targetPropertyName, String dateFormat, List qualifiers, - String sourceReference); + TypeMirror resultType, String sourceReference); /** * returns a no arg factory method diff --git a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java index a18c28306..4afa07012 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java @@ -70,6 +70,7 @@ public class PropertyMapping extends ModelElement { private String targetPropertyName; private String dateFormat; private List qualifiers; + private TypeMirror resultType; private SourceReference sourceReference; public PropertyMappingBuilder mappingContext(MappingBuilderContext mappingContext) { @@ -102,6 +103,11 @@ public class PropertyMapping extends ModelElement { return this; } + public PropertyMappingBuilder resultType(TypeMirror resultType) { + this.resultType = resultType; + return this; + } + public PropertyMappingBuilder dateFormat(String dateFormat) { this.dateFormat = dateFormat; return this; @@ -142,6 +148,7 @@ public class PropertyMapping extends ModelElement { targetPropertyName, dateFormat, qualifiers, + resultType, sourceRefStr ); @@ -450,6 +457,7 @@ public class PropertyMapping extends ModelElement { private ExecutableElement targetAccessor; private String dateFormat; private List qualifiers; + private TypeMirror resultType; public ConstantMappingBuilder mappingContext(MappingBuilderContext mappingContext) { this.ctx = mappingContext; @@ -481,6 +489,11 @@ public class PropertyMapping extends ModelElement { return this; } + public ConstantMappingBuilder resultType(TypeMirror resultType) { + this.resultType = resultType; + return this; + } + public PropertyMapping build() { // source @@ -506,6 +519,7 @@ public class PropertyMapping extends ModelElement { targetPropertyName, dateFormat, qualifiers, + resultType, constantExpression ); diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java b/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java index 96dc4b898..37e20bbb4 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java @@ -55,6 +55,7 @@ public class Mapping { private final String targetName; private final String dateFormat; private final List qualifiers; + private final TypeMirror resultType; private final boolean isIgnored; private final AnnotationMirror mirror; @@ -135,6 +136,9 @@ public class Mapping { String expression = getExpression( mappingPrism, element, messager ); String dateFormat = mappingPrism.dateFormat().isEmpty() ? null : mappingPrism.dateFormat(); + boolean resultTypeIsDefined = !TypeKind.VOID.equals( mappingPrism.resultType().getKind() ); + TypeMirror resultType = resultTypeIsDefined ? mappingPrism.resultType() : null; + return new Mapping( source, constant, @@ -145,14 +149,17 @@ public class Mapping { mappingPrism.ignore(), mappingPrism.mirror, mappingPrism.values.source(), - mappingPrism.values.target() + mappingPrism.values.target(), + resultType ); } + @SuppressWarnings( "checkstyle:parameternumber" ) private Mapping(String sourceName, String constant, String javaExpression, String targetName, String dateFormat, List qualifiers, boolean isIgnored, AnnotationMirror mirror, - AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue) { + AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue, + TypeMirror resultType ) { this.sourceName = sourceName; this.constant = constant; this.javaExpression = javaExpression; @@ -163,6 +170,7 @@ public class Mapping { this.mirror = mirror; this.sourceAnnotationValue = sourceAnnotationValue; this.targetAnnotationValue = targetAnnotationValue; + this.resultType = resultType; } private static String getExpression(MappingPrism mappingPrism, ExecutableElement element, Messager messager) { @@ -257,6 +265,10 @@ public class Mapping { return targetReference; } + public TypeMirror getResultType() { + return resultType; + } + private boolean hasPropertyInReverseMethod(String name, SourceMethod method) { CollectionMappingStrategyPrism cms = method.getConfig().getCollectionMappingStrategy(); return method.getResultType().getTargetAccessors( cms ).containsKey( name ); @@ -303,7 +315,8 @@ public class Mapping { isIgnored, mirror, sourceAnnotationValue, - targetAnnotationValue + targetAnnotationValue, + null ); reverse.init( method, messager, typeFactory ); diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java index ffbf2f285..f2bb2099d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java @@ -170,7 +170,7 @@ public class SourceMethod implements Method { } } - //CHECKSTYLE:OFF + @SuppressWarnings( "checkstyle:parameternumber" ) private SourceMethod( Type declaringMapper, ExecutableElement executable, List parameters, Type returnType, List exceptionTypes, Map> mappings, IterableMapping iterableMapping, MapMapping mapMapping, Types typeUtils, @@ -192,7 +192,6 @@ public class SourceMethod implements Method { this.messager = messager; this.config = config; } - //CHECKSTYLE:ON private Parameter determineTargetParameter(Iterable parameters) { for ( Parameter parameter : parameters ) { diff --git a/processor/src/main/java/org/mapstruct/ap/processor/creation/MappingResolverImpl.java b/processor/src/main/java/org/mapstruct/ap/processor/creation/MappingResolverImpl.java index 70ec8bc96..88cdb2e0b 100755 --- a/processor/src/main/java/org/mapstruct/ap/processor/creation/MappingResolverImpl.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/creation/MappingResolverImpl.java @@ -124,9 +124,10 @@ public class MappingResolverImpl implements MappingResolver { String targetPropertyName, String dateFormat, List qualifiers, + TypeMirror resultType, String sourceReference) { - SelectionCriteria criteria = new SelectionCriteria(qualifiers, targetPropertyName, null ); + SelectionCriteria criteria = new SelectionCriteria(qualifiers, targetPropertyName, resultType ); ResolvingAttempt attempt = new ResolvingAttempt( sourceModel, diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Apple.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Apple.java similarity index 94% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Apple.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Apple.java index dac4a30e6..7fea06efb 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Apple.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Apple.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; /** * diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/AppleDto.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleDto.java similarity index 94% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/AppleDto.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleDto.java index 3d97397b8..e123a873d 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/AppleDto.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleDto.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; /** * diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFactory.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFactory.java new file mode 100644 index 000000000..116306a9d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFactory.java @@ -0,0 +1,34 @@ +/** + * Copyright 2012-2015 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.selection.resulttype; + +/** + * + * @author Sjaak Derksen + */ +public class AppleFactory { + + public Apple createApple() { + return new Apple( "apple" ); + } + + public GoldenDelicious createGoldenDelicious() { + return new GoldenDelicious( "GoldenDelicious" ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFamily.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFamily.java new file mode 100644 index 000000000..8079731f1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFamily.java @@ -0,0 +1,37 @@ +/** + * Copyright 2012-2015 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.selection.resulttype; + +/** + * + * @author Sjaak Derksen + */ +public class AppleFamily { + + private Apple apple; + + public Apple getApple() { + return apple; + } + + public void setApple(Apple apple) { + this.apple = apple; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFamilyDto.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFamilyDto.java new file mode 100644 index 000000000..284bee0d9 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/AppleFamilyDto.java @@ -0,0 +1,37 @@ +/** + * Copyright 2012-2015 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.selection.resulttype; + +/** + * + * @author Sjaak Derksen + */ +public class AppleFamilyDto { + + private AppleDto apple; + + public AppleDto getApple() { + return apple; + } + + public void setApple(AppleDto apple) { + this.apple = apple; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Banana.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Banana.java similarity index 94% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Banana.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Banana.java index 1c15f5efa..b66aabc80 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Banana.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Banana.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; /** * diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/BananaDto.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/BananaDto.java similarity index 94% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/BananaDto.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/BananaDto.java index dff5ea65d..a3b373831 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/BananaDto.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/BananaDto.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; /** * diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/ConflictingFruitFactory.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/ConflictingFruitFactory.java similarity index 95% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/ConflictingFruitFactory.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/ConflictingFruitFactory.java index 9161bc36a..8224b3936 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/ConflictingFruitFactory.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/ConflictingFruitFactory.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; /** * @@ -31,4 +31,5 @@ public class ConflictingFruitFactory { public Banana createBanana() { return new Banana( "banana" ); } + } diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/ErroneousFruitMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/ErroneousFruitMapper.java similarity index 95% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/ErroneousFruitMapper.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/ErroneousFruitMapper.java index ebd741fba..f5e8a363f 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/ErroneousFruitMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/ErroneousFruitMapper.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; import org.mapstruct.Mapper; import org.mapstruct.Mapping; diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Fruit.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Fruit.java similarity index 95% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Fruit.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Fruit.java index e64988900..2af088ba7 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/Fruit.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/Fruit.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; /** * diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/FruitDto.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/FruitDto.java similarity index 95% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/FruitDto.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/FruitDto.java index 408b126e5..176dd0fd7 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/FruitDto.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/FruitDto.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; /** * diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/FruitFamilyMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/FruitFamilyMapper.java new file mode 100644 index 000000000..dce320cf5 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/FruitFamilyMapper.java @@ -0,0 +1,43 @@ +/** + * Copyright 2012-2015 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.selection.resulttype; + +import org.mapstruct.BeanMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +/** + * + * @author Sjaak Derksen + */ +@Mapper(uses = AppleFactory.class) +public interface FruitFamilyMapper { + + FruitFamilyMapper INSTANCE = Mappers.getMapper( FruitFamilyMapper.class ); + + @Mapping(target = "apple", resultType = GoldenDelicious.class) + AppleFamily map(AppleFamilyDto source); + + GoldenDelicious mapToGoldenDelicious(AppleDto source); + + @BeanMapping(resultType = Apple.class) + Apple mapToApple(AppleDto source); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/GoldenDelicious.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/GoldenDelicious.java new file mode 100644 index 000000000..dd508912a --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/GoldenDelicious.java @@ -0,0 +1,31 @@ +/** + * Copyright 2012-2015 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.selection.resulttype; + +/** + * + * @author Sjaak Derksen + */ +public class GoldenDelicious extends Apple { + + public GoldenDelicious(String type) { + super( type ); + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/GoldenDeliciousDto.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/GoldenDeliciousDto.java new file mode 100644 index 000000000..66f26da80 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/GoldenDeliciousDto.java @@ -0,0 +1,31 @@ +/** + * Copyright 2012-2015 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.selection.resulttype; + +/** + * + * @author Sjaak Derksen + */ +public class GoldenDeliciousDto extends AppleDto { + + public GoldenDeliciousDto(String type) { + super( type ); + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/InheritanceSelectionTest.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/InheritanceSelectionTest.java similarity index 76% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/InheritanceSelectionTest.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/InheritanceSelectionTest.java index 8dd9b6c35..5b3435e15 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/InheritanceSelectionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/InheritanceSelectionTest.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; import javax.tools.Diagnostic.Kind; import static org.fest.assertions.Assertions.assertThat; @@ -71,4 +71,28 @@ public class InheritanceSelectionTest { } + @Test + @IssueKey("433") + @WithClasses( { + FruitFamilyMapper.class, + GoldenDeliciousDto.class, + GoldenDelicious.class, + AppleFamily.class, + AppleFamilyDto.class, + AppleFactory.class, + Banana.class + } ) + public void testShouldSelectResultTypeInCaseOfAmbiguity() { + + AppleFamilyDto appleFamilyDto = new AppleFamilyDto(); + appleFamilyDto.setApple( new AppleDto("AppleDto") ); + + AppleFamily result = FruitFamilyMapper.INSTANCE.map( appleFamilyDto ); + assertThat( result ).isNotNull(); + assertThat( result.getApple() ).isNotNull(); + assertThat( result.getApple() ).isInstanceOf( GoldenDelicious.class ); + assertThat( result.getApple().getType() ).isEqualTo( "AppleDto" ); + + } + } diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/TargetTypeSelectingFruitMapper.java b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/TargetTypeSelectingFruitMapper.java similarity index 96% rename from processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/TargetTypeSelectingFruitMapper.java rename to processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/TargetTypeSelectingFruitMapper.java index e4148e112..ae00f6fa0 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/inheritance/TargetTypeSelectingFruitMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/TargetTypeSelectingFruitMapper.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.selection.inheritance; +package org.mapstruct.ap.test.selection.resulttype; import org.mapstruct.BeanMapping; import org.mapstruct.Mapper;