From b12032633d692315d3fdb6e8e6fa78d9b8390a9e Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Sun, 26 Nov 2023 22:00:36 +0100 Subject: [PATCH] #155 Fix RemoveMappersFix to use new 2023.3 RemoveUnusedVariableFix intention action --- .../WrongUsageOfMappersFactoryInspection.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/mapstruct/intellij/inspection/WrongUsageOfMappersFactoryInspection.java b/src/main/java/org/mapstruct/intellij/inspection/WrongUsageOfMappersFactoryInspection.java index 7fa60124..c9d08d5a 100644 --- a/src/main/java/org/mapstruct/intellij/inspection/WrongUsageOfMappersFactoryInspection.java +++ b/src/main/java/org/mapstruct/intellij/inspection/WrongUsageOfMappersFactoryInspection.java @@ -5,9 +5,13 @@ */ package org.mapstruct.intellij.inspection; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInsight.daemon.impl.quickfix.RemoveUnusedVariableFix; import com.intellij.codeInsight.intention.AddAnnotationPsiFix; +import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInsight.intention.QuickFixFactory; import com.intellij.codeInspection.IntentionWrapper; import com.intellij.codeInspection.LocalQuickFix; @@ -44,6 +48,20 @@ public class WrongUsageOfMappersFactoryInspection extends InspectionBase { "getMapper" ).parameterTypes( CommonClassNames.JAVA_LANG_CLASS ); + private static final Method AS_INTENTION_ACTION_METHOD; + + static { + Method asIntentionActionMethod; + try { + asIntentionActionMethod = RemoveMappersFix.class.getMethod( "asIntentAction" ); + } + catch ( NoSuchMethodException e ) { + asIntentionActionMethod = null; + } + + AS_INTENTION_ACTION_METHOD = asIntentionActionMethod; + } + @NotNull @Override PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) { @@ -145,8 +163,27 @@ public String getFamilyName() { private static LocalQuickFix createRemoveMappersFix(@NotNull PsiMethodCallExpression methodCallExpression) { PsiElement parent = methodCallExpression.getParent(); if ( parent instanceof PsiVariable ) { + RemoveMappersFix fix = new RemoveMappersFix( (PsiVariable) parent ); + IntentionAction action = null; + if ( fix instanceof IntentionAction ) { + action = (IntentionAction) fix; + } else if ( AS_INTENTION_ACTION_METHOD != null ) { + try { + Object intentionAction = AS_INTENTION_ACTION_METHOD.invoke( fix ); + if ( intentionAction instanceof IntentionAction ) { + action = (IntentionAction) intentionAction; + } + } + catch ( IllegalAccessException | InvocationTargetException e ) { + action = null; + } + } + + if ( action == null ) { + return null; + } return IntentionWrapper.wrapToQuickFix( - new RemoveMappersFix( (PsiVariable) parent ), + action, methodCallExpression.getContainingFile() ); }