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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path of this classpath entry.

Usage

From source file:com.nginious.http.plugin.ServerManager.java

License:Apache License

private boolean updateProjectWithPluginVersion(IProject project) {
    logger.log("ENTER ServerManager.updateProjectWithPluginVersion project={0}", project);

    try {/*  w  ww.j a  v  a2s  .  c  o  m*/
        URL apiJar = NginiousPlugin.getApiJar();
        String filePath = apiJar.toString();
        filePath = filePath.substring(5);
        Path apiJarPath = new Path(filePath);

        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();
        boolean changed = false;

        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IPath path = entry.getPath();

                if (path.lastSegment() != null && path.lastSegment().endsWith("nginious-api.jar")) {
                    changed = true;
                    entry = JavaCore.newLibraryEntry(apiJarPath, null, null);
                }
            }

            newEntries.add(entry);
        }

        if (changed) {
            IProgressMonitor progress = new NullProgressMonitor();
            entries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            javaProject.setRawClasspath(entries, progress);
            javaProject.save(progress, true);
        }

        logger.log("EXIT ServerManager.updateProjectWithPluginVersion changed={0}", changed);
        return changed;
    } catch (JavaModelException e) {
        logger.log("ServerManager.updateProkectWithPluginVersion exception", e);
        return false;
    } catch (IOException e) {
        logger.log("ServerManager.updateProkectWithPluginVersion exception", e);
        return false;
    }
}

From source file:com.nomagic.magicdraw.classpath.MDClasspathContainerPage.java

License:Open Source License

public void setSelection(IClasspathEntry containerEntry) {
    if (containerEntry != null) {
        IPath path = containerEntry.getPath();
        String containerID = path.segment(0);
        if (MDContainer.ID.toString().equals(containerID)) {
            String mdPath = path.removeFirstSegments(1).toString();
            String[] mdPathParts = mdPath.split("[" + MDContainer.MD_LIB_SEPARATOR + "]");
            fMDInstallRootPath = MDVariableInitializer.getMDInstallRootPath();
            fMDClasspathEntries.clear();
            for (int i = 1; i < mdPathParts.length; i++) {
                IPath mdLib = new Path(mdPathParts[i]).removeTrailingSeparator();
                fMDClasspathEntries.add(mdLib);
            }/*from  w ww. j  a  va2  s.  c  o m*/
            Collections.sort(fMDClasspathEntries, IPATH_COMPARATOR);
        }
    }
}

From source file:com.redhat.ceylon.eclipse.code.preferences.CeylonBuildPathsBlock.java

License:Open Source License

private CPListElement findElement(IClasspathEntry entry) {
    CPListElement prefixMatch = null;/* w w  w  .j  ava2  s  .c  o  m*/
    int entryKind = entry.getEntryKind();
    for (int i = 0, len = fClassPathList.getSize(); i < len; i++) {
        CPListElement curr = fClassPathList.getElement(i);
        if (curr.getEntryKind() == entryKind) {
            IPath entryPath = entry.getPath();
            IPath currPath = curr.getPath();
            if (currPath.equals(entryPath)) {
                return curr;
            }
            // in case there's no full match, look for a similar container (same ID segment):
            if (prefixMatch == null && entryKind == IClasspathEntry.CPE_CONTAINER) {
                int n = entryPath.segmentCount();
                if (n > 0) {
                    IPath genericContainerPath = n == 1 ? entryPath : entryPath.removeLastSegments(n - 1);
                    if (n > 1 && genericContainerPath.isPrefixOf(currPath)) {
                        prefixMatch = curr;
                    }
                }
            }
        }
    }
    return prefixMatch;
}

From source file:com.redhat.ceylon.eclipse.code.wizard.AddResourceFolderWizardPage.java

License:Open Source License

private StatusInfo updateRootStatus() {
    IJavaProject javaProject = fNewElement.getJavaProject();
    IProject project = javaProject.getProject();

    StatusInfo pathNameStatus = validatePathName(fRootDialogField.getText(), fParent);

    if (!pathNameStatus.isOK())
        return pathNameStatus;

    if (fLinkedMode) {
        IStatus linkNameStatus = validateLinkLocation(fRootDialogField.getText());
        if (linkNameStatus.matches(IStatus.ERROR)) {
            StatusInfo result = new StatusInfo();
            result.setError(linkNameStatus.getMessage());
            return result;
        }//from   ww  w  .  j a  v  a  2  s .co m
    }

    StatusInfo result = new StatusInfo();
    result.setOK();

    IPath projPath = project.getFullPath();
    IPath path = fParent.getFullPath().append(fRootDialogField.getText());

    restoreCPElements();

    int projectEntryIndex = -1;
    boolean createFolderForExisting = false;

    IFolder folder = fParent.getFolder(new Path(fRootDialogField.getText()));
    for (int i = 0; i < fExistingEntries.size(); i++) {
        IClasspathEntry curr = fExistingEntries.get(i).getClasspathEntry();
        if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            if (path.equals(curr.getPath()) && fExistingEntries.get(i) != fNewElement) {
                if (folder.exists()) {
                    result.setError("The folder is already a source or resource folder.");
                    return result;
                } else {
                    createFolderForExisting = true;
                }
            }
            if (projPath.equals(curr.getPath())) {
                projectEntryIndex = i;
            }
        }
    }

    if (folder.exists() && !folder.getFullPath().equals(fOrginalPath))
        return new StatusInfo(IStatus.ERROR,
                Messages.format(NewWizardMessages.NewFolderDialog_folderNameEmpty_alreadyExists,
                        BasicElementLabels.getPathLabel(folder.getFullPath(), false)));

    boolean isProjectASourceFolder = projectEntryIndex != -1;

    fModifiedElements.clear();
    updateFilters(fNewElement.getPath(), path);

    fNewElement.setPath(path);
    if (fLinkedMode) {
        fNewElement.setLinkTarget(fLinkFields.getLinkTarget());
    }
    fRemovedElements.clear();
    Set<CPListElement> modified = new HashSet<CPListElement>();
    boolean isProjectSourceFolderReplaced = false;
    if (fAddExclusionPatterns.isSelected()) {
        if (fOrginalPath == null) {
            addExclusionPatterns(fNewElement, fExistingEntries, modified);
            fModifiedElements.addAll(modified);
            if (!createFolderForExisting)
                CPListElement.insert(fNewElement, fExistingEntries);
        }
    } else {
        if (isProjectASourceFolder) {
            if (fRemoveProjectFolder.isSelected()) {
                fOldProjectSourceFolder = fExistingEntries.get(projectEntryIndex);
                fRemovedElements.add(fOldProjectSourceFolder);
                fExistingEntries.set(projectEntryIndex, fNewElement);
                isProjectSourceFolderReplaced = true;
            } else {
                if (!createFolderForExisting)
                    CPListElement.insert(fNewElement, fExistingEntries);
            }
        } else {
            if (!createFolderForExisting)
                CPListElement.insert(fNewElement, fExistingEntries);
        }
    }

    if ((!fAllowConflict && fCanCommitConflictingBuildpath) || createFolderForExisting)
        return new StatusInfo();

    fNewOutputLocation = null;
    IJavaModelStatus status = JavaConventions.validateClasspath(javaProject,
            CPListElement.convertToClasspathEntries(fExistingEntries), fOutputLocation);
    if (!status.isOK()) {
        if (fOutputLocation.equals(projPath)) {
            //Try to change the output folder
            fNewOutputLocation = projPath.append(
                    PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
            IStatus status2 = JavaConventions.validateClasspath(javaProject,
                    CPListElement.convertToClasspathEntries(fExistingEntries), fNewOutputLocation);
            if (status2.isOK()) {
                if (isProjectSourceFolderReplaced) {
                    result.setInfo(
                            Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceSFandOL,
                                    BasicElementLabels.getPathLabel(fNewOutputLocation, false)));
                } else {
                    result.setInfo(
                            Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceOL,
                                    BasicElementLabels.getPathLabel(fNewOutputLocation, false)));
                }
                return result;
            }
        }
        //Don't know what the problem is, report to user
        fNewOutputLocation = null;
        if (fCanCommitConflictingBuildpath) {
            result.setInfo(NewWizardMessages.AddSourceFolderWizardPage_conflictWarning + status.getMessage());
        } else {
            result.setError(status.getMessage());
        }
        return result;
    }
    if (!modified.isEmpty()) {
        //Added exclusion patterns to solve problem
        if (modified.size() == 1) {
            CPListElement elem = (CPListElement) modified.toArray()[0];
            String changed = BasicElementLabels.getPathLabel(elem.getPath(), false);
            String excl = BasicElementLabels.getPathLabel(fNewElement.getPath(), false);
            result.setInfo(Messages.format(NewWizardMessages.AddSourceFolderWizardPage_addSinglePattern,
                    new Object[] { excl, changed }));
        } else {
            result.setInfo(
                    Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_AddedExclusions_plural,
                            String.valueOf(modified.size())));
        }
        return result;
    }
    if (isProjectSourceFolderReplaced) {
        result.setInfo(NewWizardMessages.AddSourceFolderWizardPage_replaceSourceFolderInfo);
        return result;
    }

    return result;
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathContainer.java

License:Apache License

private IClasspathEntry[] constructModifiedClasspath(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry newEntry = JavaCore.newContainerEntry(path, null, new IClasspathAttribute[0], false);
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(Arrays.asList(entries));
    int index = 0;
    boolean mustReplace = false;
    for (IClasspathEntry entry : newEntries) {
        if (entry.getPath().equals(newEntry.getPath())) {
            mustReplace = true;//from   w  w  w. jav  a  2  s  . co m
            break;
        }
        index++;
    }
    if (mustReplace) {
        newEntries.set(index, newEntry);
    } else {
        newEntries.add(newEntry);
    }
    return (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathUtil.java

License:Apache License

/**
 * Work around the non adaptability of ClassPathContainer
 * /*from   w w w.j ava 2s.  c o  m*/
 * @param cpc
 *            the container to transform into an CeylonClasspathContainer
 * @return the CeylonClasspathContainer is such, null, if not
 */
public static CeylonClasspathContainer jdt2CeylonCPC(ClassPathContainer cpc) {
    IClasspathEntry entry = cpc.getClasspathEntry();
    try {
        IClasspathContainer icp = JavaCore.getClasspathContainer(entry.getPath(), cpc.getJavaProject());
        if (icp instanceof CeylonClasspathContainer) {
            return (CeylonClasspathContainer) icp;
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        e.printStackTrace();
    }
    return null;
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathUtil.java

License:Apache License

/**
 * Search the Ceylon classpath containers within the specified Java project
 * //  w  ww  . j av  a 2  s . co m
 * @param javaProject
 *            the project to search into
 * @return the Ivy classpath container if found
 */
public static List<CeylonClasspathContainer> getCeylonClasspathContainers(IJavaProject javaProject) {
    List<CeylonClasspathContainer> containers = new ArrayList<CeylonClasspathContainer>();
    if (FakeProjectManager.isFake(javaProject) || !javaProject.exists()) {
        return containers;
    }
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath path = entry.getPath();
                if (isCeylonClasspathContainer(path)) {
                    IClasspathContainer cp = JavaCore.getClasspathContainer(path, javaProject);
                    if (cp instanceof CeylonClasspathContainer) {
                        containers.add((CeylonClasspathContainer) cp);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        e.printStackTrace();
    }
    return containers;
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathUtil.java

License:Apache License

/**
 * Search the Ivy classpath entry within the specified Java project with the specific path
 * /* w  w  w .  j a v  a2 s . c o  m*/
 * @param containerPath
 *            the path of the container
 * @param javaProject
 *            the project to search into
 * @return the Ivy classpath container if found, otherwise return <code>null</code>
 */
public static IClasspathEntry getCeylonClasspathEntry(IPath containerPath, IJavaProject javaProject) {
    if (FakeProjectManager.isFake(javaProject) || !javaProject.exists()) {
        return null;
    }
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (containerPath.equals(entry.getPath())) {
                    return entry;
                }
            }
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        e.printStackTrace();
    }
    return null;
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonProjectModulesContainer.java

License:Apache License

public IClasspathEntry addNewClasspathEntryIfNecessary(IPath modulePath) {
    synchronized (classpathEntries) {
        for (IClasspathEntry cpEntry : classpathEntries) {
            if (cpEntry.getPath().equals(modulePath)) {
                return null;
            }/*ww w. ja  v a  2  s. c  o m*/
        }
        IClasspathEntry newEntry = newLibraryEntry(modulePath, null, null);
        IClasspathEntry[] newClasspathEntries = new IClasspathEntry[classpathEntries.length + 1];
        if (classpathEntries.length > 0) {
            System.arraycopy(classpathEntries, 0, newClasspathEntries, 0, classpathEntries.length);
        }
        newClasspathEntries[classpathEntries.length] = newEntry;
        classpathEntries = newClasspathEntries;
        return newEntry;
    }
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonProjectModulesContainer.java

License:Apache License

private IClasspathEntry[] constructModifiedClasspath(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry newEntry = JavaCore.newContainerEntry(path, null, new IClasspathAttribute[0], false);
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(asList(entries));
    int index = 0;
    boolean mustReplace = false;
    boolean projectModulesEntryWasExported = false;
    for (IClasspathEntry entry : newEntries) {
        if (entry.getPath().equals(newEntry.getPath())) {
            mustReplace = true;/* w  w w.  ja v a2s  . co m*/
            projectModulesEntryWasExported = entry.isExported();
            break;
        }
        index++;
    }

    newEntry = JavaCore.newContainerEntry(path, null, new IClasspathAttribute[0],
            projectModulesEntryWasExported);
    if (mustReplace) {
        newEntries.set(index, newEntry);
    } else {
        newEntries.add(newEntry);
    }
    return (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
}