Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public DeclaredType visitDeclared(DeclaredType t, Void p) {
* @throws MoreThanOneBuilderCreationMethodException if there are multiple builder creation methods
*/
protected BuilderInfo findBuilderInfo(TypeElement typeElement) {
return findBuilderInfo( typeElement, true );
}

protected BuilderInfo findBuilderInfo(TypeElement typeElement, boolean checkParent) {
if ( shouldIgnore( typeElement ) ) {
return null;
}
Expand Down Expand Up @@ -203,7 +207,10 @@ else if ( builderInfo.size() > 1 ) {
throw new MoreThanOneBuilderCreationMethodException( typeElement.asType(), builderInfo );
}

return findBuilderInfo( typeElement.getSuperclass() );
if ( checkParent ) {
return findBuilderInfo( typeElement.getSuperclass() );
}
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,31 @@ protected BuilderInfo findBuilderInfo(TypeElement typeElement) {
if ( name.length() == 0 || JAVA_JAVAX_PACKAGE.matcher( name ).matches() ) {
return null;
}

// First look if there is a builder defined in my own type
BuilderInfo info = findBuilderInfo( typeElement, false );
if ( info != null ) {
return info;
}

// Check for a builder in the generated immutable type
BuilderInfo immutableInfo = findBuilderInfoForImmutables( typeElement );
if ( immutableInfo != null ) {
return immutableInfo;
}

return super.findBuilderInfo( typeElement.getSuperclass() );
}

protected BuilderInfo findBuilderInfoForImmutables(TypeElement typeElement) {
TypeElement immutableAnnotation = elementUtils.getTypeElement( IMMUTABLE_FQN );
if ( immutableAnnotation != null ) {
BuilderInfo info = findBuilderInfoForImmutables(
return findBuilderInfoForImmutables(
typeElement,
immutableAnnotation
);
if ( info != null ) {
return info;
}
}

return super.findBuilderInfo( typeElement );
return null;
}

protected BuilderInfo findBuilderInfoForImmutables(TypeElement typeElement,
Expand All @@ -55,7 +68,7 @@ protected BuilderInfo findBuilderInfoForImmutables(TypeElement typeElement,
if ( typeUtils.isSameType( annotationMirror.getAnnotationType(), immutableAnnotation.asType() ) ) {
TypeElement immutableElement = asImmutableElement( typeElement );
if ( immutableElement != null ) {
return super.findBuilderInfo( immutableElement );
return super.findBuilderInfo( immutableElement, false );
}
else {
// Immutables processor has not run yet. Trigger a postpone to the next round for MapStruct
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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._3370;

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 Issue3370BuilderProvider extends ImmutablesBuilderProvider implements BuilderProvider {

@Override
protected BuilderInfo findBuilderInfoForImmutables(TypeElement typeElement) {
Name name = typeElement.getQualifiedName();
if ( name.toString().endsWith( ".Item" ) ) {
return super.findBuilderInfo( asImmutableElement( typeElement ) );
}
return super.findBuilderInfo( typeElement );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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._3370;

import java.util.HashMap;
import java.util.Map;

import org.mapstruct.ap.spi.AccessorNamingStrategy;
import org.mapstruct.ap.spi.BuilderProvider;
import org.mapstruct.ap.spi.ImmutablesAccessorNamingStrategy;
import org.mapstruct.ap.test.bugs._3370.domain.ImmutableItem;
import org.mapstruct.ap.test.bugs._3370.domain.Item;
import org.mapstruct.ap.test.bugs._3370.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,
ImmutableItem.class,
ItemDTO.class,
})
@IssueKey("3370")
@WithServiceImplementations({
@WithServiceImplementation(provides = BuilderProvider.class, value = Issue3370BuilderProvider.class),
@WithServiceImplementation(provides = AccessorNamingStrategy.class,
value = ImmutablesAccessorNamingStrategy.class),
})
public class Issue3370Test {

@ProcessorTest
public void shouldUseBuilderOfImmutableSuperClass() {

Map<String, String> attributesMap = new HashMap<>();
attributesMap.put( "a", "b" );
attributesMap.put( "c", "d" );

ItemDTO item = new ItemDTO( "test", attributesMap );

Item target = ItemMapper.INSTANCE.map( item );

assertThat( target ).isNotNull();
assertThat( target.getId() ).isEqualTo( "test" );
assertThat( target.getAttributes() ).isEqualTo( attributesMap );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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._3370;

import org.mapstruct.Mapper;
import org.mapstruct.ap.test.bugs._3370.domain.Item;
import org.mapstruct.ap.test.bugs._3370.dto.ItemDTO;
import org.mapstruct.factory.Mappers;

@Mapper
public abstract class ItemMapper {

public static final ItemMapper INSTANCE = Mappers.getMapper( ItemMapper.class );

public abstract Item map(ItemDTO itemDTO);
}
Loading