From ce005dce545cbd15068a570366d2bfc3c14350b1 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 20 Jun 2022 14:25:46 +0200 Subject: [PATCH] [java-postfix] IDEA-296188 Erroneous Arrays.stream completion in array field assignment GitOrigin-RevId: b48d3b0ffaa8abd3e3676a6a02880ea5a691173b --- .../postfix/templates/StreamPostfixTemplate.java | 2 ++ .../template/postfix/templates/stream/assignment.java | 9 +++++++++ .../postfix/templates/stream/assignment_after.java | 9 +++++++++ .../postfix/templates/StreamPostfixTemplateTest.java | 4 ++++ 4 files changed, 24 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/stream/assignment.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/stream/assignment_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplate.java index 86fd754881e4..90548e2977ea 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/StreamPostfixTemplate.java @@ -27,6 +27,8 @@ public class StreamPostfixTemplate extends StringBasedPostfixTemplate { private static final Condition IS_SUPPORTED_ARRAY = element -> { if (!(element instanceof PsiExpression)) return false; + if (element instanceof PsiAssignmentExpression && element.getParent() instanceof PsiExpressionStatement) return false; + PsiType type = ((PsiExpression)element).getType(); if (!(type instanceof PsiArrayType)) return false; diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/stream/assignment.java b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/assignment.java new file mode 100644 index 000000000000..f9792758e60a --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/assignment.java @@ -0,0 +1,9 @@ +package templates; + +public class Foo { + public static void main(String[] args) { + Object[] a; + Object o; + a = o.str + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/stream/assignment_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/assignment_after.java new file mode 100644 index 000000000000..0ff273b1b8af --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/stream/assignment_after.java @@ -0,0 +1,9 @@ +package templates; + +public class Foo { + public static void main(String[] args) { + Object[] a; + Object o; + a = o.str + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/StreamPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/StreamPostfixTemplateTest.java index d5e99532725c..551170aff119 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/StreamPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/StreamPostfixTemplateTest.java @@ -44,6 +44,10 @@ public class StreamPostfixTemplateTest extends PostfixTemplateTestCase { doTest(); } + public void testAssignment() { + doTest(); + } + public void testNotAvailable() { doTest(); }