forked from mapstruct/mapstruct
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapstruct#1801 Using constructor as builderCreationMethod in custom b…
…uilder (mapstruct#1905)
- Loading branch information
Showing
10 changed files
with
586 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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
96 changes: 96 additions & 0 deletions
96
processor/src/test/java/org/mapstruct/ap/test/bugs/_1801/Issue1801BuilderProvider.java
This file contains 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,96 @@ | ||
/* | ||
* 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.bugs._1801; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.Name; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.util.ElementFilter; | ||
|
||
import org.mapstruct.ap.spi.BuilderInfo; | ||
import org.mapstruct.ap.spi.BuilderProvider; | ||
import org.mapstruct.ap.spi.ImmutablesBuilderProvider; | ||
|
||
/** | ||
* @author Zhizhi Deng | ||
*/ | ||
public class Issue1801BuilderProvider extends ImmutablesBuilderProvider implements BuilderProvider { | ||
|
||
@Override | ||
protected BuilderInfo findBuilderInfo(TypeElement typeElement) { | ||
Name name = typeElement.getQualifiedName(); | ||
if ( name.toString().endsWith( ".Item" ) ) { | ||
BuilderInfo info = findBuilderInfoFromInnerBuilderClass( typeElement ); | ||
if ( info != null ) { | ||
return info; | ||
} | ||
} | ||
return super.findBuilderInfo( typeElement ); | ||
} | ||
|
||
/** | ||
* Looks for inner builder class in the Immutable interface / abstract class. | ||
* | ||
* The inner builder class should be be declared with the following line | ||
* | ||
* <pre> | ||
* public static Builder() extends ImmutableItem.Builder { } | ||
* </pre> | ||
* | ||
* The Immutable instance should be created with the following line | ||
* | ||
* <pre> | ||
* new Item.Builder().withId("123").build(); | ||
* </pre> | ||
* | ||
* @see org.mapstruct.ap.test.bugs._1801.domain.Item | ||
* | ||
* @param typeElement | ||
* @return | ||
*/ | ||
private BuilderInfo findBuilderInfoFromInnerBuilderClass(TypeElement typeElement) { | ||
if (shouldIgnore( typeElement )) { | ||
return null; | ||
} | ||
|
||
List<TypeElement> innerTypes = ElementFilter.typesIn( typeElement.getEnclosedElements() ); | ||
ExecutableElement defaultConstructor = innerTypes.stream() | ||
.filter( this::isBuilderCandidate ) | ||
.map( this::getEmptyArgPublicConstructor ) | ||
.filter( Objects::nonNull ) | ||
.findAny() | ||
.orElse( null ); | ||
|
||
if ( defaultConstructor != null ) { | ||
return new BuilderInfo.Builder() | ||
.builderCreationMethod( defaultConstructor ) | ||
.buildMethod( findBuildMethods( (TypeElement) defaultConstructor.getEnclosingElement(), typeElement ) ) | ||
.build(); | ||
} | ||
return null; | ||
} | ||
|
||
private boolean isBuilderCandidate(TypeElement innerType ) { | ||
TypeElement outerType = (TypeElement) innerType.getEnclosingElement(); | ||
String packageName = this.elementUtils.getPackageOf( outerType ).getQualifiedName().toString(); | ||
Name outerSimpleName = outerType.getSimpleName(); | ||
String builderClassName = packageName + ".Immutable" + outerSimpleName + ".Builder"; | ||
return innerType.getSimpleName().contentEquals( "Builder" ) | ||
&& getTypeElement( innerType.getSuperclass() ).getQualifiedName().contentEquals( builderClassName ) | ||
&& innerType.getModifiers().contains( Modifier.PUBLIC ); | ||
} | ||
|
||
private ExecutableElement getEmptyArgPublicConstructor(TypeElement builderType) { | ||
return ElementFilter.constructorsIn( builderType.getEnclosedElements() ).stream() | ||
.filter( c -> c.getParameters().isEmpty() ) | ||
.filter( c -> c.getModifiers().contains( Modifier.PUBLIC ) ) | ||
.findAny() | ||
.orElse( null ); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
processor/src/test/java/org/mapstruct/ap/test/bugs/_1801/Issue1801Test.java
This file contains 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,53 @@ | ||
/* | ||
* 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.bugs._1801; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mapstruct.ap.spi.AccessorNamingStrategy; | ||
import org.mapstruct.ap.spi.BuilderProvider; | ||
import org.mapstruct.ap.spi.ImmutablesAccessorNamingStrategy; | ||
import org.mapstruct.ap.test.bugs._1801.domain.ImmutableItem; | ||
import org.mapstruct.ap.test.bugs._1801.domain.Item; | ||
import org.mapstruct.ap.test.bugs._1801.dto.ImmutableItemDTO; | ||
import org.mapstruct.ap.test.bugs._1801.dto.ItemDTO; | ||
import org.mapstruct.ap.testutil.IssueKey; | ||
import org.mapstruct.ap.testutil.WithClasses; | ||
import org.mapstruct.ap.testutil.WithServiceImplementation; | ||
import org.mapstruct.ap.testutil.WithServiceImplementations; | ||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Zhizhi Deng | ||
*/ | ||
@WithClasses({ | ||
ItemMapper.class, | ||
Item.class, | ||
ImmutableItem.class, | ||
ItemDTO.class, | ||
ImmutableItemDTO.class | ||
}) | ||
@RunWith(AnnotationProcessorTestRunner.class) | ||
@IssueKey("1801") | ||
@WithServiceImplementations( { | ||
@WithServiceImplementation( provides = BuilderProvider.class, value = Issue1801BuilderProvider.class), | ||
@WithServiceImplementation( provides = AccessorNamingStrategy.class, value = ImmutablesAccessorNamingStrategy.class) | ||
}) | ||
public class Issue1801Test { | ||
|
||
@Test | ||
public void shouldIncludeBuildeType() { | ||
|
||
ItemDTO item = ImmutableItemDTO.builder().id( "test" ).build(); | ||
|
||
Item target = ItemMapper.INSTANCE.map( item ); | ||
|
||
assertThat( target ).isNotNull(); | ||
assertThat( target.getId() ).isEqualTo( "test" ); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
processor/src/test/java/org/mapstruct/ap/test/bugs/_1801/ItemMapper.java
This file contains 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.ap.test.bugs._1801; | ||
|
||
import org.mapstruct.Builder; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.ap.test.bugs._1801.domain.Item; | ||
import org.mapstruct.ap.test.bugs._1801.dto.ItemDTO; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
/** | ||
* @author Zhizhi Deng | ||
*/ | ||
@Mapper( builder = @Builder) | ||
public abstract class ItemMapper { | ||
|
||
public static final ItemMapper INSTANCE = Mappers.getMapper( ItemMapper.class ); | ||
|
||
public abstract Item map(ItemDTO itemDTO); | ||
} |
Oops, something went wrong.