Skip to content

Commit

Permalink
Fix for #1076
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 8, 2020
1 parent 83200e6 commit 43a4ada
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SearchMatch> 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<SearchMatch> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''\
Expand Down

0 comments on commit 43a4ada

Please sign in to comment.