From 3e15e5f77a7b4b980cb76cdb13f9c78ad75b0686 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 13 Apr 2021 13:19:15 +0700 Subject: [PATCH] Avoid assignment to catch block parameter GitOrigin-RevId: 9d28cc640717e4a1513e3c2a2c30fd0db4977b42 --- .../daemon/impl/DaemonCodeAnalyzerImpl.java | 6 +++--- .../script/IdeScriptEngineManagerImpl.java | 5 ++--- .../com/intellij/util/ExceptionUtilRt.java | 20 +++++++++++++++++++ .../groovy/compiler/rt/GroovycRunner.java | 8 ++------ .../PropertyInspectorTable.java | 8 +++----- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java index 664a1f643d20..f81946cff7d5 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerImpl.java @@ -393,11 +393,11 @@ public final class DaemonCodeAnalyzerImpl extends DaemonCodeAnalyzerEx implement assert progress.isCanceled() && progress.isDisposed(); } catch (Throwable e) { - if (e instanceof ExecutionException) e = e.getCause(); + Throwable unwrapped = ExceptionUtilRt.unwrapException(e, ExecutionException.class); if (progress.isCanceled() && progress.isRunning()) { - e.addSuppressed(new RuntimeException("Daemon progress was canceled unexpectedly: " + progress)); + unwrapped.addSuppressed(new RuntimeException("Daemon progress was canceled unexpectedly: " + progress)); } - ExceptionUtil.rethrow(e); + ExceptionUtil.rethrow(unwrapped); } finally { DaemonProgressIndicator.setDebug(false); diff --git a/platform/platform-impl/src/com/intellij/ide/script/IdeScriptEngineManagerImpl.java b/platform/platform-impl/src/com/intellij/ide/script/IdeScriptEngineManagerImpl.java index bada95c3e700..efff5d243553 100644 --- a/platform/platform-impl/src/com/intellij/ide/script/IdeScriptEngineManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/ide/script/IdeScriptEngineManagerImpl.java @@ -12,6 +12,7 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.extensions.PluginDescriptor; import com.intellij.openapi.util.ClassLoaderUtil; import com.intellij.openapi.util.text.StringHash; +import com.intellij.util.ExceptionUtilRt; import com.intellij.util.concurrency.AppExecutorUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.JBIterable; @@ -232,9 +233,7 @@ final class IdeScriptEngineManagerImpl extends IdeScriptEngineManager { return myEngine.eval(script); } catch (Throwable ex) { - //noinspection InstanceofCatchParameter - while (ex instanceof ScriptException && ex.getCause() != null) ex = ex.getCause(); - throw new IdeScriptException(ex); + throw new IdeScriptException(ExceptionUtilRt.unwrapException(ex, ScriptException.class)); } }); } diff --git a/platform/util-rt/src/com/intellij/util/ExceptionUtilRt.java b/platform/util-rt/src/com/intellij/util/ExceptionUtilRt.java index 5bce1107754f..366b524c39ab 100644 --- a/platform/util-rt/src/com/intellij/util/ExceptionUtilRt.java +++ b/platform/util-rt/src/com/intellij/util/ExceptionUtilRt.java @@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable; import java.io.PrintWriter; import java.io.StringWriter; +import java.lang.reflect.InvocationTargetException; public class ExceptionUtilRt { public static void rethrowUnchecked(@Nullable Throwable t) { @@ -34,6 +35,25 @@ public class ExceptionUtilRt { return findCause(e, klass) != null; } + /** + * @param throwable exception to unwrap + * @return the supplied exception, or unwrapped exception (if the supplied exception is InvocationTargetException) + */ + public static @NotNull Throwable unwrapInvocationTargetException(@NotNull Throwable throwable) { + return unwrapException(throwable, InvocationTargetException.class); + } + + /** + * @param throwable exception to unwrap + * @param classToUnwrap exception class to unwrap + * @return the supplied exception, or unwrapped exception (if the supplied exception class is classToUnwrap) + */ + public static @NotNull Throwable unwrapException(@NotNull Throwable throwable, @NotNull Class classToUnwrap) { + while (classToUnwrap.isInstance(throwable) && throwable.getCause() != null && throwable.getCause() != throwable) { + throwable = throwable.getCause(); + } + return throwable; + } @NotNull public static String getThrowableText(@NotNull Throwable aThrowable, @NotNull String stackFrameSkipPattern) { diff --git a/plugins/groovy/rt/src/org/jetbrains/groovy/compiler/rt/GroovycRunner.java b/plugins/groovy/rt/src/org/jetbrains/groovy/compiler/rt/GroovycRunner.java index 4486b7add585..9163c9c6e1a5 100644 --- a/plugins/groovy/rt/src/org/jetbrains/groovy/compiler/rt/GroovycRunner.java +++ b/plugins/groovy/rt/src/org/jetbrains/groovy/compiler/rt/GroovycRunner.java @@ -1,12 +1,12 @@ // 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. package org.jetbrains.groovy.compiler.rt; +import com.intellij.util.ExceptionUtilRt; import com.intellij.util.lang.java6.UrlClassLoader; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.*; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.nio.charset.Charset; @@ -81,11 +81,7 @@ public final class GroovycRunner { method.invoke(null, Boolean.valueOf(forStubs), argPath, configScript, targetBytecode, mailbox, out, err); } catch (Throwable e) { - //noinspection InstanceofCatchParameter - while (e instanceof InvocationTargetException) { - e = e.getCause(); - } - e.printStackTrace(err); + ExceptionUtilRt.unwrapInvocationTargetException(e).printStackTrace(err); return 1; } return 0; diff --git a/plugins/ui-designer/src/com/intellij/uiDesigner/propertyInspector/PropertyInspectorTable.java b/plugins/ui-designer/src/com/intellij/uiDesigner/propertyInspector/PropertyInspectorTable.java index 02a96964ddad..fd1159efbf3d 100644 --- a/plugins/ui-designer/src/com/intellij/uiDesigner/propertyInspector/PropertyInspectorTable.java +++ b/plugins/ui-designer/src/com/intellij/uiDesigner/propertyInspector/PropertyInspectorTable.java @@ -40,6 +40,7 @@ import com.intellij.uiDesigner.designSurface.GuiEditor; import com.intellij.uiDesigner.palette.Palette; import com.intellij.uiDesigner.propertyInspector.properties.*; import com.intellij.uiDesigner.radComponents.*; +import com.intellij.util.ExceptionUtilRt; import com.intellij.util.ui.EmptyIcon; import com.intellij.util.ui.IndentedIcon; import com.intellij.util.ui.UIUtil; @@ -59,7 +60,6 @@ import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.*; @@ -875,10 +875,8 @@ public final class PropertyInspectorTable extends JBTable implements DataProvide } catch (Throwable e) { LOG.debug(e); - if(e instanceof InvocationTargetException){ // special handling of warapped exceptions - e = ((InvocationTargetException)e).getTargetException(); - } - Messages.showMessageDialog(e.getMessage(), UIDesignerBundle.message("title.invalid.input"), Messages.getErrorIcon()); + String message = ExceptionUtilRt.unwrapInvocationTargetException(e).getMessage(); + Messages.showMessageDialog(message, UIDesignerBundle.message("title.invalid.input"), Messages.getErrorIcon()); return false; } return true;