mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1660 Consider only public not static accessor methods as possible getter, setter, presence check and / or adder methods
This commit is contained in:
parent
9a43b210d3
commit
d1fe65dbad
@ -18,7 +18,7 @@ import org.mapstruct.ap.internal.util.accessor.ExecutableElementAccessor;
|
|||||||
import org.mapstruct.ap.spi.AccessorNamingStrategy;
|
import org.mapstruct.ap.spi.AccessorNamingStrategy;
|
||||||
import org.mapstruct.ap.spi.MethodType;
|
import org.mapstruct.ap.spi.MethodType;
|
||||||
|
|
||||||
import static org.mapstruct.ap.internal.util.Executables.isPublic;
|
import static org.mapstruct.ap.internal.util.Executables.isPublicNotStatic;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utils for working with the {@link AccessorNamingStrategy}.
|
* Utils for working with the {@link AccessorNamingStrategy}.
|
||||||
@ -35,7 +35,7 @@ public final class AccessorNamingUtils {
|
|||||||
|
|
||||||
public boolean isGetterMethod(Accessor method) {
|
public boolean isGetterMethod(Accessor method) {
|
||||||
ExecutableElement executable = method.getExecutable();
|
ExecutableElement executable = method.getExecutable();
|
||||||
return executable != null && isPublic( method ) &&
|
return executable != null && isPublicNotStatic( method ) &&
|
||||||
executable.getParameters().isEmpty() &&
|
executable.getParameters().isEmpty() &&
|
||||||
accessorNamingStrategy.getMethodType( executable ) == MethodType.GETTER;
|
accessorNamingStrategy.getMethodType( executable ) == MethodType.GETTER;
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ public final class AccessorNamingUtils {
|
|||||||
}
|
}
|
||||||
ExecutableElement executable = method.getExecutable();
|
ExecutableElement executable = method.getExecutable();
|
||||||
return executable != null
|
return executable != null
|
||||||
&& isPublic( method )
|
&& isPublicNotStatic( method )
|
||||||
&& executable.getParameters().isEmpty()
|
&& executable.getParameters().isEmpty()
|
||||||
&& ( executable.getReturnType().getKind() == TypeKind.BOOLEAN ||
|
&& ( executable.getReturnType().getKind() == TypeKind.BOOLEAN ||
|
||||||
"java.lang.Boolean".equals( getQualifiedName( executable.getReturnType() ) ) )
|
"java.lang.Boolean".equals( getQualifiedName( executable.getReturnType() ) ) )
|
||||||
@ -56,7 +56,7 @@ public final class AccessorNamingUtils {
|
|||||||
public boolean isSetterMethod(Accessor method) {
|
public boolean isSetterMethod(Accessor method) {
|
||||||
ExecutableElement executable = method.getExecutable();
|
ExecutableElement executable = method.getExecutable();
|
||||||
return executable != null
|
return executable != null
|
||||||
&& isPublic( method )
|
&& isPublicNotStatic( method )
|
||||||
&& executable.getParameters().size() == 1
|
&& executable.getParameters().size() == 1
|
||||||
&& accessorNamingStrategy.getMethodType( executable ) == MethodType.SETTER;
|
&& accessorNamingStrategy.getMethodType( executable ) == MethodType.SETTER;
|
||||||
}
|
}
|
||||||
@ -64,7 +64,7 @@ public final class AccessorNamingUtils {
|
|||||||
public boolean isAdderMethod(Accessor method) {
|
public boolean isAdderMethod(Accessor method) {
|
||||||
ExecutableElement executable = method.getExecutable();
|
ExecutableElement executable = method.getExecutable();
|
||||||
return executable != null
|
return executable != null
|
||||||
&& isPublic( method )
|
&& isPublicNotStatic( method )
|
||||||
&& executable.getParameters().size() == 1
|
&& executable.getParameters().size() == 1
|
||||||
&& accessorNamingStrategy.getMethodType( executable ) == MethodType.ADDER;
|
&& accessorNamingStrategy.getMethodType( executable ) == MethodType.ADDER;
|
||||||
}
|
}
|
||||||
|
@ -67,6 +67,10 @@ public class Executables {
|
|||||||
return executable == null && isPublic( accessor ) && isNotStatic( accessor );
|
return executable == null && isPublic( accessor ) && isNotStatic( accessor );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static boolean isPublicNotStatic(Accessor accessor) {
|
||||||
|
return isPublic( accessor ) && isNotStatic( accessor );
|
||||||
|
}
|
||||||
|
|
||||||
static boolean isPublic(Accessor method) {
|
static boolean isPublic(Accessor method) {
|
||||||
return method.getModifiers().contains( Modifier.PUBLIC );
|
return method.getModifiers().contains( Modifier.PUBLIC );
|
||||||
}
|
}
|
||||||
|
@ -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.bugs._1660;
|
||||||
|
|
||||||
|
import org.mapstruct.CollectionMappingStrategy;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
|
||||||
|
public interface Issue1660Mapper {
|
||||||
|
|
||||||
|
Issue1660Mapper INSTANCE = Mappers.getMapper( Issue1660Mapper.class );
|
||||||
|
|
||||||
|
Target map(Source source);
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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._1660;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
|
@IssueKey("1660")
|
||||||
|
@WithClasses({
|
||||||
|
Issue1660Mapper.class,
|
||||||
|
Source.class,
|
||||||
|
Target.class,
|
||||||
|
})
|
||||||
|
public class Issue1660Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotUseStaticMethods() {
|
||||||
|
Source source = new Source();
|
||||||
|
source.setValue( "source" );
|
||||||
|
Target target = Issue1660Mapper.INSTANCE.map( source );
|
||||||
|
|
||||||
|
assertThat( target.getValue() ).isEqualTo( "source" );
|
||||||
|
assertThat( Target.getStaticValue() ).isEqualTo( "targetStatic" );
|
||||||
|
assertThat( Target.getOtherStaticValues() ).containsExactly( "targetOtherStatic" );
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* 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._1660;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class Source {
|
||||||
|
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getStaticValue() {
|
||||||
|
return "sourceStatic";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOtherStaticValue() {
|
||||||
|
return "sourceOtherStatic";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* 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._1660;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class Target {
|
||||||
|
|
||||||
|
private static String staticValue = "targetStatic";
|
||||||
|
private static List<String> otherStaticValues = new ArrayList<>( Collections.singletonList( "targetOtherStatic" ) );
|
||||||
|
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getStaticValue() {
|
||||||
|
return staticValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setStaticValue(String staticValue) {
|
||||||
|
Target.staticValue = staticValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getOtherStaticValues() {
|
||||||
|
return otherStaticValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addOtherStaticValue(String otherStaticValue) {
|
||||||
|
Target.otherStaticValues.add( otherStaticValue );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setOtherStaticValues(List<String> otherStaticValues) {
|
||||||
|
Target.otherStaticValues = otherStaticValues;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user