mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1449 Adders should not be considered as builder setter methods
This commit is contained in:
parent
7306c52529
commit
adde6826a6
@ -102,6 +102,7 @@ public class DefaultAccessorNamingStrategy implements AccessorNamingStrategy {
|
|||||||
protected boolean isBuilderSetter(ExecutableElement method) {
|
protected boolean isBuilderSetter(ExecutableElement method) {
|
||||||
return method.getParameters().size() == 1 &&
|
return method.getParameters().size() == 1 &&
|
||||||
!JAVA_JAVAX_PACKAGE.matcher( method.getEnclosingElement().asType().toString() ).matches() &&
|
!JAVA_JAVAX_PACKAGE.matcher( method.getEnclosingElement().asType().toString() ).matches() &&
|
||||||
|
!isAdderMethod( method ) &&
|
||||||
//TODO The Types need to be compared with Types#isSameType(TypeMirror, TypeMirror)
|
//TODO The Types need to be compared with Types#isSameType(TypeMirror, TypeMirror)
|
||||||
method.getReturnType().toString().equals( method.getEnclosingElement().asType().toString() );
|
method.getReturnType().toString().equals( method.getEnclosingElement().asType().toString() );
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.builder.simple;
|
package org.mapstruct.ap.test.builder.simple;
|
||||||
|
|
||||||
|
import org.mapstruct.CollectionMappingStrategy;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
import org.mapstruct.Mappings;
|
import org.mapstruct.Mappings;
|
||||||
|
|
||||||
@Mapper
|
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
|
||||||
public interface SimpleBuilderMapper {
|
public interface SimpleBuilderMapper {
|
||||||
|
|
||||||
@Mappings({
|
@Mappings({
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.builder.simple;
|
package org.mapstruct.ap.test.builder.simple;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -48,6 +50,7 @@ public class SimpleImmutableBuilderTest {
|
|||||||
SimpleMutablePerson source = new SimpleMutablePerson();
|
SimpleMutablePerson source = new SimpleMutablePerson();
|
||||||
source.setAge( 3 );
|
source.setAge( 3 );
|
||||||
source.setFullName( "Bob" );
|
source.setFullName( "Bob" );
|
||||||
|
source.setChildren( Arrays.asList( "Alice", "Tom" ) );
|
||||||
|
|
||||||
SimpleImmutablePerson targetObject = mapper.toImmutable( source );
|
SimpleImmutablePerson targetObject = mapper.toImmutable( source );
|
||||||
|
|
||||||
@ -55,6 +58,7 @@ public class SimpleImmutableBuilderTest {
|
|||||||
assertThat( targetObject.getName() ).isEqualTo( "Bob" );
|
assertThat( targetObject.getName() ).isEqualTo( "Bob" );
|
||||||
assertThat( targetObject.getJob() ).isEqualTo( "programmer" );
|
assertThat( targetObject.getJob() ).isEqualTo( "programmer" );
|
||||||
assertThat( targetObject.getCity() ).isEqualTo( "Bengalore" );
|
assertThat( targetObject.getCity() ).isEqualTo( "Bengalore" );
|
||||||
|
assertThat( targetObject.getChildren() ).contains( "Alice", "Tom" );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -18,17 +18,22 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.builder.simple;
|
package org.mapstruct.ap.test.builder.simple;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class SimpleImmutablePerson {
|
public class SimpleImmutablePerson {
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int age;
|
private final int age;
|
||||||
private final String job;
|
private final String job;
|
||||||
private final String city;
|
private final String city;
|
||||||
|
private final List<String> children;
|
||||||
|
|
||||||
SimpleImmutablePerson(Builder builder) {
|
SimpleImmutablePerson(Builder builder) {
|
||||||
this.name = builder.name;
|
this.name = builder.name;
|
||||||
this.age = builder.age;
|
this.age = builder.age;
|
||||||
this.job = builder.job;
|
this.job = builder.job;
|
||||||
this.city = builder.city;
|
this.city = builder.city;
|
||||||
|
this.children = new ArrayList<String>( builder.children );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Builder builder() {
|
public static Builder builder() {
|
||||||
@ -51,11 +56,16 @@ public class SimpleImmutablePerson {
|
|||||||
return city;
|
return city;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private String name;
|
private String name;
|
||||||
private int age;
|
private int age;
|
||||||
private String job;
|
private String job;
|
||||||
private String city;
|
private String city;
|
||||||
|
private List<String> children = new ArrayList<String>();
|
||||||
|
|
||||||
public Builder age(int age) {
|
public Builder age(int age) {
|
||||||
this.age = age;
|
this.age = age;
|
||||||
@ -80,5 +90,14 @@ public class SimpleImmutablePerson {
|
|||||||
this.city = city;
|
this.city = city;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getChildren() {
|
||||||
|
throw new UnsupportedOperationException( "This is just a marker method" );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder addChild(String child) {
|
||||||
|
this.children.add( child );
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,9 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.builder.simple;
|
package org.mapstruct.ap.test.builder.simple;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class SimpleMutablePerson {
|
public class SimpleMutablePerson {
|
||||||
private String fullName;
|
private String fullName;
|
||||||
private int age;
|
private int age;
|
||||||
|
private List<String> children;
|
||||||
|
|
||||||
public int getAge() {
|
public int getAge() {
|
||||||
return age;
|
return age;
|
||||||
@ -37,4 +40,12 @@ public class SimpleMutablePerson {
|
|||||||
public void setFullName(String fullName) {
|
public void setFullName(String fullName) {
|
||||||
this.fullName = fullName;
|
this.fullName = fullName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChildren(List<String> children) {
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user