Skip to content

Commit db7e0dc

Browse files
committed
Support BeanMapping#ignoreByDefault for the unmapped target properties inspection
Fixes #54
1 parent 0a9440f commit db7e0dc

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
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.2</h2>
33
<ul>
44
<li>Support for mapping nested bean properties to current target</li>
5+
<li>Support <code>@BeanMapping#ignoreByDefault</code> for the unmapped target properties inspection</li>
56
<li>Bug fix: ClassCastException in language injection in expressions</li>
67
</ul>
78
<h2>1.2.1</h2>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.mapstruct.intellij.util.MapstructUtil;
3838
import org.mapstruct.intellij.util.TargetUtils;
3939

40+
import static com.intellij.codeInsight.AnnotationUtil.findAnnotation;
41+
import static com.intellij.codeInsight.AnnotationUtil.getBooleanAttributeValue;
4042
import static org.mapstruct.intellij.util.MapstructAnnotationUtils.addMappingAnnotation;
4143
import static org.mapstruct.intellij.util.MapstructUtil.isInheritInverseConfiguration;
4244
import static org.mapstruct.intellij.util.MapstructUtil.isMapper;
@@ -75,6 +77,10 @@ public void visitMethod(PsiMethod method) {
7577
return;
7678
}
7779

80+
if ( isBeanMappingIgnoreByDefault( method ) ) {
81+
return;
82+
}
83+
7884
Set<String> allTargetProperties = findAllTargetProperties( targetType, mapStructVersion );
7985

8086
// find and remove all defined mapping targets
@@ -134,6 +140,18 @@ public void visitMethod(PsiMethod method) {
134140
}
135141
}
136142

143+
private static boolean isBeanMappingIgnoreByDefault(PsiMethod method) {
144+
PsiAnnotation beanMapping = findAnnotation( method, true, MapstructUtil.BEAN_MAPPING_FQN );
145+
if ( beanMapping != null ) {
146+
Boolean ignoreByDefault = getBooleanAttributeValue( beanMapping, "ignoreByDefault" );
147+
if ( ignoreByDefault != null ) {
148+
return ignoreByDefault;
149+
}
150+
}
151+
152+
return false;
153+
}
154+
137155
/**
138156
* @param method the method to be used
139157
*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.jetbrains.annotations.NonNls;
4747
import org.jetbrains.annotations.NotNull;
4848
import org.jetbrains.annotations.Nullable;
49+
import org.mapstruct.BeanMapping;
4950
import org.mapstruct.InheritInverseConfiguration;
5051
import org.mapstruct.Mapper;
5152
import org.mapstruct.MapperConfig;
@@ -78,6 +79,8 @@ public final class MapstructUtil {
7879
public static final String MAPPING_ANNOTATION_FQN = Mapping.class.getName();
7980
public static final String MAPPERS_FQN = Mappers.class.getName();
8081

82+
public static final String BEAN_MAPPING_FQN = BeanMapping.class.getName();
83+
8184
static final String MAPPINGS_ANNOTATION_FQN = Mappings.class.getName();
8285
static final String VALUE_MAPPING_ANNOTATION_FQN = ValueMapping.class.getName();
8386
static final String VALUE_MAPPINGS_ANNOTATION_FQN = ValueMappings.class.getName();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.inspection;
7+
8+
import java.util.List;
9+
10+
import com.intellij.codeInsight.intention.IntentionAction;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
/**
16+
* @author Filip Hrisafov
17+
*/
18+
public class UnmappedTargetPropertiesWithBeanMappingIgnoreByDefaultInspectionTest extends BaseInspectionTest {
19+
20+
@NotNull
21+
@Override
22+
protected Class<UnmappedTargetPropertiesInspection> getInspection() {
23+
return UnmappedTargetPropertiesInspection.class;
24+
}
25+
26+
@Override
27+
protected void setUp() throws Exception {
28+
super.setUp();
29+
myFixture.copyFileToProject(
30+
"UnmappedTargetPropertiesData.java",
31+
"org/example/data/UnmappedTargetPropertiesData.java"
32+
);
33+
}
34+
35+
public void testUnmappedTargetPropertiesWithBeanMappingIgnoreByDefault() {
36+
doTest();
37+
String testName = getTestName( false );
38+
List<IntentionAction> allQuickFixes = myFixture.getAllQuickFixes();
39+
40+
assertThat( allQuickFixes )
41+
.extracting( IntentionAction::getText )
42+
.as( "Intent Text" )
43+
.containsExactly(
44+
// For SingleMappingMapper
45+
"Ignore unmapped target property: 'moreTarget'",
46+
"Add unmapped target property: 'moreTarget'",
47+
48+
// For UpdateMapper
49+
"Ignore unmapped target property: 'testName'",
50+
"Add unmapped target property: 'testName'"
51+
);
52+
53+
allQuickFixes.forEach( myFixture::launchAction );
54+
myFixture.checkResultByFile( testName + "_after.java" );
55+
}
56+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.BeanMapping;
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
import org.mapstruct.MappingTarget;
11+
import org.example.data.UnmappedTargetPropertiesData.Target;
12+
import org.example.data.UnmappedTargetPropertiesData.Source;
13+
14+
@Mapper
15+
interface SingleMappingMapper {
16+
17+
@Mapping(target = "testName", source = "name")
18+
Target <warning descr="Unmapped target property: moreTarget">map</warning>(Source source);
19+
}
20+
21+
@Mapper
22+
interface SingleMappingMapperWithIgnoreByDefault {
23+
24+
@Mapping(target = "testName", source = "name")
25+
@BeanMapping(ignoreByDefault = true)
26+
Target map(Source source);
27+
}
28+
29+
@Mapper
30+
interface UpdateMapper {
31+
32+
@Mapping(target = "moreTarget", source = "moreSource")
33+
void <warning descr="Unmapped target property: testName">update</warning>(@MappingTarget Target target, Source source);
34+
}
35+
36+
@Mapper
37+
interface UpdateMapperWithIgnoreByDefault {
38+
39+
@Mapping(target = "moreTarget", source = "moreSource")
40+
@BeanMapping(ignoreByDefault = true)
41+
void update(@MappingTarget Target target, Source source);
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.BeanMapping;
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
import org.mapstruct.MappingTarget;
11+
import org.example.data.UnmappedTargetPropertiesData.Target;
12+
import org.example.data.UnmappedTargetPropertiesData.Source;
13+
14+
@Mapper
15+
interface SingleMappingMapper {
16+
17+
@Mapping(target = "moreTarget", source = "")
18+
@Mapping(target = "moreTarget", ignore = true)
19+
@Mapping(target = "testName", source = "name")
20+
Target map(Source source);
21+
}
22+
23+
@Mapper
24+
interface SingleMappingMapperWithIgnoreByDefault {
25+
26+
@Mapping(target = "testName", source = "name")
27+
@BeanMapping(ignoreByDefault = true)
28+
Target map(Source source);
29+
}
30+
31+
@Mapper
32+
interface UpdateMapper {
33+
34+
@Mapping(target = "testName", source = "")
35+
@Mapping(target = "testName", ignore = true)
36+
@Mapping(target = "moreTarget", source = "moreSource")
37+
void update(@MappingTarget Target target, Source source);
38+
}
39+
40+
@Mapper
41+
interface UpdateMapperWithIgnoreByDefault {
42+
43+
@Mapping(target = "moreTarget", source = "moreSource")
44+
@BeanMapping(ignoreByDefault = true)
45+
void update(@MappingTarget Target target, Source source);
46+
}

0 commit comments

Comments
 (0)