Skip to content

Commit

Permalink
Fix for issue #368: infer type of nested method call to select overload
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 7, 2017
1 parent aae372c commit 58b6382
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package org.eclipse.jdt.core.groovy.tests.search;

import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

import java.util.Comparator;
import java.util.List;

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.eclipse.core.compiler.CompilerUtils;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.eclipse.jdt.groovy.search.TypeInferencingVisitorWithRequestor;
Expand Down Expand Up @@ -2049,6 +2051,25 @@ public void testListSort2() {
assertDeclaringType(contents, start, end, jdkListSort ? "java.util.List<java.lang.Object>" : "org.codehaus.groovy.runtime.DefaultGroovyMethods");
}

@Test // https://github.com/groovy/groovy-eclipse/issues/368
public void testListRemove() {
String contents =
"class Util {\n" +
" static int remove(List<?> list, Object item) {\n" +
" //...\n" +
" }\n" +
" void doSomething() {\n" +
" List a = []\n" +
" List b = []\n" +
" // List.remove(int) or List.remove(Object)\n" +
" a.remove(Util.remove(b, ''))\n" +
" }\n" +
"}";
int offset = contents.indexOf("a.remove") + 2;
MethodNode m = assertDeclaration(contents, offset, offset + "remove".length(), "java.util.List", "remove", DeclarationKind.METHOD);
assertEquals("Should resolve to remove(int) due to return type of inner call", "int", printTypeName(m.getParameters()[0].getType()));
}

@Test // GRECLIPSE-1013
public void testCategoryMethodAsProperty() {
String contents = "''.toURL().text";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,8 @@ public void visitMethodCallExpression(MethodCallExpression node) {
visitGenericTypes(node.getGenericsTypes(), null);
}

// visit method before arguments to provide @ClosureParams, @DeleagtesTo, etc. to closures
// NOTE: this makes choosing from overloads imprecise since argument types aren't complete
node.getMethod().visit(this);

// this is the inferred return type of this method
Expand Down Expand Up @@ -2058,7 +2060,15 @@ private List<ClassNode> getMethodCallArgumentTypes(ASTNode node) {
types.add(VariableScope.NULL_TYPE); // sentinel value
} else {
scopes.getLast().setMethodCallArgumentTypes(getMethodCallArgumentTypes(expression));
TypeLookupResult tlr = lookupExpressionType(expression, null, false, scopes.getLast());

TypeLookupResult tlr;
if (!(expression instanceof MethodCallExpression)) {
tlr = lookupExpressionType(expression, null, false, scopes.getLast());
} else {
MethodCallExpression call = (MethodCallExpression) expression;
tlr = lookupExpressionType(call.getObjectExpression(), null, false, scopes.getLast());
tlr = lookupExpressionType(call.getMethod(), tlr.type, call.getObjectExpression() instanceof ClassExpression, scopes.getLast());
}

types.add(tlr.type);
}
Expand Down

0 comments on commit 58b6382

Please sign in to comment.