Skip to content

Commit 08d758c

Browse files
committed
#243: Do not warn for unmapped source properties on maps
1 parent 9729dd9 commit 08d758c

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public abstract class BaseReference extends PsiReferenceBase<PsiElement> impleme
4343
* @return The mapping method that this reference belongs to
4444
*/
4545
@Nullable
46-
PsiMethod getMappingMethod() {
46+
public PsiMethod getMappingMethod() {
4747
PsiElement element = getElement();
4848
UExpression expression = UastContextKt.toUElement( element, UExpression.class );
4949
if ( expression != null ) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final PsiElement resolve() {
4747

4848
@Override
4949
@Nullable
50-
PsiMethod getMappingMethod() {
50+
public PsiMethod getMappingMethod() {
5151
PsiMethod mappingMethod = super.getMappingMethod();
5252
if ( isNotValueMapping( mappingMethod ) ) {
5353
return null;

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
import com.intellij.codeInspection.ProblemHighlightType;
99
import com.intellij.codeInspection.ProblemsHolder;
1010
import com.intellij.openapi.util.TextRange;
11+
import com.intellij.psi.CommonClassNames;
1112
import com.intellij.psi.ContributedReferenceHost;
1213
import com.intellij.psi.PsiClass;
1314
import com.intellij.psi.PsiElement;
1415
import com.intellij.psi.PsiElementVisitor;
1516
import com.intellij.psi.PsiLanguageInjectionHost;
17+
import com.intellij.psi.PsiMethod;
18+
import com.intellij.psi.PsiParameter;
1619
import com.intellij.psi.PsiReference;
20+
import com.intellij.psi.PsiType;
1721
import com.intellij.psi.util.PsiTreeUtil;
22+
import com.intellij.psi.util.PsiUtil;
1823
import org.jetbrains.annotations.NotNull;
24+
import org.jetbrains.annotations.Nullable;
1925
import org.mapstruct.intellij.codeinsight.references.BaseReference;
2026
import org.mapstruct.intellij.codeinsight.references.BaseValueMappingReference;
2127

@@ -67,9 +73,35 @@ private boolean shouldRegisterProblem(BaseReference reference) {
6773
return valueMappingReference.getEnumClass() != null;
6874
}
6975

76+
if ( singleSourceParameterIsOfTypeMap( reference.getMappingMethod() ) ) {
77+
return false;
78+
}
79+
7080
return !containingClassIsAnnotationType( reference.getElement() );
7181
}
7282

83+
private boolean singleSourceParameterIsOfTypeMap(@Nullable PsiMethod mappingMethod) {
84+
85+
if (mappingMethod != null) {
86+
PsiParameter[] parameters = mappingMethod.getParameterList().getParameters();
87+
if (parameters.length > 0) {
88+
PsiType parameterType = parameters[0].getType();
89+
return isMapType( parameterType );
90+
}
91+
}
92+
93+
return false;
94+
}
95+
96+
private boolean isMapType(PsiType type) {
97+
PsiClass psiClass = PsiUtil.resolveClassInType(type);
98+
if (psiClass == null) {
99+
return false;
100+
}
101+
return CommonClassNames.JAVA_UTIL_MAP.equals(psiClass.getQualifiedName());
102+
}
103+
104+
73105
private boolean containingClassIsAnnotationType(PsiElement element) {
74106

75107
PsiClass containingClass = PsiTreeUtil.getParentOfType( element, PsiClass.class );
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.intellij.bugs._243;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
import org.mapstruct.intellij.inspection.BaseInspectionTest;
10+
import org.mapstruct.intellij.inspection.MapstructReferenceInspection;
11+
12+
/**
13+
* @author Oliver Erhart
14+
*/
15+
public class DisableSourcePropertyInspectionOnMapTest extends BaseInspectionTest {
16+
17+
@Override
18+
protected String getTestDataPath() {
19+
return "testData/bugs/_243";
20+
}
21+
22+
@NotNull
23+
@Override
24+
protected Class<MapstructReferenceInspection> getInspection() {
25+
return MapstructReferenceInspection.class;
26+
}
27+
28+
public void testDisableSourcePropertyInspectionOnMapTest() {
29+
doTest();
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
import java.time.LocalDate;
8+
import java.util.Map;
9+
10+
import org.mapstruct.Mapper;
11+
import org.mapstruct.Mapping;
12+
13+
@Mapper
14+
abstract class Issue243Mapper {
15+
16+
@Mapping(source = "exDate", target = "exDate", dateFormat = "yyyy-MM-dd")
17+
@Mapping(source = "payDate", target = "payDate", dateFormat = "yyyy-MM-dd")
18+
@Mapping(source = "recordDate", target = "recordDate", dateFormat = "yyyy-MM-dd")
19+
@Mapping(source = "annDate", target = "annDate", dateFormat = "yyyy-MM-dd")
20+
public abstract CorporateAction toCorporateAction(Map<String, String> rowValues);
21+
}
22+
23+
class CorporateAction {
24+
public LocalDate exDate;
25+
public LocalDate payDate;
26+
public LocalDate recordDate;
27+
public LocalDate annDate;
28+
}

0 commit comments

Comments
 (0)