|
| 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.inspection; |
| 7 | + |
| 8 | +import com.intellij.codeInspection.LocalQuickFix; |
| 9 | +import com.intellij.codeInspection.LocalQuickFixOnPsiElement; |
| 10 | +import com.intellij.codeInspection.ProblemsHolder; |
| 11 | +import com.intellij.codeInspection.util.IntentionFamilyName; |
| 12 | +import com.intellij.codeInspection.util.IntentionName; |
| 13 | +import com.intellij.openapi.project.Project; |
| 14 | +import com.intellij.psi.PsiAnnotation; |
| 15 | +import com.intellij.psi.PsiElement; |
| 16 | +import com.intellij.psi.PsiFile; |
| 17 | +import com.intellij.psi.PsiMethod; |
| 18 | +import com.intellij.psi.PsiNameValuePair; |
| 19 | +import com.intellij.psi.PsiParameter; |
| 20 | +import com.intellij.psi.impl.source.tree.java.PsiAnnotationParamListImpl; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | +import org.mapstruct.intellij.MapStructBundle; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import static com.intellij.psi.PsiElementFactory.getInstance; |
| 28 | +import static org.mapstruct.intellij.util.MapstructAnnotationUtils.getAnnotatedMethod; |
| 29 | +import static org.mapstruct.intellij.util.MapstructUtil.getSourceParameters; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author hduelme |
| 33 | + */ |
| 34 | +public class ThisUsedAsSourcePropertyInspection extends MappingAnnotationInspectionBase { |
| 35 | + @Override |
| 36 | + void visitMappingAnnotation(@NotNull ProblemsHolder problemsHolder, @NotNull PsiAnnotation psiAnnotation, |
| 37 | + @NotNull MappingAnnotation mappingAnnotation) { |
| 38 | + PsiNameValuePair sourceProperty = mappingAnnotation.getSourceProperty(); |
| 39 | + if (sourceProperty == null || sourceProperty.getValue() == null) { |
| 40 | + return; |
| 41 | + } |
| 42 | + if ( !".".equals( sourceProperty.getLiteralValue() ) ) { |
| 43 | + return; |
| 44 | + } |
| 45 | + List<LocalQuickFix> fixes = new ArrayList<>(); |
| 46 | + PsiMethod annotatedMethod = getAnnotatedMethod( psiAnnotation ); |
| 47 | + if (annotatedMethod != null) { |
| 48 | + for (PsiParameter sourceParameter : getSourceParameters( annotatedMethod )) { |
| 49 | + fixes.add( new ReplaceSourceParameterValueQuickFix(sourceProperty, sourceParameter.getName() ) ); |
| 50 | + } |
| 51 | + } |
| 52 | + problemsHolder.registerProblem( sourceProperty.getValue(), |
| 53 | + MapStructBundle.message( "inspection.source.property.this.used" ), |
| 54 | + fixes.toArray( new LocalQuickFix[0] ) ); |
| 55 | + } |
| 56 | + |
| 57 | + private static class ReplaceSourceParameterValueQuickFix extends LocalQuickFixOnPsiElement { |
| 58 | + |
| 59 | + private final String targetMethodeParameterName; |
| 60 | + private final String text; |
| 61 | + private final String family; |
| 62 | + |
| 63 | + private ReplaceSourceParameterValueQuickFix(@NotNull PsiNameValuePair element, |
| 64 | + @NotNull String targetMethodeParameterName) { |
| 65 | + super( element ); |
| 66 | + this.targetMethodeParameterName = targetMethodeParameterName; |
| 67 | + this.text = MapStructBundle.message( "intention.replace.source.property", targetMethodeParameterName ); |
| 68 | + this.family = MapStructBundle.message( "inspection.source.property.this.used" ); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean isAvailable(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, |
| 73 | + @NotNull PsiElement endElement ) { |
| 74 | + if ( !endElement.isValid() ) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + PsiElement parent = endElement.getParent(); |
| 78 | + return parent.isValid() && parent instanceof PsiAnnotationParamListImpl; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public @IntentionName @NotNull String getText() { |
| 83 | + return text; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void invoke( @NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, |
| 88 | + @NotNull PsiElement endElement ) { |
| 89 | + if (endElement instanceof PsiNameValuePair end) { |
| 90 | + PsiAnnotationParamListImpl parent = (PsiAnnotationParamListImpl) end.getParent(); |
| 91 | + PsiElement parent1 = parent.getParent(); |
| 92 | + |
| 93 | + // don't replace inside of strings. Only the constant value name |
| 94 | + String annotationText = parent1.getText().replaceFirst( "(?<!\")\\s*,?\\s*source\\s*=\\s*\"\\.\"", |
| 95 | + "source = \"" + targetMethodeParameterName + "\"" ); |
| 96 | + parent1.replace( getInstance( project ).createAnnotationFromText( annotationText, parent1 ) ); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public @IntentionFamilyName @NotNull String getFamilyName() { |
| 102 | + return family; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments