Skip to content

Commit 9ec6b98

Browse files
committed
#218: Do not mark String source / target in ValueMapping as error
1 parent 4aab074 commit 9ec6b98

File tree

6 files changed

+87
-52
lines changed

6 files changed

+87
-52
lines changed

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

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
package org.mapstruct.intellij.codeinsight.references;
77

88
import com.intellij.codeInsight.lookup.LookupElement;
9+
import com.intellij.psi.PsiClass;
910
import com.intellij.psi.PsiElement;
11+
import com.intellij.psi.PsiEnumConstant;
12+
import com.intellij.psi.PsiField;
1013
import com.intellij.psi.PsiMethod;
1114
import org.jetbrains.annotations.NotNull;
1215
import org.jetbrains.annotations.Nullable;
@@ -16,7 +19,7 @@
1619
*
1720
* @author Filip Hrisafov
1821
*/
19-
abstract class BaseValueMappingReference extends BaseReference {
22+
public abstract class BaseValueMappingReference extends BaseReference {
2023

2124
/**
2225
* @param element the element for which a reference should be found
@@ -33,29 +36,77 @@ public final PsiElement resolve() {
3336
return null;
3437
}
3538

36-
PsiMethod mappingMethod = getMappingMethod();
39+
PsiClass enumClass = getEnumClass();
40+
if ( enumClass == null ) {
41+
return null;
42+
}
43+
44+
return resolveInternal( value, enumClass );
45+
}
46+
47+
@Override
48+
@Nullable
49+
PsiMethod getMappingMethod() {
50+
PsiMethod mappingMethod = super.getMappingMethod();
3751
if ( isNotValueMapping( mappingMethod ) ) {
3852
return null;
3953
}
54+
return mappingMethod;
55+
}
56+
57+
public PsiClass getEnumClass() {
58+
PsiMethod mappingMethod = getMappingMethod();
59+
if ( mappingMethod == null ) {
60+
return null;
61+
}
62+
63+
PsiClass enumClass = getEnumClass( mappingMethod );
64+
65+
if ( enumClass == null || !enumClass.isEnum() ) {
66+
return null;
67+
}
4068

41-
return resolveInternal( value, mappingMethod );
69+
return enumClass;
4270
}
4371

4472
@Nullable
45-
abstract PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMethod);
73+
PsiElement resolveInternal(@NotNull String value, @NotNull PsiClass enumClass) {
74+
PsiField field = enumClass.findFieldByName( value, false );
75+
76+
if ( field instanceof PsiEnumConstant ) {
77+
return field;
78+
}
79+
80+
return null;
81+
}
82+
83+
PsiClass getEnumClass(@NotNull PsiMethod mappingMethod) {
84+
PsiClass enumClass = determineEnumClass( mappingMethod );
85+
86+
if ( enumClass == null || !enumClass.isEnum() ) {
87+
return null;
88+
}
89+
return enumClass;
90+
}
91+
92+
abstract PsiClass determineEnumClass(@NotNull PsiMethod mappingMethod);
4693

4794
@NotNull
4895
@Override
4996
public final Object[] getVariants() {
5097
PsiMethod mappingMethod = getMappingMethod();
51-
if ( isNotValueMapping( mappingMethod ) ) {
98+
if ( mappingMethod == null ) {
99+
return LookupElement.EMPTY_ARRAY;
100+
}
101+
PsiClass enumClass = getEnumClass( mappingMethod );
102+
if ( enumClass == null ) {
52103
return LookupElement.EMPTY_ARRAY;
53104
}
54-
return getVariantsInternal( mappingMethod );
105+
return getVariantsInternal( mappingMethod, enumClass );
55106
}
56107

57108
@NotNull
58-
abstract Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod);
109+
abstract Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod, @NotNull PsiClass enumClass);
59110

60111
private static boolean isNotValueMapping(@Nullable PsiMethod mappingMethod) {
61112
return mappingMethod == null || mappingMethod.getParameterList().getParametersCount() != 1;

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import com.intellij.psi.PsiClass;
1414
import com.intellij.psi.PsiElement;
1515
import com.intellij.psi.PsiEnumConstant;
16-
import com.intellij.psi.PsiField;
1716
import com.intellij.psi.PsiMethod;
1817
import com.intellij.psi.PsiReference;
1918
import org.jetbrains.annotations.NotNull;
20-
import org.jetbrains.annotations.Nullable;
2119
import org.mapstruct.intellij.util.MapstructUtil;
2220
import org.mapstruct.intellij.util.ValueMappingUtils;
2321

@@ -37,31 +35,14 @@ private ValueMappingSourceReference(@NotNull PsiElement element) {
3735
super( element );
3836
}
3937

40-
@Nullable
4138
@Override
42-
PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMethod) {
43-
//TODO should we resolve and suggest MappingConstants as well?
44-
PsiClass sourceClass = getParameterClass( mappingMethod.getParameterList().getParameters()[0] );
45-
if ( sourceClass == null || !sourceClass.isEnum() ) {
46-
return null;
47-
}
48-
PsiField field = sourceClass.findFieldByName( value, false );
49-
50-
if ( field instanceof PsiEnumConstant ) {
51-
return field;
52-
}
53-
54-
return null;
39+
PsiClass determineEnumClass(@NotNull PsiMethod mappingMethod) {
40+
return getParameterClass( mappingMethod.getParameterList().getParameters()[0] );
5541
}
5642

5743
@NotNull
5844
@Override
59-
Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) {
60-
PsiClass sourceClass = getParameterClass( mappingMethod.getParameterList().getParameters()[0] );
61-
if ( sourceClass == null || !sourceClass.isEnum() ) {
62-
return LookupElement.EMPTY_ARRAY;
63-
}
64-
45+
Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod, @NotNull PsiClass sourceClass) {
6546
Set<String> alreadyDefinedValues = ValueMappingUtils.findAllDefinedValueMappingSources( mappingMethod )
6647
.collect( Collectors.toSet() );
6748

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.intellij.psi.PsiClass;
1212
import com.intellij.psi.PsiElement;
1313
import com.intellij.psi.PsiEnumConstant;
14-
import com.intellij.psi.PsiField;
1514
import com.intellij.psi.PsiMethod;
1615
import com.intellij.psi.PsiReference;
1716
import com.intellij.psi.util.PsiUtil;
@@ -34,31 +33,14 @@ private ValueMappingTargetReference(@NotNull PsiElement element) {
3433
super( element );
3534
}
3635

37-
@Nullable
3836
@Override
39-
PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMethod) {
40-
//TODO should we resolve and suggest MappingConstants as well?
41-
PsiClass targetClass = methodReturnClass( mappingMethod );
42-
if ( targetClass == null || !targetClass.isEnum() ) {
43-
return null;
44-
}
45-
PsiField field = targetClass.findFieldByName( value, false );
46-
47-
if ( field instanceof PsiEnumConstant ) {
48-
return field;
49-
}
50-
51-
return null;
37+
PsiClass determineEnumClass(@NotNull PsiMethod mappingMethod) {
38+
return methodReturnClass( mappingMethod );
5239
}
5340

5441
@NotNull
5542
@Override
56-
Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod) {
57-
PsiClass targetClass = methodReturnClass( mappingMethod );
58-
if ( targetClass == null || !targetClass.isEnum() ) {
59-
return LookupElement.EMPTY_ARRAY;
60-
}
61-
43+
Object[] getVariantsInternal(@NotNull PsiMethod mappingMethod, @NotNull PsiClass targetClass) {
6244
return Stream.of( targetClass.getFields() )
6345
.filter( psiField -> psiField instanceof PsiEnumConstant )
6446
.map( psiEnumConstant -> asLookup( (PsiEnumConstant) psiEnumConstant ) )

src/main/java/org/mapstruct/intellij/inspection/MapstructReferenceInspection.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.intellij.psi.PsiReference;
1616
import org.jetbrains.annotations.NotNull;
1717
import org.mapstruct.intellij.codeinsight.references.BaseReference;
18+
import org.mapstruct.intellij.codeinsight.references.BaseValueMappingReference;
1819

1920
/**
2021
* Inspection that checks if mapstruct references can be resolved.
@@ -43,20 +44,27 @@ private MapstructReferenceVisitor(ProblemsHolder holder) {
4344
public void visitElement(@NotNull PsiElement element) {
4445
if (element instanceof ContributedReferenceHost r && element instanceof PsiLanguageInjectionHost) {
4546
for (PsiReference psiReference : r.getReferences()) {
46-
if (psiReference instanceof BaseReference && psiReference.resolve() == null) {
47+
if ( psiReference instanceof BaseReference baseReference && psiReference.resolve() == null ) {
4748
TextRange range = psiReference.getRangeInElement();
4849
if (range.isEmpty() && range.getStartOffset() == 1 && "\"\"".equals( element.getText() ) ) {
4950
String message = ProblemsHolder.unresolvedReferenceMessage( psiReference );
5051
holder.registerProblem( element, message, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL,
5152
TextRange.create( 0, 2 ) );
5253
}
53-
else {
54+
else if ( shouldRegisterProblem( baseReference ) ) {
5455
holder.registerProblem( psiReference );
5556
}
5657
}
5758
}
5859
}
5960
super.visitElement( element );
6061
}
62+
63+
private boolean shouldRegisterProblem(BaseReference reference) {
64+
if ( reference instanceof BaseValueMappingReference valueMappingReference ) {
65+
return valueMappingReference.getEnumClass() != null;
66+
}
67+
return true;
68+
}
6169
}
6270
}

testData/inspection/UnknownValueMappingSourceReference.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ interface SingleValueMappingsMapper {
3636
Target map(Source source);
3737
}
3838

39+
@Mapper
40+
interface StringToEnumMapper {
41+
42+
@ValueMapping(target = "FIST", source = "OTHER")
43+
Target map(String source);
44+
}
45+

testData/inspection/UnknownValueMappingTargetReference.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ interface SingleValueMappingsMapper {
3636
Target map(Source source);
3737
}
3838

39+
@Mapper
40+
interface EnumToStringMapper {
41+
42+
@ValueMapping(target = "OTHER", source = "FIST")
43+
String map(Source source);
44+
}

0 commit comments

Comments
 (0)