From 682b16295afc3aafe20af571a4c263f4dee7044f Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Tue, 28 Apr 2020 09:58:49 -0500 Subject: [PATCH] Fix for #1105: reset type to Object for unknown property expressions --- .../tests/search/ArrayInferencingTests.java | 6 +++--- .../groovy/tests/search/InferencingTests.java | 19 ++++++++++++++----- .../OperatorOverloadingInferencingTests.java | 6 +++--- .../jdt/groovy/search/SimpleTypeLookup.java | 4 +++- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ArrayInferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ArrayInferencingTests.java index 01e4548d0c..5ddfc55b60 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ArrayInferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ArrayInferencingTests.java @@ -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. @@ -107,8 +107,8 @@ public void testArrayGenerics2() { @Test // https://github.com/groovy/groovy-eclipse/issues/763 public void testArrayGenerics3() { - String contents = "Collection>[] array = []; array*.trim()"; - assertType(contents, "trim", "java.lang.String"); + String contents = "class C {\n int x\n}\n" + "Collection >[] array = []; array*.x"; + assertType(contents, "x", "java.lang.Integer"); } @Test diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java index 38ebe4b775..d31c3b4cdb 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java @@ -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 = @@ -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 @@ -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"); diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/OperatorOverloadingInferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/OperatorOverloadingInferencingTests.java index eba6d3a14a..dc5f925e14 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/OperatorOverloadingInferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/OperatorOverloadingInferencingTests.java @@ -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"); @@ -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"); diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java index af7d386f0d..384d7dcc98 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java @@ -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; }