Skip to content

Commit

Permalink
Fix for groovy#892: look up type of object expression for property ex…
Browse files Browse the repository at this point in the history
…pressions
  • Loading branch information
eric-milles committed May 25, 2019
1 parent 1cd4950 commit 0811341
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3005,4 +3005,31 @@ public void testMethodOverloadsArgumentMatching6b() {
MethodNode m = assertDeclaration(contents, offset, offset + 3, "Foo", "setBar", DeclarationKind.METHOD);
Assert.assertEquals("Expected 'setBar(Date)' but was 'setBar(int)'", "java.util.Date", m.getParameters()[0].getType().toString(false));
}

@Test // https://github.com/groovy/groovy-eclipse/issues/892
public void testMethodOverloadsArgumentMatching7() {
createJavaUnit("Face",
"interface Face {\n" +
" setValue(String key, int val);\n" +
" setValue(String key, long val);\n" +
" setValue(String key, double val);\n" +
" setValue(String key, boolean val);\n" +
"}\n");
createJavaUnit("Keys",
"interface Keys {\n" +
" String ONE = \"one\";\n" +
" String TWO = \"two\";\n" +
"}\n");

String contents =
"void meth(Face face) {\n" +
" face.with {\n" +
" setValue(Keys.ONE, false)\n" +
" }\n" +
"}\n" +
"}";
int offset = contents.indexOf("setValue");
MethodNode m = assertDeclaration(contents, offset, offset + "setValue".length(), "Face", "setValue", DeclarationKind.METHOD);
Assert.assertEquals("Expected 'setValue(String,boolean)'", "boolean", m.getParameters()[1].getType().toString(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2070,12 +2070,13 @@ private List<ClassNode> getMethodCallArgumentTypes(ASTNode node) {
List<ClassNode> types = new ArrayList<>(expressions.size());
for (Expression expression : expressions) {
ClassNode exprType = expression.getType();
/*if (expression instanceof ClosureExpression) {
types.add(VariableScope.CLOSURE_CLASS_NODE);
} else if (expression instanceof MapExpression) {
/*if (expression instanceof MapExpression) {
types.add(VariableScope.MAP_CLASS_NODE);
} else if (expression instanceof ListExpression) {
types.add(VariableScope.LIST_CLASS_NODE);
} else if (expression instanceof ClosureExpression ||
expression instanceof MethodPointerExpression) {
types.add(VariableScope.CLOSURE_CLASS_NODE);
} else*/ if (expression instanceof ClassExpression) {
types.add(VariableScope.newClassClassNode(exprType));
} else if (expression instanceof CastExpression || expression instanceof ConstructorCallExpression) {
Expand All @@ -2094,12 +2095,16 @@ private List<ClassNode> getMethodCallArgumentTypes(ASTNode node) {
scopes.getLast().setMethodCallArgumentTypes(getMethodCallArgumentTypes(expression));

TypeLookupResult tlr;
if (!(expression instanceof MethodCallExpression)) {
tlr = lookupExpressionType(expression, null, false, scopes.getLast());
} else {
if (expression instanceof PropertyExpression) {
PropertyExpression path = (PropertyExpression) expression;
tlr = lookupExpressionType(path.getObjectExpression(), null, false, scopes.getLast());
tlr = lookupExpressionType(path.getProperty(), tlr.type, path.getObjectExpression() instanceof ClassExpression || VariableScope.CLASS_CLASS_NODE.equals(tlr.type), scopes.getLast());
} else if (expression instanceof MethodCallExpression) {
MethodCallExpression call = (MethodCallExpression) expression;
tlr = lookupExpressionType(call.getObjectExpression(), null, false, scopes.getLast());
tlr = lookupExpressionType(call.getMethod(), tlr.type, call.getObjectExpression() instanceof ClassExpression, scopes.getLast());
tlr = lookupExpressionType(call.getMethod(), tlr.type, call.getObjectExpression() instanceof ClassExpression || VariableScope.CLASS_CLASS_NODE.equals(tlr.type), scopes.getLast());
} else {
tlr = lookupExpressionType(expression, null, false, scopes.getLast());
}

types.add(tlr.type);
Expand Down

0 comments on commit 0811341

Please sign in to comment.