Skip to content

Commit 9eb646c

Browse files
committed
Suppress redundant default parameter value assignment warning
The MapStruct annotation processor makes a difference between a parameter annotation value with a user defined value and the default value. For `Mapping#constant` and `Mapping#defaultValue` an empty value means that MapStruct should use that empty value in the generated code. With this commit we are using a new extension point added in 2021.2 to ignore IntelliJ warning for the specific annotation parameters Closes #93
1 parent 38f3104 commit 9eb646c

File tree

7 files changed

+111
-3
lines changed

7 files changed

+111
-3
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
strategy:
88
fail-fast: false
99
matrix:
10-
ideaVersion: [2020.1, 2020.2, 2020.3, 2021.1, 2021.2, LATEST-EAP-SNAPSHOT]
10+
ideaVersion: [2021.2, 2021.3, LATEST-EAP-SNAPSHOT]
1111
name: 'IDEA ${{ matrix.ideaVersion }}'
1212
env:
1313
IDEA_VERSION: ${{ matrix.ideaVersion }}

change-notes.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<html lang="en">
2+
<h2>1.4.0</h2>
3+
<ul>
4+
<li>Suppress redundant default parameter value assignment warning for <code>Mapping#constant</code> and <code>Mapping#defaultValue</code></li>
5+
</ul>
26
<h2>1.3.1</h2>
37
<ul>
48
<li>Improve language injections, especially for generics</li>

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# https://www.jetbrains.com/intellij-repository/releases
33
# https://www.jetbrains.com/intellij-repository/snapshots
44

5-
ideaVersion = 2020.1
5+
ideaVersion = 2021.2
66
ideaType = IC
77
sources = true
88
isEAP = false
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 com.intellij.codeInspection.DefaultAnnotationParamInspection;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
11+
import org.mapstruct.intellij.util.MapstructUtil;
12+
13+
/**
14+
* @author Filip Hrisafov
15+
*/
16+
public class MapStructDefaultAnnotationParamSupport
17+
implements DefaultAnnotationParamInspection.IgnoreAnnotationParamSupport {
18+
19+
@Override
20+
public boolean ignoreAnnotationParam(@Nullable String annotationFQN, @NotNull String annotationParameterName) {
21+
if ( MapstructUtil.MAPPING_ANNOTATION_FQN.equals( annotationFQN ) ) {
22+
return annotationParameterName.equals( "constant" ) || annotationParameterName.equals( "defaultValue" );
23+
}
24+
25+
return false;
26+
}
27+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<vendor url="http://www.mapstruct.org">MapStruct</vendor>
2525

2626
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
27-
<idea-version since-build="201"/>
27+
<idea-version since-build="212"/>
2828

2929
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
3030
on how to target different products -->
@@ -39,6 +39,7 @@
3939
<methodReferencesSearch implementation="org.mapstruct.intellij.search.MappingMethodUsagesSearcher" />
4040
<renameHandler implementation="org.mapstruct.intellij.rename.MapstructSourceTargetParameterRenameHandler"/>
4141
<multiHostInjector implementation="org.mapstruct.intellij.expression.JavaExpressionInjector"/>
42+
<lang.jvm.ignoreAnnotationParamSupport implementation="org.mapstruct.intellij.inspection.MapStructDefaultAnnotationParamSupport"/>
4243

4344
<projectConfigurable groupId="language"
4445
id="preferences.language.MapStruct"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 com.intellij.codeInspection.DefaultAnnotationParamInspection;
9+
import com.intellij.codeInspection.LocalInspectionTool;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
/**
13+
* @author Filip Hrisafov
14+
*/
15+
public class MapStructDefaultAnnotationParamSupportTest extends BaseInspectionTest {
16+
17+
@NotNull
18+
@Override
19+
protected Class<? extends LocalInspectionTool> getInspection() {
20+
return DefaultAnnotationParamInspection.class;
21+
}
22+
23+
@Override
24+
protected void setUp() throws Exception {
25+
super.setUp();
26+
myFixture.copyFileToProject(
27+
"UnmappedConstructorTargetPropertiesData.java",
28+
"org/example/data/UnmappedConstructorTargetPropertiesData.java"
29+
);
30+
}
31+
32+
public void testIgnoreRedundantDefaultParameterValueInMapping() {
33+
doTest();
34+
}
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.Mapper;
8+
import org.mapstruct.Mapping;
9+
import org.example.data.UnmappedFluentTargetPropertiesData.Target;
10+
import org.example.data.UnmappedFluentTargetPropertiesData.Source;
11+
12+
@Mapper
13+
interface MyMapper {
14+
15+
@Mapping(target = "name", constant = "")
16+
@Mapping(target = "name", defaultValue = "")
17+
@Mapping(target = "age", ignore = <warning descr="Redundant default parameter value assignment">false</warning>)
18+
Target map(String source);
19+
20+
class Target {
21+
22+
private String name;
23+
private String age;
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getAge() {
34+
return age;
35+
}
36+
37+
public void setAge(String age) {
38+
this.age = age;
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)