Skip to content

Commit

Permalink
#155 Fix RemoveMappersFix to use new 2023.3 RemoveUnusedVariableFix i…
Browse files Browse the repository at this point in the history
…ntention action
  • Loading branch information
filiphr committed Nov 26, 2023
1 parent 553a9f9 commit 51a4549
Showing 1 changed file with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
);
}
Expand Down

0 comments on commit 51a4549

Please sign in to comment.