-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for conditions on source parameters + fix incorrect use of source parameter in presence check method #3543
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
filiphr
merged 7 commits into
mapstruct:main
from
filiphr:2610-source-parameter-presenc-check
Apr 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2630ea5
Add support for conditions on source parameters + fix incorrect use o…
filiphr 50f089b
Use new SourceCondition
filiphr 3bb3735
Use ConditionStrategy
filiphr b98ac32
Cleanups
filiphr ebf7362
Polishing
filiphr 64862c3
Apply review feedback
filiphr 4834ca5
Remove unused import
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
Use ConditionStrategy
- Loading branch information
commit 3bb37355bca05c44d1208f6ce1ad65b88d75c1ca
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
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,23 @@ | ||
| /* | ||
| * Copyright MapStruct Authors. | ||
| * | ||
| * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
| package org.mapstruct; | ||
|
|
||
| /** | ||
| * Strategy for defining what to what a condition (check) method is applied to | ||
| * | ||
| * @author Filip Hrisafov | ||
| * @since 1.6 | ||
| */ | ||
| public enum ConditionStrategy { | ||
| /** | ||
| * The condition method should be applied whether a property should be mapped. | ||
| */ | ||
| PROPERTIES, | ||
| /** | ||
| * The condition method should be applied to check if a source parameters should be mapped. | ||
| */ | ||
| SOURCE_PARAMETERS, | ||
| } |
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
15 changes: 15 additions & 0 deletions
15
processor/src/main/java/org/mapstruct/ap/internal/gem/ConditionStrategyGem.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,15 @@ | ||
| /* | ||
| * 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.gem; | ||
|
|
||
| /** | ||
| * @author Filip Hrisafov | ||
| */ | ||
| public enum ConditionStrategyGem { | ||
|
|
||
| PROPERTIES, | ||
| SOURCE_PARAMETERS | ||
| } |
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
processor/src/main/java/org/mapstruct/ap/internal/model/source/ConditionMethodOptions.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; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
|
|
||
| import org.mapstruct.ap.internal.gem.ConditionStrategyGem; | ||
|
|
||
| /** | ||
| * Encapsulates all options specific for a condition check method. | ||
| * | ||
| * @author Filip Hrisafov | ||
| */ | ||
| public class ConditionMethodOptions { | ||
|
|
||
| private static final ConditionMethodOptions EMPTY = new ConditionMethodOptions( Collections.emptyList() ); | ||
|
|
||
| private final Collection<ConditionOptions> conditionOptions; | ||
|
|
||
| public ConditionMethodOptions(Collection<ConditionOptions> conditionOptions) { | ||
| this.conditionOptions = conditionOptions; | ||
| } | ||
|
|
||
| public boolean isStrategyApplicable(ConditionStrategyGem strategy) { | ||
| for ( ConditionOptions conditionOption : conditionOptions ) { | ||
| if ( conditionOption.getConditionStrategies().contains( strategy ) ) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public boolean isAnyStrategyApplicable() { | ||
| return !conditionOptions.isEmpty(); | ||
| } | ||
|
|
||
| public static ConditionMethodOptions empty() { | ||
| return EMPTY; | ||
| } | ||
| } |
170 changes: 170 additions & 0 deletions
170
processor/src/main/java/org/mapstruct/ap/internal/model/source/ConditionOptions.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,170 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.EnumSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import javax.lang.model.element.ExecutableElement; | ||
| import javax.lang.model.element.TypeElement; | ||
| import javax.lang.model.type.DeclaredType; | ||
| import javax.lang.model.type.TypeKind; | ||
| import javax.lang.model.type.TypeMirror; | ||
|
|
||
| import org.mapstruct.ap.internal.gem.ConditionGem; | ||
| import org.mapstruct.ap.internal.gem.ConditionStrategyGem; | ||
| import org.mapstruct.ap.internal.model.common.Parameter; | ||
| import org.mapstruct.ap.internal.util.FormattingMessager; | ||
| import org.mapstruct.ap.internal.util.Message; | ||
|
|
||
| /** | ||
| * @author Filip Hrisafov | ||
| */ | ||
| public class ConditionOptions { | ||
|
|
||
| private final Set<ConditionStrategyGem> conditionStrategies; | ||
|
|
||
| private ConditionOptions(Set<ConditionStrategyGem> conditionStrategies) { | ||
| this.conditionStrategies = conditionStrategies; | ||
| } | ||
|
|
||
| public Collection<ConditionStrategyGem> getConditionStrategies() { | ||
| return conditionStrategies; | ||
| } | ||
|
|
||
| public static ConditionOptions getInstanceOn(ConditionGem condition, ExecutableElement method, | ||
| List<Parameter> parameters, | ||
| FormattingMessager messager) { | ||
| if ( condition == null ) { | ||
| return null; | ||
| } | ||
|
|
||
| TypeMirror returnType = method.getReturnType(); | ||
| TypeKind returnTypeKind = returnType.getKind(); | ||
| // We only allow methods that return boolean or Boolean to be condition methods | ||
| if ( returnTypeKind != TypeKind.BOOLEAN ) { | ||
| if ( returnTypeKind != TypeKind.DECLARED ) { | ||
| return null; | ||
| } | ||
| DeclaredType declaredType = (DeclaredType) returnType; | ||
| TypeElement returnTypeElement = (TypeElement) declaredType.asElement(); | ||
| if ( !returnTypeElement.getQualifiedName().contentEquals( Boolean.class.getCanonicalName() ) ) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| Set<ConditionStrategyGem> strategies = condition.appliesTo().get() | ||
| .stream() | ||
| .map( ConditionStrategyGem::valueOf ) | ||
| .collect( Collectors.toCollection( () -> EnumSet.noneOf( ConditionStrategyGem.class ) ) ); | ||
|
|
||
| if ( strategies.isEmpty() ) { | ||
| messager.printMessage( | ||
| method, | ||
| condition.mirror(), | ||
| condition.appliesTo().getAnnotationValue(), | ||
| Message.CONDITION_MISSING_APPLIES_TO_STRATEGY | ||
| ); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| boolean allStrategiesValid = true; | ||
|
|
||
| for ( ConditionStrategyGem strategy : strategies ) { | ||
| boolean isStrategyValid = isValid( strategy, condition, method, parameters, messager ); | ||
| allStrategiesValid &= isStrategyValid; | ||
| } | ||
|
|
||
| return allStrategiesValid ? new ConditionOptions( strategies ) : null; | ||
| } | ||
|
|
||
| protected static boolean isValid(ConditionStrategyGem strategy, ConditionGem condition, | ||
| ExecutableElement method, List<Parameter> parameters, | ||
| FormattingMessager messager) { | ||
| if ( strategy == ConditionStrategyGem.SOURCE_PARAMETERS ) { | ||
| return hasValidStrategyForSourceProperties( condition, method, parameters, messager ); | ||
| } | ||
| else if ( strategy == ConditionStrategyGem.PROPERTIES ) { | ||
| return hasValidStrategyForProperties( condition, method, parameters, messager ); | ||
| } | ||
| else { | ||
| throw new IllegalStateException( "Invalid condition strategy: " + strategy ); | ||
| } | ||
| } | ||
|
|
||
| protected static boolean hasValidStrategyForSourceProperties(ConditionGem condition, ExecutableElement method, | ||
| List<Parameter> parameters, | ||
| FormattingMessager messager) { | ||
| for ( Parameter parameter : parameters ) { | ||
| if ( parameter.isSourceParameter() ) { | ||
| // source parameter is a valid parameter for a source condition check | ||
| continue; | ||
| } | ||
|
|
||
| if ( parameter.isMappingContext() ) { | ||
| // mapping context parameter is a valid parameter for a source condition check | ||
| continue; | ||
| } | ||
|
|
||
| messager.printMessage( | ||
| method, | ||
| condition.mirror(), | ||
| Message.CONDITION_SOURCE_PARAMETERS_INVALID_PARAMETER, | ||
| parameter.describe() | ||
| ); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| protected static boolean hasValidStrategyForProperties(ConditionGem condition, ExecutableElement method, | ||
| List<Parameter> parameters, | ||
| FormattingMessager messager) { | ||
| for ( Parameter parameter : parameters ) { | ||
| if ( parameter.isSourceParameter() ) { | ||
| // source parameter is a valid parameter for a property condition check | ||
| continue; | ||
| } | ||
|
|
||
| if ( parameter.isMappingContext() ) { | ||
| // mapping context parameter is a valid parameter for a property condition check | ||
| continue; | ||
| } | ||
|
|
||
| if ( parameter.isTargetType() ) { | ||
| // target type parameter is a valid parameter for a property condition check | ||
| continue; | ||
| } | ||
|
|
||
| if ( parameter.isMappingTarget() ) { | ||
| // mapping target parameter is a valid parameter for a property condition check | ||
| continue; | ||
| } | ||
|
|
||
| if ( parameter.isSourcePropertyName() ) { | ||
| // source property name parameter is a valid parameter for a property condition check | ||
| continue; | ||
| } | ||
|
|
||
| if ( parameter.isTargetPropertyName() ) { | ||
| // target property name parameter is a valid parameter for a property condition check | ||
| continue; | ||
| } | ||
|
|
||
| messager.printMessage( | ||
| method, | ||
| condition.mirror(), | ||
| Message.CONDITION_PROPERTIES_INVALID_PARAMETER, | ||
| parameter | ||
| ); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } |
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
Oops, something went wrong.
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.