Search in sources :

Example 11 with JavaProject

use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.

the class IndexManager method indexAll.

/**
     * Trigger addition of the entire content of a project
     * Note: the actual operation is performed in background
     */
public void indexAll(IProject project) {
    // determine the new children
    try {
        JavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
        JavaProject javaProject = (JavaProject) model.getJavaProject(project);
        // only consider immediate libraries - each project will do the same
        // NOTE: force to resolve CP variables before calling indexer - 19303, so that initializers
        // will be run in the current thread.
        IClasspathEntry[] entries = javaProject.getResolvedClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
                indexLibrary(entry.getPath(), project, ((ClasspathEntry) entry).getLibraryIndexLocation());
        }
    } catch (JavaModelException e) {
    // cannot retrieve classpath info
    }
    // check if the same request is not already in the queue
    IndexRequest request = new IndexAllProject(project, this);
    if (!isJobWaiting(request))
        request(request);
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) JavaModel(org.eclipse.jdt.internal.core.JavaModel) ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 12 with JavaProject

use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.

the class JavaSearchScope method add.

/**
     * Add a path to current java search scope or all project fragment roots if null.
     * Use project resolved classpath to retrieve and store access restriction on each classpath entry.
     * Recurse if dependent projects are found.
     * @param javaProject Project used to get resolved classpath entries
     * @param pathToAdd Path to add in case of single element or null if user want to add all project package fragment roots
     * @param includeMask Mask to apply on classpath entries
     * @param projectsToBeAdded Set to avoid infinite recursion
     * @param visitedProjects Set to avoid adding twice the same project
     * @param referringEntry Project raw entry in referring project classpath
     * @throws JavaModelException May happen while getting java model info
     */
void add(JavaProject javaProject, IPath pathToAdd, int includeMask, HashSet projectsToBeAdded, HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException {
    IProject project = javaProject.getProject();
    if (!project.isAccessible() || !visitedProjects.add(project))
        return;
    IPath projectPath = project.getFullPath();
    String projectPathString = projectPath.toString();
    addEnclosingProjectOrJar(projectPath);
    IClasspathEntry[] entries = javaProject.getResolvedClasspath();
    IJavaModel model = javaProject.getJavaModel();
    JavaModelManager.PerProjectInfo perProjectInfo = javaProject.getPerProjectInfo();
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        AccessRuleSet access = null;
        ClasspathEntry cpEntry = (ClasspathEntry) entry;
        if (referringEntry != null) {
            // Source folder are implicitly exported.
            if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            cpEntry = cpEntry.combineWith((ClasspathEntry) referringEntry);
        //				cpEntry = ((ClasspathEntry)referringEntry).combineWith(cpEntry);
        }
        access = cpEntry.getAccessRuleSet();
        switch(entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                IClasspathEntry rawEntry = null;
                Map rootPathToRawEntries = perProjectInfo.rootPathToRawEntries;
                if (rootPathToRawEntries != null) {
                    rawEntry = (IClasspathEntry) rootPathToRawEntries.get(entry.getPath());
                }
                if (rawEntry == null)
                    break;
                rawKind: switch(rawEntry.getEntryKind()) {
                    case IClasspathEntry.CPE_LIBRARY:
                    case IClasspathEntry.CPE_VARIABLE:
                        if ((includeMask & APPLICATION_LIBRARIES) != 0) {
                            IPath path = entry.getPath();
                            if (pathToAdd == null || pathToAdd.equals(path)) {
                                Object target = JavaModel.getTarget(path, false);
                                if (// case of an external folder
                                target instanceof IFolder)
                                    path = ((IFolder) target).getFullPath();
                                String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                                //$NON-NLS-1$
                                add(projectPath.toString(), "", pathToString, false, /*not a package*/
                                access);
                                addEnclosingProjectOrJar(entry.getPath());
                            }
                        }
                        break;
                    case IClasspathEntry.CPE_CONTAINER:
                        IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), javaProject);
                        if (container == null)
                            break;
                        switch(container.getKind()) {
                            case IClasspathContainer.K_APPLICATION:
                                if ((includeMask & APPLICATION_LIBRARIES) == 0)
                                    break rawKind;
                                break;
                            case IClasspathContainer.K_SYSTEM:
                            case IClasspathContainer.K_DEFAULT_SYSTEM:
                                if ((includeMask & SYSTEM_LIBRARIES) == 0)
                                    break rawKind;
                                break;
                            default:
                                break rawKind;
                        }
                        IPath path = entry.getPath();
                        if (pathToAdd == null || pathToAdd.equals(path)) {
                            Object target = JavaModel.getTarget(path, false);
                            if (// case of an external folder
                            target instanceof IFolder)
                                path = ((IFolder) target).getFullPath();
                            String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                            //$NON-NLS-1$
                            add(projectPath.toString(), "", pathToString, false, /*not a package*/
                            access);
                            addEnclosingProjectOrJar(entry.getPath());
                        }
                        break;
                }
                break;
            case IClasspathEntry.CPE_PROJECT:
                if ((includeMask & REFERENCED_PROJECTS) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        JavaProject referencedProject = (JavaProject) model.getJavaProject(path.toOSString());
                        if (!projectsToBeAdded.contains(referencedProject)) {
                            // do not recurse if depending project was used to create the scope
                            add(referencedProject, null, includeMask, projectsToBeAdded, visitedProjects, cpEntry);
                        }
                    }
                }
                break;
            case IClasspathEntry.CPE_SOURCE:
                if ((includeMask & SOURCES) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        add(projectPath.toString(), Util.relativePath(path, projectPath.segmentCount()), projectPathString, false, /*not a package*/
                        access);
                    }
                }
                break;
        }
    }
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet) IProject(org.eclipse.core.resources.IProject) JavaModelManager(org.eclipse.jdt.internal.core.JavaModelManager) ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) Map(java.util.Map) IJavaModel(org.eclipse.jdt.core.IJavaModel) IFolder(org.eclipse.core.resources.IFolder)

Example 13 with JavaProject

use of org.eclipse.jdt.internal.core.JavaProject 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)

Example 14 with JavaProject

use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.

the class FindReferencesTest method testSearchCantFindElement.

@Test(expected = SearchException.class)
public void testSearchCantFindElement() 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");
    ICompilationUnit compilationUnitA = packageFragment.createCompilationUnit("A.java", a.toString(), true, null);
    SearchManager manager = new SearchManager();
    manager.findUsage(JUnitSourceSetup.getProject(), compilationUnitA.getResource().getFullPath().toOSString(), 24);
}
Also used : Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SearchManager(org.eclipse.che.plugin.java.server.search.SearchManager) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test) BaseTest(org.eclipse.che.plugin.java.server.che.BaseTest)

Example 15 with JavaProject

use of org.eclipse.jdt.internal.core.JavaProject 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

JavaProject (org.eclipse.jdt.internal.core.JavaProject)18 IJavaProject (org.eclipse.jdt.core.IJavaProject)17 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)11 IPath (org.eclipse.core.runtime.IPath)8 Path (org.eclipse.core.runtime.Path)8 IJavaElement (org.eclipse.jdt.core.IJavaElement)8 IProject (org.eclipse.core.resources.IProject)7 MavenServerManagerTest (org.eclipse.che.plugin.maven.server.rmi.MavenServerManagerTest)6 Test (org.testng.annotations.Test)6 JsonObject (com.google.gson.JsonObject)4 Provider (com.google.inject.Provider)4 File (java.io.File)4 RemoteException (java.rmi.RemoteException)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 List (java.util.List)4 ListIterator (java.util.ListIterator)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4