Skip to content

Commit

Permalink
Add parameter matching to constructor reference search
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 26, 2018
1 parent 7741295 commit 6a76cae
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 107 deletions.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import org.eclipse.jdt.groovy.search.TypeRequestorFactory;
import org.junit.Test;

/**
* Tests for {@link org.eclipse.jdt.groovy.search.MethodReferenceSearchRequestor}
*/
public final class MethodReferenceSearchTests extends SearchTestSuite {

@Test
Expand Down Expand Up @@ -324,90 +327,6 @@ public void testMethodWithDefaultParameters2() throws Exception {
false, 0, "xxx");
}

@Test
public void testConstructorReferenceSearch1() throws Exception {
GroovyCompilationUnit foo = createUnit("p", "Foo", "package p\n" +
"class Foo {\n" +
" Foo() {\n" +
" new Foo()\n" +
" }\n" +
" Foo(a) {\n" +
" new Foo(a)\n" +
" }\n" +
"}");
createUnit("", "Other", "import p.Foo\n" +
"new Foo()\n" +
"new Foo(a)\n" +
"new p.Foo()\n" +
"new p.Foo(a)\n");

IMethod constructor = foo.getType("Foo").getMethods()[0];
new SearchEngine().search(
SearchPattern.createPattern(constructor, IJavaSearchConstants.REFERENCES),
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
SearchEngine.createJavaSearchScope(new IJavaElement[] {foo.getPackageFragmentRoot()}, false),
searchRequestor, new NullProgressMonitor());

List<SearchMatch> matches = searchRequestor.getMatches();
assertEquals("Incorrect number of matches;", 6, 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", 2, fooCount);
assertEquals("Should have found 4 matches in Other.groovy", 4, otherCount);
}

@Test // https://github.com/groovy/groovy-eclipse/issues/765
public void testConstructorReferenceSearch2() 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" +
" }\n" +
" Baz(def arg) {\n" +
" super()\n" +
" }\n" +
" }\n" +
" }\n" +
"}");
createUnit("", "Other", "import p.Foo.Bar.Baz\n" +
"new Baz()\n" +
"new Baz(a)\n");

IMethod constructor = foo.getType("Foo").getType("Bar").getType("Baz").getMethods()[0];
new SearchEngine().search(
SearchPattern.createPattern(constructor, IJavaSearchConstants.REFERENCES),
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
SearchEngine.createJavaSearchScope(new IJavaElement[] {foo.getPackageFragmentRoot()}, false),
searchRequestor, new NullProgressMonitor());

List<SearchMatch> matches = searchRequestor.getMatches();
assertEquals("Incorrect number of matches;", 2, 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", 2, otherCount);
}

@Test
public void testStaticMethodReferenceSearch() throws Exception {
String contents =
Expand Down
Loading

0 comments on commit 6a76cae

Please sign in to comment.