Search in sources :

Example 26 with IClasspathEntry

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

the class JavaModelUtil method getClasspathEntry.

/**
     * Returns the classpath entry of the given package fragment root. This is the raw entry, except
     * if the root is a referenced library, in which case it's the resolved entry.
     *
     * @param root a package fragment root
     * @return the corresponding classpath entry
     * @throws JavaModelException if accessing the entry failed
     * @since 3.6
     */
public static IClasspathEntry getClasspathEntry(IPackageFragmentRoot root) throws JavaModelException {
    IClasspathEntry rawEntry = root.getRawClasspathEntry();
    int rawEntryKind = rawEntry.getEntryKind();
    switch(rawEntryKind) {
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_VARIABLE:
        case // should not happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=305037
        IClasspathEntry.CPE_CONTAINER:
            if (root.isArchive() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
                IClasspathEntry resolvedEntry = root.getResolvedClasspathEntry();
                if (resolvedEntry.getReferencingEntry() != null)
                    return resolvedEntry;
                else
                    return rawEntry;
            }
    }
    return rawEntry;
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 27 with IClasspathEntry

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

the class WorkspaceTest method testSingleProjectClasspath.

@Test
public void testSingleProjectClasspath() 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>";
    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).lastSegment().endsWith("junit-4.12.jar");
                }
                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)

Example 28 with IClasspathEntry

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

the class WorkspaceTest method testShouldContainsCustomTestSourceDirectory.

@Test
public void testShouldContainsCustomTestSourceDirectory() 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>" + "<testSourceDirectory>/mytest</testSourceDirectory>" + "</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("test");
                }
                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)

Example 29 with IClasspathEntry

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

the class WorkspaceTest method testWsShouldAddSourceFolderFromBuildHelperPlugin.

@Test
public void testWsShouldAddSourceFolderFromBuildHelperPlugin() throws Exception {
    String pom = "<groupId>test</groupId>\n" + "<artifactId>testArtifact</artifactId>\n" + "<version>42</version>\n" + "<properties>\n" + "    <dto-generator-out-directory>${project.build.directory}/generated-sources/dto/</dto-generator-out-directory>\n" + "</properties>\n" + "<dependencies>\n" + "    <dependency>\n" + "        <groupId>junit</groupId>\n" + "        <artifactId>junit</artifactId>\n" + "        <version>4.12</version>\n" + "    </dependency>\n" + "</dependencies>\n" + "<build>\n" + "   <plugins>\n" + "       <plugin>\n" + "          <groupId>org.codehaus.mojo</groupId>\n" + "          <artifactId>build-helper-maven-plugin</artifactId>\n" + "              <executions>\n" + "                  <execution>\n" + "                      <id>add-source</id>\n" + "                      <phase>process-sources</phase>\n" + "                      <goals>\n" + "                          <goal>add-source</goal>\n" + "                      </goals>\n" + "                      <configuration>\n" + "                          <sources>\n" + "                              <source>${dto-generator-out-directory}</source>\n" + "                          </sources>\n" + "                      </configuration>\n" + "                  </execution>\n" + "                  <execution>\n" + "                      <id>add-test-source</id>\n" + "                      <phase>generate-sources</phase>\n" + "                      <goals>\n" + "                          <goal>add-test-source</goal>\n" + "                      </goals>\n" + "                      <configuration>\n" + "                          <sources>\n" + "                              <source>${dto-generator-out-directory}src-gen/test/java</source>\n" + "                          </sources>\n" + "                      </configuration>\n" + "                  </execution>" + "              </executions>\n" + "       </plugin>\n" + "   </plugins>\n" + "</build>";
    createTestProject("test", pom);
    IProject test = ResourcesPlugin.getWorkspace().getRoot().getProject("test");
    mavenWorkspace.update(Collections.singletonList(test));
    mavenWorkspace.waitForUpdate();
    IJavaProject javaProject = JavaCore.create(test);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    assertThat(rawClasspath).onProperty("path").contains(new Path("/test/target/generated-sources/dto/"));
    //attributes should be updated
    List<String> sourceFolders = projectRegistry.getProject("test").getAttributes().get(Constants.SOURCE_FOLDER);
    List<String> testSourceFolders = projectRegistry.getProject("test").getAttributes().get(TEST_SOURCE_FOLDER);
    assertEquals(2, sourceFolders.size());
    assertThat(sourceFolders, hasItems("src/main/java", "target/generated-sources/dto/"));
    assertEquals(2, testSourceFolders.size());
    assertThat(testSourceFolders, hasItems("src/test/java", "target/generated-sources/dto/src-gen/test/java"));
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IProject(org.eclipse.core.resources.IProject) Test(org.testng.annotations.Test) MavenServerManagerTest(org.eclipse.che.plugin.maven.server.rmi.MavenServerManagerTest)

Example 30 with IClasspathEntry

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

the class JREContainer method computeClasspathEntries.

/**
	 * Computes the classpath entries associated with a VM - one entry per library
	 * in the context of the given path and project.
	 * 
	 * @param vm the VM
	 * @param project the project the resolution is for
	 * @param environmentId execution environment the resolution is for, or <code>null</code>
	 * @return classpath entries
	 */
private static IClasspathEntry[] computeClasspathEntries(IVMInstallType vm, IJavaProject project, String environmentId) {
    //vm.getLibraryLocations();
    LibraryLocation[] libs = null;
    boolean overrideJavaDoc = false;
    if (libs == null) {
        libs = getLibraryLocations(vm);
        overrideJavaDoc = true;
    }
    IAccessRule[][] rules = null;
    //		if (environmentId != null) {
    // compute access rules for execution environment
    IExecutionEnvironment environment = JavaRuntime.getEnvironment(environmentId);
    if (environment != null) {
        rules = environment.getAccessRules(vm, libs, project);
    }
    //		}
    RuleKey key = null;
    if (vm != null && rules != null && environmentId != null) {
        key = new RuleKey(vm, environmentId);
        RuleEntry entry = fgClasspathEntriesWithRules.get(key);
        if (entry != null && entry.equals(rules)) {
            return entry.getClasspathEntries();
        }
    }
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(libs.length);
    for (int i = 0; i < libs.length; i++) {
        if (!libs[i].getSystemLibraryPath().isEmpty()) {
            IPath sourcePath = libs[i].getSystemLibrarySourcePath();
            if (sourcePath.isEmpty()) {
                sourcePath = null;
            }
            IPath rootPath = libs[i].getPackageRootPath();
            if (rootPath.isEmpty()) {
                rootPath = null;
            }
            // construct the classpath attributes for this library location
            IClasspathAttribute[] attributes = JREContainer.buildClasspathAttributes(vm, libs[i], overrideJavaDoc);
            IAccessRule[] libRules = null;
            if (rules != null) {
                libRules = rules[i];
            } else {
                libRules = EMPTY_RULES;
            }
            entries.add(JavaCore.newLibraryEntry(libs[i].getSystemLibraryPath(), sourcePath, rootPath, libRules, attributes, false));
        }
    }
    IClasspathEntry[] cpEntries = entries.toArray(new IClasspathEntry[entries.size()]);
    if (key != null && rules != null) {
        fgClasspathEntriesWithRules.put(key, new RuleEntry(rules, cpEntries));
    }
    return cpEntries;
}
Also used : IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IExecutionEnvironment(org.eclipse.che.jdt.core.launching.environments.IExecutionEnvironment) IAccessRule(org.eclipse.jdt.core.IAccessRule)

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