diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/AccessorNamingUtils.java b/processor/src/main/java/org/mapstruct/ap/internal/util/AccessorNamingUtils.java index 3f80ab34e..3d0d11686 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/AccessorNamingUtils.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/AccessorNamingUtils.java @@ -18,7 +18,7 @@ import org.mapstruct.ap.internal.util.accessor.ExecutableElementAccessor; import org.mapstruct.ap.spi.AccessorNamingStrategy; 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}. @@ -35,7 +35,7 @@ public final class AccessorNamingUtils { public boolean isGetterMethod(Accessor method) { ExecutableElement executable = method.getExecutable(); - return executable != null && isPublic( method ) && + return executable != null && isPublicNotStatic( method ) && executable.getParameters().isEmpty() && accessorNamingStrategy.getMethodType( executable ) == MethodType.GETTER; } @@ -46,7 +46,7 @@ public final class AccessorNamingUtils { } ExecutableElement executable = method.getExecutable(); return executable != null - && isPublic( method ) + && isPublicNotStatic( method ) && executable.getParameters().isEmpty() && ( executable.getReturnType().getKind() == TypeKind.BOOLEAN || "java.lang.Boolean".equals( getQualifiedName( executable.getReturnType() ) ) ) @@ -56,7 +56,7 @@ public final class AccessorNamingUtils { public boolean isSetterMethod(Accessor method) { ExecutableElement executable = method.getExecutable(); return executable != null - && isPublic( method ) + && isPublicNotStatic( method ) && executable.getParameters().size() == 1 && accessorNamingStrategy.getMethodType( executable ) == MethodType.SETTER; } @@ -64,7 +64,7 @@ public final class AccessorNamingUtils { public boolean isAdderMethod(Accessor method) { ExecutableElement executable = method.getExecutable(); return executable != null - && isPublic( method ) + && isPublicNotStatic( method ) && executable.getParameters().size() == 1 && accessorNamingStrategy.getMethodType( executable ) == MethodType.ADDER; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java b/processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java index 6d085ea76..6c16b6a8a 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/Executables.java @@ -67,6 +67,10 @@ public class Executables { return executable == null && isPublic( accessor ) && isNotStatic( accessor ); } + static boolean isPublicNotStatic(Accessor accessor) { + return isPublic( accessor ) && isNotStatic( accessor ); + } + static boolean isPublic(Accessor method) { return method.getModifiers().contains( Modifier.PUBLIC ); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Issue1660Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Issue1660Mapper.java new file mode 100644 index 000000000..512455970 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Issue1660Mapper.java @@ -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); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Issue1660Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Issue1660Test.java new file mode 100644 index 000000000..838f705c2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Issue1660Test.java @@ -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" ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Source.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Source.java new file mode 100644 index 000000000..bd324f2f1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Source.java @@ -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"; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Target.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Target.java new file mode 100644 index 000000000..2e2e3cd27 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1660/Target.java @@ -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 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 getOtherStaticValues() { + return otherStaticValues; + } + + public static void addOtherStaticValue(String otherStaticValue) { + Target.otherStaticValues.add( otherStaticValue ); + } + + public static void setOtherStaticValues(List otherStaticValues) { + Target.otherStaticValues = otherStaticValues; + } +}