Search in sources :

Example 1 with FindUsagesResponse

use of org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse 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 2 with FindUsagesResponse

use of org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse in project che by eclipse.

the class FindUsagesPresenter method findUsages.

public void findUsages(TextEditor activeEditor) {
    final VirtualFile virtualFile = activeEditor.getEditorInput().getFile();
    if (virtualFile instanceof Resource) {
        final Project project = ((Resource) virtualFile).getRelatedProject().get();
        if (project == null) {
            return;
        }
        final Optional<Resource> srcFolder = ((Resource) virtualFile).getParentWithMarker(SourceFolderMarker.ID);
        if (!srcFolder.isPresent()) {
            return;
        }
        final String fqn = JavaUtil.resolveFQN((Container) srcFolder.get(), (Resource) virtualFile);
        String projectPath = project.getLocation().toString();
        FindUsagesRequest request = dtoFactory.createDto(FindUsagesRequest.class);
        request.setFQN(fqn);
        request.setProjectPath(projectPath);
        request.setOffset(activeEditor.getCursorOffset());
        Promise<FindUsagesResponse> promise = searchService.findUsages(request);
        promise.then(new Operation<FindUsagesResponse>() {

            @Override
            public void apply(FindUsagesResponse arg) throws OperationException {
                handleResponse(arg);
            }
        }).catchError(new Operation<PromiseError>() {

            @Override
            public void apply(PromiseError arg) throws OperationException {
                Throwable cause = arg.getCause();
                if (cause instanceof ServerException) {
                    handleError(((ServerException) cause).getHTTPStatus(), cause.getMessage());
                    return;
                }
                //in case websocket request
                if (cause instanceof org.eclipse.che.ide.websocket.rest.exceptions.ServerException) {
                    handleError(((org.eclipse.che.ide.websocket.rest.exceptions.ServerException) cause).getHTTPStatus(), cause.getMessage());
                    return;
                }
                Log.error(getClass(), arg);
                manager.notify(localizationConstant.failedToProcessFindUsage(), arg.getMessage(), FAIL, FLOAT_MODE);
            }
        });
    }
}
Also used : VirtualFile(org.eclipse.che.ide.api.resources.VirtualFile) FindUsagesResponse(org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse) ServerException(org.eclipse.che.ide.commons.exception.ServerException) FindUsagesRequest(org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesRequest) SVGResource(org.vectomatic.dom.svg.ui.SVGResource) Resource(org.eclipse.che.ide.api.resources.Resource) Operation(org.eclipse.che.api.promises.client.Operation) Project(org.eclipse.che.ide.api.resources.Project) PromiseError(org.eclipse.che.api.promises.client.PromiseError) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 3 with FindUsagesResponse

use of org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse in project che by eclipse.

the class JavaSearchServiceWS method findUsages.

@Override
public Promise<FindUsagesResponse> findUsages(final FindUsagesRequest request) {
    final MessageBus messageBus = provider.getMachineMessageBus();
    return createFromAsyncRequest(callback -> {
        final MessageBuilder builder = new MessageBuilder(POST, pathToService + "find/usages");
        builder.data(dtoFactory.toJson(request)).header(CONTENTTYPE, APPLICATION_JSON).header(ACCEPT, APPLICATION_JSON);
        loader.show();
        try {
            messageBus.send(builder.build(), new RequestCallback<FindUsagesResponse>(unmarshallerFactory.newWSUnmarshaller(FindUsagesResponse.class)) {

                @Override
                protected void onSuccess(FindUsagesResponse result) {
                    loader.hide();
                    callback.onSuccess(result);
                }

                @Override
                protected void onFailure(Throwable exception) {
                    loader.hide();
                    callback.onFailure(exception);
                }
            });
        } catch (WebSocketException e) {
            loader.hide();
            callback.onFailure(e);
        }
    });
}
Also used : MessageBus(org.eclipse.che.ide.websocket.MessageBus) FindUsagesResponse(org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse) MessageBuilder(org.eclipse.che.ide.websocket.MessageBuilder) WebSocketException(org.eclipse.che.ide.websocket.WebSocketException)

Example 4 with FindUsagesResponse

use of org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse in project che by eclipse.

the class FindReferencesTest method testSearchManagerFindUsage.

@Test
public void testSearchManagerFindUsage() throws Exception {
    IJavaProject aProject = JUnitSourceSetup.getProject();
    IPackageFragmentRoot root = ((JavaProject) aProject).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");
    ICompilationUnit compilationUnitA = 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("import che.A;\n");
    b.append("public class B extends A implements Comparator<A>{\n");
    b.append("   private A a = null;\n");
    b.append("   static{\n");
    b.append("       A ddd = null;\n");
    b.append("   }\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("   class SubB{\n");
    b.append("     public A ccc = null;\n");
    b.append("   }\n");
    b.append("}\n");
    b.append("class SubB2{\n");
    b.append("    private final A foo = null;\n");
    b.append("}\n");
    packageFragment.createCompilationUnit("B.java", b.toString(), true, null);
    SearchManager manager = new SearchManager();
    FindUsagesResponse response = manager.findUsage(aProject, "che.A", 26);
    Assertions.assertThat(response.getSearchElementLabel()).isEqualTo("A");
    List<org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject> projects = response.getProjects();
    Assertions.assertThat(projects).isNotNull().isNotEmpty().hasSize(1);
    String expectedProjectPath = JUnitSourceSetup.getProject().getPath().toOSString();
    org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject project = projects.get(0);
    Assertions.assertThat(project.getName()).isEqualTo(JUnitSourceSetup.PROJECT_NAME);
    Assertions.assertThat(project.getPath()).isEqualTo(expectedProjectPath);
    Assertions.assertThat(project.getPackageFragmentRoots()).isNotNull().isNotEmpty().hasSize(1);
    PackageFragmentRoot fragmentRoot = project.getPackageFragmentRoots().get(0);
    Assertions.assertThat(fragmentRoot.getElementName()).isEqualTo(JUnitSourceSetup.SRC_CONTAINER);
    Assertions.assertThat(fragmentRoot.getProjectPath()).isEqualTo(expectedProjectPath);
    Assertions.assertThat(fragmentRoot.getPackageFragments()).isNotNull().isNotEmpty().hasSize(1);
    PackageFragment fragment = fragmentRoot.getPackageFragments().get(0);
    Assertions.assertThat(fragment.getElementName()).isEqualTo("che");
    Assertions.assertThat(fragment.getProjectPath()).isEqualTo(expectedProjectPath);
    Assertions.assertThat(fragment.getPath()).isEqualTo(expectedProjectPath + "/" + JUnitSourceSetup.SRC_CONTAINER + "/che");
    Assertions.assertThat(fragment.getClassFiles()).isNotNull().isEmpty();
    Assertions.assertThat(fragment.getCompilationUnits()).isNotNull().isNotEmpty().hasSize(1);
    CompilationUnit compilationUnit = fragment.getCompilationUnits().get(0);
    Assertions.assertThat(compilationUnit.getElementName()).isEqualTo("B.java");
    Assertions.assertThat(compilationUnit.getPath()).isEqualTo(expectedProjectPath + "/" + JUnitSourceSetup.SRC_CONTAINER + "/che/B.java");
    Assertions.assertThat(compilationUnit.getImports()).hasSize(1);
    Assertions.assertThat(compilationUnit.getTypes()).hasSize(2);
}
Also used : Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) PackageFragment(org.eclipse.che.ide.ext.java.shared.dto.model.PackageFragment) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) PackageFragmentRoot(org.eclipse.che.ide.ext.java.shared.dto.model.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) FindUsagesResponse(org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse) SearchManager(org.eclipse.che.plugin.java.server.search.SearchManager) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) Test(org.junit.Test) BaseTest(org.eclipse.che.plugin.java.server.che.BaseTest)

Aggregations

FindUsagesResponse (org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Operation (org.eclipse.che.api.promises.client.Operation)1 OperationException (org.eclipse.che.api.promises.client.OperationException)1 PromiseError (org.eclipse.che.api.promises.client.PromiseError)1 Project (org.eclipse.che.ide.api.resources.Project)1 Resource (org.eclipse.che.ide.api.resources.Resource)1 VirtualFile (org.eclipse.che.ide.api.resources.VirtualFile)1 ServerException (org.eclipse.che.ide.commons.exception.ServerException)1 Region (org.eclipse.che.ide.ext.java.shared.dto.Region)1 CompilationUnit (org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit)1 JavaProject (org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject)1 PackageFragment (org.eclipse.che.ide.ext.java.shared.dto.model.PackageFragment)1 PackageFragmentRoot (org.eclipse.che.ide.ext.java.shared.dto.model.PackageFragmentRoot)1 FindUsagesRequest (org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesRequest)1 MessageBuilder (org.eclipse.che.ide.websocket.MessageBuilder)1