From 10c335b44e81678964d052adbfb9108005d0bb05 Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Mon, 27 Jun 2016 15:36:28 +0300 Subject: [PATCH] Java inspection: In "Join Declaration and Assignment" intention action warn about side effects of the initializer being removed (IDEA-157727) --- .../impl/JoinDeclarationAndAssignmentAction.java | 10 +++++++--- .../intellij/codeInspection/RemoveInitializerFix.java | 10 +++++----- .../quickFix/joinDeclaration/afterSideEffect.java | 11 +++++++++++ .../quickFix/joinDeclaration/beforeSideEffect.java | 11 +++++++++++ 4 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/joinDeclaration/afterSideEffect.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/joinDeclaration/beforeSideEffect.java diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/JoinDeclarationAndAssignmentAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/JoinDeclarationAndAssignmentAction.java index bdf63bcc5061..ac595190f917 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/JoinDeclarationAndAssignmentAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/JoinDeclarationAndAssignmentAction.java @@ -19,6 +19,7 @@ import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.codeInsight.FileModificationService; import com.intellij.codeInsight.editorActions.DeclarationJoinLinesHandler; import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; +import com.intellij.codeInspection.RemoveInitializerFix; import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; @@ -70,7 +71,7 @@ public class JoinDeclarationAndAssignmentAction extends PsiElementBaseIntentionA if (ReferencesSearch.search(variable, new LocalSearchScope(rExpression), false).findFirst() != null) { return null; } - return Pair.create(variable, assignmentExpression); + return Pair.createNonNull(variable, assignmentExpression); } } } @@ -104,12 +105,15 @@ public class JoinDeclarationAndAssignmentAction extends PsiElementBaseIntentionA if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return; final Pair pair = getPair(element); + if (pair == null) return; final PsiLocalVariable variable = pair.getFirst(); final PsiAssignmentExpression assignmentExpression = pair.getSecond(); + final PsiExpression initializer = variable.getInitializer(); + if (initializer != null && assignmentExpression.getOperationTokenType() == JavaTokenType.EQ) { + RemoveInitializerFix.sideEffectAwareRemove(project, initializer, initializer, variable); + } final PsiExpression initializerExpression = DeclarationJoinLinesHandler.getInitializerExpression(variable, assignmentExpression); variable.setInitializer(initializerExpression); assignmentExpression.delete(); } - - } diff --git a/java/java-impl/src/com/intellij/codeInspection/RemoveInitializerFix.java b/java/java-impl/src/com/intellij/codeInspection/RemoveInitializerFix.java index af596325bad0..376d9e308ff1 100644 --- a/java/java-impl/src/com/intellij/codeInspection/RemoveInitializerFix.java +++ b/java/java-impl/src/com/intellij/codeInspection/RemoveInitializerFix.java @@ -49,10 +49,10 @@ public class RemoveInitializerFix implements LocalQuickFix { sideEffectAwareRemove(project, (PsiExpression)psiInitializer, psiInitializer, variable); } - protected static void sideEffectAwareRemove(Project project, - PsiExpression psiInitializer, - PsiElement elementToDelete, - PsiVariable variable) { + public static void sideEffectAwareRemove(Project project, + PsiExpression psiInitializer, + PsiElement elementToDelete, + PsiVariable variable) { if (!FileModificationService.getInstance().prepareFileForWrite(elementToDelete.getContainingFile())) return; final PsiElement declaration = variable.getParent(); @@ -82,7 +82,7 @@ public class RemoveInitializerFix implements LocalQuickFix { if (parent instanceof PsiExpressionStatement) { parent.replace(statementFromText); } else { - declaration.getParent().addAfter(statementFromText, declaration); + declaration.getParent().addBefore(statementFromText, declaration); elementToDelete.delete(); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/joinDeclaration/afterSideEffect.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/joinDeclaration/afterSideEffect.java new file mode 100644 index 000000000000..2b3e56bf230f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/joinDeclaration/afterSideEffect.java @@ -0,0 +1,11 @@ +// "Join declaration and assignment" "true" +class T { + { + foo(1); + int a = foo(2); + } + static int foo(int n) { + System.out.println(n); + return n + 1; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/joinDeclaration/beforeSideEffect.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/joinDeclaration/beforeSideEffect.java new file mode 100644 index 000000000000..cbcff02921de --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/joinDeclaration/beforeSideEffect.java @@ -0,0 +1,11 @@ +// "Join declaration and assignment" "true" +class T { + { + int a = foo(1); + a = foo(2); + } + static int foo(int n) { + System.out.println(n); + return n + 1; + } +} \ No newline at end of file