IDEA-112555 Bad code is green with method references on instance

This commit is contained in:
Anna Kozlova
2013-08-26 18:07:34 +04:00
parent 8f5139db60
commit 04a76c3faf
4 changed files with 30 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -107,6 +107,15 @@ public class PsiMethodReferenceUtil {
}
return new QualifierResolveResult(containingClass, substitutor, false);
}
public static boolean isStaticallyReferenced(@NotNull PsiMethodReferenceExpression methodReferenceExpression) {
final PsiExpression qualifierExpression = methodReferenceExpression.getQualifierExpression();
if (qualifierExpression != null) {
return qualifierExpression instanceof PsiReferenceExpression &&
((PsiReferenceExpression)qualifierExpression).resolve() instanceof PsiClass;
}
return true;
}
public static boolean isAcceptable(@Nullable final PsiMethodReferenceExpression methodReferenceExpression, PsiType left) {
if (methodReferenceExpression == null) return false;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -427,7 +427,9 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase
boolean hasReceiver = false;
final PsiType[] parameterTypes = mySignature.getParameterTypes();
if (parameterTypes.length > 0 && PsiMethodReferenceUtil.isReceiverType(parameterTypes[0], myContainingClass, mySubstitutor)) {
if (parameterTypes.length > 0 &&
PsiMethodReferenceUtil.isReceiverType(parameterTypes[0], myContainingClass, mySubstitutor) &&
PsiMethodReferenceUtil.isStaticallyReferenced(PsiMethodReferenceExpressionImpl.this)) {
hasReceiver = true;
}

View File

@@ -0,0 +1,15 @@
class ThreadExample {
interface Function<T, R> {
R apply(T t);
}
{
A a = new A();
<error descr="Incompatible types. Found: '<method reference>', required: 'ThreadExample.Function<? super ThreadExample.A,? extends java.lang.String>'">Function<? super A,? extends String> foo = a::foo;</error>
}
static class A {
public String foo() { return "a"; }
}
}

View File

@@ -78,6 +78,7 @@ public class MethodRefHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testAbstractMethod() { doTest(); }
public void testMethodRefAcceptance() { doTest(); }
public void testVarargsMethodRef() { doTest(); }
public void testExprReceiver() { doTest(); }
public void testTypeParameterWithExtendsList() throws Exception {
doTest();