From eaa9ce63f6754cd6dcee38ed3eb7737752ff9a39 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 26 May 2014 09:19:31 +0400 Subject: [PATCH 1/3] method refs: check if interface functional (IDEA-125511) --- .../impl/analysis/HighlightVisitorImpl.java | 18 +++++++++++++----- .../lambda/methodRef/Assignability.java | 2 +- .../InvalidFunctionalTypeInReturnStmt.java | 8 ++++++++ .../lambda/MethodRefHighlightingTest.java | 3 +++ 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/methodRef/InvalidFunctionalTypeInReturnStmt.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java index e390d435707c..2186e43d17f1 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java @@ -1234,11 +1234,19 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh } if (!myHolder.hasErrorResults()) { final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType(); - if (functionalInterfaceType != null && LambdaUtil.dependsOnTypeParams(functionalInterfaceType, functionalInterfaceType, expression)) { - HighlightInfo result1 = - HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Cyclic inference").create(); - myHolder.add(result1); //todo[ann] append not inferred type params info - } else { + if (functionalInterfaceType != null) { + final boolean notFunctional = !LambdaUtil.isFunctionalType(functionalInterfaceType); + if (notFunctional) { + myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression) + .descriptionAndTooltip(functionalInterfaceType.getPresentableText() + " is not a functional interface").create()); + } + else if (LambdaUtil.dependsOnTypeParams(functionalInterfaceType, functionalInterfaceType, expression)) { + HighlightInfo result1 = + HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Cyclic inference").create(); + myHolder.add(result1); //todo[ann] append not inferred type params info + } + } + if (!myHolder.hasErrorResults()) { final PsiElement referenceNameElement = expression.getReferenceNameElement(); if (referenceNameElement instanceof PsiKeyword) { if (!PsiMethodReferenceUtil.isValidQualifier(expression)) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/methodRef/Assignability.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/methodRef/Assignability.java index a5dbcd07db18..de25cbcb6d90 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/methodRef/Assignability.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/methodRef/Assignability.java @@ -38,5 +38,5 @@ class Test2 { void foo(Integer i) {} - Object o = Test2::foo; + Object o = Test2::foo; } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/methodRef/InvalidFunctionalTypeInReturnStmt.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/methodRef/InvalidFunctionalTypeInReturnStmt.java new file mode 100644 index 000000000000..6beccd20be4e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/methodRef/InvalidFunctionalTypeInReturnStmt.java @@ -0,0 +1,8 @@ +class Java8 { + public void test() { + } + + private int m() { + return Java8::test; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/MethodRefHighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/MethodRefHighlightingTest.java index b5c69c1358f9..fbf8036e8e35 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/MethodRefHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/MethodRefHighlightingTest.java @@ -102,6 +102,9 @@ public class MethodRefHighlightingTest extends LightDaemonAnalyzerTestCase { public void testQualifiersInStaticContext() throws Exception { doTest(); } + public void testInvalidFunctionalTypeInReturnStmt() throws Exception { + doTest(); + } private void doTest() { doTest(false); From 1d8d3f1134473acdeb58e4515dcf70e60a228c01 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 26 May 2014 09:45:00 +0400 Subject: [PATCH 2/3] lambda: ignore results of assignment is used for expression lambda (IDEA-125473) --- .../NestedAssignmentInspection.java | 6 ++-- .../NestedAssignmentInspectionTest.java | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/assignment/NestedAssignmentInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/NestedAssignmentInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/NestedAssignmentInspection.java index e28f3238282e..d8a2fe116114 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/NestedAssignmentInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/NestedAssignmentInspection.java @@ -15,10 +15,7 @@ */ package com.siyeh.ig.assignment; -import com.intellij.psi.PsiAssignmentExpression; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiExpressionListStatement; -import com.intellij.psi.PsiExpressionStatement; +import com.intellij.psi.*; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -57,6 +54,7 @@ public class NestedAssignmentInspection extends BaseInspection { } final PsiElement grandparent = parent.getParent(); if (parent instanceof PsiExpressionStatement || + parent instanceof PsiLambdaExpression || grandparent instanceof PsiExpressionListStatement) { return; } diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/assignment/NestedAssignmentInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/assignment/NestedAssignmentInspectionTest.java new file mode 100644 index 000000000000..61474dbebf9c --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/assignment/NestedAssignmentInspectionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.siyeh.ig.assignment; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; + +public class NestedAssignmentInspectionTest extends LightInspectionTestCase { + + public void testLambda() throws Exception { + doTest("class Test {" + + " {" + + " int[] array = new int[1];" + + " Runnable r = () -> array[0] = 0;" + + " }" + + "}"); + } + + @Override + protected InspectionProfileEntry getInspection() { + return new NestedAssignmentInspection(); + } +} \ No newline at end of file From 892e199bd1c7130762192d6e63dd6ee6a08f6a9b Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 26 May 2014 10:04:58 +0400 Subject: [PATCH 3/3] fix test --- .../codeInspection/ex/GlobalInspectionContextImpl.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java index 68ace93bf894..8551542c42ad 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java @@ -649,7 +649,7 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp } }); - ApplicationManager.getApplication().invokeLater(new Runnable() { + Runnable runnable = new Runnable() { @Override public void run() { if (!FileModificationService.getInstance().preparePsiElementsForWrite(results.keySet())) return; @@ -673,7 +673,12 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp } }, commandName, null); } - }); + }; + if (ApplicationManager.getApplication().isUnitTestMode()) { + runnable.run(); + } else { + ApplicationManager.getApplication().invokeLater(runnable); + } } });