method refs: check access problems

This commit is contained in:
anna
2012-10-04 14:00:52 +02:00
parent 120111d01b
commit 9f5d27f863
4 changed files with 45 additions and 0 deletions

View File

@@ -977,6 +977,10 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
if (myRefCountHolder != null) {
myRefCountHolder.registerReference(expression, result);
}
if (result.getElement() != null && !result.isAccessible()) {
final String accessProblem = HighlightUtil.buildProblemWithAccessDescription(expression, result);
myHolder.add(HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, expression, accessProblem));
}
if (!myHolder.hasErrorResults()) {
final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
if (LambdaUtil.dependsOnTypeParams(functionalInterfaceType, functionalInterfaceType, expression, null)) {

View File

@@ -302,6 +302,7 @@ public class PsiMethodReferenceExpressionImpl extends PsiReferenceExpressionBase
};
processor.setIsConstructor(isConstructor);
processor.setName(isConstructor ? containingClass.getName() : element.getText());
processor.setAccessClass(containingClass);
if (beginsWithReferenceType) {
if (containingClass.getContainingClass() == null || !containingClass.hasModifierProperty(PsiModifier.STATIC)) {

View File

@@ -0,0 +1,36 @@
class MyTest {
private static class Foo {
static int foo(int i) { return i; }
}
static Foo foo = new Foo();
static void foo(String s) {}
static void bar(Integer i, Number n) {}
static void bar(Number n, Integer i) {}
void baz(int i) {}
}
class AlienTest {
interface IInt {
int _(int i);
}
interface IIntInt {
int _(Integer i1, Integer i2);
}
static {
<error descr="Incompatible types. Found: '<method reference>', required: 'AlienTest.IInt'">IInt i1 = MyTest::abracadabra;</error>
<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 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

@@ -97,6 +97,10 @@ public class MethodRefHighlightingTest extends LightDaemonAnalyzerTestCase {
doTest();
}
public void testAccessModifiers() throws Exception {
doTest();
}
public void testReturnTypeSpecific() throws Exception {
doTest(true);
}