Search in sources :

Example 6 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class ClasspathUpdaterService method createModifiedEntry.

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));
        }
    }
    return coreClasspathEntries.toArray(new IClasspathEntry[coreClasspathEntries.size()]);
}
Also used : IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) ClasspathEntryDto(org.eclipse.che.ide.ext.java.shared.dto.classpath.ClasspathEntryDto)

Example 7 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class ClasspathBuilderTest method classpathShouldBeUpdatedOnlyWithJREContainer.

@Test
public void classpathShouldBeUpdatedOnlyWithJREContainer() throws Exception {
    classpathBuilder.generateClasspath(iJavaProject, sourceFolders, library);
    ArgumentCaptor<IClasspathEntry[]> classpathEntries = ArgumentCaptor.forClass(IClasspathEntry[].class);
    verify(iJavaProject).setRawClasspath(classpathEntries.capture(), eq(null));
    assertEquals(1, classpathEntries.getValue().length);
    assertEquals(new Path(JREContainerInitializer.JRE_CONTAINER), classpathEntries.getValue()[0].getPath());
}
Also used : Path(org.eclipse.core.runtime.Path) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Test(org.testng.annotations.Test) BaseTest(org.eclipse.che.plugin.java.plain.server.BaseTest)

Example 8 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class ClasspathBuilder method addJars.

private void addJars(IJavaProject project, List<String> library, final List<IClasspathEntry> classpathEntries) {
    if (library == null || library.isEmpty()) {
        return;
    }
    for (String libFolder : library) {
        if (libFolder.isEmpty()) {
            continue;
        }
        IFolder libraryFolder = project.getProject().getFolder(libFolder);
        if (!libraryFolder.exists()) {
            return;
        }
        try {
            libraryFolder.accept(proxy -> {
                if (IResource.FILE != proxy.getType()) {
                    return true;
                }
                IPath path = proxy.requestFullPath();
                if (!path.toString().endsWith(".jar")) {
                    return false;
                }
                IClasspathEntry libEntry = JavaCore.newLibraryEntry(proxy.requestResource().getLocation(), null, null);
                classpathEntries.add(libEntry);
                return false;
            }, IContainer.INCLUDE_PHANTOMS);
        } catch (CoreException e) {
            LOG.warn("Can't read folder structure: " + libraryFolder.getFullPath().toString());
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IFolder(org.eclipse.core.resources.IFolder)

Example 9 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class ClasspathBuilder method generateClasspath.

/**
     * Generates classpath with default entries.
     *
     * @param project
     *         java project which need to contain classpath
     * @param sourceFolders
     *         list of the project's source folders
     * @param library
     *         list of the project's library folders
     * @throws ServerException
     *         happens when some problems with setting classpath
     */
public void generateClasspath(IJavaProject project, List<String> sourceFolders, List<String> library) throws ServerException {
    List<IClasspathEntry> classpathEntries = new ArrayList<>();
    //create classpath container for default JRE
    IClasspathEntry jreContainer = JavaCore.newContainerEntry(new Path(JREContainerInitializer.JRE_CONTAINER));
    classpathEntries.add(jreContainer);
    addSourceFolders(project, sourceFolders, classpathEntries);
    addJars(project, library, classpathEntries);
    try {
        project.setRawClasspath(classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]), null);
    } catch (JavaModelException e) {
        LOG.warn("Can't set classpath for: " + project.getProject().getFullPath().toOSString(), e);
        throw new ServerException(e);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) JavaModelException(org.eclipse.jdt.core.JavaModelException) ServerException(org.eclipse.che.api.core.ServerException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList)

Example 10 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class JavaProjectHelper method addClassFolder.

/**
     * Creates and adds a class folder to the class path.
     * @param jproject The parent project
     * @param containerName
     * @param sourceAttachPath The source attachment path
     * @param sourceAttachRoot The source attachment root path
     * @return The handle of the created root
     * @throws CoreException
     */
public static IPackageFragmentRoot addClassFolder(IJavaProject jproject, String containerName, IPath sourceAttachPath, IPath sourceAttachRoot) throws CoreException {
    IProject project = jproject.getProject();
    IContainer container = null;
    if (containerName == null || containerName.length() == 0) {
        container = project;
    } else {
        IFolder folder = project.getFolder(containerName);
        if (!folder.exists()) {
            CoreUtility.createFolder(folder, false, true, null);
        }
        container = folder;
    }
    IClasspathEntry cpe = JavaCore.newLibraryEntry(container.getFullPath(), sourceAttachPath, sourceAttachRoot);
    addToClasspath(jproject, cpe);
    return jproject.getPackageFragmentRoot(container);
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IContainer(org.eclipse.core.resources.IContainer) IProject(org.eclipse.core.resources.IProject) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)115 IPath (org.eclipse.core.runtime.IPath)50 IJavaProject (org.eclipse.jdt.core.IJavaProject)40 ArrayList (java.util.ArrayList)34 Path (org.eclipse.core.runtime.Path)25 JavaModelException (org.eclipse.jdt.core.JavaModelException)20 IProject (org.eclipse.core.resources.IProject)17 IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)16 File (java.io.File)15 IFolder (org.eclipse.core.resources.IFolder)13 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IJavaElement (org.eclipse.jdt.core.IJavaElement)12 CoreException (org.eclipse.core.runtime.CoreException)11 JavaProject (org.eclipse.jdt.internal.core.JavaProject)11 HashMap (java.util.HashMap)9 IResource (org.eclipse.core.resources.IResource)9 IFile (org.eclipse.core.resources.IFile)8 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 URL (java.net.URL)6