new inference: method refs: apply reference rules first

This commit is contained in:
Anna Kozlova
2014-02-27 20:35:26 +01:00
parent cf7dff45a9
commit e7cb908c95
7 changed files with 61 additions and 24 deletions

View File

@@ -639,20 +639,26 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase
}
}
if (myQualifierResolveResult.isReferenceTypeQualified() && getReferenceNameElement() instanceof PsiIdentifier) {
//If the first search produces a static method, and no non-static method is applicable for the second search, then the result of the first search is the compile-time declaration.
CandidateInfo candidateInfo = filterStaticCorrectCandidates(firstCandidates, secondCandidates, true);
if (candidateInfo != null) {
return candidateInfo;
}
//If the second search produces a non-static method, and no static method is applicable for the first search, then the result of the second search is the compile-time declaration.
candidateInfo = filterStaticCorrectCandidates(secondCandidates, firstCandidates, false);
if (candidateInfo != null) {
return candidateInfo;
}
}
checkSpecifics(firstCandidates,
varargs ? MethodCandidateInfo.ApplicabilityLevel.VARARGS : MethodCandidateInfo.ApplicabilityLevel.FIXED_ARITY, myLanguageLevel);
checkSpecifics(secondCandidates,
varargs ? MethodCandidateInfo.ApplicabilityLevel.VARARGS : MethodCandidateInfo.ApplicabilityLevel.FIXED_ARITY, myLanguageLevel);
if (myQualifierResolveResult.isReferenceTypeQualified() && getReferenceNameElement() instanceof PsiIdentifier) {
//If the first search produces a static method, and no non-static method is applicable for the second search, then the result of the first search is the compile-time declaration.
filterStaticCorrectCandidates(firstCandidates, true);
//If the second search produces a non-static method, and no static method is applicable for the first search, then the result of the second search is the compile-time declaration.
filterStaticCorrectCandidates(secondCandidates, false);
}
final int acceptedCount = firstCandidates.size() + secondCandidates.size();
if (acceptedCount == 1) {
return !firstCandidates.isEmpty() ? firstCandidates.get(0) : secondCandidates.get(0);
@@ -685,17 +691,29 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase
/**
* 15.13.1
*/
private void filterStaticCorrectCandidates(List<CandidateInfo> firstCandidates,
private CandidateInfo filterStaticCorrectCandidates(List<CandidateInfo> firstCandidates,
List<CandidateInfo> secondCandidates,
boolean shouldBeStatic) {
for (Iterator<CandidateInfo> iterator = firstCandidates.iterator(); iterator.hasNext(); ) {
final PsiElement element = iterator.next().getElement();
if (firstCandidates.size() == 1) {
final CandidateInfo candidateInfo = firstCandidates.get(0);
final PsiElement element = candidateInfo.getElement();
if (element instanceof PsiMethod) {
final boolean isStatic = ((PsiMethod)element).hasModifierProperty(PsiModifier.STATIC);
if (shouldBeStatic && !isStatic || !shouldBeStatic && isStatic) {
iterator.remove();
if (shouldBeStatic && isStatic || !shouldBeStatic && !isStatic) {
for (CandidateInfo secondCandidate : secondCandidates) {
final PsiElement psiElement = secondCandidate.getElement();
if (psiElement instanceof PsiMethod) {
final boolean oppositeStatic = ((PsiMethod)psiElement).hasModifierProperty(PsiModifier.STATIC);
if (shouldBeStatic && !oppositeStatic || !shouldBeStatic && oppositeStatic) {
return null;
}
}
}
return candidateInfo;
}
}
}
return null;
}
private boolean isCorrectAssignment(PsiType[] signatureParameterTypes2,

View File

@@ -29,7 +29,7 @@ class AlienTest {
<error descr="Incompatible types. Found: '<method reference>', required: 'AlienTest.IInt'">IInt i2 = MyTest::foo;</error>
<error descr="Incompatible types. Found: '<method reference>', required: 'AlienTest.IInt'">IInt i3 = MyTest::bar;</error>
<error descr="Incompatible types. Found: '<method reference>', required: 'AlienTest.IIntInt'">IIntInt i4 = MyTest::bar;</error>
<error descr="Incompatible types. Found: '<method reference>', required: 'AlienTest.IInt'">IInt i5 = MyTest::baz;</error>
IInt i5 = <error descr="Non-static method cannot be referenced from a static context">MyTest::baz</error>;
IInt i6 = <error descr="'foo(int)' is not public in 'MyTest.Foo'. Cannot be accessed from outside package">MyTest.foo::foo</error>;
IInt i7 = MyTest.<error descr="'MyTest.Foo' has private access in 'MyTest'">Foo</error>::foo;
}

View File

@@ -41,14 +41,14 @@ class MyTest1 {
static void call2(I2 s) { }
static void test1() {
<error descr="Incompatible types. Found: '<method reference>', required: 'MyTest1.I1'">I1 s1 = MyTest1 ::m1;</error>
call1<error descr="'call1(MyTest1.I1)' in 'MyTest1' cannot be applied to '(<method reference>)'">(MyTest1::m1)</error>;
I1 s1 = <error descr="Non-static method cannot be referenced from a static context">MyTest1 ::m1</error>;
call1(<error descr="Non-static method cannot be referenced from a static context">MyTest1::m1</error>);
I1 s2 = MyTest1 :: m2;
call1(MyTest1::m2);
I1 s3 = MyTest1::m3;
call1(MyTest1::m3);
<error descr="Incompatible types. Found: '<method reference>', required: 'MyTest1.I1'">I1 s4 = MyTest1::m4;</error>
call1<error descr="'call1(MyTest1.I1)' in 'MyTest1' cannot be applied to '(<method reference>)'">(MyTest1::m4)</error>;
I1 s4 = <error descr="Non-static method cannot be referenced from a static context">MyTest1::m4</error>;
call1(<error descr="Non-static method cannot be referenced from a static context">MyTest1::m4</error>);
}
static void test2() {
@@ -90,14 +90,14 @@ class MyTest2 {
static void call2(I2 s) { }
static void test1() {
<error descr="Incompatible types. Found: '<method reference>', required: 'MyTest2.I1'">I1 s1 = MyTest2 ::m1;</error>
call1<error descr="'call1(MyTest2.I1)' in 'MyTest2' cannot be applied to '(<method reference>)'">(MyTest2::m1)</error>;
I1 s1 = <error descr="Non-static method cannot be referenced from a static context">MyTest2 ::m1</error>;
call1(<error descr="Non-static method cannot be referenced from a static context">MyTest2::m1</error>);
I1 s2 = MyTest2 :: m2;
call1(MyTest2::m2);
I1 s3 = MyTest2::m3;
call1(MyTest2::m3);
<error descr="Incompatible types. Found: '<method reference>', required: 'MyTest2.I1'">I1 s4 = MyTest2::m4;</error>
call1<error descr="'call1(MyTest2.I1)' in 'MyTest2' cannot be applied to '(<method reference>)'">(MyTest2::m4)</error>;
I1 s4 = <error descr="Non-static method cannot be referenced from a static context">MyTest2::m4</error>;
call1(<error descr="Non-static method cannot be referenced from a static context">MyTest2::m4</error>);
}
static void test2() {

View File

@@ -55,7 +55,7 @@ class MyTest {
{
<error descr="Incompatible types. Found: '<method reference>', required: 'MyTest.I1'">I1 i_1 = MyTest::_1;</error>
I1 i_1 = <error descr="Non-static method cannot be referenced from a static context">MyTest::_1</error>;
<error descr="Incompatible types. Found: '<method reference>', required: 'MyTest.I1'">I1 i_2 = MyTest::_2;</error>
<error descr="Incompatible types. Found: '<method reference>', required: 'MyTest.I1'">I1 i_3 = MyTest::_3;</error>
<error descr="Incompatible types. Found: '<method reference>', required: 'MyTest.I1'">I1 i_4 = MyTest::_4;</error>

View File

@@ -3,7 +3,7 @@ import java.util.*;
class Test {
void test() {
Comparator<Test> r2 = Test::yyy;
<error descr="Incompatible types. Found: '<method reference>', required: 'Comparator1<Test>'">Comparator1<Test> c1 = Test::yyy;</error>
Comparator1<Test> c1 = <error descr="Non-static method cannot be referenced from a static context">Test::yyy</error>;
<error descr="Incompatible types. Found: '<method reference>', required: 'Comparator1<Test>'">Comparator1<Test> c2 = Test::xxx;</error>
}
int yyy(Test... p) { return 1; }

View File

@@ -0,0 +1,15 @@
class Test {
interface I {
void m(Test rec, String s);
}
void m(Test t, String s) {}
void m(String s) {}
static void m(Test t, Object s) {}
static void test() {
<error descr="Incompatible types. Found: '<method reference>', required: 'Test.I'">I i = Test::m;</error>
}
}

View File

@@ -177,6 +177,10 @@ public class NewMethodRefHighlightingTest extends LightDaemonAnalyzerTestCase {
doTest();
}
public void testStaticNonStaticReferenceTypeAmbiguity() throws Exception {
doTest();
}
private void doTest() {
doTest(false);
}