-
-
Notifications
You must be signed in to change notification settings - Fork 1k
#1601 Immutables fluent getters support #3914
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a56aa97
Adds Interface as source test
thmasker b749cc7
Adds header and author
thmasker e53da50
Adds fluent getters detection for immutables
thmasker dc83748
Adds Interface as source test
thmasker 81384b7
Adds header and author
thmasker b1c495d
Adds fluent getters detection for immutables
thmasker 2988281
Merge remote-tracking branch 'origin/1601-immutables-fluent-getters' …
thmasker 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
Adds fluent getters detection for immutables
- Loading branch information
commit e53da5054c674edb2de4fc913c26e9cd876f244b
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
39 changes: 39 additions & 0 deletions
39
processor/src/test/java/org/mapstruct/ap/test/fluent/getters/Issue1601BuilderProvider.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,39 @@ | ||
| /* | ||
| * 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.fluent.getters; | ||
|
|
||
| import javax.lang.model.element.Name; | ||
| import javax.lang.model.element.TypeElement; | ||
|
|
||
| import org.mapstruct.ap.spi.BuilderInfo; | ||
| import org.mapstruct.ap.spi.BuilderProvider; | ||
| import org.mapstruct.ap.spi.ImmutablesBuilderProvider; | ||
|
|
||
| public class Issue1601BuilderProvider extends ImmutablesBuilderProvider implements BuilderProvider { | ||
|
|
||
| @Override | ||
| protected BuilderInfo findBuilderInfo(TypeElement typeElement) { | ||
| Name name = typeElement.getQualifiedName(); | ||
| if ( name.toString().endsWith( ".Item" ) ) { | ||
| BuilderInfo info = findBuilderInfoForImmutables( typeElement ); | ||
| if ( info != null ) { | ||
| return info; | ||
| } | ||
| } | ||
|
|
||
| return super.findBuilderInfo( typeElement ); | ||
| } | ||
|
|
||
| @Override | ||
| protected BuilderInfo findBuilderInfoForImmutables(TypeElement typeElement) { | ||
| TypeElement immutableElement = asImmutableElement( typeElement ); | ||
| if ( immutableElement != null ) { | ||
| return super.findBuilderInfo( immutableElement ); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| } |
47 changes: 47 additions & 0 deletions
47
processor/src/test/java/org/mapstruct/ap/test/fluent/getters/Issue1601Test.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,47 @@ | ||
| /* | ||
| * 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.fluent.getters; | ||
|
|
||
| import org.mapstruct.ap.spi.AccessorNamingStrategy; | ||
| import org.mapstruct.ap.spi.BuilderProvider; | ||
| import org.mapstruct.ap.spi.ImmutablesAccessorNamingStrategy; | ||
| import org.mapstruct.ap.test.fluent.getters.domain.ImmutableItem; | ||
| import org.mapstruct.ap.test.fluent.getters.domain.Item; | ||
| import org.mapstruct.ap.test.fluent.getters.dto.ImmutableItemDto; | ||
| import org.mapstruct.ap.test.fluent.getters.dto.ItemDto; | ||
| import org.mapstruct.ap.testutil.IssueKey; | ||
| import org.mapstruct.ap.testutil.ProcessorTest; | ||
| import org.mapstruct.ap.testutil.WithClasses; | ||
| import org.mapstruct.ap.testutil.WithServiceImplementation; | ||
| import org.mapstruct.ap.testutil.WithServiceImplementations; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| @WithClasses({ | ||
| ItemMapper.class, | ||
| Item.class, | ||
| ItemDto.class, | ||
| ImmutableItem.class, | ||
| ImmutableItemDto.class | ||
| }) | ||
| @IssueKey("1601") | ||
| @WithServiceImplementations({ | ||
| @WithServiceImplementation(provides = BuilderProvider.class, value = Issue1601BuilderProvider.class), | ||
| @WithServiceImplementation(provides = AccessorNamingStrategy.class, value = ImmutablesAccessorNamingStrategy.class) | ||
| }) | ||
| public class Issue1601Test { | ||
|
|
||
| @ProcessorTest | ||
| public void shouldIncludeBuildType() { | ||
| var item = ImmutableItemDto.builder().id( "test" ).build(); | ||
|
|
||
| var target = ItemMapper.INSTANCE.map( item ); | ||
|
Comment on lines
+39
to
+41
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not use |
||
|
|
||
| assertThat( target ).isNotNull(); | ||
| assertThat( target.id() ).isEqualTo( "test" ); | ||
| } | ||
|
|
||
| } | ||
21 changes: 21 additions & 0 deletions
21
processor/src/test/java/org/mapstruct/ap/test/fluent/getters/ItemMapper.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,21 @@ | ||
| /* | ||
| * 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.fluent.getters; | ||
|
|
||
| import org.mapstruct.Builder; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.ap.test.fluent.getters.domain.Item; | ||
| import org.mapstruct.ap.test.fluent.getters.dto.ItemDto; | ||
| import org.mapstruct.factory.Mappers; | ||
|
|
||
| @Mapper(builder = @Builder) | ||
| public abstract class ItemMapper { | ||
|
|
||
| public static final ItemMapper INSTANCE = Mappers.getMapper( ItemMapper.class ); | ||
|
|
||
| public abstract Item map(ItemDto itemDTO); | ||
|
|
||
| } |
101 changes: 101 additions & 0 deletions
101
processor/src/test/java/org/mapstruct/ap/test/fluent/getters/domain/ImmutableItem.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,101 @@ | ||
| /* | ||
| * 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.fluent.getters.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public final class ImmutableItem extends Item { | ||
|
|
||
| private final String id; | ||
|
|
||
| private ImmutableItem(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| @Override | ||
| public String id() { | ||
| return id; | ||
| } | ||
|
|
||
| public ImmutableItem withId(String value) { | ||
| if ( Objects.equals( this.id, value ) ) { | ||
| return this; | ||
| } | ||
| return new ImmutableItem( value ); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object another) { | ||
| if ( this == another ) { | ||
| return true; | ||
| } | ||
| return another instanceof ImmutableItem && id.equals( ( (ImmutableItem) another ).id ); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int h = 5381; | ||
| h += ( h << 5 ) + id.hashCode(); | ||
| return h; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Item{id=" + id + "}"; | ||
| } | ||
|
|
||
| public static ImmutableItem copyOf(Item instance) { | ||
| if ( instance instanceof ImmutableItem ) { | ||
| return (ImmutableItem) instance; | ||
| } | ||
| return ImmutableItem.builder().from( instance ).build(); | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
|
|
||
| private static final long INIT_BIT_ID = 0x1L; | ||
| private long initBits = 0x1L; | ||
|
|
||
| private String id; | ||
|
|
||
| private Builder() { | ||
| } | ||
|
|
||
| public Builder from(Item instance) { | ||
| id( instance.id() ); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder id(String id) { | ||
| this.id = id; | ||
| initBits &= ~INIT_BIT_ID; | ||
| return this; | ||
| } | ||
|
|
||
| public ImmutableItem build() { | ||
| if ( initBits != 0 ) { | ||
| throw new IllegalStateException( formatRequiredAttributesMessage() ); | ||
| } | ||
| return new ImmutableItem( id ); | ||
| } | ||
|
|
||
| private String formatRequiredAttributesMessage() { | ||
| List<String> attributes = new ArrayList<>(); | ||
| if ( ( initBits & INIT_BIT_ID ) != 0 ) { | ||
| attributes.add( "id" ); | ||
| } | ||
| return "Cannot build Item, some of required attributes are not set " + attributes; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
7 changes: 7 additions & 0 deletions
7
processor/src/test/java/org/mapstruct/ap/test/fluent/getters/domain/Item.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,7 @@ | ||
| package org.mapstruct.ap.test.fluent.getters.domain; | ||
|
|
||
| public abstract class Item { | ||
|
|
||
| public abstract String id(); | ||
|
|
||
| } |
101 changes: 101 additions & 0 deletions
101
processor/src/test/java/org/mapstruct/ap/test/fluent/getters/dto/ImmutableItemDto.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,101 @@ | ||
| /* | ||
| * 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.fluent.getters.dto; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public final class ImmutableItemDto extends ItemDto { | ||
|
|
||
| private final String id; | ||
|
|
||
| private ImmutableItemDto(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| @Override | ||
| public String id() { | ||
| return id; | ||
| } | ||
|
|
||
| public ImmutableItemDto withId(String value) { | ||
| if ( Objects.equals( this.id, value ) ) { | ||
| return this; | ||
| } | ||
| return new ImmutableItemDto( value ); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object another) { | ||
| if ( this == another ) { | ||
| return true; | ||
| } | ||
| return another instanceof ImmutableItemDto && id.equals( ( (ImmutableItemDto) another ).id ); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int h = 5381; | ||
| h += ( h << 5 ) + id.hashCode(); | ||
| return h; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ItemDTO{id=" + id + "}"; | ||
| } | ||
|
|
||
| public static ImmutableItemDto copyOf(ItemDto instance) { | ||
| if ( instance instanceof ImmutableItemDto ) { | ||
| return (ImmutableItemDto) instance; | ||
| } | ||
| return ImmutableItemDto.builder().from( instance ).build(); | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
|
|
||
| private static final long INIT_BIT_ID = 0x1L; | ||
| private long initBits = 0x1L; | ||
|
|
||
| private String id; | ||
|
|
||
| private Builder() { | ||
| } | ||
|
|
||
| public Builder from(ItemDto instance) { | ||
| id( instance.id() ); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder id(String id) { | ||
| this.id = id; | ||
| initBits &= ~INIT_BIT_ID; | ||
| return this; | ||
| } | ||
|
|
||
| public ImmutableItemDto build() { | ||
| if ( initBits != 0 ) { | ||
| throw new IllegalStateException( formatRequiredAttributesMessage() ); | ||
| } | ||
| return new ImmutableItemDto( id ); | ||
| } | ||
|
|
||
| private String formatRequiredAttributesMessage() { | ||
| List<String> attributes = new ArrayList<>(); | ||
| if ( ( initBits & INIT_BIT_ID ) != 0 ) { | ||
| attributes.add( "id" ); | ||
| } | ||
| return "Cannot build ItemDTO, some of required attributes are not set " + attributes; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
7 changes: 7 additions & 0 deletions
7
processor/src/test/java/org/mapstruct/ap/test/fluent/getters/dto/ItemDto.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,7 @@ | ||
| package org.mapstruct.ap.test.fluent.getters.dto; | ||
|
|
||
| public abstract class ItemDto { | ||
|
|
||
| public abstract String id(); | ||
|
|
||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that we should just make this like this. We should look at the Immutables
@Styles, see https://immutables.github.io/style.html and determine based on that.Otherwise, this just applies to everything.
Additionally, I really do not understand how this even works, the
getElementNameis not overridden, so I don't think that this works properly