From 7f91247966c41edebe817426093b4d39d25b3ff3 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Mon, 4 Dec 2017 14:51:15 +0100 Subject: [PATCH] surround with auto-closeable: ensure final is restored from existing var otherwise with generateFinalLocals=true, non-final variables could be made final which leads to red code --- .../intention/impl/SurroundAutoCloseableAction.java | 4 +++- .../CommentsInVarDeclaration.java | 3 ++- .../CommentsInVarDeclaration_after.java | 7 +++++-- .../intention/SurroundAutoCloseableActionTest.java | 13 ++++++++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java index 93417bdb4de8..88a35f376472 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SurroundAutoCloseableAction.java @@ -203,7 +203,9 @@ public class SurroundAutoCloseableAction extends PsiElementBaseIntentionAction { String name = var.getName(); assert name != null : child.getText(); - toFormat.add(parent.addBefore(factory.createVariableDeclarationStatement(name, var.getType(), null), statement)); + PsiDeclarationStatement declarationStatement = factory.createVariableDeclarationStatement(name, var.getType(), null); + PsiUtil.setModifierProperty((PsiLocalVariable)declarationStatement.getDeclaredElements()[0], PsiModifier.FINAL, var.hasModifierProperty(PsiModifier.FINAL)); + toFormat.add(parent.addBefore(declarationStatement, statement)); CommentTracker commentTracker = new CommentTracker(); PsiExpression varInit = var.getInitializer(); diff --git a/java/java-tests/testData/codeInsight/surroundAutoCloseable/CommentsInVarDeclaration.java b/java/java-tests/testData/codeInsight/surroundAutoCloseable/CommentsInVarDeclaration.java index 1ba64d1fafc9..f2c3b0cc5feb 100644 --- a/java/java-tests/testData/codeInsight/surroundAutoCloseable/CommentsInVarDeclaration.java +++ b/java/java-tests/testData/codeInsight/surroundAutoCloseable/CommentsInVarDeclaration.java @@ -6,6 +6,7 @@ class C { void m(File file) throws IOException { FileInputStream fileInputStream = new FileInputStream(file); String s = "initial value";//Non-NLS - String bar = s + fileInputStream.read(); + s = s + fileInputStream.read(); + s += "end"; } } diff --git a/java/java-tests/testData/codeInsight/surroundAutoCloseable/CommentsInVarDeclaration_after.java b/java/java-tests/testData/codeInsight/surroundAutoCloseable/CommentsInVarDeclaration_after.java index 1f337be55908..d4ef7c1d4fd4 100644 --- a/java/java-tests/testData/codeInsight/surroundAutoCloseable/CommentsInVarDeclaration_after.java +++ b/java/java-tests/testData/codeInsight/surroundAutoCloseable/CommentsInVarDeclaration_after.java @@ -4,9 +4,12 @@ import java.io.IOException; class C { void m(File file) throws IOException { + String s; try (FileInputStream fileInputStream = new FileInputStream(file)) { - String s = "initial value";//Non-NLS - String bar = s + fileInputStream.read(); + //Non-NLS + s = "initial value"; + s = s + fileInputStream.read(); } + s += "end"; } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/SurroundAutoCloseableActionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/SurroundAutoCloseableActionTest.java index 73083076ee20..8f088d62bc3b 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/SurroundAutoCloseableActionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/SurroundAutoCloseableActionTest.java @@ -17,6 +17,7 @@ package com.intellij.java.codeInsight.intention; import com.intellij.JavaTestUtil; import com.intellij.codeInsight.CodeInsightBundle; +import com.intellij.psi.codeStyle.JavaCodeStyleSettings; import com.intellij.testFramework.fixtures.CodeInsightTestUtil; import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; @@ -34,7 +35,17 @@ public class SurroundAutoCloseableActionTest extends LightCodeInsightFixtureTest public void testSplitVar() { doTest(); } public void testExpression() { doTest(); } public void testExpressionIncomplete() { doTest(); } - public void testCommentsInVarDeclaration() { doTest(); } + public void testCommentsInVarDeclaration() { + JavaCodeStyleSettings styleSettings = JavaCodeStyleSettings.getInstance(getProject()); + boolean finalLocals = styleSettings.GENERATE_FINAL_LOCALS; + try { + styleSettings.GENERATE_FINAL_LOCALS = true; + doTest(); + } + finally { + styleSettings.GENERATE_FINAL_LOCALS = finalLocals; + } + } private void doTest() { String name = getTestName(false);