-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inspection for mapping to
target = "."
without source (#175)
- Loading branch information
Showing
13 changed files
with
400 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...n/java/org/mapstruct/intellij/inspection/TargetThisMappingNoSourcePropertyInspection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.inspection; | ||
|
||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.psi.PsiAnnotation; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.mapstruct.intellij.MapStructBundle; | ||
|
||
/** | ||
* @author hduelme | ||
*/ | ||
public class TargetThisMappingNoSourcePropertyInspection extends MappingAnnotationInspectionBase { | ||
|
||
@Override | ||
void visitMappingAnnotation(@NotNull ProblemsHolder problemsHolder, @NotNull PsiAnnotation psiAnnotation, | ||
@NotNull MappingAnnotation mappingAnnotation) { | ||
if ( mappingAnnotation.isNotThisTarget() || mappingAnnotation.getIgnoreProperty() != null) { | ||
return; | ||
} | ||
if (mappingAnnotation.getSourceProperty() == null ) { | ||
problemsHolder.registerProblem( psiAnnotation, | ||
MapStructBundle.message( "inspection.this.target.mapping.no.source.property" ) ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/resources/inspectionDescriptions/TargetThisMappingNoSourcePropertyInspection.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<html> | ||
<body> | ||
<p> | ||
This inspection reports when <code> @Mapping( target = ".") </code> is used without a source property. | ||
</p> | ||
<p> | ||
<pre><code> | ||
//wrong | ||
@Mapper | ||
public interface EmployeeMapper { | ||
@Mapping(target = ".", expression = "java(company.getEmployee())" | ||
Employee toEmployee(Company company, @Context CycleAvoidingMappingContext context); | ||
} | ||
</code></pre> | ||
</p> | ||
<p> | ||
<pre><code> | ||
//correct | ||
@Mapper | ||
public interface EmployeeMapper { | ||
@Mapping(target = ".", source = "employee") | ||
Employee toEmployee(Company company, @Context CycleAvoidingMappingContext context); | ||
} | ||
</code></pre> | ||
</p> | ||
<!-- tooltip end --> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...va/org/mapstruct/intellij/inspection/TargetThisMappingNoSourcePropertyInspectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
package org.mapstruct.intellij.inspection; | ||
|
||
import com.intellij.codeInspection.LocalInspectionTool; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* @author hduelme | ||
*/ | ||
public class TargetThisMappingNoSourcePropertyInspectionTest extends BaseInspectionTest { | ||
@Override | ||
protected @NotNull Class<? extends LocalInspectionTool> getInspection() { | ||
return TargetThisMappingNoSourcePropertyInspection.class; | ||
} | ||
|
||
public void testTargetThisMappingNoSourceProperty() { | ||
doTest(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
testData/inspection/MoreThanOneSourceConstantAndSourceThisMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.Mappings; | ||
|
||
class Source { | ||
|
||
private Target innerTarget; | ||
|
||
public Target getInnerTarget() { | ||
return innerTarget; | ||
} | ||
|
||
public void setInnerTarget(Target innerTarget) { | ||
this.innerTarget = innerTarget; | ||
} | ||
} | ||
|
||
class Target { | ||
|
||
private String testName; | ||
|
||
public String getTestName() { | ||
return testName; | ||
} | ||
|
||
public void setTestName(String testName) { | ||
this.testName = testName; | ||
} | ||
} | ||
|
||
@Mapper | ||
interface SingleMappingMapper { | ||
|
||
<error descr="More than one source property defined">@Mapping(target = ".", source = "innerTarget", constant = "My name")</error> | ||
Target map(Source source); | ||
} | ||
|
||
@Mapper | ||
interface SingleMappingsMapper { | ||
|
||
@Mappings({ | ||
<error descr="More than one source property defined">@Mapping(target = ".", source = "innerTarget", constant = "My name")</error> | ||
}) | ||
Target map(Source source); | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
testData/inspection/MoreThanOneSourceConstantAndSourceThisMapping_after.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.Mappings; | ||
|
||
class Source { | ||
|
||
private Target innerTarget; | ||
|
||
public Target getInnerTarget() { | ||
return innerTarget; | ||
} | ||
|
||
public void setInnerTarget(Target innerTarget) { | ||
this.innerTarget = innerTarget; | ||
} | ||
} | ||
|
||
class Target { | ||
|
||
private String testName; | ||
|
||
public String getTestName() { | ||
return testName; | ||
} | ||
|
||
public void setTestName(String testName) { | ||
this.testName = testName; | ||
} | ||
} | ||
|
||
@Mapper | ||
interface SingleMappingMapper { | ||
|
||
@Mapping(target = ".", source = "innerTarget") | ||
Target map(Source source); | ||
} | ||
|
||
@Mapper | ||
interface SingleMappingsMapper { | ||
|
||
@Mappings({ | ||
@Mapping(target = ".", source = "innerTarget") | ||
}) | ||
Target map(Source source); | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
testData/inspection/MoreThanOneSourceExpressionAndSourceThisMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright MapStruct Authors. | ||
* | ||
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.Mappings; | ||
|
||
class Source { | ||
|
||
private Target innerTarget; | ||
|
||
public Target getInnerTarget() { | ||
return innerTarget; | ||
} | ||
|
||
public void setInnerTarget(Target innerTarget) { | ||
this.innerTarget = innerTarget; | ||
} | ||
} | ||
|
||
class Target { | ||
|
||
private String testName; | ||
|
||
public String getTestName() { | ||
return testName; | ||
} | ||
|
||
public void setTestName(String testName) { | ||
this.testName = testName; | ||
} | ||
} | ||
|
||
@Mapper | ||
interface SingleMappingMapper { | ||
|
||
<error descr="More than one source property defined">@Mapping(target = ".", source = "innerTarget", expression = "java()")</error> | ||
Target map(Source source); | ||
} | ||
|
||
@Mapper | ||
interface SingleMappingsMapper { | ||
|
||
@Mappings({ | ||
<error descr="More than one source property defined">@Mapping(target = ".", source = "innerTarget", expression = "java()")</error> | ||
}) | ||
Target map(Source source); | ||
} | ||
|
Oops, something went wrong.