Example usage for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE

List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.

Prototype

int CPE_SOURCE

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a folder containing package fragments with source code to be compiled.

Usage

From source file:org.eclipse.che.jdt.internal.core.search.JavaSearchScope.java

License:Open Source License

/**
 * 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//from   w w w  .ja va2 s.  c  o  m
 *         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 org.eclipse.jdt.core.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 = javaProject.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) {
            // Add only exported entries.
            // 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 (cpEntry.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/*don't check existence*/);
                        //                                    if (target instanceof IFolder) // case of an external folder
                        //                                        path = ((IFolder)target).getFullPath();
                        String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                        add(projectPath.toString(), "", pathToString, false/*not a package*/, access); //$NON-NLS-1$
                        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/*don't check existence*/);
                    if (target instanceof IFolder) // case of an external folder
                        path = ((IFolder) target).getFullPath();
                    String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                    add(projectPath.toString(), "", pathToString, false/*not a package*/, access); //$NON-NLS-1$
                    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.lastSegment());
            //                            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(),
                            path.toOSString()
                                    .substring(projectPath.toString().length()
                                            + 1)/*Util.relativePath(path, 1*//*remove project segment*//*)*/,
                            projectPathString, false/*not a
                                                    package*/, access);
                }
            }
            break;
        }
    }
}

From source file:org.eclipse.che.plugin.java.plain.server.rest.ClasspathUpdaterService.java

License:Open Source License

private IClasspathEntry[] createModifiedEntry(List<ClasspathEntryDto> entries) {
    List<IClasspathEntry> coreClasspathEntries = new ArrayList<>(entries.size());
    for (ClasspathEntryDto entry : entries) {
        IPath path = fromOSString(entry.getPath());
        int entryKind = entry.getEntryKind();
        if (IClasspathEntry.CPE_LIBRARY == entryKind) {
            coreClasspathEntries.add(newLibraryEntry(path, null, null));
        } else if (IClasspathEntry.CPE_SOURCE == entryKind) {
            coreClasspathEntries.add(newSourceEntry(path));
        } else if (IClasspathEntry.CPE_VARIABLE == entryKind) {
            coreClasspathEntries.add(newVariableEntry(path, null, null));
        } else if (IClasspathEntry.CPE_CONTAINER == entryKind) {
            coreClasspathEntries.add(newContainerEntry(path));
        } else if (IClasspathEntry.CPE_PROJECT == entryKind) {
            coreClasspathEntries.add(newProjectEntry(path));
        }//from ww w. j a v a  2 s  .  co m
    }
    return coreClasspathEntries.toArray(new IClasspathEntry[coreClasspathEntries.size()]);
}

From source file:org.eclipse.che.plugin.java.testing.AbstractJavaTestRunner.java

License:Open Source License

protected String getOutputDirectory(IJavaProject javaProject) {
    String path = workspacePath + javaProject.getPath() + TEST_OUTPUT_FOLDER;
    try {/*from   w w w  .jav a  2  s.c o  m*/
        IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry iClasspathEntry : resolvedClasspath) {
            if (iClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outputLocation = iClasspathEntry.getOutputLocation();
                if (outputLocation == null) {
                    continue;
                }
                return workspacePath + outputLocation.removeLastSegments(1).append(TEST_OUTPUT_FOLDER);
            }
        }
    } catch (JavaModelException e) {
        return path;
    }
    return path;
}

From source file:org.eclipse.che.plugin.java.testing.MavenTestClasspathProvider.java

License:Open Source License

public URL[] getProjectClasspath(String projectAbsolutePath, String projectRelativePath, IWorkspaceRoot root)
        throws JavaModelException {
    Stream<ClasspathEntryDto> rawClasspath = classpathService.getClasspath(projectRelativePath).stream();
    Stream<ClasspathEntryDto> resolvedClasspath = toResolvedClassPath(rawClasspath);
    return resolvedClasspath.map(dto -> {
        try {//from ww  w  . ja v  a 2s  .c om
            String dtoPath = dto.getPath();
            IResource res = root.findMember(new Path(dtoPath));
            File path;
            switch (dto.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                if (res == null) {
                    path = new File(dtoPath);
                } else {
                    path = res.getLocation().toFile();
                }
                break;
            case IClasspathEntry.CPE_SOURCE:
                IPath relativePathFromProjectRoot = new Path(dtoPath).removeFirstSegments(1);
                String relativePathFromProjectRootStr = relativePathFromProjectRoot.toString();
                switch (relativePathFromProjectRootStr) {
                case "src/main/java":
                    path = Paths.get(projectAbsolutePath, "target", "classes").toFile();
                    break;
                case "src/test/java":
                    path = Paths.get(projectAbsolutePath, "target", "test-classes").toFile();
                    break;
                default:
                    path = Paths.get(projectAbsolutePath, "target", "classes").toFile();
                }
                break;
            default:
                path = new File(dtoPath);
            }
            return path.toURI().toURL();
        } catch (MalformedURLException e) {
            return null;
        }
    }).filter(url -> url != null).distinct().toArray(URL[]::new);
}

From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProvider.java

License:Open Source License

/**
 * Builds classpath for the java project.
 *
 * @param javaProject java project/*from   w ww  .  j a  va2  s.c  o  m*/
 * @return set of resources which are included to the classpath
 */
public Set<String> getProjectClassPath(IJavaProject javaProject) {
    try {
        IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(false);
        Set<String> result = new HashSet<>();
        for (IClasspathEntry classpathEntry : resolvedClasspath) {
            switch (classpathEntry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                IPath path = classpathEntry.getPath();
                result.add(path.toOSString());
                break;

            case IClasspathEntry.CPE_SOURCE:
                IPath outputLocation = classpathEntry.getOutputLocation();
                if (outputLocation != null) {
                    result.add(workspacePath + outputLocation.toOSString());
                }
                break;

            case IClasspathEntry.CPE_PROJECT:
                IPath projectPath = classpathEntry.getPath();
                JavaModel javaModel = JavaModelManager.getJavaModelManager().getJavaModel();
                IJavaProject project = javaModel.getJavaProject(projectPath.toOSString());
                result.addAll(getProjectClassPath(project));
                break;
            }
        }
        return result;
    } catch (JavaModelException e) {
        LOG.debug(e.getMessage(), e);
    }

    return Collections.emptySet();
}

From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProviderTest.java

License:Open Source License

@Test
public void classpathProviderShouldProvideClasspathPaths() throws Exception {
    IClasspathEntry classpathEntry = mock(IClasspathEntry.class);
    when(classpathEntry.getEntryKind()).thenReturn(IClasspathEntry.CPE_SOURCE);
    IPath path = new Path("/testProject/target/classes");
    when(classpathEntry.getOutputLocation()).thenReturn(path);

    IClasspathEntry[] entries = new IClasspathEntry[] { classpathEntry };
    when(javaProject.getResolvedClasspath(false)).thenReturn(entries);

    Set<String> classPath = classpathProvider.getProjectClassPath(javaProject);
    assertThat(classPath).isNotNull().isNotEmpty().contains(PROJECTS_PATH + "/testProject/target/classes");
}

From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProviderTest.java

License:Open Source License

@Test
public void classpathProviderShouldProvideClasspathPathsWithExternalDependencies() throws Exception {
    IClasspathEntry classpathEntry = mock(IClasspathEntry.class);
    when(classpathEntry.getEntryKind()).thenReturn(IClasspathEntry.CPE_SOURCE);
    IPath path = new Path("/testProject/target/classes");
    when(classpathEntry.getOutputLocation()).thenReturn(path);

    IClasspathEntry jarClasspathEntry = mock(IClasspathEntry.class);
    when(jarClasspathEntry.getEntryKind()).thenReturn(IClasspathEntry.CPE_LIBRARY);
    IPath jarPath = new Path("/absolute/path/to/jar.file");
    when(jarClasspathEntry.getPath()).thenReturn(jarPath);

    IClasspathEntry[] entries = new IClasspathEntry[] { classpathEntry, jarClasspathEntry };
    when(javaProject.getResolvedClasspath(false)).thenReturn(entries);

    Set<String> classPath = classpathProvider.getProjectClassPath(javaProject);

    assertThat(classPath).isNotNull().isNotEmpty().contains(PROJECTS_PATH + "/testProject/target/classes",
            "/absolute/path/to/jar.file");
}

From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProviderTest.java

License:Open Source License

@Test
public void classpathProviderShouldProvideClasspathPathsWithAnotherProject() throws Exception {
    JavaModel model = mock(JavaModel.class);
    Field javaModel = JavaModelManager.class.getDeclaredField("javaModel");
    javaModel.setAccessible(true);//from   www  .ja va  2  s.  com
    javaModel.set(JavaModelManager.getJavaModelManager(), model);
    IClasspathEntry entry = mockClasspathEntry(IClasspathEntry.CPE_SOURCE, "/anotherProject/src",
            "/anotherProject/target/classes");

    IJavaProject anotherProject = mock(IJavaProject.class);
    when(anotherProject.getResolvedClasspath(false)).thenReturn(new IClasspathEntry[] { entry });
    when(model.getJavaProject("/anotherProject")).thenReturn(anotherProject);

    IClasspathEntry classpathEntry = mockClasspathEntry(IClasspathEntry.CPE_SOURCE, "",
            "/testProject/target/classes");
    IClasspathEntry jarClasspathEntry = mockClasspathEntry(IClasspathEntry.CPE_LIBRARY,
            "/absolute/path/to/jar.file", null);
    IClasspathEntry projectEntry = mockClasspathEntry(IClasspathEntry.CPE_PROJECT, "/anotherProject", null);

    IClasspathEntry[] entries = new IClasspathEntry[] { classpathEntry, jarClasspathEntry, projectEntry };
    when(javaProject.getResolvedClasspath(false)).thenReturn(entries);

    Set<String> classPath = classpathProvider.getProjectClassPath(javaProject);

    assertThat(classPath).isNotNull().isNotEmpty().contains(PROJECTS_PATH + "/testProject/target/classes",
            "/absolute/path/to/jar.file", PROJECTS_PATH + "/anotherProject/target/classes");
}

From source file:org.eclipse.che.plugin.maven.server.classpath.OutputPathTest.java

License:Open Source License

@Test
public void testSourceClasspathEntryShouldHaveOutputLocationPath() throws Exception {
    String pom = "<groupId>test</groupId>" + "<artifactId>testOutputLocation</artifactId>"
            + "<version>42</version>" + "<dependencies>" + "    <dependency>"
            + "        <groupId>junit</groupId>" + "        <artifactId>junit</artifactId>"
            + "        <version>4.12</version>" + "    </dependency>" + "</dependencies>";
    createTestProject("test", pom);

    IProject test = ResourcesPlugin.getWorkspace().getRoot().getProject("test");
    mavenWorkspace.update(Collections.singletonList(test));
    mavenWorkspace.waitForUpdate();/*from   w  w w  . j a  v  a  2s.c  om*/

    JavaProject javaProject = (JavaProject) JavaCore.create(test);
    IClasspathEntry[] classpath = javaProject.getResolvedClasspath();
    IClasspathEntry srcMainJava = null;
    for (IClasspathEntry entry : classpath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                && entry.getPath().toOSString().endsWith("src/main/java")) {
            srcMainJava = entry;
            break;
        }
    }

    assertThat(srcMainJava).isNotNull();
    assertThat(srcMainJava.getOutputLocation()).isNotNull();
    assertThat(srcMainJava.getOutputLocation().toOSString()).endsWith("target/classes");
}

From source file:org.eclipse.che.plugin.maven.server.classpath.OutputPathTest.java

License:Open Source License

@Test
public void testSourceClasspathEntryShouldHaveCustomOutputLocationPath() throws Exception {
    String pom = "<groupId>test</groupId>" + "<artifactId>testOutputLocation</artifactId>"
            + "<version>42</version>" + "<dependencies>" + "    <dependency>"
            + "        <groupId>junit</groupId>" + "        <artifactId>junit</artifactId>"
            + "        <version>4.12</version>" + "    </dependency>" + "</dependencies>" + "<build>"
            + "  <outputDirectory>bin/classes</outputDirectory>" + "</build>";

    createTestProject("test", pom);

    IProject test = ResourcesPlugin.getWorkspace().getRoot().getProject("test");
    mavenWorkspace.update(Collections.singletonList(test));
    mavenWorkspace.waitForUpdate();/*from  www .  j  av a2 s  .c  om*/

    JavaProject javaProject = (JavaProject) JavaCore.create(test);
    IClasspathEntry[] classpath = javaProject.getResolvedClasspath();
    IClasspathEntry srcMainJava = null;
    for (IClasspathEntry entry : classpath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                && entry.getPath().toOSString().endsWith("src/main/java")) {
            srcMainJava = entry;
            break;
        }
    }

    assertThat(srcMainJava).isNotNull();
    assertThat(srcMainJava.getOutputLocation()).isNotNull();
    assertThat(srcMainJava.getOutputLocation().toOSString()).endsWith("bin/classes");
}