Skip to content

Commit a0ed5e2

Browse files
committed
#38 Only treat public non static getters and setter as potential accessors
1 parent b55b2e0 commit a0ed5e2

File tree

6 files changed

+122
-2
lines changed

6 files changed

+122
-2
lines changed

src/main/java/org/mapstruct/intellij/util/MapstructUtil.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ public static boolean isPublic(@NotNull PsiMethod method) {
153153
return method.hasModifierProperty( PsiModifier.PUBLIC );
154154
}
155155

156+
public static boolean isPublicNonStatic(@NotNull PsiMethod method) {
157+
return isPublic( method ) && !method.hasModifierProperty( PsiModifier.STATIC );
158+
}
159+
156160
public static boolean isPublicStatic(@NotNull PsiMethod method) {
157161
return isPublic( method ) && method.hasModifierProperty( PsiModifier.STATIC );
158162
}

src/main/java/org/mapstruct/intellij/util/SourceUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private static Map<String, Pair<PsiMethod, PsiSubstitutor>> publicGetters(@NotNu
135135

136136
@Nullable
137137
private static String extractPublicGetterPropertyName(PsiMethod method) {
138-
if ( method.getParameterList().getParametersCount() != 0 || !MapstructUtil.isPublic( method ) ) {
138+
if ( method.getParameterList().getParametersCount() != 0 || !MapstructUtil.isPublicNonStatic( method ) ) {
139139
return null;
140140
}
141141
// This logic is aligned with the DefaultAccessorNamingStrategy

src/main/java/org/mapstruct/intellij/util/TargetUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private static Map<String, Pair<? extends PsiMember, PsiSubstitutor>> publicSett
133133
@Nullable
134134
private static String extractPublicSetterPropertyName(PsiMethod method, @NotNull PsiType typeToUse,
135135
boolean builderSupportPresent) {
136-
if ( method.getParameterList().getParametersCount() != 1 || !MapstructUtil.isPublic( method ) ) {
136+
if ( method.getParameterList().getParametersCount() != 1 || !MapstructUtil.isPublicNonStatic( method ) ) {
137137
// If the method does not have 1 parameter or is not public then there is no property
138138
return null;
139139
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.intellij.bugs._38;
7+
8+
import com.intellij.codeInsight.lookup.LookupElement;
9+
import org.mapstruct.intellij.MapstructBaseCompletionTestCase;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
/**
14+
* @author Filip Hrisafov
15+
*/
16+
public class Issue38Test extends MapstructBaseCompletionTestCase {
17+
18+
@Override
19+
protected String getTestDataPath() {
20+
return "testData/bugs/_38";
21+
}
22+
23+
public void testUnmappedTargetWithPublicStaticProperty() {
24+
configureByTestName();
25+
assertThat( myItems )
26+
.extracting( LookupElement::getLookupString )
27+
.containsExactlyInAnyOrder(
28+
"isbn"
29+
);
30+
}
31+
32+
public void testUnmappedSourceWithPublicStaticProperty() {
33+
configureByTestName();
34+
assertThat( myItems )
35+
.extracting( LookupElement::getLookupString )
36+
.containsExactlyInAnyOrder(
37+
"isbn"
38+
);
39+
}
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
import org.mapstruct.Mapper;
8+
import org.mapstruct.Mapping;
9+
10+
@Mapper
11+
public interface Issue38Mapper {
12+
13+
@Mapping(source = "<caret>")
14+
Book map(BookDto isbn);
15+
}
16+
17+
public class Book {
18+
19+
public String isbn;
20+
21+
}
22+
23+
public class BookDto {
24+
25+
protected static Integer inStock = 10;
26+
27+
protected String isbn;
28+
29+
public String getIsbn() {
30+
return isbn;
31+
}
32+
33+
public void setIsbn(String isbn) {
34+
this.isbn = isbn;
35+
}
36+
37+
public static Integer getInStock() {
38+
return inStock;
39+
}
40+
}
41+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
import org.mapstruct.Mapper;
8+
import org.mapstruct.Mapping;
9+
10+
@Mapper
11+
public interface Issue38Mapper {
12+
13+
@Mapping(target = "<caret>")
14+
BookDto map(String isbn);
15+
}
16+
17+
public class BookDto {
18+
19+
protected String isbn;
20+
21+
public String getIsbn() {
22+
return isbn;
23+
}
24+
25+
public void setIsbn(String isbn) {
26+
this.isbn = isbn;
27+
}
28+
29+
public static BookDto fromIsbn(String isbn) {
30+
BookDto dto = new BookDto();
31+
dto.setIsbn( isbn );
32+
return dto;
33+
}
34+
}
35+

0 commit comments

Comments
 (0)