add exception to existing catch: ensure qNames are adjusted

This commit is contained in:
Anna.Kozlova
2018-03-29 16:11:08 +02:00
parent 71875dc334
commit 34ef66e51c
3 changed files with 11 additions and 6 deletions
@@ -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<PsiClassType> 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));
});
}
@@ -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) {
}
}
@@ -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<caret>();
new File("path").getCanonical<caret>Path();
} catch (IndexOutOfBoundsException e) {
}
}