From c11569008bdb3fa16ae3b72183ef704baaed9ef2 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 29 Apr 2014 11:50:48 +0200 Subject: [PATCH] IDEA-124435 (Fix "Collapse catch blocks" produces non-compiling code) --- .../TryWithIdenticalCatchesInspection.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java index 532589663a57..a3fb5fb6c35b 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryWithIdenticalCatchesInspection.java @@ -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) {