|
| 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.codeInsight.intention.QuickFixFactory; |
| 9 | +import com.intellij.codeInspection.LocalQuickFix; |
| 10 | +import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement; |
| 11 | +import com.intellij.codeInspection.LocalQuickFixOnPsiElement; |
| 12 | +import com.intellij.codeInspection.ProblemsHolder; |
| 13 | +import com.intellij.codeInspection.util.IntentionFamilyName; |
| 14 | +import com.intellij.codeInspection.util.IntentionName; |
| 15 | +import com.intellij.openapi.editor.CaretState; |
| 16 | +import com.intellij.openapi.editor.Editor; |
| 17 | +import com.intellij.openapi.editor.LogicalPosition; |
| 18 | +import com.intellij.openapi.editor.ScrollType; |
| 19 | +import com.intellij.openapi.fileEditor.FileEditor; |
| 20 | +import com.intellij.openapi.fileEditor.FileEditorManager; |
| 21 | +import com.intellij.openapi.fileEditor.TextEditor; |
| 22 | +import com.intellij.openapi.project.Project; |
| 23 | +import com.intellij.openapi.util.TextRange; |
| 24 | +import com.intellij.openapi.util.text.Strings; |
| 25 | +import com.intellij.psi.JavaElementVisitor; |
| 26 | +import com.intellij.psi.PsiAnnotation; |
| 27 | +import com.intellij.psi.PsiAnnotationMemberValue; |
| 28 | +import com.intellij.psi.PsiClass; |
| 29 | +import com.intellij.psi.PsiElement; |
| 30 | +import com.intellij.psi.PsiElementVisitor; |
| 31 | +import com.intellij.psi.PsiFile; |
| 32 | +import com.intellij.psi.PsiMethod; |
| 33 | +import com.intellij.psi.PsiType; |
| 34 | +import com.intellij.psi.impl.source.tree.java.PsiAnnotationImpl; |
| 35 | +import org.jetbrains.annotations.NotNull; |
| 36 | +import org.mapstruct.intellij.MapStructBundle; |
| 37 | +import org.mapstruct.intellij.util.MapStructVersion; |
| 38 | +import org.mapstruct.intellij.util.MapstructUtil; |
| 39 | +import org.mapstruct.intellij.util.TargetUtils; |
| 40 | + |
| 41 | +import java.util.ArrayList; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.HashMap; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.Optional; |
| 47 | + |
| 48 | +import static com.intellij.codeInsight.AnnotationUtil.getStringAttributeValue; |
| 49 | +import static org.mapstruct.intellij.util.MapstructAnnotationUtils.extractMappingAnnotationsFromMappings; |
| 50 | +import static org.mapstruct.intellij.util.MapstructUtil.MAPPINGS_ANNOTATION_FQN; |
| 51 | +import static org.mapstruct.intellij.util.MapstructUtil.MAPPING_ANNOTATION_FQN; |
| 52 | +import static org.mapstruct.intellij.util.TargetUtils.getTargetType; |
| 53 | + |
| 54 | +/** |
| 55 | + * @author hduelme |
| 56 | + */ |
| 57 | +public class TargetPropertyMappedMoreThanOnceInspection extends InspectionBase { |
| 58 | + @NotNull |
| 59 | + @Override |
| 60 | + PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 61 | + return new TargetPropertyMappedMoreThanOnceInspection.MyJavaElementVisitor( holder, |
| 62 | + MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ) ); |
| 63 | + } |
| 64 | + |
| 65 | + private static class MyJavaElementVisitor extends JavaElementVisitor { |
| 66 | + private final ProblemsHolder holder; |
| 67 | + private final MapStructVersion mapStructVersion; |
| 68 | + |
| 69 | + private MyJavaElementVisitor(ProblemsHolder holder, MapStructVersion mapStructVersion) { |
| 70 | + this.holder = holder; |
| 71 | + this.mapStructVersion = mapStructVersion; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void visitMethod(PsiMethod method) { |
| 76 | + if ( !MapstructUtil.isMapper( method.getContainingClass() ) ) { |
| 77 | + return; |
| 78 | + } |
| 79 | + PsiType targetType = getTargetType( method ); |
| 80 | + if ( targetType == null ) { |
| 81 | + return; |
| 82 | + } |
| 83 | + Map<String, List<PsiElement>> problemMap = new HashMap<>(); |
| 84 | + for (PsiAnnotation psiAnnotation : method.getAnnotations()) { |
| 85 | + String qualifiedName = psiAnnotation.getQualifiedName(); |
| 86 | + if ( MAPPING_ANNOTATION_FQN.equals( qualifiedName ) ) { |
| 87 | + handleMappingAnnotation( psiAnnotation, problemMap ); |
| 88 | + } |
| 89 | + else if (MAPPINGS_ANNOTATION_FQN.equals( qualifiedName )) { |
| 90 | + extractMappingAnnotationsFromMappings( psiAnnotation ) |
| 91 | + .forEach( a -> handleMappingAnnotation( a, problemMap ) ); |
| 92 | + } |
| 93 | + else { |
| 94 | + // Handle annotations containing at least one Mapping annotation |
| 95 | + handleAnnotationWithMappingAnnotation( psiAnnotation, problemMap ); |
| 96 | + } |
| 97 | + } |
| 98 | + QuickFixFactory quickFixFactory = QuickFixFactory.getInstance(); |
| 99 | + for (Map.Entry<String, List<PsiElement>> problem : problemMap.entrySet()) { |
| 100 | + List<PsiElement> problemElements = problem.getValue(); |
| 101 | + if (problemElements.size() > 1) { |
| 102 | + for (PsiElement problemElement : problemElements) { |
| 103 | + LocalQuickFix[] quickFixes = getLocalQuickFixes( problemElement, quickFixFactory ); |
| 104 | + holder.registerProblem( problemElement, |
| 105 | + MapStructBundle.message( "inspection.target.property.mapped.more.than.once", |
| 106 | + problem.getKey() ), quickFixes ); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static @NotNull LocalQuickFix[] getLocalQuickFixes(PsiElement problemElement, |
| 113 | + QuickFixFactory quickFixFactory) { |
| 114 | + List<LocalQuickFix> quickFixes = new ArrayList<>(2); |
| 115 | + if (problemElement instanceof PsiAnnotation) { |
| 116 | + quickFixes.add( getDeleteFix( problemElement, quickFixFactory ) ); |
| 117 | + } |
| 118 | + else if (problemElement instanceof PsiAnnotationMemberValue problemPsiAnnotationMemberValue) { |
| 119 | + Optional.ofNullable( problemElement.getParent() ).map( PsiElement::getParent ) |
| 120 | + .map( PsiElement::getParent ).filter( PsiAnnotation.class::isInstance ) |
| 121 | + .ifPresent( annotation -> quickFixes.add( |
| 122 | + getDeleteFix( annotation, quickFixFactory ) ) ); |
| 123 | + quickFixes.add( new ChangeTargetQuickFix( problemPsiAnnotationMemberValue ) ); |
| 124 | + } |
| 125 | + return quickFixes.toArray( new LocalQuickFix[]{} ); |
| 126 | + } |
| 127 | + |
| 128 | + private static @NotNull LocalQuickFixAndIntentionActionOnPsiElement getDeleteFix( |
| 129 | + @NotNull PsiElement problemElement, @NotNull QuickFixFactory quickFixFactory) { |
| 130 | + |
| 131 | + String annotationName = PsiAnnotationImpl.getAnnotationShortName( problemElement.getText() ); |
| 132 | + return quickFixFactory.createDeleteFix( problemElement, |
| 133 | + MapStructBundle.message( "intention.remove.annotation", annotationName ) ); |
| 134 | + } |
| 135 | + |
| 136 | + private void handleAnnotationWithMappingAnnotation(PsiAnnotation psiAnnotation, |
| 137 | + Map<String, List<PsiElement>> problemMap) { |
| 138 | + PsiClass annotationClass = psiAnnotation.resolveAnnotationType(); |
| 139 | + if (annotationClass == null) { |
| 140 | + return; |
| 141 | + } |
| 142 | + TargetUtils.findAllDefinedMappingTargets( annotationClass, mapStructVersion ) |
| 143 | + .forEach( target -> |
| 144 | + problemMap.computeIfAbsent( target, k -> new ArrayList<>() ).add( psiAnnotation ) ); |
| 145 | + } |
| 146 | + |
| 147 | + private static void handleMappingAnnotation(PsiAnnotation psiAnnotation, |
| 148 | + Map<String, List<PsiElement>> problemMap) { |
| 149 | + PsiAnnotationMemberValue value = psiAnnotation.findDeclaredAttributeValue( "target" ); |
| 150 | + if (value != null) { |
| 151 | + String target = getStringAttributeValue( value ); |
| 152 | + if (target != null) { |
| 153 | + problemMap.computeIfAbsent( target, k -> new ArrayList<>() ).add( value ); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static class ChangeTargetQuickFix extends LocalQuickFixOnPsiElement { |
| 159 | + |
| 160 | + private final String myText; |
| 161 | + private final String myFamilyName; |
| 162 | + |
| 163 | + private ChangeTargetQuickFix(@NotNull PsiAnnotationMemberValue element) { |
| 164 | + super( element ); |
| 165 | + myText = MapStructBundle.message( "intention.change.target.property" ); |
| 166 | + myFamilyName = MapStructBundle.message( "inspection.target.property.mapped.more.than.once", |
| 167 | + element.getText() ); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public @IntentionName @NotNull String getText() { |
| 172 | + return myText; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull PsiElement psiElement, |
| 177 | + @NotNull PsiElement psiElement1) { |
| 178 | + FileEditor selectedEditor = FileEditorManager.getInstance( project ).getSelectedEditor(); |
| 179 | + if ( selectedEditor instanceof TextEditor textEditor) { |
| 180 | + Editor editor = textEditor.getEditor(); |
| 181 | + |
| 182 | + TextRange textRange = psiElement.getTextRange(); |
| 183 | + String textOfElement = String.valueOf( editor.getDocument() |
| 184 | + .getCharsSequence() |
| 185 | + .subSequence( textRange.getStartOffset(), textRange.getEndOffset() ) ); |
| 186 | + int targetStart = Strings.indexOf( textOfElement, "\"" ) + 1; |
| 187 | + int targetEnd = textOfElement.lastIndexOf( "\"" ); |
| 188 | + |
| 189 | + editor.getCaretModel().moveToOffset( textRange.getStartOffset() + targetStart ); |
| 190 | + LogicalPosition startPosition = editor.getCaretModel().getLogicalPosition(); |
| 191 | + editor.getCaretModel().moveToOffset( textRange.getStartOffset() + targetEnd ); |
| 192 | + editor.getCaretModel().setCaretsAndSelections( |
| 193 | + Collections.singletonList( new CaretState(startPosition, startPosition, |
| 194 | + editor.getCaretModel().getLogicalPosition() ) ) ); |
| 195 | + editor.getScrollingModel().scrollToCaret( ScrollType.MAKE_VISIBLE ); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public @IntentionFamilyName @NotNull String getFamilyName() { |
| 201 | + return myFamilyName; |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public boolean availableInBatchMode() { |
| 206 | + return false; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments