IDEA-124435 (Fix "Collapse catch blocks" produces non-compiling code)

This commit is contained in:
Bas Leijdekkers
2014-04-29 11:50:48 +02:00
parent c04841407d
commit c11569008b
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection {
@NotNull
@Override
protected String buildErrorString(Object... infos) {
final PsiType type = (PsiType)infos[1];
final PsiType type = (PsiType)infos[0];
return InspectionGadgetsBundle.message("try.with.identical.catches.problem.descriptor", type.getPresentableText());
}
@@ -67,7 +67,7 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
return new CollapseCatchSectionsFix(((Integer)infos[0]).intValue());
return new CollapseCatchSectionsFix(((Integer)infos[1]).intValue(), ((Integer)infos[2]).intValue());
}
private static class TryWithIdenticalCatchesVisitor extends BaseInspectionVisitor {
@@ -125,7 +125,8 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection {
}
final PsiJavaToken rParenth = otherSection.getRParenth();
if (rParenth != null) {
registerErrorAtOffset(otherSection, 0, rParenth.getStartOffsetInParent() + 1, Integer.valueOf(i), parameter.getType());
registerErrorAtOffset(otherSection, 0, rParenth.getStartOffsetInParent() + 1, parameter.getType(),
Integer.valueOf(i), Integer.valueOf(j));
}
duplicates[i] = true;
duplicates[j] = true;
@@ -160,9 +161,11 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection {
private static class CollapseCatchSectionsFix extends InspectionGadgetsFix {
private final int myCollapseIntoIndex;
private final int mySectionIndex;
public CollapseCatchSectionsFix(int collapseIntoIndex) {
public CollapseCatchSectionsFix(int collapseIntoIndex, int sectionIndex) {
myCollapseIntoIndex = collapseIntoIndex;
mySectionIndex = sectionIndex;
}
@Override
@@ -179,13 +182,15 @@ public class TryWithIdenticalCatchesInspection extends BaseInspection {
@Override
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiCatchSection section = (PsiCatchSection)descriptor.getPsiElement();
final PsiTryStatement tryStatement = (PsiTryStatement)section.getParent();
// smart psi pointer lost correct catch section when multiple catch section were collapsed in batch mode
// so use index of catch section to retrieve it instead.
final PsiTryStatement tryStatement = (PsiTryStatement)descriptor.getPsiElement().getParent();
final PsiCatchSection[] catchSections = tryStatement.getCatchSections();
if (myCollapseIntoIndex >= catchSections.length) {
if (myCollapseIntoIndex >= catchSections.length || mySectionIndex >= catchSections.length) {
return; // something has gone stale
}
final PsiCatchSection collapseInto = catchSections[myCollapseIntoIndex];
final PsiCatchSection section = catchSections[mySectionIndex];
final PsiParameter parameter1 = collapseInto.getParameter();
final PsiParameter parameter2 = section.getParameter();
if (parameter1 == null || parameter2 == null) {