-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
384 additions
and
47 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
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
105 changes: 105 additions & 0 deletions
105
src/main/java/org/mapstruct/intellij/inspection/ThisUsedAsSourcePropertyInspection.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,105 @@ | ||
/* | ||
* 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.LocalQuickFix; | ||
import com.intellij.codeInspection.LocalQuickFixOnPsiElement; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.codeInspection.util.IntentionFamilyName; | ||
import com.intellij.codeInspection.util.IntentionName; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiAnnotation; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiMethod; | ||
import com.intellij.psi.PsiNameValuePair; | ||
import com.intellij.psi.PsiParameter; | ||
import com.intellij.psi.impl.source.tree.java.PsiAnnotationParamListImpl; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.mapstruct.intellij.MapStructBundle; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.intellij.psi.PsiElementFactory.getInstance; | ||
import static org.mapstruct.intellij.util.MapstructAnnotationUtils.getAnnotatedMethod; | ||
import static org.mapstruct.intellij.util.MapstructUtil.getSourceParameters; | ||
|
||
/** | ||
* @author hduelme | ||
*/ | ||
public class ThisUsedAsSourcePropertyInspection extends MappingAnnotationInspectionBase { | ||
@Override | ||
void visitMappingAnnotation(@NotNull ProblemsHolder problemsHolder, @NotNull PsiAnnotation psiAnnotation, | ||
@NotNull MappingAnnotation mappingAnnotation) { | ||
PsiNameValuePair sourceProperty = mappingAnnotation.getSourceProperty(); | ||
if (sourceProperty == null || sourceProperty.getValue() == null) { | ||
return; | ||
} | ||
if ( !".".equals( sourceProperty.getLiteralValue() ) ) { | ||
return; | ||
} | ||
List<LocalQuickFix> fixes = new ArrayList<>(); | ||
PsiMethod annotatedMethod = getAnnotatedMethod( psiAnnotation ); | ||
if (annotatedMethod != null) { | ||
for (PsiParameter sourceParameter : getSourceParameters( annotatedMethod )) { | ||
fixes.add( new ReplaceSourceParameterValueQuickFix(sourceProperty, sourceParameter.getName() ) ); | ||
} | ||
} | ||
problemsHolder.registerProblem( sourceProperty.getValue(), | ||
MapStructBundle.message( "inspection.source.property.this.used" ), | ||
fixes.toArray( new LocalQuickFix[0] ) ); | ||
} | ||
|
||
private static class ReplaceSourceParameterValueQuickFix extends LocalQuickFixOnPsiElement { | ||
|
||
private final String targetMethodeParameterName; | ||
private final String text; | ||
private final String family; | ||
|
||
private ReplaceSourceParameterValueQuickFix(@NotNull PsiNameValuePair element, | ||
@NotNull String targetMethodeParameterName) { | ||
super( element ); | ||
this.targetMethodeParameterName = targetMethodeParameterName; | ||
this.text = MapStructBundle.message( "intention.replace.source.property", targetMethodeParameterName ); | ||
this.family = MapStructBundle.message( "inspection.source.property.this.used" ); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, | ||
@NotNull PsiElement endElement ) { | ||
if ( !endElement.isValid() ) { | ||
return false; | ||
} | ||
PsiElement parent = endElement.getParent(); | ||
return parent.isValid() && parent instanceof PsiAnnotationParamListImpl; | ||
} | ||
|
||
@Override | ||
public @IntentionName @NotNull String getText() { | ||
return text; | ||
} | ||
|
||
@Override | ||
public void invoke( @NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, | ||
@NotNull PsiElement endElement ) { | ||
if (endElement instanceof PsiNameValuePair end) { | ||
PsiAnnotationParamListImpl parent = (PsiAnnotationParamListImpl) end.getParent(); | ||
PsiElement parent1 = parent.getParent(); | ||
|
||
// don't replace inside of strings. Only the constant value name | ||
String annotationText = parent1.getText().replaceFirst( "(?<!\")\\s*,?\\s*source\\s*=\\s*\"\\.\"", | ||
"source = \"" + targetMethodeParameterName + "\"" ); | ||
parent1.replace( getInstance( project ).createAnnotationFromText( annotationText, parent1 ) ); | ||
} | ||
} | ||
|
||
@Override | ||
public @IntentionFamilyName @NotNull String getFamilyName() { | ||
return family; | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/main/resources/inspectionDescriptions/ThisUsedAsSourcePropertyInspection.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 "." is used as a source in <code>@Mapping</code> | ||
</p> | ||
<p> | ||
<pre><code> | ||
//wrong | ||
@Mapper | ||
public interface EmployeeMapper { | ||
@Mapping(target = "dto", source = ".") | ||
Employee toEmployee(EmployeeDto employeeDto, @Context CycleAvoidingMappingContext context); | ||
} | ||
</code></pre> | ||
</p> | ||
<p> | ||
<pre><code> | ||
//correct | ||
@Mapper | ||
public interface EmployeeMapper { | ||
@Mapping(source = "employeeDto", target = "dto") | ||
Employee toEmployee(EmployeeDto employeeDto, @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
45 changes: 45 additions & 0 deletions
45
src/test/java/org/mapstruct/intellij/inspection/ThisUsedAsSourcePropertyInspectionTest.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,45 @@ | ||
/* | ||
* 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.codeInsight.intention.IntentionAction; | ||
import com.intellij.codeInspection.LocalInspectionTool; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author hduelme | ||
*/ | ||
public class ThisUsedAsSourcePropertyInspectionTest extends BaseInspectionTest { | ||
@Override | ||
protected @NotNull Class<? extends LocalInspectionTool> getInspection() { | ||
return ThisUsedAsSourcePropertyInspection.class; | ||
} | ||
|
||
public void testThisUsedAsSourcePropertyInspection() { | ||
doTest(); | ||
List<IntentionAction> allQuickFixes = myFixture.getAllQuickFixes(); | ||
|
||
assertThat( allQuickFixes ) | ||
.extracting( IntentionAction::getText ) | ||
.as( "Intent Text" ) | ||
.containsExactlyInAnyOrder( | ||
"Replace source '.' with 'source'", | ||
"Replace source '.' with 'source'", | ||
"Replace source '.' with 'source'", | ||
"Replace source '.' with 'age'" | ||
); | ||
|
||
myFixture.launchAction( allQuickFixes.get( 0 ) ); | ||
myFixture.launchAction( allQuickFixes.get( 1 ) ); | ||
myFixture.launchAction( allQuickFixes.get( 2 ) ); | ||
String testName = getTestName( false ); | ||
myFixture.checkResultByFile( testName + "_after.java" ); | ||
} | ||
} |
Oops, something went wrong.