Skip to content

Commit 9cb4c71

Browse files
committed
Fix for #1378: super class private methods (GROOVY-8999, GROOVY-9851)
1 parent 506a16a commit 9cb4c71

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ public void testSuperClassMethod3() {
16421642
" protected void method() {}\n" +
16431643
"}\n" +
16441644
"class B extends A {\n" +
1645-
" void something() {\n" +
1645+
" void test() {\n" +
16461646
" method()\n" +
16471647
" }\n" +
16481648
"}\n";
@@ -1656,7 +1656,7 @@ public void testSuperClassMethod4() {
16561656
" private void method() {}\n" +
16571657
"}\n" +
16581658
"class B extends A {\n" +
1659-
" void something() {\n" +
1659+
" void test() {\n" +
16601660
" method()\n" +
16611661
" }\n" +
16621662
"}\n";
@@ -1670,25 +1670,29 @@ public void testSuperClassMethod4a() {
16701670
" private void method() {}\n" +
16711671
"}\n" +
16721672
"class B extends A {\n" +
1673-
" void something() {\n" +
1673+
" void test() {\n" +
16741674
" this.method()\n" +
16751675
" }\n" +
16761676
"}\n";
16771677
assertUnknown(contents, "method");
16781678
}
16791679

1680-
@Test
1680+
@Test // https://github.com/groovy/groovy-eclipse/issues/1378
16811681
public void testSuperClassMethod4b() {
16821682
String contents =
16831683
"class A {\n" +
16841684
" private void method() {}\n" +
16851685
"}\n" +
16861686
"class B extends A {\n" +
1687-
" void something() {\n" +
1688-
" super.method()\n" + // this is ok
1687+
" void test() {\n" +
1688+
" super.method()\n" + // GROOVY-9851
16891689
" }\n" +
16901690
"}\n";
1691-
assertDeclaringType(contents, "method", "A");
1691+
if (isAtLeastGroovy(40)) {
1692+
assertUnknown(contents, "method");
1693+
} else {
1694+
assertDeclaringType(contents, "method", "A");
1695+
}
16921696
}
16931697

16941698
@Test
@@ -1698,11 +1702,11 @@ public void testSuperClassMethod4c() {
16981702
" private void method() {}\n" +
16991703
"}\n" +
17001704
"class B extends A {\n" +
1701-
" void something() {\n" +
1702-
" super.&method\n" + // GROOVY-8999: resolves to MethodClosure, but it NPEs when called
1705+
" void test() {\n" +
1706+
" super.&method\n" + // GROOVY-9851: resolves to MethodClosure, but it fails when called
17031707
" }\n" +
17041708
"}\n";
1705-
assertDeclaringType(contents, "method", "A");
1709+
assertUnknown(contents, "method");
17061710
}
17071711

17081712
@Test

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ protected TypeLookupResult findTypeForNameWithKnownObjectExpression(final String
397397
if (isStaticObjectExpression && !field.isStatic()) {
398398
confidence = TypeConfidence.UNKNOWN;
399399
} else if (field.isPrivate()) {
400-
// "super.field" reference to private field yields MissingMethodException
400+
// "super.field" reference to private field yields MissingPropertyException
401401
if (isSuperObjectExpression(scope)) {
402402
confidence = TypeConfidence.UNKNOWN;
403403
// "this.field" reference to private field of super class yields MissingPropertyException
@@ -417,9 +417,16 @@ protected TypeLookupResult findTypeForNameWithKnownObjectExpression(final String
417417
MethodNode method = (MethodNode) declaration;
418418
if (isStaticObjectExpression && !method.isStatic() && !isStaticReferenceToInstanceMethod(scope)) {
419419
confidence = TypeConfidence.UNKNOWN;
420-
} else if (method.isPrivate() && isThisObjectExpression(scope) && isNotThisOrOuterClass(declaringType, resolvedDeclaringType)) {
421-
// "this.method()" reference to private method of super class yields MissingMethodException; "super.method()" is okay
422-
confidence = TypeConfidence.UNKNOWN;
420+
} else if (method.isPrivate()) {
421+
// "super.method()" reference to private method yields MissingMethodException
422+
if (isSuperObjectExpression(scope)) {
423+
if (scope.getEnclosingNode() instanceof MethodPointerExpression || // GROOVY-8999
424+
GroovyUtils.getGroovyVersion().getMajor() > 3) // GROOVY-9851
425+
confidence = TypeConfidence.UNKNOWN;
426+
// "this.method()" reference to private method of super class yields MissingMethodException
427+
} else if (isThisObjectExpression(scope) && isNotThisOrOuterClass(declaringType, resolvedDeclaringType)) {
428+
confidence = TypeConfidence.UNKNOWN;
429+
}
423430
} else if (method.getName().startsWith("is") && !name.startsWith("is") && !scope.isMethodCall() && isSuperObjectExpression(scope) && GroovyUtils.getGroovyVersion().getMajor() < 4) {
424431
// GROOVY-1736, GROOVY-6097: "super.name" => "super.getName()" in AsmClassGenerator
425432
String newName = "get" + MetaClassHelper.capitalize(name);
@@ -1102,6 +1109,9 @@ protected static Expression getObjectExpression(final VariableScope scope) {
11021109
if (node instanceof MethodCallExpression) {
11031110
return ((MethodCallExpression) node).getObjectExpression();
11041111
}
1112+
if (node instanceof MethodPointerExpression) {
1113+
return ((MethodPointerExpression) node).getExpression();
1114+
}
11051115
return null;
11061116
}
11071117

0 commit comments

Comments
 (0)