From 55e85c1cf084fecde507772456302d24fffbd443 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 16 Jun 2023 16:15:57 +0200 Subject: [PATCH] [java-intentions] RedundantArrayForVarargsCallFix, RemoveRedundantParameterTypesFix: ModCommand migration GitOrigin-RevId: 1233736a6935608aa42afd4d0df0c6855a02e8a1 --- ...edundantLambdaParameterTypeInspection.java | 4 +-- ...edundantArrayForVarargsCallInspection.java | 9 +++---- .../RemoveRedundantParameterTypesFix.java | 25 +++++-------------- .../impl/config/QuickFixFactoryImpl.java | 7 +++--- .../quickFix/InsertSeparatingComma.java | 2 +- .../quickFix/Parentheses_after.java | 2 +- ...undantArray4VarargsCallInspectionTest.java | 4 ++- .../RedundantLambdaParameterTypeFixTest.java | 2 ++ 8 files changed, 22 insertions(+), 33 deletions(-) diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/lambda/RedundantLambdaParameterTypeInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/lambda/RedundantLambdaParameterTypeInspection.java index 6f1f7da0d9d3..9f4bbe94047c 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/lambda/RedundantLambdaParameterTypeInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/lambda/RedundantLambdaParameterTypeInspection.java @@ -23,8 +23,8 @@ public class RedundantLambdaParameterTypeInspection extends AbstractBaseJavaLoca RemoveRedundantParameterTypesFix.isApplicable(parameterList)) { for (PsiParameter parameter : parameterList.getParameters()) { if (parameter.getTypeElement() != null) { - holder.registerProblem(parameter.getTypeElement(), JavaBundle.message("inspection.message.lambda.parameter.type.is.redundant"), - new RemoveRedundantParameterTypesFix((PsiLambdaExpression)parameterList.getParent())); + holder.problem(parameter.getTypeElement(), JavaBundle.message("inspection.message.lambda.parameter.type.is.redundant")) + .fix(new RemoveRedundantParameterTypesFix((PsiLambdaExpression)parameterList.getParent())).register(); } } } diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/miscGenerics/RedundantArrayForVarargsCallInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/miscGenerics/RedundantArrayForVarargsCallInspection.java index e883d0f0b80b..84f19835163a 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/miscGenerics/RedundantArrayForVarargsCallInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/miscGenerics/RedundantArrayForVarargsCallInspection.java @@ -90,12 +90,11 @@ public class RedundantArrayForVarargsCallInspection extends AbstractBaseJavaLoca } - private static final class RedundantArrayForVarargsCallFix implements LocalQuickFix { + private static final class RedundantArrayForVarargsCallFix extends PsiUpdateModCommandQuickFix { @Override - public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { - PsiElement arrayCreation = descriptor.getPsiElement(); - if (!(arrayCreation instanceof PsiNewExpression)) return; - CommonJavaRefactoringUtil.inlineArrayCreationForVarargs((PsiNewExpression)arrayCreation); + protected void applyFix(@NotNull Project project, @NotNull PsiElement arrayCreation, @NotNull EditorUpdater updater) { + if (!(arrayCreation instanceof PsiNewExpression newExpression)) return; + CommonJavaRefactoringUtil.inlineArrayCreationForVarargs(newExpression); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/RemoveRedundantParameterTypesFix.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/RemoveRedundantParameterTypesFix.java index b75c2025191b..7745a0fc6a9d 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/RemoveRedundantParameterTypesFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/RemoveRedundantParameterTypesFix.java @@ -2,45 +2,32 @@ package com.intellij.codeInsight.intention.impl; import com.intellij.codeInspection.AnonymousCanBeLambdaInspection; -import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement; +import com.intellij.codeInspection.EditorUpdater; +import com.intellij.codeInspection.PsiUpdateModCommandAction; import com.intellij.java.JavaBundle; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; import com.intellij.psi.util.PsiUtil; import com.intellij.util.containers.ContainerUtil; import com.siyeh.ig.psiutils.CommentTracker; -import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.Objects; -public class RemoveRedundantParameterTypesFix extends LocalQuickFixAndIntentionActionOnPsiElement { +public class RemoveRedundantParameterTypesFix extends PsiUpdateModCommandAction { public RemoveRedundantParameterTypesFix(@NotNull PsiLambdaExpression lambdaExpression) { super(lambdaExpression); } - @Nls @Override - public @NotNull String getText() { + public @NotNull String getFamilyName() { return JavaBundle.message("quickfix.family.remove.redundant.parameter.types"); } @Override - public @NotNull String getFamilyName() { - return getText(); - } - - @Override - public void invoke(@NotNull Project project, - @NotNull PsiFile file, - @Nullable Editor editor, - @NotNull PsiElement startElement, - @NotNull PsiElement endElement) { - removeLambdaParameterTypesIfPossible((PsiLambdaExpression)startElement); + protected void invoke(@NotNull ActionContext context, @NotNull PsiLambdaExpression lambda, @NotNull EditorUpdater updater) { + removeLambdaParameterTypesIfPossible(lambda); } public static boolean isApplicable(@NotNull PsiParameterList parameterList) { diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java index edd2636bfb0a..133e99b2cf0e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/config/QuickFixFactoryImpl.java @@ -1167,7 +1167,7 @@ public final class QuickFixFactoryImpl extends QuickFixFactory { @Override public @NotNull IntentionAction createRemoveRedundantLambdaParameterTypesFix(@NotNull PsiLambdaExpression lambdaExpression, @IntentionName String message) { - return new RemoveRedundantLambdaParameterTypesFix(lambdaExpression, message); + return new RemoveRedundantLambdaParameterTypesFix(lambdaExpression, message).asIntention(); } @Override @@ -1183,10 +1183,9 @@ public final class QuickFixFactoryImpl extends QuickFixFactory { myMessage = message; } - @Nls @Override - public @NotNull String getText() { - return myMessage; + protected @NotNull Presentation getPresentation(@NotNull ActionContext context, @NotNull PsiLambdaExpression element) { + return Presentation.of(myMessage); } } diff --git a/java/java-tests/testData/inspection/redundantArrayForVarargs/quickFix/InsertSeparatingComma.java b/java/java-tests/testData/inspection/redundantArrayForVarargs/quickFix/InsertSeparatingComma.java index 2c12a846e715..dc17cee02bce 100644 --- a/java/java-tests/testData/inspection/redundantArrayForVarargs/quickFix/InsertSeparatingComma.java +++ b/java/java-tests/testData/inspection/redundantArrayForVarargs/quickFix/InsertSeparatingComma.java @@ -2,6 +2,6 @@ class X { void varargFunc(String s, Object... ss) {} { - varargFunc("hello", new Object[]{/* 1 */ "World" }); + varargFunc("hello", new Object[]{/* 1 */ "World" }); } } \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantArrayForVarargs/quickFix/Parentheses_after.java b/java/java-tests/testData/inspection/redundantArrayForVarargs/quickFix/Parentheses_after.java index c5adc0891fd0..22d8ec61a543 100644 --- a/java/java-tests/testData/inspection/redundantArrayForVarargs/quickFix/Parentheses_after.java +++ b/java/java-tests/testData/inspection/redundantArrayForVarargs/quickFix/Parentheses_after.java @@ -1,7 +1,7 @@ public class Main { public static void main(String[] args) { display(//c1 - "hi"); + "hi"); } private static void display(String... messages) { } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantArray4VarargsCallInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantArray4VarargsCallInspectionTest.java index 3660e15fa424..3255d6babb45 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantArray4VarargsCallInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantArray4VarargsCallInspectionTest.java @@ -1,9 +1,10 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInspection; import com.intellij.JavaTestUtil; import com.intellij.codeInspection.miscGenerics.RedundantArrayForVarargsCallInspection; import com.intellij.java.JavaBundle; +import com.intellij.openapi.application.impl.NonBlockingReadActionImpl; import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; /** @@ -30,6 +31,7 @@ public class RedundantArray4VarargsCallInspectionTest extends LightJavaCodeInsig String name = getTestName(false); myFixture.configureByFile(name + ".java"); myFixture.launchAction(myFixture.findSingleIntention(JavaBundle.message("inspection.redundant.array.creation.quickfix"))); + NonBlockingReadActionImpl.waitForAsyncTaskCompletion(); myFixture.checkResultByFile(name + "_after.java"); } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantLambdaParameterTypeFixTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantLambdaParameterTypeFixTest.java index 753945b2a639..a39671bf61e1 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantLambdaParameterTypeFixTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/RedundantLambdaParameterTypeFixTest.java @@ -4,6 +4,7 @@ package com.intellij.java.codeInspection; import com.intellij.JavaTestUtil; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInspection.lambda.RedundantLambdaParameterTypeInspection; +import com.intellij.openapi.application.impl.NonBlockingReadActionImpl; import com.intellij.testFramework.LightProjectDescriptor; import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; import org.jetbrains.annotations.NotNull; @@ -86,6 +87,7 @@ public class RedundantLambdaParameterTypeFixTest extends LightJavaCodeInsightFix myFixture.configureByFiles(getTestName(false) + ".java"); final IntentionAction singleIntention = myFixture.findSingleIntention(ourIntentionName); myFixture.launchAction(singleIntention); + NonBlockingReadActionImpl.waitForAsyncTaskCompletion(); myFixture.checkResultByFile(getTestName(false) + ".java", getTestName(false) + "_after.java", true); }