From cbbd0e4456aeda4949f05616e36a5393881b9caa Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 13 Nov 2015 13:10:52 +0100 Subject: [PATCH] detect array initializer type based on short variable array notation (IDEA-147881) --- .../src/com/intellij/psi/util/PsiTypesUtil.java | 6 ++++++ ...ExpectedTypeBasedOnArrayCreationWithoutExplicitType.java | 6 ++++++ .../daemon/lambda/GenericsHighlighting8Test.java | 4 ++++ 3 files changed, 16 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/ExpectedTypeBasedOnArrayCreationWithoutExplicitType.java diff --git a/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java b/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java index ad93972d3a5a..bd5e4d45ba85 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java @@ -218,6 +218,12 @@ public class PsiTypesUtil { return ((PsiArrayType)type).getComponentType(); } } + else if (gParent instanceof PsiVariable) { + final PsiType type = ((PsiVariable)gParent).getType(); + if (type instanceof PsiArrayType) { + return ((PsiArrayType)type).getComponentType(); + } + } else if (gParent instanceof PsiArrayInitializerExpression) { final PsiType expectedTypeByParent = getExpectedTypeByParent((PsiExpression)parent); return expectedTypeByParent != null && expectedTypeByParent instanceof PsiArrayType diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/ExpectedTypeBasedOnArrayCreationWithoutExplicitType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/ExpectedTypeBasedOnArrayCreationWithoutExplicitType.java new file mode 100644 index 000000000000..5ae565a29142 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting8/ExpectedTypeBasedOnArrayCreationWithoutExplicitType.java @@ -0,0 +1,6 @@ +class Test { + { + Object x = null; + Object[] objs = {x == null ? "N/A" : x}; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GenericsHighlighting8Test.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GenericsHighlighting8Test.java index 5201367aa1bf..b2a2654d1c82 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GenericsHighlighting8Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/GenericsHighlighting8Test.java @@ -877,4 +877,8 @@ public class GenericsHighlighting8Test extends LightDaemonAnalyzerTestCase { public void testUncheckedWarningsWhenInferredTypeLeadsToRawRoGenericAssignment() throws Exception { doTest(true); } + + public void testExpectedTypeBasedOnArrayCreationWithoutExplicitType() throws Exception { + doTest(); + } }