Search in sources :

Example 81 with IClasspathEntry

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

the class WorkspaceTest method testProjectHasBuildWithoutSources.

@Test
public void testProjectHasBuildWithoutSources() 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/main/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)

Example 82 with IClasspathEntry

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

the class JavaProject method resolveClasspath.

public ResolvedClasspath resolveClasspath(IClasspathEntry[] rawClasspath, IClasspathEntry[] referencedEntries, boolean usePreviousSession, boolean resolveChainedLibraries) throws JavaModelException {
    JavaModelManager manager = getJavaModelManager();
    //        ExternalFoldersManager externalFoldersManager = JavaModelManager.getExternalManager();
    ResolvedClasspath result = new ResolvedClasspath();
    Map knownDrives = new HashMap();
    Map referencedEntriesMap = new HashMap();
    List rawLibrariesPath = new ArrayList();
    LinkedHashSet resolvedEntries = new LinkedHashSet();
    if (resolveChainedLibraries) {
        for (int index = 0; index < rawClasspath.length; index++) {
            IClasspathEntry currentEntry = rawClasspath[index];
            if (currentEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                rawLibrariesPath.add(ClasspathEntry.resolveDotDot(getProject().getLocation(), currentEntry.getPath()));
            }
        }
        if (referencedEntries != null) {
            // The Set is required to keep the order intact while the referencedEntriesMap (Map)
            // is used to map the referenced entries with path
            LinkedHashSet referencedEntriesSet = new LinkedHashSet();
            for (int index = 0; index < referencedEntries.length; index++) {
                IPath path = referencedEntries[index].getPath();
                if (!rawLibrariesPath.contains(path) && referencedEntriesMap.get(path) == null) {
                    referencedEntriesMap.put(path, referencedEntries[index]);
                    referencedEntriesSet.add(referencedEntries[index]);
                }
            }
            if (referencedEntriesSet.size() > 0) {
                result.referencedEntries = new IClasspathEntry[referencedEntriesSet.size()];
                referencedEntriesSet.toArray(result.referencedEntries);
            }
        }
    }
    int length = rawClasspath.length;
    for (int i = 0; i < length; i++) {
        IClasspathEntry rawEntry = rawClasspath[i];
        IClasspathEntry resolvedEntry = rawEntry;
        switch(rawEntry.getEntryKind()) {
            case IClasspathEntry.CPE_VARIABLE:
                try {
                    resolvedEntry = manager.resolveVariableEntry(rawEntry, usePreviousSession);
                } catch (ClasspathEntry.AssertionFailedException e) {
                    // Catch the assertion failure and set status instead
                    // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=55992
                    result.unresolvedEntryStatus = new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, e.getMessage());
                    break;
                }
                if (resolvedEntry == null) {
                    result.unresolvedEntryStatus = new JavaModelStatus(IJavaModelStatusConstants.CP_VARIABLE_PATH_UNBOUND, this, rawEntry.getPath());
                } else {
                    // have already been processed. So, skip it.
                    if (resolveChainedLibraries && resolvedEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && result.rawReverseMap.get(resolvedEntry.getPath()) == null) {
                        // resolve Class-Path: in manifest
                        ClasspathEntry[] extraEntries = ((ClasspathEntry) resolvedEntry).resolvedChainedLibraries();
                        for (int j = 0, length2 = extraEntries.length; j < length2; j++) {
                            if (!rawLibrariesPath.contains(extraEntries[j].getPath())) {
                                // https://bugs.eclipse.org/bugs/show_bug.cgi?id=305037
                                // referenced entries for variable entries could also be persisted with extra attributes, so addAsChainedEntry = true
                                addToResult(rawEntry, extraEntries[j], result, resolvedEntries, /*externalFoldersManager*/
                                referencedEntriesMap, true, knownDrives);
                            }
                        }
                    }
                    addToResult(rawEntry, resolvedEntry, result, resolvedEntries, /*externalFoldersManager,*/
                    referencedEntriesMap, false, knownDrives);
                }
                break;
            case IClasspathEntry.CPE_CONTAINER:
                IClasspathContainer container = usePreviousSession ? manager.getPreviousSessionContainer(rawEntry.getPath(), this) : JavaCore.getClasspathContainer(rawEntry.getPath(), this);
                if (container == null) {
                    result.unresolvedEntryStatus = new JavaModelStatus(IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND, this, rawEntry.getPath());
                    break;
                }
                IClasspathEntry[] containerEntries = container.getClasspathEntries();
                if (containerEntries == null) {
                    if (JavaModelManager.CP_RESOLVE_VERBOSE || JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
                    //                            JavaModelManager.getJavaModelManager().verbose_missbehaving_container_null_entries(this, rawEntry.getPath());
                    }
                    break;
                }
                // container was bound
                for (int j = 0, containerLength = containerEntries.length; j < containerLength; j++) {
                    ClasspathEntry cEntry = (ClasspathEntry) containerEntries[j];
                    if (cEntry == null) {
                        if (JavaModelManager.CP_RESOLVE_VERBOSE || JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
                        //                                JavaModelManager.getJavaModelManager().verbose_missbehaving_container(this, rawEntry.getPath(), containerEntries);
                        }
                        break;
                    }
                    // if container is exported or restricted, then its nested entries must in turn be exported  (21749) and/or propagate restrictions
                    cEntry = cEntry.combineWith((ClasspathEntry) rawEntry);
                    if (cEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        // resolve ".." in library path
                        cEntry = cEntry.resolvedDotDot(getProject().getLocation());
                        // Do not resolve if the system attribute is set to false
                        if (resolveChainedLibraries && getJavaModelManager().resolveReferencedLibrariesForContainers && result.rawReverseMap.get(cEntry.getPath()) == null) {
                            // resolve Class-Path: in manifest
                            ClasspathEntry[] extraEntries = cEntry.resolvedChainedLibraries();
                            for (int k = 0, length2 = extraEntries.length; k < length2; k++) {
                                if (!rawLibrariesPath.contains(extraEntries[k].getPath())) {
                                    addToResult(rawEntry, extraEntries[k], result, resolvedEntries, /*externalFoldersManager,*/
                                    referencedEntriesMap, false, knownDrives);
                                }
                            }
                        }
                    }
                    addToResult(rawEntry, cEntry, result, resolvedEntries, /*externalFoldersManager,*/
                    referencedEntriesMap, false, knownDrives);
                }
                break;
            case IClasspathEntry.CPE_LIBRARY:
                // resolve ".." in library path
                resolvedEntry = ((ClasspathEntry) rawEntry).resolvedDotDot(getProject().getLocation());
                if (resolveChainedLibraries && result.rawReverseMap.get(resolvedEntry.getPath()) == null) {
                    // resolve Class-Path: in manifest
                    ClasspathEntry[] extraEntries = ((ClasspathEntry) resolvedEntry).resolvedChainedLibraries();
                    for (int k = 0, length2 = extraEntries.length; k < length2; k++) {
                        if (!rawLibrariesPath.contains(extraEntries[k].getPath())) {
                            addToResult(rawEntry, extraEntries[k], result, resolvedEntries, /*externalFoldersManager,*/
                            referencedEntriesMap, true, knownDrives);
                        }
                    }
                }
                addToResult(rawEntry, resolvedEntry, result, resolvedEntries, /*externalFoldersManager,*/
                referencedEntriesMap, false, knownDrives);
                break;
            default:
                addToResult(rawEntry, resolvedEntry, result, resolvedEntries, /* externalFoldersManager,*/
                referencedEntriesMap, false, knownDrives);
                break;
        }
    }
    result.resolvedClasspath = new IClasspathEntry[resolvedEntries.size()];
    resolvedEntries.toArray(result.resolvedClasspath);
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IJavaModelStatus(org.eclipse.jdt.core.IJavaModelStatus) JavaModelManager.getJavaModelManager(org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) Map(java.util.Map) HashMap(java.util.HashMap)

Example 83 with IClasspathEntry

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

the class JavaProject method getClasspathEntryFor.

/**
     * Returns the classpath entry that refers to the given path
     * or <code>null</code> if there is no reference to the path.
     *
     * @param path
     *         IPath
     * @return IClasspathEntry
     * @throws JavaModelException
     */
public IClasspathEntry getClasspathEntryFor(IPath path) throws JavaModelException {
    // force resolution
    getResolvedClasspath();
    PerProjectInfo perProjectInfo = getPerProjectInfo();
    if (perProjectInfo == null)
        return null;
    Map rootPathToResolvedEntries = perProjectInfo.rootPathToResolvedEntries;
    if (rootPathToResolvedEntries == null)
        return null;
    IClasspathEntry classpathEntry = (IClasspathEntry) rootPathToResolvedEntries.get(path);
    if (classpathEntry == null) {
        path = getProject().getWorkspace().getRoot().getLocation().append(path);
        classpathEntry = (IClasspathEntry) rootPathToResolvedEntries.get(path);
    }
    return classpathEntry;
}
Also used : PerProjectInfo(org.eclipse.jdt.internal.core.JavaModelManager.PerProjectInfo) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Map(java.util.Map) HashMap(java.util.HashMap)

Example 84 with IClasspathEntry

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

the class JavaProject method addToResult.

private void addToResult(IClasspathEntry rawEntry, IClasspathEntry resolvedEntry, ResolvedClasspath result, LinkedHashSet resolvedEntries, Map oldChainedEntriesMap, boolean addAsChainedEntry, Map knownDrives) {
    IPath resolvedPath;
    // If it's already been resolved, do not add to resolvedEntries
    if (result.rawReverseMap.get(resolvedPath = resolvedEntry.getPath()) == null) {
        result.rawReverseMap.put(resolvedPath, rawEntry);
        result.rootPathToResolvedEntries.put(resolvedPath, resolvedEntry);
        resolvedEntries.add(resolvedEntry);
        if (addAsChainedEntry) {
            IClasspathEntry chainedEntry = null;
            chainedEntry = (ClasspathEntry) oldChainedEntriesMap.get(resolvedPath);
            if (chainedEntry != null) {
                // This is required to keep the attributes if any added by the user in
                // the previous session such as source attachment path etc.
                copyFromOldChainedEntry((ClasspathEntry) resolvedEntry, (ClasspathEntry) chainedEntry);
            }
        }
    }
//        if (resolvedEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY &&/* ExternalFoldersManager.isExternalFolderPath(resolvedPath)*/ false) {
//            externalFoldersManager.addFolder(resolvedPath, true/*scheduleForCreation*/); // no-op if not an external folder or if already registered
//        }
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=336046
//        // The source attachment path could be external too and in which case, must be added.
//        IPath sourcePath = resolvedEntry.getSourceAttachmentPath();
//        if (sourcePath != null && driveExists(sourcePath, knownDrives) && ExternalFoldersManager.isExternalFolderPath(sourcePath)) {
//            externalFoldersManager.addFolder(sourcePath, true);
//        }
}
Also used : IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 85 with IClasspathEntry

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

the class JavaProject method getResolvedClasspath.

/**
     * This is a helper method returning the resolved classpath for the project
     * as a list of simple (non-variable, non-container) classpath entries.
     * All classpath variable and classpath container entries in the project's
     * raw classpath will be replaced by the simple classpath entries they
     * resolve to.
     * <p>
     * The resulting resolved classpath is accurate for the given point in time.
     * If the project's raw classpath is later modified, or if classpath
     * variables are changed, the resolved classpath can become out of date.
     * Because of this, hanging on resolved classpath is not recommended.
     * </p>
     * <p>
     * Note that if the resolution creates duplicate entries
     * (i.e. {@link IClasspathEntry entries} which are {@link Object#equals(Object)}),
     * only the first one is added to the resolved classpath.
     * </p>
     *
     * @see IClasspathEntry
     */
public IClasspathEntry[] getResolvedClasspath() throws JavaModelException {
    PerProjectInfo perProjectInfo = getPerProjectInfo();
    IClasspathEntry[] resolvedClasspath = perProjectInfo.getResolvedClasspath();
    if (resolvedClasspath == null) {
        resolveClasspath(perProjectInfo, false, /*don't use previous session values*/
        true);
        resolvedClasspath = perProjectInfo.getResolvedClasspath();
        if (resolvedClasspath == null) {
            // another thread reset the resolved classpath, use a temporary PerProjectInfo
            PerProjectInfo temporaryInfo = newTemporaryInfo();
            resolveClasspath(temporaryInfo, false, /*don't use previous session values*/
            true);
            resolvedClasspath = temporaryInfo.getResolvedClasspath();
        }
    }
    return resolvedClasspath;
}
Also used : PerProjectInfo(org.eclipse.jdt.internal.core.JavaModelManager.PerProjectInfo) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

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