diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DGMInferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DGMInferencingTests.java index e2579ff77a..3efe664811 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DGMInferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DGMInferencingTests.java @@ -601,7 +601,7 @@ public void testDGM46() { } @Test - public void testDGM47() throws Throwable { + public void testDGM47() { String contents = //@formatter:off "java.util.regex.Pattern[] pats = [~/one/, ~/two/]\n" + @@ -725,6 +725,45 @@ public void testDGM53a() { assertUnknownConfidence(contents, offset, offset + "allWhitespace".length()); } + @Test // https://github.com/groovy/groovy-eclipse/issues/1076 + public void testDGM54() { + String contents = + //@formatter:off + "void test(String[] strings) {\n" + + " strings.toString()\n" + + "}\n"; + //@formatter:on + + assertExprType(contents, "toString", "java.lang.String"); + assertDeclType(contents, "toString", "org.codehaus.groovy.runtime.DefaultGroovyMethods"); + } + + @Test // https://github.com/groovy/groovy-eclipse/issues/1076 + public void testDGM54a() { + String contents = + //@formatter:off + "void test(String[] strings) {\n" + + " strings.equals([])\n" + + "}\n"; + //@formatter:on + + assertExprType(contents, "equals", "java.lang.Boolean"); + assertDeclType(contents, "equals", "org.codehaus.groovy.runtime.DefaultGroovyMethods"); + } + + @Test // https://github.com/groovy/groovy-eclipse/issues/1076 + public void testDGM54b() { + String contents = + //@formatter:off + "void test(String[] strings) {\n" + + " [].equals(strings)\n" + + "}\n"; + //@formatter:on + + assertExprType(contents, "equals", "java.lang.Boolean"); + assertDeclType(contents, "equals", "org.codehaus.groovy.runtime.DefaultGroovyMethods"); + } + @Test // GRECLIPSE-1131 public void testDGMClosure1() { String contents = diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/MethodReferenceSearchTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/MethodReferenceSearchTests.java index 6db2e1c677..2e018df082 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/MethodReferenceSearchTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/MethodReferenceSearchTests.java @@ -862,6 +862,54 @@ public void testGenericsMethodReferenceSearch() throws Exception { assertEquals("Baz.java", ((IJavaElement) matches.get(0).getElement()).getResource().getName()); } + @Test + public void testObjectMethodReferenceSearch1() throws Exception { + GroovyCompilationUnit groovyUnit = createUnit("foo", "Bar", + "package foo\n" + + "class Bar {\n" + + " def baz(obj) {\n" + + " obj.notifyAll()\n" + + " }\n" + + "}"); + + IMethod method = groovyUnit.getJavaProject().findType("java.lang.Object").getMethod("notifyAll", new String[0]); + new SearchEngine().search( + SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES), + new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, + SearchEngine.createJavaSearchScope(new IJavaElement[] {groovyUnit.getPackageFragmentRoot()}, false), + searchRequestor, new NullProgressMonitor()); + + List matches = searchRequestor.getMatches(); + + assertEquals(1, matches.size()); + assertEquals(SearchMatch.A_ACCURATE, matches.get(0).getAccuracy()); + assertEquals("Bar.groovy", ((IJavaElement) matches.get(0).getElement()).getResource().getName()); + } + + @Test + public void testObjectMethodReferenceSearch2() throws Exception { + GroovyCompilationUnit groovyUnit = createUnit("foo", "Bar", + "package foo\n" + + "class Bar {\n" + + " def baz(String[] strings) {\n" + + " println strings.toString()\n" + + " }\n" + + "}"); + + IMethod method = groovyUnit.getJavaProject().findType("java.lang.Object").getMethod("toString", new String[0]); + new SearchEngine().search( + SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES), + new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, + SearchEngine.createJavaSearchScope(new IJavaElement[] {groovyUnit.getPackageFragmentRoot()}, false), + searchRequestor, new NullProgressMonitor()); + + List matches = searchRequestor.getMatches(); + + assertEquals(1, matches.size()); + assertEquals(SearchMatch.A_INACCURATE, matches.get(0).getAccuracy()); + assertEquals("Bar.groovy", ((IJavaElement) matches.get(0).getElement()).getResource().getName()); + } + //-------------------------------------------------------------------------- private void doTestForTwoMethodReferencesInScript(String secondContents) throws Exception { diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java index 0afb41cb53..eea14bbf23 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java @@ -2585,7 +2585,7 @@ private TypeLookupResult lookupExpressionType(final Expression node, final Class candidate = lookup.lookupType(node, scope, objExprType); } if (candidate != null) { - if (result == null || result.confidence.isLessThan(candidate.confidence)) { + if (result == null || result.confidence.isLessThan(candidate.confidence) && !VariableScope.OBJECT_CLASS_NODE.equals(candidate.declaringType)) { result = candidate; } if (result.confidence.isAtLeast(TypeConfidence.INFERRED) || diff --git a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy index 10d806b14d..8efd6e035d 100644 --- a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy @@ -78,6 +78,22 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite { new HighlightedTypedPosition(contents.lastIndexOf('String'), 6, CLASS)) } + @Test + void testArrays4() { + String contents = '''\ + |def test(String[] strings) { + | return strings.toString() + |} + |'''.stripMargin() + + assertHighlighting(contents, + new HighlightedTypedPosition(contents.indexOf('test'), 4, METHOD), + new HighlightedTypedPosition(contents.indexOf('String'), 6, CLASS), + new HighlightedTypedPosition(contents.indexOf('strings'), 7, PARAMETER), + new HighlightedTypedPosition(contents.lastIndexOf('strings'), 7, PARAMETER), + new HighlightedTypedPosition(contents.lastIndexOf('toString'), 8, GROOVY_CALL)) + } + @Test void testFields1() { String contents = '''\