Search in sources :

Example 1 with CompilationUnit

use of org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit in project che by eclipse.

the class TypeNode method getChildrenImpl.

@Override
protected Promise<List<Node>> getChildrenImpl() {
    return createFromAsyncRequest(callback -> {
        List<Node> children = new ArrayList<>();
        if (compilationUnit != null && type.isPrimary()) {
            for (ImportDeclaration importDeclaration : compilationUnit.getImports()) {
                createNodeForAllMatches(importDeclaration.getHandleIdentifier(), children);
            }
            for (Type subType : compilationUnit.getTypes()) {
                if (subType == type) {
                    continue;
                }
                children.add(nodeFactory.create(subType, compilationUnit, classFile, matches));
            }
        }
        createNodeForAllMatches(type.getHandleIdentifier(), children);
        for (Initializer initializer : type.getInitializers()) {
            createNodeForAllMatches(initializer.getHandleIdentifier(), children);
        }
        for (Field field : type.getFields()) {
            createNodeForAllMatches(field.getHandleIdentifier(), children);
        }
        final List<Node> typeNodes = type.getTypes().stream().map(subType -> nodeFactory.create(subType, compilationUnit, classFile, matches)).collect(Collectors.toList());
        children.addAll(typeNodes);
        final List<Node> methodNodes = type.getMethods().stream().map(method -> nodeFactory.create(method, matches, compilationUnit, classFile)).collect(Collectors.toList());
        children.addAll(methodNodes);
        Collections.sort(children, new NodeComparator());
        callback.onSuccess(children);
    });
}
Also used : AsyncPromiseHelper.createFromAsyncRequest(org.eclipse.che.api.promises.client.callback.AsyncPromiseHelper.createFromAsyncRequest) Inject(com.google.inject.Inject) Flags(org.eclipse.che.ide.ext.java.client.util.Flags) JavaResources(org.eclipse.che.ide.ext.java.client.JavaResources) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Nullable(org.eclipse.che.commons.annotation.Nullable) ArrayList(java.util.ArrayList) Assisted(com.google.inject.assistedinject.Assisted) ImportDeclaration(org.eclipse.che.ide.ext.java.shared.dto.model.ImportDeclaration) Promise(org.eclipse.che.api.promises.client.Promise) NodePresentation(org.eclipse.che.ide.ui.smartTree.presentation.NodePresentation) List(java.util.List) Match(org.eclipse.che.ide.ext.java.shared.dto.search.Match) Initializer(org.eclipse.che.ide.ext.java.shared.dto.model.Initializer) Node(org.eclipse.che.ide.api.data.tree.Node) CompilationUnit(org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit) Type(org.eclipse.che.ide.ext.java.shared.dto.model.Type) Map(java.util.Map) Field(org.eclipse.che.ide.ext.java.shared.dto.model.Field) SVGResource(org.vectomatic.dom.svg.ui.SVGResource) ClassFile(org.eclipse.che.ide.ext.java.shared.dto.model.ClassFile) Collections(java.util.Collections) Field(org.eclipse.che.ide.ext.java.shared.dto.model.Field) Type(org.eclipse.che.ide.ext.java.shared.dto.model.Type) Initializer(org.eclipse.che.ide.ext.java.shared.dto.model.Initializer) Node(org.eclipse.che.ide.api.data.tree.Node) ArrayList(java.util.ArrayList) ImportDeclaration(org.eclipse.che.ide.ext.java.shared.dto.model.ImportDeclaration)

Example 2 with CompilationUnit

use of org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit in project che by eclipse.

the class JavaElementToDtoConverter method getPackageFragments.

private List<PackageFragment> getPackageFragments(IPackageFragmentRoot parent, String projectPath) throws JavaModelException {
    List<PackageFragment> result = new ArrayList<>();
    Set<Object> objects = childrens.get(parent);
    if (objects == null) {
        return result;
    }
    for (Object object : objects) {
        if (object instanceof IPackageFragment) {
            IPackageFragment packageFragment = (IPackageFragment) object;
            PackageFragment fragment = DtoFactory.newDto(PackageFragment.class);
            fragment.setProjectPath(projectPath);
            fragment.setPath(packageFragment.getPath().toOSString());
            fragment.setHandleIdentifier(packageFragment.getHandleIdentifier());
            fragment.setElementName(packageFragment.getElementName());
            fragment.setKind(packageFragment.getKind());
            fragment.setDefaultPackage(packageFragment.isDefaultPackage());
            List<CompilationUnit> compilationUnits = new ArrayList<>();
            List<ClassFile> classFiles = new ArrayList<>();
            addCompilationUnitsAndClassFiles(object, compilationUnits, classFiles);
            fragment.setCompilationUnits(compilationUnits);
            fragment.setClassFiles(classFiles);
            result.add(fragment);
        }
    }
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) PackageFragment(org.eclipse.che.ide.ext.java.shared.dto.model.PackageFragment) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.che.ide.ext.java.shared.dto.model.ClassFile) ArrayList(java.util.ArrayList)

Example 3 with CompilationUnit

use of org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit 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)

Example 4 with CompilationUnit

use of org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit in project che by eclipse.

the class JavaElementToDtoConverter method addCompilationUnitsAndClassFiles.

private void addCompilationUnitsAndClassFiles(Object parent, List<CompilationUnit> compilationUnits, List<ClassFile> classFiles) throws JavaModelException {
    Set<Object> childrens = this.childrens.get(parent);
    for (Object children : childrens) {
        if (children instanceof ICompilationUnit) {
            ICompilationUnit unit = (ICompilationUnit) children;
            CompilationUnit compilationUnit = DtoFactory.newDto(CompilationUnit.class);
            compilationUnit.setElementName(unit.getElementName());
            compilationUnit.setProjectPath(unit.getJavaProject().getPath().toOSString());
            compilationUnit.setPath(unit.getResource().getFullPath().toOSString());
            compilationUnit.setHandleIdentifier(unit.getHandleIdentifier());
            compilationUnit.setLabel(JavaElementLabels.getElementLabel(unit, JavaElementLabels.ALL_DEFAULT));
            compilationUnit.setImports(getImports(children));
            compilationUnit.setTypes(getTypes(children));
            compilationUnits.add(compilationUnit);
        } else if (children instanceof IClassFile) {
            ClassFile classFile = DtoFactory.newDto(ClassFile.class);
            IClassFile clazz = (IClassFile) children;
            classFile.setHandleIdentifier(clazz.getHandleIdentifier());
            classFile.setElementName(clazz.getElementName());
            classFile.setPath(clazz.getType().getFullyQualifiedName());
            classFile.setLabel(JavaElementLabels.getElementLabel(clazz, JavaElementLabels.ALL_DEFAULT));
            classFile.setProjectPath(clazz.getJavaProject().getPath().toOSString());
            classFile.setType(getTypes(children).get(0));
            classFiles.add(classFile);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IClassFile(org.eclipse.jdt.core.IClassFile) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.che.ide.ext.java.shared.dto.model.ClassFile)

Example 5 with CompilationUnit

use of org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit in project che by eclipse.

the class JavaNavigation method getCompilationUnitByPath.

/**
     * Get the compilation unit representation of the java file.
     *
     * @param javaProject
     *         path to the project which is contained class file
     * @param fqn
     *         fully qualified name of the class file
     * @param isShowingInheritedMembers
     *         <code>true</code> iff inherited members are shown
     * @return instance of {@link CompilationUnit}
     * @throws JavaModelException
     *         when JavaModel has a failure
     */
public CompilationUnit getCompilationUnitByPath(IJavaProject javaProject, String fqn, boolean isShowingInheritedMembers) throws JavaModelException {
    IType type = javaProject.findType(fqn);
    CompilationUnit compilationUnit = DtoFactory.newDto(CompilationUnit.class);
    ITypeRoot unit;
    if (type.isBinary()) {
        unit = type.getClassFile();
        compilationUnit.setPath(((IClassFile) unit).getType().getFullyQualifiedName());
    } else {
        unit = type.getCompilationUnit();
        compilationUnit.setProjectPath(unit.getJavaProject().getPath().toOSString());
        compilationUnit.setPath(unit.getResource().getFullPath().toOSString());
    }
    compilationUnit.setElementName(unit.getElementName());
    compilationUnit.setHandleIdentifier(unit.getHandleIdentifier());
    compilationUnit.setLabel(org.eclipse.jdt.ui.JavaElementLabels.getElementLabel(unit, org.eclipse.jdt.ui.JavaElementLabels.ALL_DEFAULT));
    List<Type> types = new ArrayList<>(1);
    Type dtoType = convertToDTOType(type);
    dtoType.setPrimary(true);
    types.add(dtoType);
    compilationUnit.setTypes(types);
    if (isShowingInheritedMembers) {
        compilationUnit.setSuperTypes(calculateSuperTypes(type));
    }
    return compilationUnit;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit) JarEntryType(org.eclipse.che.ide.ext.java.shared.JarEntry.JarEntryType) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.che.ide.ext.java.shared.dto.model.Type) IClassFile(org.eclipse.jdt.core.IClassFile) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) ArrayList(java.util.ArrayList) IType(org.eclipse.jdt.core.IType)

Aggregations

CompilationUnit (org.eclipse.che.ide.ext.java.shared.dto.model.CompilationUnit)7 ArrayList (java.util.ArrayList)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ClassFile (org.eclipse.che.ide.ext.java.shared.dto.model.ClassFile)3 PackageFragment (org.eclipse.che.ide.ext.java.shared.dto.model.PackageFragment)3 Type (org.eclipse.che.ide.ext.java.shared.dto.model.Type)3 IClassFile (org.eclipse.jdt.core.IClassFile)3 Inject (com.google.inject.Inject)2 Assisted (com.google.inject.assistedinject.Assisted)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 NotNull (javax.validation.constraints.NotNull)2 Promise (org.eclipse.che.api.promises.client.Promise)2 AsyncPromiseHelper.createFromAsyncRequest (org.eclipse.che.api.promises.client.callback.AsyncPromiseHelper.createFromAsyncRequest)2 Node (org.eclipse.che.ide.api.data.tree.Node)2 JavaResources (org.eclipse.che.ide.ext.java.client.JavaResources)2 PackageFragmentRoot (org.eclipse.che.ide.ext.java.shared.dto.model.PackageFragmentRoot)2 Match (org.eclipse.che.ide.ext.java.shared.dto.search.Match)2 NodePresentation (org.eclipse.che.ide.ui.smartTree.presentation.NodePresentation)2