-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
#1216 Pick candidate method with most specific return type #2993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5146fd7
#1216 Pick candidate method with most specific return type
filiphr 1446ef2
Fix checkstyle
filiphr 8f13f7f
Use correct error line
filiphr befa82b
Remove test for issue being fixed in another PR
filiphr 6dcb650
Add update test
filiphr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../java/org/mapstruct/ap/internal/model/source/selector/MostSpecificResultTypeSelector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.internal.model.source.selector; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.mapstruct.ap.internal.model.common.Type; | ||
| import org.mapstruct.ap.internal.model.source.Method; | ||
|
|
||
| /** | ||
| * @author Filip Hrisafov | ||
| */ | ||
| public class MostSpecificResultTypeSelector implements MethodSelector { | ||
|
|
||
| @Override | ||
| public <T extends Method> List<SelectedMethod<T>> getMatchingMethods(Method mappingMethod, | ||
| List<SelectedMethod<T>> candidates, | ||
| List<Type> sourceTypes, Type mappingTargetType, | ||
| Type returnType, SelectionCriteria criteria) { | ||
| if ( candidates.size() < 2 || !criteria.isForMapping() || criteria.getQualifyingResultType() != null) { | ||
| return candidates; | ||
| } | ||
|
|
||
| List<SelectedMethod<T>> result = new ArrayList<>(); | ||
|
|
||
| for ( SelectedMethod<T> candidate : candidates ) { | ||
| if ( candidate.getMethod() | ||
| .getResultType() | ||
|
filiphr marked this conversation as resolved.
|
||
| .getBoxedEquivalent() | ||
| .equals( mappingTargetType.getBoxedEquivalent() ) ) { | ||
| // If the result type is the same as the target type | ||
| // then this candidate has the most specific match and should be used | ||
| result.add( candidate ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // If not most specific types were found then return the current candidates | ||
| return result.isEmpty() ? candidates : result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ap/test/selection/resulttype/ErroneousAmbiguousMostSpecificResultTypeSelectingMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.test.selection.resulttype; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| /** | ||
| * @author Filip Hrisafov | ||
| */ | ||
| @Mapper | ||
| public interface ErroneousAmbiguousMostSpecificResultTypeSelectingMapper { | ||
|
|
||
| @Mapping( target = "apple", source = "fruit") | ||
| AppleFamily map(FruitFamily fruitFamily); | ||
|
|
||
| default GoldenDelicious toGolden(IsFruit fruit) { | ||
| return fruit != null ? new GoldenDelicious( fruit.getType() ) : null; | ||
| } | ||
|
|
||
| default Apple toApple1(IsFruit fruit) { | ||
|
sjaakd marked this conversation as resolved.
|
||
| return fruit != null ? new Apple( fruit.getType() ) : null; | ||
| } | ||
|
|
||
| default Apple toApple2(IsFruit fruit) { | ||
| return fruit != null ? new Apple( fruit.getType() ) : null; | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
processor/src/test/java/org/mapstruct/ap/test/selection/resulttype/FruitFamily.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.test.selection.resulttype; | ||
|
|
||
| /** | ||
| * @author Filip Hrisafov | ||
| */ | ||
| public class FruitFamily { | ||
|
|
||
| private IsFruit fruit; | ||
|
|
||
| public IsFruit getFruit() { | ||
| return fruit; | ||
| } | ||
|
|
||
| public void setFruit(IsFruit fruit) { | ||
| this.fruit = fruit; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...ava/org/mapstruct/ap/test/selection/resulttype/MostSpecificResultTypeSelectingMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.test.selection.resulttype; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import org.mapstruct.factory.Mappers; | ||
|
|
||
| /** | ||
| * @author Filip Hrisafov | ||
| */ | ||
| @Mapper | ||
| public interface MostSpecificResultTypeSelectingMapper { | ||
|
|
||
| MostSpecificResultTypeSelectingMapper INSTANCE = Mappers.getMapper( MostSpecificResultTypeSelectingMapper.class ); | ||
|
|
||
| @Mapping( target = "apple", source = "fruit") | ||
| AppleFamily map(FruitFamily fruitFamily); | ||
|
|
||
| default GoldenDelicious toGolden(IsFruit fruit) { | ||
| return fruit != null ? new GoldenDelicious( fruit.getType() ) : null; | ||
| } | ||
|
|
||
| default Apple toApple(IsFruit fruit) { | ||
| return fruit != null ? new Apple( fruit.getType() ) : null; | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
...g/mapstruct/ap/test/selection/resulttype/MostSpecificResultTypeSelectingUpdateMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct.ap.test.selection.resulttype; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import org.mapstruct.MappingTarget; | ||
| import org.mapstruct.ObjectFactory; | ||
| import org.mapstruct.factory.Mappers; | ||
|
|
||
| /** | ||
| * @author Filip Hrisafov | ||
| */ | ||
| @Mapper | ||
| public interface MostSpecificResultTypeSelectingUpdateMapper { | ||
|
|
||
| MostSpecificResultTypeSelectingUpdateMapper INSTANCE = Mappers.getMapper( | ||
| MostSpecificResultTypeSelectingUpdateMapper.class ); | ||
|
|
||
| @Mapping(target = "apple", source = "fruit") | ||
| @Mapping(target = "goldenApple", source = "fruit") | ||
| void update(@MappingTarget Target target, FruitFamily fruitFamily); | ||
|
|
||
| default void updateGolden(@MappingTarget GoldenDelicious target, IsFruit fruit) { | ||
| target.setType( "golden updated " + fruit.getType() ); | ||
| } | ||
|
|
||
| default void updateApple(@MappingTarget Apple target, IsFruit fruit) { | ||
| target.setType( "apple updated " + fruit.getType() ); | ||
| } | ||
|
|
||
| @ObjectFactory | ||
| default GoldenDelicious createGolden() { | ||
| return new GoldenDelicious( "from_object_factory" ); | ||
| } | ||
|
|
||
| class Target { | ||
| protected Apple apple; | ||
| protected GoldenDelicious goldenApple; | ||
|
|
||
| public Target(Apple apple, GoldenDelicious goldenApple) { | ||
| this.apple = apple; | ||
| this.goldenApple = goldenApple; | ||
| } | ||
|
|
||
| public Apple getApple() { | ||
| return apple; | ||
| } | ||
|
|
||
| public void setApple(Apple apple) { | ||
| this.apple = apple; | ||
| } | ||
|
|
||
| public GoldenDelicious getGoldenApple() { | ||
| return goldenApple; | ||
| } | ||
|
|
||
| public void setGoldenApple(GoldenDelicious goldenApple) { | ||
| this.goldenApple = goldenApple; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.