Example usage for org.eclipse.jdt.core IJavaProject readRawClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject readRawClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject readRawClasspath.

Prototype

IClasspathEntry[] readRawClasspath();

Source Link

Document

Returns the raw classpath for the project as defined by its .classpath file from disk, or null if unable to read the file.

Usage

From source file:org.eclipse.birt.report.debug.internal.ui.launcher.util.WorkspaceClassPathFinder.java

License:Open Source License

private List getProjectPath(IProject project) {
    List retValue = new ArrayList();
    if (!hasJavaNature(project)) {
        return retValue;
    }//  www .j a  v  a 2s .  c o m

    IJavaProject fCurrJProject = JavaCore.create(project);
    IClasspathEntry[] classpathEntries = null;
    boolean projectExists = (project.exists() && project.getFile(".classpath").exists()); //$NON-NLS-1$
    if (projectExists) {
        if (classpathEntries == null) {
            classpathEntries = fCurrJProject.readRawClasspath();
        }
    }

    if (classpathEntries != null) {
        retValue = getExistingEntries(classpathEntries, project);
    }
    return retValue;
}

From source file:org.eclipse.birt.report.designer.internal.ui.ide.adapters.IDEReportClasspathResolver.java

License:Open Source License

private List<String> getProjectDependentClasspath(IProject project, boolean needExported) {
    if (!hasJavaNature(project)) {
        return Collections.emptyList();
    }//from ww w . jav  a  2  s .com

    List<String> retValue = new ArrayList<String>();

    IJavaProject fCurrJProject = JavaCore.create(project);
    IClasspathEntry[] classpathEntries = null;

    boolean projectExists = (project.exists() && project.getFile(".classpath").exists()); //$NON-NLS-1$

    if (projectExists) {
        if (classpathEntries == null) {
            classpathEntries = fCurrJProject.readRawClasspath();
        }
    }

    if (classpathEntries != null) {
        retValue = resolveClasspathEntries(classpathEntries, needExported, fCurrJProject);
    }

    return retValue;
}

From source file:org.eclipse.buckminster.jdt.internal.BMClasspathInitializer.java

License:Open Source License

@Override
public void resourceChanged(IResourceChangeEvent event) {
    IPath path = BMClasspathContainer.PATH;
    try {// ww  w.  j  a  v a2s .c  om
        IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        for (IJavaProject javaProject : model.getJavaProjects()) {
            for (IClasspathEntry rawEntry : javaProject.readRawClasspath()) {
                if (rawEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    IPath entryPath = rawEntry.getPath();
                    if (path.isPrefixOf(entryPath)) {
                        this.initialize(entryPath, javaProject);
                        return;
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

From source file:org.eclipse.buckminster.jdt.internal.ClasspathEmitter.java

License:Open Source License

/**
 * This method obtains the raw classpath from the javaProject and scans it
 * for BMClasspathContainer. The first one found is either kept if it
 * corresponds to the target or replaced if not. All other
 * BMClasspathContainers are removed. If no BMClasspathContainer was found,
 * a new one that represents the target is added first in the list. All
 * IClasspathEntry instances are then resolved.
 * /* www .  ja v a  2 s  .  c  o  m*/
 * @param javaProject
 * @param target
 * @return
 * @throws JavaModelException
 */
private static IClasspathEntry[] changeClasspathForTarget(IJavaProject javaProject, String target)
        throws CoreException {
    boolean entriesChanged = false;
    boolean haveOtherBMCPs = false;
    boolean targetContainerInstalled = false;

    Logger log = Buckminster.getLogger();
    log.debug("Changing classpath for project %s into %s", javaProject.getProject().getName(), target); //$NON-NLS-1$
    IPath desiredContainer = BMClasspathContainer.PATH.append(target);
    IClasspathEntry[] rawEntries = javaProject.readRawClasspath();
    int top = rawEntries.length;
    for (int idx = 0; idx < top; ++idx) {
        IClasspathEntry rawEntry = rawEntries[idx];
        if (rawEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            IPath entryPath = rawEntry.getPath();
            if (BMClasspathContainer.PATH.isPrefixOf(entryPath)) {
                if (!targetContainerInstalled) {
                    if (!desiredContainer.equals(entryPath)) {
                        // This is not the desired container. Replace it.
                        //
                        rawEntries[idx] = JavaCore.newContainerEntry(desiredContainer);
                        entriesChanged = true;
                    }
                    targetContainerInstalled = true;
                } else
                    haveOtherBMCPs = true;
            }
        }
    }

    if (targetContainerInstalled) {
        if (haveOtherBMCPs) {
            // Remove other Buckminster containers
            //
            ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(top);
            for (int idx = 0; idx < top; ++idx) {
                IClasspathEntry rawEntry = rawEntries[idx];
                if (rawEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER
                        || rawEntry.getPath().equals(desiredContainer))
                    newEntries.add(rawEntry);
            }
            rawEntries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            entriesChanged = true;
        }
    } else {
        rawEntries = ArrayUtils.appendFirst(rawEntries,
                new IClasspathEntry[] { JavaCore.newContainerEntry(desiredContainer) });
        entriesChanged = true;
    }

    log.debug(entriesChanged ? " changes detected" : " no changes detected"); //$NON-NLS-1$ //$NON-NLS-2$

    return entriesChanged ? getResolvedClasspath(javaProject, rawEntries)
            : javaProject.getResolvedClasspath(false);
}

From source file:org.eclipse.edt.ide.core.utils.EclipseUtilities.java

License:Open Source License

/**
 * Adds the outputFolder as a Java source folder if the project is a Java project.
 * /*from  ww  w .  j a v  a2  s. com*/
 * @param project       The project containing the folder (used when outputFolder is a relative path)
 * @param outputFolder  The path of the folder. It may be project-relative, or workspace-relative. If workspace-relative
 *                      it should start with 'F/' for a folder or 'P/' for a project.
 * @param forceClasspathRefresh A classpath needs to be refreshed if an entry already exists for the output folder, but the folder has yet to be
 *                         created.  This can occur when a project is exported without a generation directory.
 * @throws CoreException
 */
public static void addToJavaBuildPathIfNecessary(IProject project, String outputFolder,
        boolean forceClasspathRefresh) throws CoreException {
    if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject.exists()) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            IPath outputFolderPath = new Path(convertFromInternalPath(outputFolder));
            boolean needToAdd = true;

            IPath fullPath = outputFolderPath.isAbsolute() ? outputFolderPath
                    : outputFolderPath.segmentCount() == 0 ? project.getFullPath()
                            : project.getFolder(outputFolderPath).getFullPath();

            for (int i = 0; i < entries.length; i++) {
                if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath nextPath = entries[i].getPath();
                    // JDT throws an error if you have a source folder within a source folder. We could add exclusions to support this, but
                    // for now we just won't add the folder.
                    if (nextPath.isPrefixOf(fullPath) || fullPath.isPrefixOf(nextPath)) {
                        needToAdd = false;
                        break;
                    }
                }
            }

            if (needToAdd) {
                IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
                System.arraycopy(entries, 0, newEntries, 0, entries.length);
                newEntries[newEntries.length - 1] = JavaCore.newSourceEntry(fullPath);
                javaProject.setRawClasspath(newEntries, null);
            }

            if (!needToAdd && forceClasspathRefresh) {
                javaProject.setRawClasspath(javaProject.readRawClasspath(), javaProject.readOutputLocation(),
                        null);
            }
        }
    }
}

From source file:org.eclipse.jem.workbench.utility.JemProjectUtilities.java

License:Open Source License

/**
 * Is this project a binary project./*from ww w  . ja  va  2  s.  c o  m*/
 * <p>
 * Typically a Java project is considered binary if it does not have a source entry in the classpath.
 * 
 * @param project
 *            Project to test
 * @return <code>true</code> if project is a binary project.
 */
public static boolean isBinaryProject(IProject aProject) {

    IJavaProject javaProj = getJavaProject(aProject);
    if (javaProj == null)
        return false;
    IClasspathEntry[] entries = null;
    entries = javaProj.readRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE)
            return false;
    }
    return true;
}

From source file:org.eclipse.jem.workbench.utility.JemProjectUtilities.java

License:Open Source License

/**
 * Hack to force a reload of the .classpath file
 * /*from  ww  w  .  j  a  va  2 s.  co m*/
 * @param project
 *            project to reload
 * @since 1.0.0
 */
public static void forceClasspathReload(IProject project) throws JavaModelException {
    IJavaProject javaProj = getJavaProject(project);
    if (javaProj != null) {
        IClasspathEntry[] entries = javaProj.readRawClasspath();
        if (entries != null) {
            IPath output = javaProj.readOutputLocation();
            if (output != null)
                javaProj.setRawClasspath(entries, output, null);
        }
    }
}

From source file:org.eclipse.jem.workbench.utility.JemProjectUtilities.java

License:Open Source License

/**
 * Get the paths of all of the local jars in the classpath for the project. It does not recurse into referenced projects.
 * //w w  w  .  ja  v  a  2 s  .  c om
 * @param proj
 *            project to search (should be a java project).
 * @return A list of IPath, where each entry is a project relative path to a JAR contained in the project.
 */
public static List getLocalJARPathsFromClasspath(IProject proj) {
    IJavaProject javaProj = getJavaProject(proj);
    if (javaProj == null)
        return null;
    IPath projectPath = proj.getFullPath();
    List result = new ArrayList();
    IClasspathEntry[] entries = javaProj.readRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IPath path = entry.getPath();
            int segments = path.matchingFirstSegments(projectPath);
            if (segments > 0)
                result.add(path.removeFirstSegments(segments));
        }
    }
    return result;
}

From source file:org.eclipse.jst.j2ee.application.internal.operations.BinaryProjectHelper.java

License:Open Source License

/**
 * //  ww w  . j av  a 2  s . c om
 */
public static void removeImportedClassesFromClasspathIfNecessary(IProject project) {
    IJavaProject javaProj = JavaCore.create(project);
    if (javaProj != null) {
        IClasspathEntry[] entries = javaProj.readRawClasspath();
        if (entries != null) {
            IClasspathEntry entryToRemove = null;
            for (int i = 0; i < entries.length; i++) {
                if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY
                        && entries[i].getPath().toString().endsWith("imported_classes") //$NON-NLS-1$
                        && !project.getFolder("imported_classes").exists()) { //$NON-NLS-1$
                    entryToRemove = entries[i];
                    break;
                }
            }
            if (null != entryToRemove) {
                IClasspathEntry[] newEntries = new IClasspathEntry[entries.length - 1];
                for (int i = 0, j = 0; i < newEntries.length && j < entries.length; j++) {
                    if (entryToRemove != entries[j]) {
                        newEntries[i] = entries[j];
                        i++;
                    }
                }
                entries = newEntries;
                IPath output = javaProj.readOutputLocation();
                if (output != null)
                    try {
                        javaProj.setRawClasspath(entries, output, null);
                    } catch (JavaModelException e) {
                    }
            }

        }
    }
}

From source file:org.eclipse.jst.jsf.common.ui.internal.utils.PathUtil.java

License:Open Source License

/**
 * @param javaProject/* www .j a  va  2  s .  c o m*/
 * @param parent
 * @return the IPath for a a classpath object (?)
 */
public static IPath getPathOnClasspath(IJavaProject javaProject, Object parent) {
    IPath result = null;
    if (javaProject == null || parent == null) {
        return new Path(""); //$NON-NLS-1$
    }
    IClasspathEntry[] entries = javaProject.readRawClasspath();
    IPath classPath = null;
    if (parent instanceof IResource) {
        if (((javaProject != null) && !javaProject.isOnClasspath((IResource) parent))) {
            return new Path(""); //$NON-NLS-1$
        }
        if (parent instanceof IFile) {
            IPath elementPath = ((IFile) parent).getFullPath();
            if (((IFile) parent).getFileExtension().equalsIgnoreCase(IFileFolderConstants.EXT_PROPERTIES)) {
                int machings = 0;
                try {
                    for (int i = 0; i < entries.length; i++) {
                        // Determine whether on this classentry's path
                        int n = entries[i].getPath().matchingFirstSegments(elementPath);
                        if (n > machings) {
                            // Get package name
                            machings = n;
                            classPath = elementPath.removeFirstSegments(machings).removeLastSegments(1);
                        }
                    }

                    // Not on the classpath?
                    if (classPath == null) {
                        return null;
                    } else if (classPath.segmentCount() > 0) {
                        IJavaElement element = javaProject.findElement(classPath);
                        if (element != null) {
                            IPath path = element.getPath();
                            if (path != null) {
                                IPath path1 = path.removeFirstSegments(machings);

                                String fileName = ((IFile) parent).getName();
                                if (fileName != null) {
                                    result = path1.append(fileName);
                                }
                            }
                        }

                    } else {
                        result = ((IFile) parent).getFullPath().removeFirstSegments(machings);
                    }
                } catch (Exception e) {
                    return null;
                }
            }
        }
    } else if (parent instanceof IJarEntryResource) {
        IPath elementPath = ((IJarEntryResource) parent).getFullPath();
        if (elementPath.getFileExtension().equalsIgnoreCase(IFileFolderConstants.EXT_PROPERTIES)) {
            result = elementPath;
        }
    }
    if (result != null) {
        return result;
    }
    return new Path(""); //$NON-NLS-1$
}