diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddExceptionToExistingCatchFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddExceptionToExistingCatchFix.java index 01f5d90047d3..5450f4153345 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddExceptionToExistingCatchFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddExceptionToExistingCatchFix.java @@ -19,6 +19,8 @@ import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.ui.popup.LightweightWindowEvent; import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; +import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.ui.components.JBList; @@ -113,16 +115,18 @@ public class AddExceptionToExistingCatchFix extends PsiElementBaseIntentionActio private static void addTypeToCatch(@NotNull List exceptionsToAdd, @NotNull PsiCatchSection catchSection) { - WriteCommandAction.runWriteCommandAction(catchSection.getProject(), () -> { + Project project = catchSection.getProject(); + WriteCommandAction.runWriteCommandAction(project, () -> { if (!catchSection.isValid() || !exceptionsToAdd.stream().allMatch(type -> type.isValid())) return; PsiParameter parameter = catchSection.getParameter(); if (parameter == null) return; PsiTypeElement typeElement = parameter.getTypeElement(); if (typeElement == null) return; PsiType parameterType = parameter.getType(); - PsiElementFactory factory = JavaPsiFacade.getElementFactory(catchSection.getProject()); + PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); String flattenText = getTypeText(exceptionsToAdd, parameter, parameterType, factory); - typeElement.replace(factory.createTypeElementFromText(flattenText, parameter)); + PsiElement newTypeElement = typeElement.replace(factory.createTypeElementFromText(flattenText, parameter)); + CodeStyleManager.getInstance(project).reformat(JavaCodeStyleManager.getInstance(project).shortenClassReferences(newTypeElement)); }); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addExceptionToExistingCatch/afterSingleCatch.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addExceptionToExistingCatch/afterSingleCatch.java index 8db6762d32ab..4b83934c184d 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addExceptionToExistingCatch/afterSingleCatch.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addExceptionToExistingCatch/afterSingleCatch.java @@ -1,10 +1,11 @@ // "Add exception to existing catch clause" "true" +import java.io.File; import java.io.IOException; class Test { public static void main(String[] args) { try { - throw new IOException(); + new File("path").getCanonicalPath(); } catch (IndexOutOfBoundsException | IOException e) { } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addExceptionToExistingCatch/beforeSingleCatch.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addExceptionToExistingCatch/beforeSingleCatch.java index dc22faae4118..f5e44f831633 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addExceptionToExistingCatch/beforeSingleCatch.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addExceptionToExistingCatch/beforeSingleCatch.java @@ -1,10 +1,10 @@ // "Add exception to existing catch clause" "true" -import java.io.IOException; +import java.io.File; class Test { public static void main(String[] args) { try { - throw new IOException(); + new File("path").getCanonicalPath(); } catch (IndexOutOfBoundsException e) { } }