Search in sources :

Example 1 with JavaSearchQuery

use of org.eclipse.jdt.internal.ui.search.JavaSearchQuery in project che by eclipse.

the class FindReferencesTest method testFind.

@Test
public void testFind() throws Exception {
    JavaSearchQuery query = SearchTestHelper.runMethodRefQuery("junit.framework.Test", "countTestCases", new String[0]);
    ISearchResult result = query.getSearchResult();
    JavaSearchResult javaResult = ((JavaSearchResult) result);
    Assertions.assertThat(javaResult.getElements()).hasSize(9);
}
Also used : ISearchResult(org.eclipse.search.ui.ISearchResult) JavaSearchQuery(org.eclipse.jdt.internal.ui.search.JavaSearchQuery) JavaSearchResult(org.eclipse.jdt.internal.ui.search.JavaSearchResult) Test(org.junit.Test) BaseTest(org.eclipse.che.plugin.java.server.che.BaseTest)

Example 2 with JavaSearchQuery

use of org.eclipse.jdt.internal.ui.search.JavaSearchQuery in project che by eclipse.

the class SearchTestHelper method countMethodRefs.

static int countMethodRefs(String TypeName, String methodName, String[] parameterTypes) throws JavaModelException {
    JavaSearchQuery query = runMethodRefQuery(TypeName, methodName, parameterTypes);
    JavaSearchResult result = (JavaSearchResult) query.getSearchResult();
    return result.getMatchCount();
}
Also used : JavaSearchQuery(org.eclipse.jdt.internal.ui.search.JavaSearchQuery) JavaSearchResult(org.eclipse.jdt.internal.ui.search.JavaSearchResult)

Example 3 with JavaSearchQuery

use of org.eclipse.jdt.internal.ui.search.JavaSearchQuery in project che by eclipse.

the class SearchTestHelper method runMethodRefQuery.

static JavaSearchQuery runMethodRefQuery(String TypeName, String methodName, String[] parameterTypes) throws JavaModelException {
    IMethod method = getMethod(TypeName, methodName, parameterTypes);
    JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(method, IJavaSearchConstants.REFERENCES, JavaSearchScopeFactory.getInstance().createWorkspaceScope(true), "workspace scope"));
    NewSearchUI.runQueryInForeground(null, query);
    return query;
}
Also used : JavaSearchQuery(org.eclipse.jdt.internal.ui.search.JavaSearchQuery) ElementQuerySpecification(org.eclipse.jdt.ui.search.ElementQuerySpecification) IMethod(org.eclipse.jdt.core.IMethod)

Example 4 with JavaSearchQuery

use of org.eclipse.jdt.internal.ui.search.JavaSearchQuery in project che by eclipse.

the class SearchManager method performFindUsageSearch.

private FindUsagesResponse performFindUsageSearch(IJavaElement element) throws JavaModelException, BadLocationException {
    JavaSearchScopeFactory factory = JavaSearchScopeFactory.getInstance();
    boolean isInsideJRE = factory.isInsideJRE(element);
    JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(element, IJavaSearchConstants.REFERENCES, factory.createWorkspaceScope(isInsideJRE), "workspace scope"));
    NewSearchUI.runQueryInForeground(null, query);
    ISearchResult result = query.getSearchResult();
    JavaSearchResult javaResult = ((JavaSearchResult) result);
    FindUsagesResponse response = DtoFactory.newDto(FindUsagesResponse.class);
    Map<String, List<org.eclipse.che.ide.ext.java.shared.dto.search.Match>> mapMaches = new HashMap<>();
    JavaElementToDtoConverter converter = new JavaElementToDtoConverter(javaResult);
    for (Object o : javaResult.getElements()) {
        IJavaElement javaElement = (IJavaElement) o;
        IDocument document = null;
        if (javaElement instanceof IMember) {
            IMember member = ((IMember) javaElement);
            if (member.isBinary()) {
                if (member.getClassFile().getSource() != null) {
                    document = new Document(member.getClassFile().getSource());
                }
            } else {
                document = getDocument(member.getCompilationUnit());
            }
        } else if (javaElement instanceof IPackageDeclaration) {
            ICompilationUnit ancestor = (ICompilationUnit) (javaElement).getAncestor(IJavaElement.COMPILATION_UNIT);
            document = getDocument(ancestor);
        }
        converter.addElementToProjectHierarchy(javaElement);
        Match[] matches = javaResult.getMatches(o);
        List<org.eclipse.che.ide.ext.java.shared.dto.search.Match> matchList = new ArrayList<>();
        for (Match match : matches) {
            org.eclipse.che.ide.ext.java.shared.dto.search.Match dtoMatch = DtoFactory.newDto(org.eclipse.che.ide.ext.java.shared.dto.search.Match.class);
            if (document != null) {
                IRegion lineInformation = document.getLineInformationOfOffset(match.getOffset());
                int offsetInLine = match.getOffset() - lineInformation.getOffset();
                Region matchInLine = DtoFactory.newDto(Region.class).withOffset(offsetInLine).withLength(match.getLength());
                dtoMatch.setMatchInLine(matchInLine);
                dtoMatch.setMatchLineNumber(document.getLineOfOffset(match.getOffset()));
                dtoMatch.setMatchedLine(document.get(lineInformation.getOffset(), lineInformation.getLength()));
            }
            dtoMatch.setFileMatchRegion(DtoFactory.newDto(Region.class).withOffset(match.getOffset()).withLength(match.getLength()));
            matchList.add(dtoMatch);
        }
        mapMaches.put(javaElement.getHandleIdentifier(), matchList);
    }
    List<JavaProject> projects = converter.getProjects();
    response.setProjects(projects);
    response.setMatches(mapMaches);
    response.setSearchElementLabel(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT));
    return response;
}
Also used : ISearchResult(org.eclipse.search.ui.ISearchResult) FindUsagesResponse(org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IMember(org.eclipse.jdt.core.IMember) IRegion(org.eclipse.jface.text.IRegion) Match(org.eclipse.search.ui.text.Match) JavaSearchResult(org.eclipse.jdt.internal.ui.search.JavaSearchResult) ArrayList(java.util.ArrayList) List(java.util.List) JavaSearchScopeFactory(org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaProject(org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) ElementQuerySpecification(org.eclipse.jdt.ui.search.ElementQuerySpecification) JavaSearchQuery(org.eclipse.jdt.internal.ui.search.JavaSearchQuery) Region(org.eclipse.che.ide.ext.java.shared.dto.Region) IRegion(org.eclipse.jface.text.IRegion) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) IDocument(org.eclipse.jface.text.IDocument)

Example 5 with JavaSearchQuery

use of org.eclipse.jdt.internal.ui.search.JavaSearchQuery in project che by eclipse.

the class FindReferencesTest method testFind2.

@Test
public void testFind2() throws Exception {
    IPackageFragmentRoot root = ((JavaProject) JUnitSourceSetup.getProject()).getPackageFragmentRoot(new Path(JUnitSourceSetup.SRC_CONTAINER));
    IPackageFragment packageFragment = root.createPackageFragment("che", true, null);
    StringBuilder a = new StringBuilder();
    a.append("package che;\n");
    a.append("public class A{}\n");
    packageFragment.createCompilationUnit("A.java", a.toString(), true, null);
    StringBuilder b = new StringBuilder();
    b.append("package che;\n");
    b.append("import java.util.Comparator;\n");
    b.append("public class B extends A implements Comparator<A>{\n");
    b.append("   @Override\n");
    b.append("   public int compare(A o1, A o2) {\n");
    b.append("       A bb = null;\n");
    b.append("       return 0;\n");
    b.append("   }\n");
    b.append("}\n");
    packageFragment.createCompilationUnit("B.java", b.toString(), true, null);
    JavaSearchQuery query = SearchTestHelper.runTypeRefQuery("che.A");
    ISearchResult result = query.getSearchResult();
    JavaSearchResult javaResult = ((JavaSearchResult) result);
    Assertions.assertThat(javaResult.getElements()).hasSize(2);
}
Also used : Path(org.eclipse.core.runtime.Path) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ISearchResult(org.eclipse.search.ui.ISearchResult) JavaSearchQuery(org.eclipse.jdt.internal.ui.search.JavaSearchQuery) JavaSearchResult(org.eclipse.jdt.internal.ui.search.JavaSearchResult) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test) BaseTest(org.eclipse.che.plugin.java.server.che.BaseTest)

Aggregations

JavaSearchQuery (org.eclipse.jdt.internal.ui.search.JavaSearchQuery)8 JavaSearchResult (org.eclipse.jdt.internal.ui.search.JavaSearchResult)5 ElementQuerySpecification (org.eclipse.jdt.ui.search.ElementQuerySpecification)3 ISearchResult (org.eclipse.search.ui.ISearchResult)3 BaseTest (org.eclipse.che.plugin.java.server.che.BaseTest)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Region (org.eclipse.che.ide.ext.java.shared.dto.Region)1 JavaProject (org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject)1 FindUsagesResponse (org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse)1 Path (org.eclipse.core.runtime.Path)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IMember (org.eclipse.jdt.core.IMember)1 IMethod (org.eclipse.jdt.core.IMethod)1 IPackageDeclaration (org.eclipse.jdt.core.IPackageDeclaration)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1