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

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

Introduction

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

Prototype

IClasspathEntry[] getRawClasspath() throws JavaModelException;

Source Link

Document

Returns the raw classpath for the project, as a list of classpath entries.

Usage

From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java

License:Open Source License

private void addClasspathContainer(IJavaProject project, IPath containerPath) throws JavaModelException {
    IClasspathEntry[] entries = project.getRawClasspath();

    IClasspathEntry entry = JavaCore.newContainerEntry(containerPath);
    entries = CodegenUtils.addEntryToClasspath(entries, entry);

    project.setRawClasspath(entries, new NullProgressMonitor());
}

From source file:com.google.gdt.eclipse.appengine.rpc.wizards.helpers.RpcServiceLayerCreator.java

License:Open Source License

private String getGwtContainerPath(IJavaProject javaProject) throws CoreException {
    IClasspathEntry[] entries = null;//www.j  a v  a  2 s.  c  o m

    entries = javaProject.getRawClasspath();

    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                && entry.getPath().toString().equals("com.google.gwt.eclipse.core.GWT_CONTAINER")) { //$NON-NLS-N$
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject);
            if (container instanceof GWTRuntimeContainer) {
                IPath path = ((GWTRuntimeContainer) container).getSdk().getInstallationPath();
                return path.toString();
            }
        }
    }
    // gwt not on classpath, add to it, set nature
    GWTRuntime gwt = GWTPreferences.getDefaultRuntime();
    IPath containerPath = SdkClasspathContainer.computeContainerPath(GWTRuntimeContainer.CONTAINER_ID, gwt,
            SdkClasspathContainer.Type.DEFAULT);
    if (GaeNature.isGaeProject(javaProject.getProject())) {
        addClasspathContainer(javaProject, containerPath);
        GWTNature.addNatureToProject(javaProject.getProject());
    }
    return gwt.getInstallationPath().toString();
}

From source file:com.google.gdt.eclipse.core.ClasspathUtilities.java

License:Open Source License

/**
 * Finds all the classpath containers in the specified project that match the
 * provided container ID.//from   w w w . j a  va 2  s .  c  o  m
 * 
 * @param javaProject the project to query
 * @param containerId The container ID we are trying to match.
 * @return an array of matching classpath containers.
 */
public static IClasspathEntry[] findClasspathContainersWithContainerId(IJavaProject javaProject,
        final String containerId) throws JavaModelException {

    Predicate<IClasspathEntry> matchPredicate = new Predicate<IClasspathEntry>() {
        public boolean apply(IClasspathEntry entry) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath containerPath = entry.getPath();
                if (containerPath.segmentCount() > 0 && containerPath.segment(0).equals(containerId)) {
                    return true;
                }
            }
            return false;
        }
    };

    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    int matchCount = 0;

    for (int i = 0; i < classpathEntries.length; i++) {
        if (matchPredicate.apply(classpathEntries[i])) {
            matchCount++;
        }
    }

    IClasspathEntry[] matchingClasspathEntries = new IClasspathEntry[matchCount];
    int matchingClasspathEntriesIdx = 0;
    for (int i = 0; i < classpathEntries.length; i++) {
        if (matchPredicate.apply(classpathEntries[i])) {
            matchingClasspathEntries[matchingClasspathEntriesIdx] = classpathEntries[i];
            matchingClasspathEntriesIdx++;
        }
    }

    return matchingClasspathEntries;
}

From source file:com.google.gdt.eclipse.core.ClasspathUtilities.java

License:Open Source License

/**
 * Return the raw classpath entry on the project's classpath that contributes
 * the given type to the project.//from  w  ww  . ja va 2 s .  c o  m
 * 
 * @param javaProject The java project
 * @param fullyQualifiedName The fully-qualified type name
 * @return The raw classpath entry that contributes the type to the project,
 *         or <code>null</code> if no such classpath entry can be found.
 * @throws JavaModelException
 */
public static IClasspathEntry findRawClasspathEntryFor(IJavaProject javaProject, String fullyQualifiedName)
        throws JavaModelException {
    IType type = javaProject.findType(fullyQualifiedName);
    if (type != null) {
        IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
                .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

        JavaProject jProject = (JavaProject) javaProject;

        IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
        for (IClasspathEntry rawClasspathEntry : rawClasspath) {
            IClasspathEntry[] resolvedClasspath = jProject
                    .resolveClasspath(new IClasspathEntry[] { rawClasspathEntry });
            IPackageFragmentRoot[] computePackageFragmentRoots = jProject
                    .computePackageFragmentRoots(resolvedClasspath, true, null);
            if (Arrays.asList(computePackageFragmentRoots).contains(packageFragmentRoot)) {
                return rawClasspathEntry;
            }
        }

        return packageFragmentRoot.getRawClasspathEntry();
    }

    return null;
}

From source file:com.google.gdt.eclipse.core.ClasspathUtilities.java

License:Open Source License

/**
 * Determines whether a ClasspathContainer exists on the provided project that
 * matches the provided container ID.//w w w.  ja  v a2s. c  o m
 * 
 * @param javaProject the project to query
 * @param containerId The container ID we are trying to match.
 * @return whether at least one classpath container exists that matches the
 *         provided ID.
 */
public static boolean includesClasspathContainerWithContainerId(IJavaProject javaProject, String containerId)
        throws JavaModelException {
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    int indexOfClasspathEntryContainer = ClasspathUtilities.indexOfClasspathEntryContainer(classpathEntries,
            containerId);
    return indexOfClasspathEntryContainer >= 0;
}

From source file:com.google.gdt.eclipse.core.ClasspathUtilities.java

License:Open Source License

/**
 * Replace an {@link IClasspathEntry#CPE_CONTAINER} entry with the given
 * container ID, with its corresponding resolved classpath entries.
 * /*  ww  w .  j  ava2  s.c  om*/
 * @param javaProject java project
 * @param containerId container ID to replace
 * @return true if a container was replaced
 * 
 * @throws JavaModelException
 */
public static boolean replaceContainerWithClasspathEntries(IJavaProject javaProject, String containerId)
        throws JavaModelException {
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    int containerIndex = ClasspathUtilities.indexOfClasspathEntryContainer(classpathEntries, containerId);
    if (containerIndex != -1) {
        List<IClasspathEntry> newClasspathEntries = new ArrayList<IClasspathEntry>(
                Arrays.asList(classpathEntries));
        IClasspathEntry classpathContainerEntry = newClasspathEntries.remove(containerIndex);
        IClasspathContainer classpathContainer = JavaCore
                .getClasspathContainer(classpathContainerEntry.getPath(), javaProject);
        if (classpathContainer != null) {
            newClasspathEntries.addAll(containerIndex, Arrays.asList(classpathContainer.getClasspathEntries()));
            ClasspathUtilities.setRawClasspath(javaProject, newClasspathEntries);
            return true;
        }
    }
    return false;
}

From source file:com.google.gdt.eclipse.core.ClasspathUtilities.java

License:Open Source License

/**
 * Replaces a project's classpath container entry with a new one or appends it
 * to the classpath if none were found./*  www.  j a  v  a  2s .  c o  m*/
 * 
 * @param javaProject The target project
 * @param containerId the identifier of the classpath container type
 * @param newContainerPath the path for the new classpath. Note: this path
 *          should be partial, not including the initial segment which should
 *          in all cases be the value in containerId
 * @throws JavaModelException thrown by the call to getRawClasspath()
 */
public static void replaceOrAppendContainer(IJavaProject javaProject, String containerId,
        IPath newContainerPath, IProgressMonitor monitor) throws JavaModelException {
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    int indexOfClasspathEntryContainer = ClasspathUtilities.indexOfClasspathEntryContainer(classpathEntries,
            containerId);
    IClasspathEntry newContainer = JavaCore.newContainerEntry(new Path(containerId).append(newContainerPath));
    List<IClasspathEntry> newClasspathEntries = new ArrayList<IClasspathEntry>(Arrays.asList(classpathEntries));
    if (indexOfClasspathEntryContainer >= 0) {
        // Replace the entry
        newClasspathEntries.set(indexOfClasspathEntryContainer, newContainer);
    } else {
        // Append the entry
        newClasspathEntries.add(newContainer);
    }

    javaProject.setRawClasspath(newClasspathEntries.toArray(new IClasspathEntry[newClasspathEntries.size()]),
            monitor);
}

From source file:com.google.gdt.eclipse.core.ClasspathUtilities.java

License:Open Source License

private static boolean areEntriesOnClasspath(IJavaProject javaProject, List<IClasspathEntry> classpathEntries)
        throws JavaModelException {
    List<IClasspathEntry> projectClasspath = Arrays.asList(javaProject.getRawClasspath());

    for (IClasspathEntry entry : classpathEntries) {
        if (projectClasspath.indexOf(entry) == -1) {
            return false;
        }//from   w  w  w.  j ava2 s  . co  m
    }

    return true;
}

From source file:com.google.gdt.eclipse.core.JavaProjectTestUtilities.java

License:Open Source License

/**
 * Adds the specified classpath to the Java project's existing classpath.
 * /* w ww  .j av a2  s.  c  o m*/
 * @param javaProject project to update
 * @param rawClasspathEntry new raw classpath entry
 * @throws JavaModelException
 */
public static void addRawClassPathEntry(IJavaProject javaProject, IClasspathEntry rawClasspathEntry)
        throws JavaModelException {
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();

    IProgressMonitor monitor = new NullProgressMonitor();
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(Arrays.asList(oldEntries));
    newEntries.add(rawClasspathEntry);
    javaProject.setRawClasspath(newEntries.toArray(NO_ICLASSPATH_ENTRIES), monitor);
}

From source file:com.google.gdt.eclipse.core.JavaProjectUtilities.java

License:Open Source License

/**
 * Adds the specified classpath to the Java project's existing classpath.
 * <p>//from   w ww.j  a v a2  s.co m
 * To instantiate classpath entries, see the helper methods in JavaCore (e.g.
 * {@link JavaCore#newSourceEntry(org.eclipse.core.runtime.IPath)}).
 *
 * @param javaProject project to update
 * @param rawClasspathEntry new raw classpath entry
 * @throws JavaModelException
 */
public static void addRawClassPathEntry(IJavaProject javaProject, IClasspathEntry rawClasspathEntry)
        throws JavaModelException {
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();

    IProgressMonitor monitor = new NullProgressMonitor();
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(Arrays.asList(oldEntries));
    newEntries.add(rawClasspathEntry);
    javaProject.setRawClasspath(newEntries.toArray(NO_ICLASSPATH_ENTRIES), monitor);
    try {
        ClasspathUtilities.waitUntilEntriesAreOnClasspath(javaProject, newEntries);
    } catch (InterruptedException e) {
        // Continue, with a note
        CorePluginLog.logWarning(e, "Interrupted while waiting to ensure classpath entries were added");
    }
}