Skip to content

Commit 5b87efb

Browse files
committed
Do not resolve public static fields / methods and do not use as unmapped properties
Closes #47
1 parent aefc7b8 commit 5b87efb

14 files changed

+153
-10
lines changed

change-notes.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<h2>1.2.1</h2>
33
<ul>
44
<li>Support code completion in Mapping#expression and Mapping#defaultExpression</li>
5+
<li>Bug fix: public static fields / methods should not be considered as unmapped properties or resolved in auto completion</li>
56
</ul>
67
<h2>1.2.0</h2>
78
<ul>

src/main/java/org/mapstruct/intellij/codeinsight/references/MapstructSourceReference.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.mapstruct.intellij.util.MapstructUtil;
2626

2727
import static org.mapstruct.intellij.util.MapstructUtil.asLookup;
28-
import static org.mapstruct.intellij.util.MapstructUtil.isPublic;
28+
import static org.mapstruct.intellij.util.MapstructUtil.isPublicNonStatic;
2929
import static org.mapstruct.intellij.util.SourceUtils.getParameterType;
3030
import static org.mapstruct.intellij.util.SourceUtils.publicReadAccessors;
3131

@@ -59,12 +59,12 @@ PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) {
5959
if ( methods.length == 0 ) {
6060
methods = psiClass.findMethodsByName( "is" + MapstructUtil.capitalize( value ), true );
6161
}
62-
if ( methods.length > 0 ) {
62+
if ( methods.length > 0 && isPublicNonStatic( methods[0] ) ) {
6363
return methods[0];
6464
}
6565

6666
PsiField field = psiClass.findFieldByName( value, true );
67-
if ( field != null && isPublic( field ) ) {
67+
if ( field != null && isPublicNonStatic( field ) ) {
6868
return field;
6969
}
7070
return null;

src/main/java/org/mapstruct/intellij/codeinsight/references/MapstructTargetReference.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import static org.mapstruct.intellij.util.MapstructUtil.asLookup;
3131
import static org.mapstruct.intellij.util.MapstructUtil.isPublicModifiable;
32+
import static org.mapstruct.intellij.util.MapstructUtil.isPublicNonStatic;
3233
import static org.mapstruct.intellij.util.TargetUtils.getRelevantType;
3334
import static org.mapstruct.intellij.util.TargetUtils.publicWriteAccessors;
3435
import static org.mapstruct.intellij.util.TargetUtils.resolveBuilderOrSelfClass;
@@ -79,7 +80,7 @@ PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) {
7980
}
8081

8182
PsiMethod[] methods = psiClass.findMethodsByName( "set" + MapstructUtil.capitalize( value ), true );
82-
if ( methods.length != 0 ) {
83+
if ( methods.length != 0 && isPublicNonStatic( methods[0] ) ) {
8384
return methods[0];
8485
}
8586

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,17 @@ public static boolean isPublicStatic(@NotNull PsiMethod method) {
163163
return isPublic( method ) && method.hasModifierProperty( PsiModifier.STATIC );
164164
}
165165

166-
public static boolean isPublic(@NotNull PsiField field) {
166+
public static boolean isPublicNonStatic(@NotNull PsiField field) {
167+
return isPublic( field ) && !field.hasModifierProperty( PsiModifier.STATIC );
168+
}
169+
170+
private static boolean isPublic(@NotNull PsiField field) {
167171
return field.hasModifierProperty( PsiModifier.PUBLIC );
168172
}
169173

170174
public static boolean isPublicModifiable(@NotNull PsiField field) {
171-
return isPublic( field ) &&
172-
!field.hasModifierProperty( PsiModifier.FINAL ) &&
173-
!field.hasModifierProperty( PsiModifier.STATIC );
175+
return isPublicNonStatic( field ) &&
176+
!field.hasModifierProperty( PsiModifier.FINAL );
174177
}
175178

176179
public static boolean isFluentSetter(@NotNull PsiMethod method, PsiType psiType) {
@@ -377,7 +380,7 @@ public static Map<String, Pair<PsiField, PsiSubstitutor>> publicFields(PsiClass
377380

378381
for ( Pair<PsiField, PsiSubstitutor> fieldPair : fieldPairs ) {
379382
PsiField field = fieldPair.getFirst();
380-
if ( MapstructUtil.isPublic( field ) ) {
383+
if ( MapstructUtil.isPublicNonStatic( field ) ) {
381384
publicFields.put( field.getName(), fieldPair );
382385
}
383386
}

src/test/java/org/mapstruct/intellij/MapstructCompletionTestCase.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,20 @@ public void testCarMapperReferencePublicSourceProperty() {
542542
} );
543543
}
544544

545+
public void testCarMapperReferencePublicStaticFieldSourceProperty() {
546+
myFixture.configureByFile( "CarMapperReferencePublicStaticFieldSourceProperty.java" );
547+
PsiReference reference = myFixture.getFile().findReferenceAt( myFixture.getCaretOffset() );
548+
assertThat( reference ).isNotNull();
549+
assertThat( reference.resolve() ).isNull();
550+
}
551+
552+
public void testCarMapperReferenceProtectedSourceProperty() {
553+
myFixture.configureByFile( "CarMapperReferenceProtectedSourceProperty.java" );
554+
PsiReference reference = myFixture.getFile().findReferenceAt( myFixture.getCaretOffset() );
555+
assertThat( reference ).isNotNull();
556+
assertThat( reference.resolve() ).isNull();
557+
}
558+
545559
public void testCarMapperReferenceTargetPropertyInCarDtoWithBuilder() {
546560
myFixture.configureByFile( "CarMapperReferenceBuilderTargetProperty.java" );
547561
PsiElement reference = myFixture.getElementAtCaret();
@@ -748,6 +762,20 @@ public void testFluentCarMapperReferenceUnknownTargetProperty() {
748762
assertThat( reference.resolve() ).isNull();
749763
}
750764

765+
public void testCarMapperReferencePublicStaticFieldTargetProperty() {
766+
myFixture.configureByFile( "CarMapperReferencePublicStaticFieldTargetProperty.java" );
767+
PsiReference reference = myFixture.getFile().findReferenceAt( myFixture.getCaretOffset() );
768+
assertThat( reference ).isNotNull();
769+
assertThat( reference.resolve() ).isNull();
770+
}
771+
772+
public void testCarMapperReferenceProtectedTargetProperty() {
773+
myFixture.configureByFile( "CarMapperReferenceProtectedTargetProperty.java" );
774+
PsiReference reference = myFixture.getFile().findReferenceAt( myFixture.getCaretOffset() );
775+
assertThat( reference ).isNotNull();
776+
assertThat( reference.resolve() ).isNull();
777+
}
778+
751779
public void testReferenceCarMapperNoSourceClass() {
752780
myFixture.configureByFile( "CarMapperNoSourceClass.java" );
753781
PsiReference reference = myFixture.getFile().findReferenceAt( myFixture.getCaretOffset() );

testData/inspection/UnmappedTargetPropertiesData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public void setOnlyInSource(String onlyInSource) {
4848

4949
public static class Target {
5050

51+
public static final String EMPTY_STRING = "";
52+
5153
private String testName;
5254
private String matching;
5355
private String moreTarget;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.ap.test.complex;
7+
8+
import java.util.List;
9+
10+
import org.mapstruct.Mapper;
11+
import org.mapstruct.Mapping;
12+
import org.example.dto.CarDto;
13+
import org.example.dto.Car;
14+
15+
@Mapper
16+
public interface CarMapper {
17+
18+
@Mapping(source = "privateField<caret>", target = "seatCount")
19+
CarDto carToCarDto(Car car);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.ap.test.complex;
7+
8+
import java.util.List;
9+
10+
import org.mapstruct.Mapper;
11+
import org.mapstruct.Mapping;
12+
import org.example.dto.CarDto;
13+
import org.example.dto.Car;
14+
15+
@Mapper
16+
public interface CarMapper {
17+
18+
@Mapping(source = "numberOfSeats", target = "privateField<caret>")
19+
CarDto carToCarDto(Car car);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.ap.test.complex;
7+
8+
import java.util.List;
9+
10+
import org.mapstruct.Mapper;
11+
import org.mapstruct.Mapping;
12+
import org.example.dto.CarDto;
13+
import org.example.dto.CarPublic;
14+
15+
@Mapper
16+
public interface CarMapper {
17+
18+
@Mapping(source = "EMPTY_STRING<caret>", target = "seatCount")
19+
CarDtoPublic carToCarDto(CarPublic car);
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.ap.test.complex;
7+
8+
import java.util.List;
9+
10+
import org.mapstruct.Mapper;
11+
import org.mapstruct.Mapping;
12+
import org.example.dto.CarDtoPublic;
13+
import org.example.dto.Car;
14+
15+
@Mapper
16+
public interface CarMapper {
17+
18+
@Mapping(source = "numberOfSeats", target = "EMPTY_STRING<caret>")
19+
CarDtoPublic carToCarDto(Car car);
20+
}

0 commit comments

Comments
 (0)