-
-
Notifications
You must be signed in to change notification settings - Fork 1k
#3133 Redundant null check in generated code when adding parameters #3583
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
Open
zyberzebra
wants to merge
8
commits into
mapstruct:main
Choose a base branch
from
zyberzebra:#3133-fix-Redundant-null-check-in-generated-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0acce3c
Adds logic to avoid redundant null checks
zyberzebra 32c96e0
Adds more verifying test
zyberzebra ed75a59
[#3133] change test to assert against expected Files
zyberzebra 065e15b
adss missing copyright headers
zyberzebra 1e0f05d
[mapstruct#3133] reformat using provided eclipse formatter
zyberzebra ffdd2b7
Polish
filiphr 6e88402
Remove whitespace change
filiphr 0d1561f
fix: remove unnecessary condition
zyberzebra 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
26 changes: 26 additions & 0 deletions
26
processor/src/test/java/org/mapstruct/ap/test/nullcheck/redundant/FooMapper.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,26 @@ | ||
| /* | ||
| * 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.nullcheck.redundant; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import org.mapstruct.MappingTarget; | ||
| import org.mapstruct.factory.Mappers; | ||
|
|
||
| @Mapper | ||
| public interface FooMapper { | ||
|
|
||
| FooMapper INSTANCE = Mappers.getMapper( FooMapper.class ); | ||
|
|
||
| void updateFoo(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz); | ||
|
|
||
| void updateFoo(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz, int bay); | ||
|
|
||
| FooTarget getUpdatedFooTarget(FooSource input, @MappingTarget FooTarget toUpdate, boolean baz); | ||
|
|
||
| @Mapping(source = "input.nested.bar", target = "bar") | ||
| FooTarget map(FooSourceNested input, @MappingTarget FooTarget toUpdate, boolean baz); | ||
| } |
18 changes: 18 additions & 0 deletions
18
processor/src/test/java/org/mapstruct/ap/test/nullcheck/redundant/FooSource.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,18 @@ | ||
| /* | ||
| * 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.nullcheck.redundant; | ||
|
|
||
| public class FooSource { | ||
| private String bar; | ||
|
|
||
| public String getBar() { | ||
| return bar; | ||
| } | ||
|
|
||
| public void setBar(String bar) { | ||
| this.bar = bar; | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
processor/src/test/java/org/mapstruct/ap/test/nullcheck/redundant/FooSourceNested.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,27 @@ | ||
| /* | ||
| * 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.nullcheck.redundant; | ||
|
|
||
| public class FooSourceNested { | ||
| private String bar; | ||
| private FooSource nested; | ||
|
|
||
| public FooSource getNested() { | ||
| return nested; | ||
| } | ||
|
|
||
| public void setNested(FooSource nested) { | ||
| this.nested = nested; | ||
| } | ||
|
|
||
| public String getBar() { | ||
| return bar; | ||
| } | ||
|
|
||
| public void setBar(String bar) { | ||
| this.bar = bar; | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
processor/src/test/java/org/mapstruct/ap/test/nullcheck/redundant/FooTarget.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,18 @@ | ||
| /* | ||
| * 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.nullcheck.redundant; | ||
|
|
||
| public class FooTarget { | ||
| private String bar; | ||
|
|
||
| public String getBar() { | ||
| return bar; | ||
| } | ||
|
|
||
| public void setBar(String bar) { | ||
| this.bar = bar; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...essor/src/test/java/org/mapstruct/ap/test/nullcheck/redundant/RedundantNullCheckTest.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.nullcheck.redundant; | ||
|
|
||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
| import org.mapstruct.ap.testutil.IssueKey; | ||
| import org.mapstruct.ap.testutil.ProcessorTest; | ||
| import org.mapstruct.ap.testutil.WithClasses; | ||
| import org.mapstruct.ap.testutil.runner.GeneratedSource; | ||
|
|
||
| @WithClasses({ | ||
| FooMapper.class, | ||
| FooSource.class, | ||
| FooTarget.class, | ||
| FooSourceNested.class | ||
| }) | ||
| @IssueKey("3133") | ||
| class RedundantNullCheckTest { | ||
|
|
||
| @RegisterExtension | ||
| final GeneratedSource generatedSource = new GeneratedSource(); | ||
|
|
||
| @ProcessorTest | ||
| void generatedMapperShouldNotContainAnyRedundantNullChecks() { | ||
| generatedSource.addComparisonToFixtureFor( FooMapper.class ); | ||
| } | ||
|
|
||
| } |
64 changes: 64 additions & 0 deletions
64
.../src/test/resources/fixtures/org/mapstruct/ap/test/nullcheck/redundant/FooMapperImpl.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,64 @@ | ||
| /* | ||
| * 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.nullcheck.redundant; | ||
|
|
||
| import javax.annotation.processing.Generated; | ||
|
|
||
| @Generated( | ||
| value = "org.mapstruct.ap.MappingProcessor", | ||
| date = "2024-05-07T18:50:27+0200", | ||
| comments = "version: , compiler: javac, environment: Java 21.0.2 (Azul Systems, Inc.)" | ||
| ) | ||
| public class FooMapperImpl implements FooMapper { | ||
|
|
||
| @Override | ||
| public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) { | ||
| if ( input == null ) { | ||
| return; | ||
| } | ||
|
|
||
| toUpdate.setBar( input.getBar() ); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz, int bay) { | ||
| if ( input == null ) { | ||
| return; | ||
| } | ||
|
|
||
| toUpdate.setBar( input.getBar() ); | ||
| } | ||
|
|
||
| @Override | ||
| public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate, boolean baz) { | ||
| if ( input == null ) { | ||
| return toUpdate; | ||
| } | ||
|
|
||
| toUpdate.setBar( input.getBar() ); | ||
|
|
||
| return toUpdate; | ||
| } | ||
|
|
||
| @Override | ||
| public FooTarget map(FooSourceNested input, FooTarget toUpdate, boolean baz) { | ||
| if ( input == null ) { | ||
| return toUpdate; | ||
| } | ||
|
|
||
| toUpdate.setBar( inputNestedBar( input ) ); | ||
|
|
||
| return toUpdate; | ||
| } | ||
|
|
||
| private String inputNestedBar(FooSourceNested fooSourceNested) { | ||
| FooSource nested = fooSourceNested.getNested(); | ||
| if ( nested == null ) { | ||
| return null; | ||
| } | ||
| return nested.getBar(); | ||
| } | ||
| } |
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.