Skip to content

Commit a99bfac

Browse files
authored
#194 do not apply source not found inspection when source and target property match
1 parent 4b48a62 commit a99bfac

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import com.intellij.psi.PsiElement;
1212
import com.intellij.psi.PsiLiteralExpression;
1313
import com.intellij.psi.PsiMethod;
14+
import com.intellij.psi.PsiNameValuePair;
1415
import com.intellij.psi.impl.source.tree.java.PsiAnnotationParamListImpl;
1516
import org.jetbrains.annotations.NotNull;
1617
import org.jetbrains.annotations.Nullable;
1718
import org.mapstruct.intellij.MapStructBundle;
1819
import org.mapstruct.intellij.util.MapstructUtil;
20+
import org.mapstruct.intellij.util.SourceUtils;
1921

2022
/**
2123
* Inspection that checks if inside a @Mapping at least one source property is defined
@@ -27,17 +29,26 @@ public class NoSourcePropertyDefinedInspection extends MappingAnnotationInspecti
2729
@Override
2830
void visitMappingAnnotation( @NotNull ProblemsHolder problemsHolder, @NotNull PsiAnnotation psiAnnotation,
2931
@NotNull MappingAnnotation mappingAnnotation ) {
30-
if (mappingAnnotation.hasNoSourceProperties() && !isIgnoreByDefaultEnabled( psiAnnotation ) ) {
32+
PsiNameValuePair targetProperty = mappingAnnotation.getTargetProperty();
33+
PsiMethod annotatedMethod = getAnnotatedMethod( psiAnnotation );
34+
if (targetProperty != null && annotatedMethod != null && mappingAnnotation.hasNoSourceProperties()
35+
&& !isIgnoreByDefaultEnabled( annotatedMethod ) &&
36+
!hasMatchingSourceProperty( annotatedMethod, targetProperty ) ) {
3137
problemsHolder.registerProblem( psiAnnotation,
3238
MapStructBundle.message( "inspection.no.source.property" ) );
3339
}
3440
}
3541

36-
private static boolean isIgnoreByDefaultEnabled(@NotNull PsiAnnotation psiAnnotation) {
37-
PsiMethod annotatedMethod = getAnnotatedMethod( psiAnnotation );
38-
if (annotatedMethod == null) {
42+
private boolean hasMatchingSourceProperty( @NotNull PsiMethod annotatedMethod,
43+
@NotNull PsiNameValuePair targetProperty ) {
44+
String targetValue = targetProperty.getLiteralValue();
45+
if (targetValue == null) {
3946
return false;
4047
}
48+
return SourceUtils.findAllSourceProperties( annotatedMethod ).contains( targetValue );
49+
}
50+
51+
private static boolean isIgnoreByDefaultEnabled( @NotNull PsiMethod annotatedMethod ) {
4152
PsiAnnotation beanMappingAnnotation = annotatedMethod.getAnnotation( MapstructUtil.BEAN_MAPPING_FQN );
4253
if (beanMappingAnnotation == null) {
4354
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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._194;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
import org.mapstruct.intellij.inspection.BaseInspectionTest;
10+
import org.mapstruct.intellij.inspection.NoSourcePropertyDefinedInspection;
11+
12+
/**
13+
* @author hduelme
14+
*/
15+
public class NoSourcePropertyDefinedSourcePropertyWithSameNameExistsInspectionTest extends BaseInspectionTest {
16+
17+
@Override
18+
protected @NotNull Class<NoSourcePropertyDefinedInspection> getInspection() {
19+
return NoSourcePropertyDefinedInspection.class;
20+
}
21+
22+
@Override
23+
protected String getTestDataPath() {
24+
return "testData/bugs/_194";
25+
}
26+
27+
public void testNoSourcePropertyDefinedSourcePropertyWithSameNameExists() {
28+
doTest();
29+
}
30+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 org.mapstruct.Mapper;
8+
import org.mapstruct.Mapping;
9+
import org.mapstruct.Mappings;
10+
11+
class Source {
12+
13+
private String testName;
14+
15+
public String getTestName() {
16+
return testName;
17+
}
18+
19+
public void setTestName(String testName) {
20+
this.testName = testName;
21+
}
22+
}
23+
24+
class Target {
25+
26+
private String testName;
27+
28+
public String getTestName() {
29+
return testName;
30+
}
31+
32+
public void setTestName(String testName) {
33+
this.testName = testName;
34+
}
35+
}
36+
37+
@Mapper
38+
interface SingleMappingMapper {
39+
40+
@Mapping(target = "testName")
41+
Target map(Source source);
42+
}
43+
44+
@Mapper
45+
interface SingleMappingsMapper {
46+
47+
@Mappings({
48+
@Mapping(target = "testName")
49+
})
50+
Target map(Source source);
51+
}
52+

0 commit comments

Comments
 (0)