Skip to content

Commit

Permalink
Fix for #1105: reset type to Object for unknown property expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 28, 2020
1 parent 8b6bb80 commit 682b162
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2019 the original author or authors.
* Copyright 2009-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -107,8 +107,8 @@ public void testArrayGenerics2() {

@Test // https://github.com/groovy/groovy-eclipse/issues/763
public void testArrayGenerics3() {
String contents = "Collection<List<String>>[] array = []; array*.trim()";
assertType(contents, "trim", "java.lang.String");
String contents = "class C {\n int x\n}\n" + "Collection<List<C> >[] array = []; array*.x";
assertType(contents, "x", "java.lang.Integer");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,15 @@ public void testLocalVar28() {
assertType(contents, "x", "java.lang.String");
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1105
public void testLocalVar29() {
String contents = "void test(a) {\n" +
" def x = a.b\n" +
"}";
assertType(contents, "b", "java.lang.Object");
assertType(contents, "x", "java.lang.Object");
}

@Test
public void testLocalMethod1() {
String contents =
Expand Down Expand Up @@ -953,14 +962,14 @@ public void testSpecialConstructor2() {

@Test
public void testStaticMethod1() {
String contents = "class Two { static String x() {\"\"}}\n Two.x()";
assertType(contents, "x", "java.lang.String");
String contents = "class Two { static Number x() { 42\n}\n}\n" + "Two.x()";
assertType(contents, "x", "java.lang.Number");
}

@Test
public void testStaticMethod2() {
String contents = "class Two { static String x() {\"\"}}\n Two.x";
assertType(contents, "x", "java.lang.String");
String contents = "class Two { static Number x() { 42\n}\n}\n" + "Two.x";
assertUnknown(contents, "x");
}

@Test
Expand Down Expand Up @@ -2750,7 +2759,7 @@ public void testDeclarationAtBeginningOfMethod() {
" String action() {}\n" +
" def meth() {\n" +
" def x = action()\n" +
" x.substring()\n" +
" x.substring(0)\n" +
" }\n" +
"}\n";
assertType(contents, "substring", "java.lang.String");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ public void testGetAt9() {
@Test
public void testAttributeExpr1() throws Exception {
String contents =
"class Foo { boolean str\n}\n" +
"def xxx = new Foo().@str\n" +
"class Foo { boolean flag\n}\n" +
"def xxx = new Foo().@flag\n" +
"xxx";

assertType(contents, "xxx", "java.lang.Boolean");
Expand All @@ -309,7 +309,7 @@ public void testAttributeExpr2() throws Exception {
public void testLongExpr1() throws Exception {
String contents =
"class Foo { String str\n}\n" +
"def xxx = ([ new Foo() ].str.length() + 4 - 9) % 7\n" +
"def xxx = (new Foo().str.length() + 4 - 9) % 7\n" +
"xxx";

assertType(contents, "xxx", "java.lang.Integer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,20 @@ protected TypeLookupResult findTypeForNameWithKnownObjectExpression(final String
declaration = null; // property expression "foo.bar" does not resolve to "bar(...)" or "setBar(x)" w/o call args
}

ClassNode resolvedType = type, resolvedDeclaringType;
ClassNode resolvedType, resolvedDeclaringType;
if (declaration != null) {
resolvedType = getTypeFromDeclaration(declaration);
resolvedDeclaringType = getDeclaringTypeFromDeclaration(declaration, declaringType);
} else if ("call".equals(name)) {
// assume that this is a synthetic call method for calling a closure
resolvedType = VariableScope.OBJECT_CLASS_NODE;
resolvedDeclaringType = VariableScope.CLOSURE_CLASS_NODE;
declaration = resolvedDeclaringType.getMethods("call").get(0);
} else if ("this".equals(name) && VariableScope.CLASS_CLASS_NODE.equals(declaringType)) {
// "Type.this" (aka ClassExpression.ConstantExpression) within inner class
declaration = resolvedType = resolvedDeclaringType = declaringType.getGenericsTypes()[0].getType();
} else {
resolvedType = VariableScope.OBJECT_CLASS_NODE;
resolvedDeclaringType = declaringType;
confidence = TypeConfidence.UNKNOWN;
}
Expand Down

0 comments on commit 682b162

Please sign in to comment.