-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parameter matching to constructor reference search
- Loading branch information
1 parent
7741295
commit 6a76cae
Showing
7 changed files
with
383 additions
and
107 deletions.
There are no files selected for viewing
251 changes: 251 additions & 0 deletions
251
...builder/src/org/eclipse/jdt/core/groovy/tests/search/ConstructorReferenceSearchTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
/* | ||
* Copyright 2009-2018 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.eclipse.jdt.core.groovy.tests.search; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.List; | ||
|
||
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.NullProgressMonitor; | ||
import org.eclipse.jdt.core.IJavaElement; | ||
import org.eclipse.jdt.core.IMethod; | ||
import org.eclipse.jdt.core.JavaCore; | ||
import org.eclipse.jdt.core.search.IJavaSearchConstants; | ||
import org.eclipse.jdt.core.search.SearchEngine; | ||
import org.eclipse.jdt.core.search.SearchMatch; | ||
import org.eclipse.jdt.core.search.SearchParticipant; | ||
import org.eclipse.jdt.core.search.SearchPattern; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for {@link org.eclipse.jdt.groovy.search.ConstructorReferenceSearchRequestor} | ||
*/ | ||
public final class ConstructorReferenceSearchTests extends SearchTestSuite { | ||
|
||
@Test | ||
public void testConstructorReferences1() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" Foo() {\n" + | ||
" new Foo()\n" + // yes | ||
" }\n" + | ||
" Foo(a) {\n" + | ||
" new Foo(a)\n" + // no | ||
" }\n" + | ||
"}"); | ||
createUnit("", "Other", "import p.Foo\n" + | ||
"new Foo()\n" + // yes | ||
"new Foo(a)\n" + // no | ||
"new p.Foo()\n" + // yes | ||
"new p.Foo(a)\n"); // no | ||
|
||
IMethod constructor = foo.getType("Foo").getMethods()[0]; | ||
List<SearchMatch> matches = searchForReferences(constructor); | ||
assertEquals("Incorrect number of matches;", 3, matches.size()); | ||
|
||
int fooCount = 0, otherCount = 0; | ||
for (SearchMatch match : matches) { | ||
if (match.getElement() instanceof IMethod) { | ||
if (((IMethod) match.getElement()).getResource().getName().equals("Foo.groovy")) { | ||
fooCount += 1; | ||
} else if (((IMethod) match.getElement()).getResource().getName().equals("Other.groovy")) { | ||
otherCount += 1; | ||
} | ||
} | ||
} | ||
assertEquals("Should have found 2 matches in Foo.groovy", 1, fooCount); | ||
assertEquals("Should have found 4 matches in Other.groovy", 2, otherCount); | ||
} | ||
|
||
@Test // https://github.com/groovy/groovy-eclipse/issues/765 | ||
public void testConstructorReferences2() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" static class Bar {\n" + | ||
" static class Baz {\n" + | ||
" Baz() {\n" + | ||
" this(null)\n" + // no | ||
" }\n" + | ||
" Baz(def arg) {\n" + | ||
" super()\n" + // no | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"); | ||
createUnit("", "Other", "import p.Foo.Bar.Baz\n" + | ||
"new Baz()\n" + // yes | ||
"new Baz(a)\n"); // no | ||
|
||
IMethod constructor = foo.getType("Foo").getType("Bar").getType("Baz").getMethods()[0]; | ||
List<SearchMatch> matches = searchForReferences(constructor); | ||
assertEquals("Incorrect number of matches;", 1, matches.size()); | ||
|
||
int fooCount = 0, otherCount = 0; | ||
for (SearchMatch match : matches) { | ||
if (match.getElement() instanceof IMethod) { | ||
if (((IMethod) match.getElement()).getResource().getName().equals("Foo.groovy")) { | ||
fooCount += 1; | ||
} else if (((IMethod) match.getElement()).getResource().getName().equals("Other.groovy")) { | ||
otherCount += 1; | ||
} | ||
} | ||
} | ||
assertEquals("Should have found 0 matches in Foo.groovy", 0, fooCount); | ||
assertEquals("Should have found 2 matches in Other.groovy", 1, otherCount); | ||
} | ||
|
||
@Test | ||
public void testConstructorReferences3() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" Foo(... args) {}\n" + // search for this | ||
"}"); | ||
createUnit("", "Bar", "import p.Foo\n" + | ||
"new Foo()\n" + // yes | ||
"new Foo(a)\n" + // yes | ||
"new Foo(a,b)\n"); // yes | ||
|
||
long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]).stream() | ||
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy")) | ||
.count(); | ||
assertEquals(3, ctorRefs); | ||
} | ||
|
||
@Test | ||
public void testConstructorReferences4() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" Foo(int i) {}\n" + // search for this | ||
" Foo(String s) {}\n" + | ||
"}"); | ||
createUnit("", "Bar", "import p.Foo\n" + | ||
"new Foo()\n" + // yes | ||
"new Foo(0)\n" + // yes | ||
"new Foo('')\n"); // no | ||
|
||
long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]).stream() | ||
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy")) | ||
.count(); | ||
assertEquals(2, ctorRefs); | ||
} | ||
|
||
@Test | ||
public void testConstructorReferences5() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" Foo(int i) {}\n" + | ||
" Foo(String s) {}\n" + // search for this | ||
"}"); | ||
createUnit("", "Bar", "import p.Foo\n" + | ||
"new Foo()\n" + // no -- associated with first declaration | ||
"new Foo(0)\n" + // no | ||
"new Foo('')\n"); // yes | ||
|
||
long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]).stream() | ||
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy")) | ||
.count(); | ||
assertEquals(1, ctorRefs); | ||
} | ||
|
||
@Test | ||
public void testConstructorReferences6() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" Foo() {}\n" + // search for this | ||
" Foo(a) {}\n" + | ||
"}"); | ||
createUnit("", "Bar", "import p.Foo\n" + | ||
"new Foo()\n" + // yes | ||
"new Foo(a)\n" + // no | ||
"new Foo(a,b)\n"); // yes | ||
|
||
long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]).stream() | ||
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy")) | ||
.count(); | ||
assertEquals(2, ctorRefs); | ||
} | ||
|
||
@Test | ||
public void testConstructorReferences7() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" Foo() {}\n" + | ||
" Foo(a) {}\n" + // search for this | ||
"}"); | ||
createUnit("", "Bar", "import p.Foo\n" + | ||
"new Foo()\n" + // no | ||
"new Foo(a)\n" + // yes | ||
"new Foo(a,b)\n"); // no -- associated with first declaration | ||
|
||
long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]).stream() | ||
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy")) | ||
.count(); | ||
assertEquals(1, ctorRefs); | ||
} | ||
|
||
@Test | ||
public void testConstructorReferences8() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" Foo(int i) {}\n" + // search for this | ||
" Foo(String s) {}\n" + | ||
"}"); | ||
createUnit("", "Bar", "import p.Foo\n" + | ||
"class Bar extends Foo {\n" + | ||
" Bar() {\n" + | ||
" super(0)\n" + // yes | ||
" }\n" + | ||
"}\n"); | ||
|
||
long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[0]).stream() | ||
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy")) | ||
.count(); | ||
assertEquals(1, ctorRefs); | ||
} | ||
|
||
@Test | ||
public void testConstructorReferences9() throws Exception { | ||
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" + | ||
"class Foo {\n" + | ||
" Foo(int i) {}\n" + | ||
" Foo(String s) {}\n" + // search for this | ||
"}"); | ||
createUnit("", "Bar", "import p.Foo\n" + | ||
"class Bar extends Foo {\n" + | ||
" Bar() {\n" + | ||
" super(0)\n" + // no | ||
" }\n" + | ||
"}\n"); | ||
|
||
long ctorRefs = searchForReferences(foo.getType("Foo").getMethods()[1]).stream() | ||
.filter(match -> ((IMethod) match.getElement()).getResource().getName().equals("Bar.groovy")) | ||
.count(); | ||
assertEquals(0, ctorRefs); | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
|
||
List<SearchMatch> searchForReferences(IMethod method) throws CoreException { | ||
new SearchEngine().search( | ||
SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES), | ||
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, | ||
SearchEngine.createJavaSearchScope(new IJavaElement[] {JavaCore.create(project)}, false), | ||
searchRequestor, new NullProgressMonitor()); | ||
return searchRequestor.getMatches(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.