Skip to content

Commit 5c18605

Browse files
committed
Fix for #1076: category methods take precedence over Object methods
1 parent 83200e6 commit 5c18605

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/DGMInferencingTests.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ public void testDGM46() {
601601
}
602602

603603
@Test
604-
public void testDGM47() throws Throwable {
604+
public void testDGM47() {
605605
String contents =
606606
//@formatter:off
607607
"java.util.regex.Pattern[] pats = [~/one/, ~/two/]\n" +
@@ -725,6 +725,45 @@ public void testDGM53a() {
725725
assertUnknownConfidence(contents, offset, offset + "allWhitespace".length());
726726
}
727727

728+
@Test // https://github.com/groovy/groovy-eclipse/issues/1076
729+
public void testDGM54() {
730+
String contents =
731+
//@formatter:off
732+
"void test(String[] strings) {\n" +
733+
" strings.toString()\n" +
734+
"}\n";
735+
//@formatter:on
736+
737+
assertExprType(contents, "toString", "java.lang.String");
738+
assertDeclType(contents, "toString", "org.codehaus.groovy.runtime.DefaultGroovyMethods");
739+
}
740+
741+
@Test // https://github.com/groovy/groovy-eclipse/issues/1076
742+
public void testDGM54a() {
743+
String contents =
744+
//@formatter:off
745+
"void test(String[] strings) {\n" +
746+
" strings.equals([])\n" +
747+
"}\n";
748+
//@formatter:on
749+
750+
assertExprType(contents, "equals", "java.lang.Boolean");
751+
assertDeclType(contents, "equals", "org.codehaus.groovy.runtime.DefaultGroovyMethods");
752+
}
753+
754+
@Test // https://github.com/groovy/groovy-eclipse/issues/1076
755+
public void testDGM54b() {
756+
String contents =
757+
//@formatter:off
758+
"void test(String[] strings) {\n" +
759+
" [].equals(strings)\n" +
760+
"}\n";
761+
//@formatter:on
762+
763+
assertExprType(contents, "equals", "java.lang.Boolean");
764+
assertDeclType(contents, "equals", "org.codehaus.groovy.runtime.DefaultGroovyMethods");
765+
}
766+
728767
@Test // GRECLIPSE-1131
729768
public void testDGMClosure1() {
730769
String contents =

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/MethodReferenceSearchTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,54 @@ public void testGenericsMethodReferenceSearch() throws Exception {
862862
assertEquals("Baz.java", ((IJavaElement) matches.get(0).getElement()).getResource().getName());
863863
}
864864

865+
@Test
866+
public void testObjectMethodReferenceSearch1() throws Exception {
867+
GroovyCompilationUnit groovyUnit = createUnit("foo", "Bar",
868+
"package foo\n" +
869+
"class Bar {\n" +
870+
" def baz(obj) {\n" +
871+
" obj.notifyAll()\n" +
872+
" }\n" +
873+
"}");
874+
875+
IMethod method = groovyUnit.getJavaProject().findType("java.lang.Object").getMethod("notifyAll", new String[0]);
876+
new SearchEngine().search(
877+
SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES),
878+
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
879+
SearchEngine.createJavaSearchScope(new IJavaElement[] {groovyUnit.getPackageFragmentRoot()}, false),
880+
searchRequestor, new NullProgressMonitor());
881+
882+
List<SearchMatch> matches = searchRequestor.getMatches();
883+
884+
assertEquals(1, matches.size());
885+
assertEquals(SearchMatch.A_ACCURATE, matches.get(0).getAccuracy());
886+
assertEquals("Bar.groovy", ((IJavaElement) matches.get(0).getElement()).getResource().getName());
887+
}
888+
889+
@Test
890+
public void testObjectMethodReferenceSearch2() throws Exception {
891+
GroovyCompilationUnit groovyUnit = createUnit("foo", "Bar",
892+
"package foo\n" +
893+
"class Bar {\n" +
894+
" def baz(String[] strings) {\n" +
895+
" println strings.toString()\n" +
896+
" }\n" +
897+
"}");
898+
899+
IMethod method = groovyUnit.getJavaProject().findType("java.lang.Object").getMethod("toString", new String[0]);
900+
new SearchEngine().search(
901+
SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES),
902+
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
903+
SearchEngine.createJavaSearchScope(new IJavaElement[] {groovyUnit.getPackageFragmentRoot()}, false),
904+
searchRequestor, new NullProgressMonitor());
905+
906+
List<SearchMatch> matches = searchRequestor.getMatches();
907+
908+
assertEquals(1, matches.size());
909+
assertEquals(SearchMatch.A_INACCURATE, matches.get(0).getAccuracy());
910+
assertEquals("Bar.groovy", ((IJavaElement) matches.get(0).getElement()).getResource().getName());
911+
}
912+
865913
//--------------------------------------------------------------------------
866914

867915
private void doTestForTwoMethodReferencesInScript(String secondContents) throws Exception {

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2585,7 +2585,7 @@ private TypeLookupResult lookupExpressionType(final Expression node, final Class
25852585
candidate = lookup.lookupType(node, scope, objExprType);
25862586
}
25872587
if (candidate != null) {
2588-
if (result == null || result.confidence.isLessThan(candidate.confidence)) {
2588+
if (result == null || result.confidence.isLessThan(candidate.confidence) && !VariableScope.OBJECT_CLASS_NODE.equals(candidate.declaringType)) {
25892589
result = candidate;
25902590
}
25912591
if (result.confidence.isAtLeast(TypeConfidence.INFERRED) ||

ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
7878
new HighlightedTypedPosition(contents.lastIndexOf('String'), 6, CLASS))
7979
}
8080

81+
@Test
82+
void testArrays4() {
83+
String contents = '''\
84+
|def test(String[] strings) {
85+
| return strings.toString()
86+
|}
87+
|'''.stripMargin()
88+
89+
assertHighlighting(contents,
90+
new HighlightedTypedPosition(contents.indexOf('test'), 4, METHOD),
91+
new HighlightedTypedPosition(contents.indexOf('String'), 6, CLASS),
92+
new HighlightedTypedPosition(contents.indexOf('strings'), 7, PARAMETER),
93+
new HighlightedTypedPosition(contents.lastIndexOf('strings'), 7, PARAMETER),
94+
new HighlightedTypedPosition(contents.lastIndexOf('toString'), 8, GROOVY_CALL))
95+
}
96+
8197
@Test
8298
void testFields1() {
8399
String contents = '''\

0 commit comments

Comments
 (0)