Skip to content

Commit

Permalink
Fix for #960: skip trait bridge methods for implicit-this expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 22, 2019
1 parent dace0f4 commit 7e1ae12
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -510,6 +510,52 @@ public void testGetterAndField13a() {
assertUnknownConfidence(contents, start, end, "Other", false);
}

@Test
public void testSetterAndField1() {
String contents =
"class Foo {\n" +
" String xxx\n" +
" void setXxx(String xxx) { this.xxx = xxx }\n" +
" void meth() { def closure = { xxx = ''; this.xxx = '' } }\n" +
"}";

int offset = contents.indexOf("xxx", contents.indexOf("meth"));
assertDeclaration(contents, offset, offset + 3, "Foo", "setXxx", DeclarationKind.METHOD);

offset = contents.lastIndexOf("xxx");
assertDeclaration(contents, offset, offset + 3, "Foo", "setXxx", DeclarationKind.METHOD);
}

@Test
public void testSetterAndField2() {
createUnit("Foo",
"class Foo {\n" +
" String xxx\n" +
" void setXxx(String xxx) { this.xxx = xxx }\n" +
"}");

String contents =
"class Bar extends Foo {\n" +
" String yyy\n" +
" def meth() {\n" +
" xxx = yyy\n" +
" this.xxx = this.yyy\n" +
" }\n" +
"}";

int offset = contents.indexOf("xxx");
assertDeclaration(contents, offset, offset + 3, "Foo", "setXxx", DeclarationKind.METHOD);

offset = contents.indexOf("yyy", contents.indexOf("meth"));
assertDeclaration(contents, offset, offset + 3, "Bar", "yyy", DeclarationKind.FIELD);

offset = contents.lastIndexOf("xxx");
assertDeclaration(contents, offset, offset + 3, "Foo", "setXxx", DeclarationKind.METHOD);

offset = contents.lastIndexOf("yyy");
assertDeclaration(contents, offset, offset + 3, "Bar", "yyy", DeclarationKind.PROPERTY);
}

@Test
public void testLocalAndFieldWithSameName() {
String contents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.eclipse.jdt.core.groovy.tests.search;

import org.junit.Ignore;
import org.junit.Test;

public final class TraitInferencingTests extends InferencingTestSuite {
Expand All @@ -32,7 +33,222 @@ private void assertExprType(String source, String target, String type) {
//--------------------------------------------------------------------------

@Test
public void testPublicMethod() {
public void testProperty1() {
String source =
"trait T {\n" +
" Number number\n" +
" void meth() {\n" +
" println number\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Number");
}

@Test @Ignore
public void testProperty2() {
String source =
"trait T {\n" +
" Number number\n" +
" void meth() {\n" +
" number = 42\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Number");
}

@Test @Ignore
public void testProperty3() {
String source =
"trait T {\n" +
" Number number\n" +
" void meth() {\n" +
" println this.number\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Number");
}

@Test @Ignore
public void testProperty4() {
String source =
"trait T {\n" +
" Number number\n" +
" void meth() {\n" +
" this.number = 42\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Number");
}

@Test
public void testProperty5() {
String source =
"trait T {\n" +
" Number number\n" +
" void meth() {\n" +
" println getNumber()\n" +
" }\n" +
"}\n";

assertDeclType(source, "getNumber", "T");
assertExprType(source, "getNumber", "java.lang.Number");
}

@Test
public void testProperty6() {
String source =
"trait T {\n" +
" Number number\n" +
" void meth() {\n" +
" setNumber(42)\n" +
" }\n" +
"}\n";

assertDeclType(source, "setNumber", "T");
assertExprType(source, "setNumber", "java.lang.Void");
}

@Test
public void testProperty7() {
String source =
"trait T {\n" +
" Number number\n" +
"}\n" +
"class C implements T {\n" +
" void meth() {\n" +
" println number\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Number");
}

@Test
public void testProperty8() {
String source =
"trait T {\n" +
" Number number\n" +
"}\n" +
"class C implements T {\n" +
" void meth() {\n" +
" number = 42\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Void");
}

@Test
public void testProperty9() {
String source =
"trait T {\n" +
" Number number\n" +
"}\n" +
"class C implements T {\n" +
" void meth() {\n" +
" println this.number\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Number");
}

@Test
public void testProperty10() {
String source =
"trait T {\n" +
" Number number\n" +
"}\n" +
"class C implements T {\n" +
" void meth() {\n" +
" this.number = 42\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Void");
}

@Test
public void testProperty11() {
String source =
"trait T {\n" +
" Number number\n" +
"}\n" +
"class C implements T {\n" +
" void meth() {\n" +
" println getNumber()\n" +
" }\n" +
"}\n";

assertDeclType(source, "getNumber", "T");
assertExprType(source, "getNumber", "java.lang.Number");
}

@Test
public void testProperty12() {
String source =
"trait T {\n" +
" Number number\n" +
"}\n" +
"class C implements T {\n" +
" void meth() {\n" +
" setNumber(42)\n" +
" }\n" +
"}\n";

assertDeclType(source, "setNumber", "T");
assertExprType(source, "setNumber", "java.lang.Void");
}

@Test
public void testProperty13() {
String source =
"trait T {\n" +
" Number number\n" +
"}\n" +
"class C implements T {\n" +
" void meth() {\n" +
" println T.super.number\n" +
" }\n" +
"}\n";

// TODO: assertDeclType(source, "number", "T");
// TODO: assertExprType(source, "number", "java.lang.Number");
assertUnknownConfidence(source, source.lastIndexOf("number"), source.lastIndexOf("number") + "number".length(), "T", false);
}

@Test @Ignore
public void testProperty14() {
String source =
"trait T {\n" +
" Number number\n" +
"}\n" +
"class C implements T {\n" +
" void meth() {\n" +
" T.super.number = 42\n" +
" }\n" +
"}\n";

assertDeclType(source, "number", "T");
assertExprType(source, "number", "java.lang.Void");
}

//

@Test
public void testPublicMethod1() {
String source =
"trait Auditable {\n" +
" boolean check() {\n" +
Expand Down Expand Up @@ -107,7 +323,7 @@ public void testPublicMethod4() {
}

@Test // https://issues.apache.org/jira/browse/GROOVY-8272
public void testPublicStaticSuperMethod() {
public void testPublicStaticSuperMethod1() {
String source =
"trait Checkable {\n" +
" static boolean check() {\n" +
Expand Down Expand Up @@ -147,7 +363,7 @@ public void testPublicStaticSuperMethod2() {
}

@Test
public void testPublicField() {
public void testPublicField1() {
String source =
"trait T {\n" +
" public String field\n" +
Expand Down Expand Up @@ -207,7 +423,7 @@ public void testPublicStaticFinalField() {
//

@Test
public void testPrivateMethod() {
public void testPrivateMethod1() {
String source =
"trait Auditable {\n" +
" private boolean check() {\n" +
Expand Down Expand Up @@ -300,7 +516,7 @@ public void testPrivateStaticMethod() {
}

@Test
public void testPrivateField() {
public void testPrivateField1() {
String source =
"trait T {\n" +
" private String field\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ protected TypeLookupResult findTypeForVariable(final VariableExpression var, fin
resolvedDeclaringType = getMorePreciseType(resolvedDeclaringType, variableInfo);
ASTNode candidate = findDeclarationForDynamicVariable(var, resolvedDeclaringType, scope, resolveStrategy);
if (candidate != null && (!(candidate instanceof MethodNode) || scope.isMethodCall() ||
(AccessorSupport.isGetter((MethodNode) candidate) && !var.getName().equals(((MethodNode) candidate).getName())))) {
((AccessorSupport.isGetter((MethodNode) candidate) || AccessorSupport.isSetter((MethodNode) candidate)) && !var.getName().equals(((MethodNode) candidate).getName())))) {
if (candidate instanceof FieldNode) {
FieldNode field = (FieldNode) candidate;
ClassNode owner = field.getDeclaringClass();
Expand All @@ -501,7 +501,7 @@ protected TypeLookupResult findTypeForVariable(final VariableExpression var, fin
if (argumentTypes != null && isLooseMatch(argumentTypes, parameterNodes)) {
confidence = TypeConfidence.LOOSELY_INFERRED;
}
if (Flags.isPrivate(((MethodNode) candidate).getModifiers()) && isNotThisOrOuterClass(resolvedDeclaringType, ((MethodNode) candidate).getDeclaringClass())) {
if ((((MethodNode) candidate).isPrivate()) && isNotThisOrOuterClass(resolvedDeclaringType, ((MethodNode) candidate).getDeclaringClass())) {
confidence = TypeConfidence.UNKNOWN; // reference to private method of super class yields MissingMethodException
}
}
Expand Down Expand Up @@ -763,7 +763,7 @@ protected static MethodNode findMethodDeclaration0(final List<MethodNode> candid

protected static Optional<MethodNode> findPropertyAccessorMethod(final String propertyName, final ClassNode declaringType, final boolean isLhsExpression, final boolean isStaticExpression, final List<ClassNode> methodCallArgumentTypes) {
Stream<MethodNode> accessors = AccessorSupport.findAccessorMethodsForPropertyName(propertyName, declaringType, false, !isLhsExpression ? READER : WRITER);
accessors = accessors.filter(accessor -> isCompatible(accessor, isStaticExpression));
accessors = accessors.filter(accessor -> isCompatible(accessor, isStaticExpression) && !isTraitBridge(accessor));
if (isLhsExpression) {
// use methodCallArgumentTypes to select closer match
accessors = accessors.sorted((m1, m2) -> (m1 == closer(m2, m1, methodCallArgumentTypes) ? -1 : +1));
Expand Down

0 comments on commit 7e1ae12

Please sign in to comment.