method refs: check if interface functional (IDEA-125511)

This commit is contained in:
Anna Kozlova
2014-05-26 09:19:31 +04:00
parent 5e7d32c94e
commit eaa9ce63f6
4 changed files with 25 additions and 6 deletions

View File

@@ -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)) {

View File

@@ -38,5 +38,5 @@ class Test2 {
void foo(Integer i) {}
<error descr="Incompatible types. Found: '<method reference>', required: 'java.lang.Object'">Object o = Test2::foo;</error>
Object o = <error descr="Object is not a functional interface">Test2::foo</error>;
}

View File

@@ -0,0 +1,8 @@
class Java8 {
public void test() {
}
private int m() {
return <error descr="int is not a functional interface">Java8::test</error>;
}
}

View File

@@ -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);