Search in sources :

Example 76 with IClasspathEntry

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

the class RefactoringTest method restoreTestProject.

private void restoreTestProject() throws Exception {
    IJavaProject javaProject = getRoot().getJavaProject();
    if (javaProject.exists()) {
        IClasspathEntry srcEntry = getRoot().getRawClasspathEntry();
        IClasspathEntry jreEntry = RefactoringTestSetup.getJRELibrary().getRawClasspathEntry();
        IClasspathEntry[] cpes = javaProject.getRawClasspath();
        ArrayList newCPEs = new ArrayList();
        boolean cpChanged = false;
        for (int i = 0; i < cpes.length; i++) {
            IClasspathEntry cpe = cpes[i];
            if (cpe.equals(srcEntry) || cpe.equals(jreEntry)) {
                newCPEs.add(cpe);
            } else {
                cpChanged = true;
            }
        }
        if (cpChanged) {
            IClasspathEntry[] newCPEsArray = (IClasspathEntry[]) newCPEs.toArray(new IClasspathEntry[newCPEs.size()]);
            javaProject.setRawClasspath(newCPEsArray, null);
        }
        Object[] nonJavaResources = javaProject.getNonJavaResources();
        for (int i = 0; i < nonJavaResources.length; i++) {
            Object kid = nonJavaResources[i];
            if (kid instanceof IResource) {
                IResource resource = (IResource) kid;
                if (!PROJECT_RESOURCE_CHILDREN.contains(resource.getName())) {
                    JavaProjectHelper.delete(resource);
                }
            }
        }
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IResource(org.eclipse.core.resources.IResource)

Example 77 with IClasspathEntry

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

the class MavenClasspathUtil method readMavenClasspath.

public static IClasspathContainer readMavenClasspath(IJavaProject javaProject) {
    IFile file = javaProject.getProject().getFile(".che/classpath.maven");
    IClasspathEntry[] entries;
    if (file.exists()) {
        try {
            char[] chars = Util.getResourceContentsAsCharArray(file);
            String content = new String(chars);
            if (!content.isEmpty()) {
                String[] pathToJars = content.split(":");
                List<IClasspathEntry> classpathEntry = new ArrayList<>();
                for (String path : pathToJars) {
                    String srcPath = path.substring(0, path.lastIndexOf('.')) + "-sources.jar";
                    classpathEntry.add(JavaCore.newLibraryEntry(new org.eclipse.core.runtime.Path(path), new org.eclipse.core.runtime.Path(srcPath), null));
                }
                entries = classpathEntry.toArray(new IClasspathEntry[classpathEntry.size()]);
            } else {
                entries = EMPTY;
            }
        } catch (JavaModelException e) {
            LOG.error("Can't read maven classpath.", e);
            entries = EMPTY;
        }
    } else {
        entries = EMPTY;
    }
    return new MavenClasspathContainer(entries);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList)

Example 78 with IClasspathEntry

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

the class ClasspathManager method updateClasspath.

public void updateClasspath(MavenProject mavenProject) {
    IJavaProject javaProject = JavaCore.create(mavenProject.getProject());
    if (javaProject != null) {
        IClasspathEntry[] entries = getClasspath(mavenProject);
        MavenClasspathContainer container = new MavenClasspathContainer(entries);
        try {
            JavaCore.setClasspathContainer(new Path(MavenClasspathContainer.CONTAINER_ID), new IJavaProject[] { javaProject }, new IClasspathContainer[] { container }, new NullProgressMonitor());
        } catch (JavaModelException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) MavenClasspathContainer(org.eclipse.che.plugin.maven.server.core.MavenClasspathContainer)

Example 79 with IClasspathEntry

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

the class ClasspathManager method downloadSources.

private boolean downloadSources(IPackageFragmentRoot fragmentRoot) throws JavaModelException {
    fragmentRoot.getAdapter(MavenArtifactKey.class);
    IClasspathEntry classpathEntry = fragmentRoot.getResolvedClasspathEntry();
    MavenArtifactKey artifactKey = getArtifactKey(classpathEntry);
    if (artifactKey != null) {
        MavenServerWrapper mavenServer = wrapperManager.getMavenServer(MavenWrapperManager.ServerType.DOWNLOAD);
        try {
            mavenServer.customize(projectManager.copyWorkspaceCache(), terminal, notifier, false, false);
            MavenArtifactKey sourceKey = new MavenArtifactKey(artifactKey.getGroupId(), artifactKey.getArtifactId(), artifactKey.getVersion(), artifactKey.getPackaging(), SOURCES);
            MavenArtifact mavenArtifact = mavenServer.resolveArtifact(sourceKey, Collections.emptyList());
            if (mavenArtifact.isResolved()) {
                updateClasspath(projectManager.findMavenProject(fragmentRoot.getJavaProject().getProject()));
            }
            return mavenArtifact.isResolved();
        } finally {
            wrapperManager.release(mavenServer);
        }
    }
    return false;
}
Also used : MavenArtifactKey(org.eclipse.che.maven.data.MavenArtifactKey) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) MavenServerWrapper(org.eclipse.che.plugin.maven.server.MavenServerWrapper) MavenArtifact(org.eclipse.che.maven.data.MavenArtifact)

Example 80 with IClasspathEntry

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

the class WorkspaceTest method testShouldContainsDefaultTestSourceDirectory.

@Test
public void testShouldContainsDefaultTestSourceDirectory() throws Exception {
    String pom = "<groupId>test</groupId>" + "<artifactId>testArtifact</artifactId>" + "<version>42</version>" + "<dependencies>" + "    <dependency>" + "        <groupId>junit</groupId>" + "        <artifactId>junit</artifactId>" + "        <version>4.12</version>" + "    </dependency>" + "</dependencies>" + "<build>" + "</build>";
    createTestProject("test", pom);
    IProject test = ResourcesPlugin.getWorkspace().getRoot().getProject("test");
    mavenWorkspace.update(Collections.singletonList(test));
    mavenWorkspace.waitForUpdate();
    JavaProject javaProject = (JavaProject) JavaCore.create(test);
    IClasspathEntry[] classpath = javaProject.getResolvedClasspath();
    assertThat(classpath).onProperty("path").is(new Condition<Object[]>() {

        @Override
        public boolean matches(Object[] value) {
            return Stream.of(value).filter(o -> {
                if (o instanceof IPath) {
                    return ((IPath) o).toOSString().endsWith("src/test/java");
                }
                return false;
            }).findFirst().isPresent();
        }
    });
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) ListIterator(java.util.ListIterator) ProjectRegistry(org.eclipse.che.api.project.server.ProjectRegistry) MavenWorkspace(org.eclipse.che.plugin.maven.server.core.MavenWorkspace) Test(org.testng.annotations.Test) MavenCommunication(org.eclipse.che.plugin.maven.server.core.MavenCommunication) MavenArtifact(org.eclipse.che.maven.data.MavenArtifact) Constants(org.eclipse.che.ide.ext.java.shared.Constants) MavenServerManagerTest(org.eclipse.che.plugin.maven.server.rmi.MavenServerManagerTest) Assert.assertThat(org.junit.Assert.assertThat) Assertions.assertThat(org.fest.assertions.Assertions.assertThat) IPath(org.eclipse.core.runtime.IPath) Model(org.eclipse.che.ide.maven.tools.Model) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IJavaProject(org.eclipse.jdt.core.IJavaProject) BeforeMethod(org.testng.annotations.BeforeMethod) MessageType(org.eclipse.che.plugin.maven.shared.MessageType) Set(java.util.Set) MavenExecutorService(org.eclipse.che.plugin.maven.server.core.MavenExecutorService) Collectors(java.util.stream.Collectors) RemoteException(java.rmi.RemoteException) MavenKey(org.eclipse.che.maven.data.MavenKey) List(java.util.List) Stream(java.util.stream.Stream) MavenProjectManager(org.eclipse.che.plugin.maven.server.core.MavenProjectManager) Path(org.eclipse.core.runtime.Path) RegisteredProject(org.eclipse.che.api.project.server.RegisteredProject) FolderEntry(org.eclipse.che.api.project.server.FolderEntry) MavenTerminal(org.eclipse.che.maven.server.MavenTerminal) Mockito.mock(org.mockito.Mockito.mock) EclipseWorkspaceProvider(org.eclipse.che.plugin.maven.server.core.EclipseWorkspaceProvider) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) JavaProject(org.eclipse.jdt.internal.core.JavaProject) Condition(org.fest.assertions.Condition) ArrayList(java.util.ArrayList) TEST_SOURCE_FOLDER(org.eclipse.che.plugin.maven.shared.MavenAttributes.TEST_SOURCE_FOLDER) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) IProject(org.eclipse.core.resources.IProject) ClasspathManager(org.eclipse.che.plugin.maven.server.core.classpath.ClasspathManager) JavaCore(org.eclipse.jdt.core.JavaCore) CoreMatchers.hasItems(org.hamcrest.CoreMatchers.hasItems) Mockito.when(org.mockito.Mockito.when) File(java.io.File) Provider(com.google.inject.Provider) NotificationMessage(org.eclipse.che.plugin.maven.shared.dto.NotificationMessage) IJavaElement(org.eclipse.jdt.core.IJavaElement) MavenProject(org.eclipse.che.plugin.maven.server.core.project.MavenProject) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) JsonObject(com.google.gson.JsonObject) IProject(org.eclipse.core.resources.IProject) Test(org.testng.annotations.Test) MavenServerManagerTest(org.eclipse.che.plugin.maven.server.rmi.MavenServerManagerTest)

Aggregations

IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)123 IPath (org.eclipse.core.runtime.IPath)55 IJavaProject (org.eclipse.jdt.core.IJavaProject)44 ArrayList (java.util.ArrayList)35 Path (org.eclipse.core.runtime.Path)27 JavaModelException (org.eclipse.jdt.core.JavaModelException)23 IProject (org.eclipse.core.resources.IProject)20 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 CoreException (org.eclipse.core.runtime.CoreException)12 IJavaElement (org.eclipse.jdt.core.IJavaElement)12 JavaProject (org.eclipse.jdt.internal.core.JavaProject)11 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)10 HashMap (java.util.HashMap)9 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 URL (java.net.URL)7