From af4bd9a9cd76085beccafbc12a525d32b8e08d64 Mon Sep 17 00:00:00 2001 From: Nikita Eshkeev Date: Mon, 22 Mar 2021 22:54:21 +0300 Subject: [PATCH] [EA-259285] CharTable not found in: PsiCodeBlock RedundantThrowsDeclarationInspection used to try to remove or replace try statements regardless if their corresponding PSI elements are still valid. Since PSI nodes were being removed or replace in the loop some of them got to become invalid and when BlockUtils#unwrapTryBlock was invoked on an invalid PSI element it threw the AssertionError. This patch adds checks if the PSI element that is being processed is still valid and if it is not, then stop processing such PSI elements. GitOrigin-RevId: 6d071ef7888f46bbda10b6d99853ef338055f3a6 --- .../RedundantThrowsDeclarationInspection.java | 16 +++++++++------- .../com/intellij/codeInsight/BlockUtils.java | 4 +++- .../afterNestedInCatch.java | 10 ++++++++++ .../beforeNestedInCatch.java | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 java/java-tests/testData/inspection/redundantThrowsGlobalFix/afterNestedInCatch.java create mode 100644 java/java-tests/testData/inspection/redundantThrowsGlobalFix/beforeNestedInCatch.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/unneededThrows/RedundantThrowsDeclarationInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/unneededThrows/RedundantThrowsDeclarationInspection.java index f3116d93b38e..1140a44b9531 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/unneededThrows/RedundantThrowsDeclarationInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/unneededThrows/RedundantThrowsDeclarationInspection.java @@ -1,4 +1,4 @@ -// 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-2021 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. package com.intellij.codeInspection.unneededThrows; import com.intellij.analysis.AnalysisScope; @@ -263,21 +263,23 @@ public final class RedundantThrowsDeclarationInspection extends GlobalJavaBatchI for (Map.Entry mapping : mappings.entrySet()) { final PsiElement from = mapping.getKey(); final PsiElement to = mapping.getValue(); + if (!from.isValid()) continue; if (to == null) { new CommentTracker().deleteAndRestoreComments(from); } - else { + else if (to.isValid()) { final PsiElement element = new CommentTracker().replaceAndRestoreComments(from, to); instance.shortenClassReferences(element); } } for (PsiTryStatement tryStatement : tryStatements) { - if (tryStatement.getCatchSections().length == 0 && - tryStatement.getFinallyBlock() == null && - tryStatement.getResourceList() == null) { - BlockUtils.unwrapTryBlock(tryStatement); - } + if (!tryStatement.isValid()) continue; + if (tryStatement.getCatchSections().length != 0) continue; + if (tryStatement.getFinallyBlock() != null) continue; + if (tryStatement.getResourceList() != null) continue; + + BlockUtils.unwrapTryBlock(tryStatement); } }); } diff --git a/java/java-psi-impl/src/com/intellij/codeInsight/BlockUtils.java b/java/java-psi-impl/src/com/intellij/codeInsight/BlockUtils.java index 3d16e449886a..9e3aea4db85d 100644 --- a/java/java-psi-impl/src/com/intellij/codeInsight/BlockUtils.java +++ b/java/java-psi-impl/src/com/intellij/codeInsight/BlockUtils.java @@ -1,4 +1,4 @@ -// 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-2021 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. package com.intellij.codeInsight; import com.intellij.openapi.project.Project; @@ -112,6 +112,8 @@ public final class BlockUtils { } public static void unwrapTryBlock(PsiTryStatement tryStatement) { + assert tryStatement.isValid() : "The tryStatement PSI element must be valid"; + PsiCodeBlock tryBlock = tryStatement.getTryBlock(); if (tryBlock == null) { return; diff --git a/java/java-tests/testData/inspection/redundantThrowsGlobalFix/afterNestedInCatch.java b/java/java-tests/testData/inspection/redundantThrowsGlobalFix/afterNestedInCatch.java new file mode 100644 index 000000000000..f985e3a14fd2 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantThrowsGlobalFix/afterNestedInCatch.java @@ -0,0 +1,10 @@ +// "FileNotFoundException" "true" + +import java.io.FileNotFoundException; + +class Main { + public void f() {} + { + f(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantThrowsGlobalFix/beforeNestedInCatch.java b/java/java-tests/testData/inspection/redundantThrowsGlobalFix/beforeNestedInCatch.java new file mode 100644 index 000000000000..2fdda4cb7bf9 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantThrowsGlobalFix/beforeNestedInCatch.java @@ -0,0 +1,18 @@ +// "FileNotFoundException" "true" + +import java.io.FileNotFoundException; + +class Main { + public void f() throws FileNotFoundException {} + { + try { + f(); + } catch (FileNotFoundException e1) { + try { + f(); + } catch (FileNotFoundException e2) { + e2.printStackTrace(); + } + } + } +} \ No newline at end of file