From 1a5768f1cdb7c7ba6cbaf1faeaf7bd01e21bbd30 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 20 Jun 2018 13:07:18 +0300 Subject: [PATCH] surround with try/catch: replace var with explicit type (IDEA-191069) when declaration has to be split --- .../surroundWith/SurroundWithUtil.java | 6 ++++++ .../afterVarToBeReplacedWithExplicitTYpe.java | 16 ++++++++++++++++ .../beforeVarToBeReplacedWithExplicitTYpe.java | 11 +++++++++++ 3 files changed, 33 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/afterVarToBeReplacedWithExplicitTYpe.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/beforeVarToBeReplacedWithExplicitTYpe.java diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithUtil.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithUtil.java index b75e6dd5821e..b38a0ee98912 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/SurroundWithUtil.java @@ -78,6 +78,12 @@ public class SurroundWithUtil { PsiVariable var = (PsiVariable)element1; PsiExpression initializer = var.getInitializer(); if (initializer != null) { + PsiTypeElement typeElement = var.getTypeElement(); + if (typeElement != null && + typeElement.isInferredType() && + PsiTypesUtil.replaceWithExplicitType(typeElement) == null) { + continue; + } if (!generateInitializers || var.hasModifierProperty(PsiModifier.FINAL)) { initializer.delete(); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/afterVarToBeReplacedWithExplicitTYpe.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/afterVarToBeReplacedWithExplicitTYpe.java new file mode 100644 index 000000000000..98ed7cce79d8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/afterVarToBeReplacedWithExplicitTYpe.java @@ -0,0 +1,16 @@ +// "Surround with try/catch" "true" +public class Test { + public static void main(String[] args) { + Object foo = null; + try { + foo = bar(); + } catch (Exception e) { + e.printStackTrace(); + } + foo.toString(); + } + + private static Object bar() throws Exception { + return null; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/beforeVarToBeReplacedWithExplicitTYpe.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/beforeVarToBeReplacedWithExplicitTYpe.java new file mode 100644 index 000000000000..c345aba12432 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/beforeVarToBeReplacedWithExplicitTYpe.java @@ -0,0 +1,11 @@ +// "Surround with try/catch" "true" +public class Test { + public static void main(String[] args) { + var foo = bar(); + foo.toString(); + } + + private static Object bar() throws Exception { + return null; + } +} \ No newline at end of file