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.core.markers.quickfixes.CopyToServerClasspathMarkerResolution.java

License:Open Source License

public void run(IMarker marker) {
    String buildClasspathFileName = buildClasspathFilePath.lastSegment();
    IProject project = marker.getResource().getProject();
    IPath projRelativeWebInfLibFolderPath = null;

    try {//from   ww w  .j  a v a 2s . co  m
        WebAppUtilities.verifyHasManagedWarOut(project);

        projRelativeWebInfLibFolderPath = WebAppUtilities.getWebInfLib(project).getProjectRelativePath();

        ResourceUtils.createFolderStructure(project, projRelativeWebInfLibFolderPath);
    } catch (CoreException e) {
        CorePluginLog.logError(e);
        MessageDialog.openError(CorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
                "Error While Attempting to Copy " + buildClasspathFileName,
                "Unable to create <WAR>/WEB-INF/lib directory. See the Error Log for more details.");
        return;
    }

    IFolder webInfLibFolder = project.getFolder(projRelativeWebInfLibFolderPath);

    assert (webInfLibFolder.exists());

    IFile serverClasspathFile = webInfLibFolder.getFile(buildClasspathFileName);

    try {
        serverClasspathFile.create(new FileInputStream(buildClasspathFilePath.toFile()), false, null);
    } catch (FileNotFoundException e) {
        MessageDialog.openError(CorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
                "Error While Attempting to Copy " + buildClasspathFileName,
                "The file " + buildClasspathFilePath.toOSString() + " does not exist.");
        return;
    } catch (CoreException e) {
        CorePluginLog.logError(e);
        MessageDialog.openError(CorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
                "Error While Attempting to Copy " + buildClasspathFileName,
                "Unable to copy " + buildClasspathFilePath.toOSString() + " to "
                        + projRelativeWebInfLibFolderPath.toString() + ". See the Error Log for more details.");
        return;
    }

    // Update the project's classpath to use the file copied into
    // <WAR>/WEB-INF/lib
    IJavaProject javaProject = JavaCore.create(project);

    if (!JavaProjectUtilities.isJavaProjectNonNullAndExists(javaProject)) {
        MessageDialog.openError(CorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
                "Error While Attempting to Update Project Classpath",
                "Unable to update project " + project.getName() + "'s classpath.");
        return;
    }

    try {

        List<IClasspathEntry> newRawClasspath = new ArrayList<IClasspathEntry>();

        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            IClasspathEntry resolvedEntry = JavaCore.getResolvedClasspathEntry(entry);
            IPath resolvedEntryPath = ResourceUtils.resolveToAbsoluteFileSystemPath(resolvedEntry.getPath());

            if (resolvedEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                    && resolvedEntryPath.equals(buildClasspathFilePath)) {
                newRawClasspath.add(JavaCore.newLibraryEntry(serverClasspathFile.getFullPath(), null, null));
            } else {
                newRawClasspath.add(entry);
            }
        }

        ClasspathUtilities.setRawClasspath(javaProject, newRawClasspath.toArray(new IClasspathEntry[0]));

    } catch (JavaModelException e) {
        CorePluginLog.logError(e);
        MessageDialog.openError(CorePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
                "Error While Attempting to Update Project Classpath", "Unable to update project "
                        + project.getName() + "'s classpath. See the Error Log for more details.");
    }
}

From source file:com.google.gdt.eclipse.core.sdk.ClasspathContainerUpdateJob.java

License:Open Source License

@Override
protected IStatus run(IProgressMonitor jobMonitor) {
    try {//  www . j av a2s  . co  m
        IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
            public void run(IProgressMonitor runnableMonitor) throws CoreException {
                IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot())
                        .getJavaProjects();
                runnableMonitor.beginTask(LaunchingMessages.LaunchingPlugin_0, projects.length + 1);
                rebindContainers(runnableMonitor, projects);
                runnableMonitor.done();
            }

            /**
             * Rebind all of the classpath containers whose comparison ID matches
             * the expected ID.
             */
            private void rebindContainers(IProgressMonitor runnableMonitor, IJavaProject[] projects)
                    throws CoreException {
                for (IJavaProject project : projects) {
                    // Update the progress monitor
                    runnableMonitor.worked(1);
                    IClasspathEntry[] rawClasspathEntries = project.getRawClasspath();
                    for (IClasspathEntry rawClasspathEntry : rawClasspathEntries) {
                        if (rawClasspathEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER) {
                            continue;
                        }

                        IPath path = rawClasspathEntry.getPath();
                        if (path == null || path.segmentCount() == 0) {
                            continue;
                        }

                        Object actualComparisonId = classpathContainerInitializer.getComparisonID(path,
                                project);
                        if (!actualComparisonId.equals(expectedComparisonId)) {
                            continue;
                        }

                        classpathContainerInitializer.initialize(path, project);
                    }
                }
            }
        };
        JavaCore.run(runnable, null, jobMonitor);
        return Status.OK_STATUS;
    } catch (CoreException e) {
        return e.getStatus();
    }
}

From source file:com.google.gdt.eclipse.core.validators.WebAppProjectValidator.java

License:Open Source License

private boolean validateBuildClasspath(IJavaProject javaProject) throws CoreException {
    IPath webInfLibFolderLocation = null;

    IFolder webInfLibFolder = WebAppUtilities.getWebInfOut(getProject()).getFolder("lib");

    if (webInfLibFolder.exists()) {
        webInfLibFolderLocation = webInfLibFolder.getLocation();
    }/*ww  w. ja  va2 s  . co  m*/

    IClasspathEntry[] rawClasspaths = javaProject.getRawClasspath();
    boolean isOk = true;
    List<IPath> excludedJars = WebAppProjectProperties.getJarsExcludedFromWebInfLib(javaProject.getProject());

    for (IClasspathEntry rawClasspath : rawClasspaths) {
        rawClasspath = JavaCore.getResolvedClasspathEntry(rawClasspath);
        if (rawClasspath.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IPath entryPath = ResourceUtils.resolveToAbsoluteFileSystemPath(rawClasspath.getPath());
            if (excludedJars.contains(entryPath)) {
                continue;
            }

            if (webInfLibFolderLocation == null || !webInfLibFolderLocation.isPrefixOf(entryPath)) {
                MarkerUtilities.createQuickFixMarker(PROBLEM_MARKER_ID,
                        ProjectStructureOrSdkProblemType.JAR_OUTSIDE_WEBINF_LIB, entryPath.toPortableString(),
                        javaProject.getProject(), entryPath.toOSString());
                isOk = false;
            }
        }
    }

    return isOk;
}

From source file:com.google.gdt.eclipse.designer.wizards.ui.JUnitWizardPage.java

License:Open Source License

private IPackageFragmentRoot handleTestSourceFolder(IJavaProject javaProject) throws Exception {
    String testSourceFolderName = com.google.gdt.eclipse.designer.Activator.getStore()
            .getString(Constants.P_GWT_TESTS_SOURCE_FOLDER);
    IFolder testSourceFolder = javaProject.getProject().getFolder(testSourceFolderName);
    IPackageFragmentRoot testSourceFragmentRoot = (IPackageFragmentRoot) JavaCore.create(testSourceFolder);
    // check create
    if (!testSourceFolder.exists() || testSourceFragmentRoot == null || !testSourceFragmentRoot.exists()) {
        // create folder
        if (!testSourceFolder.exists()) {
            testSourceFolder.create(true, false, null);
        }/*from w  w  w  .  j  a va  2 s . c  om*/
        IClasspathEntry[] classpath = javaProject.getRawClasspath();
        // find last source entry
        int insertIndex = -1;
        for (int i = 0; i < classpath.length; i++) {
            if (classpath[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                insertIndex = i + 1;
            }
        }
        // insert new source to entries
        IClasspathEntry testSourceEntry = JavaCore.newSourceEntry(testSourceFolder.getFullPath());
        if (insertIndex == -1) {
            classpath = (IClasspathEntry[]) ArrayUtils.add(classpath, testSourceEntry);
        } else {
            classpath = (IClasspathEntry[]) ArrayUtils.add(classpath, insertIndex, testSourceEntry);
        }
        // modify classpath
        javaProject.setRawClasspath(classpath, javaProject.getOutputLocation(), null);
        testSourceFragmentRoot = (IPackageFragmentRoot) JavaCore.create(testSourceFolder);
    }
    //
    setPackageFragmentRoot(testSourceFragmentRoot, true);
    return testSourceFragmentRoot;
}

From source file:com.google.gdt.eclipse.managedapis.impl.ManagedApiProjectImpl.java

License:Open Source License

public static String getAndroidSdk(IProject androidProject) throws JavaModelException {
    if (androidProject == null) {
        return null;
    }/*from ww w  . ja  va  2  s.  c om*/
    IJavaProject androidJavaProject = JavaCore.create(androidProject);
    List<IClasspathEntry> rawClasspathList = new ArrayList<IClasspathEntry>();
    rawClasspathList.addAll(Arrays.asList(androidJavaProject.getRawClasspath()));
    for (IClasspathEntry e : rawClasspathList) {
        if (e.getEntryKind() != IClasspathEntry.CPE_CONTAINER) {
            continue;
        }
        IClasspathContainer c = JavaCore.getClasspathContainer(e.getPath(), androidJavaProject);
        if (c.getDescription().contains(ANDROID_2_CLASSPATH_CONTAINER)) {
            return ANDROID2_ENVIRONMENT;
        } else if (c.getDescription().contains(ANDROID_3_CLASSPATH_CONTAINER)
                || c.getDescription().contains(ANDROID_4_CLASSPATH_CONTAINER)) {
            return ANDROID3_ENVIRONMENT;
        }
    }
    return null;
}

From source file:com.google.gdt.eclipse.managedapis.impl.ManagedApiProjectImpl.java

License:Open Source License

public ManagedApi[] getManagedApis() {
    List<ManagedApi> installedApis = new ArrayList<ManagedApi>();
    if (eProject != null) {
        IJavaProject project = eProject.getJavaProject();
        if (project != null) {
            try {
                IClasspathEntry[] rawClasspath = project.getRawClasspath();
                for (IClasspathEntry entry : rawClasspath) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        if (ManagedApiPlugin.API_CONTAINER_PATH.isPrefixOf(entry.getPath())) {
                            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                                    project);
                            if (container instanceof ManagedApiContainer) {
                                ManagedApiContainer managedApiContainer = (ManagedApiContainer) container;
                                installedApis.add(managedApiContainer.getManagedApi());
                            }/*from  w ww . java 2  s  .  c om*/
                        }
                    }
                }
            } catch (JavaModelException e) {
                ManagedApiLogger.warn(e, "Error reading classpath");
            }
        }
    }
    return installedApis.toArray(new ManagedApi[installedApis.size()]);
}

From source file:com.google.gdt.eclipse.managedapis.ui.ManagedApiContainerFilter.java

License:Open Source License

/**
 * @return false if the Java element is a file that is contained in a
 *         SimpleDirContainer that is in the classpath of the owning Java
 *         project (non-Javadoc)//w  ww. j a va 2  s  .com
 * 
 * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer,
 *      java.lang.Object, java.lang.Object)
 */
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
    if (element instanceof IResource) {
        IResource resource = (IResource) element;
        if (resource.getType() == IResource.FILE || resource.getType() == IResource.FOLDER) {
            IProject project = resource.getProject();
            try {
                if (project != null && project.exists() && NatureUtils.hasNature(project, JavaCore.NATURE_ID)) {
                    IJavaProject jp = JavaCore.create(resource.getProject());
                    // lets see if this file is included within a ManagedApiRoot
                    IClasspathEntry[] entries = jp.getRawClasspath();
                    for (IClasspathEntry entry : entries) {
                        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                            if (ManagedApiPlugin.API_CONTAINER_PATH.isPrefixOf(entry.getPath())) {
                                // this is likely a ManagedApiContainer, but the container
                                // could be a ghost, and thus unmapped to a
                                // ManagedApiContainer - check below
                                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                                        jp);
                                if (container instanceof ManagedApiContainer) {
                                    ManagedApiContainer managedApiContainer = (ManagedApiContainer) container;
                                    if (managedApiContainer.contains(resource)) {
                                        // this file will is included in the container, so hide it
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (JavaModelException e) {
                ManagedApiLogger.warn(e, "Error reading the classpath");
            } catch (CoreException e) {
                ManagedApiLogger.warn(e, "Error accessing Java Project");
            }
        }
    }
    return true;
}

From source file:com.google.gdt.eclipse.mobile.android.wizards.helpers.AndroidProjectCreator.java

License:Open Source License

/**
 * Add the given library to the project's class path
 * /* w w  w.  ja  v a 2s . c  o  m*/
 * @param javaProject
 * @param libraryPath
 * @param monitor
 * @throws JavaModelException
 */
private void setupLibraryPath(IJavaProject javaProject, IPath libraryPath, IProgressMonitor monitor)
        throws JavaModelException {

    // get the list of entries.
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    entries = addEntryToClasspath(entries, JavaCore.newLibraryEntry(libraryPath, null, null));
    javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 10));
}

From source file:com.google.gdt.eclipse.mobile.android.wizards.helpers.AndroidProjectCreator.java

License:Open Source License

/**
 * Adds the given folder to the project's class path.
 * /*from  w  ww  . j a  v  a 2  s.c  om*/
 * @param javaProject The Java Project to update.
 * @param sourceFolder Template Parameters.
 * @param monitor An existing monitor.
 * @throws CoreException
 */
private void setupSourceFolders(IJavaProject javaProject, String[] sourceFolders, IProgressMonitor monitor)
        throws CoreException {
    IProject project = javaProject.getProject();
    // get the list of entries.
    IClasspathEntry[] entries = javaProject.getRawClasspath();

    // remove the project as a source folder (This is the default)
    entries = removeSourceClasspath(entries, project);

    // add the source folders.
    for (String sourceFolder : sourceFolders) {
        IFolder srcFolder = project.getFolder(sourceFolder);

        // remove it first in case.
        entries = removeSourceClasspath(entries, srcFolder);
        entries = addEntryToClasspath(entries, JavaCore.newSourceEntry(srcFolder.getFullPath()));
    }

    IProject gaeProject = ResourcesPlugin.getWorkspace().getRoot()
            .getProject(projectName + AppEngineRPCPlugin.GAE_PROJECT_NAME_SUFFIX);
    IFolder sharedFolder = gaeProject.getFolder(ProjectCreationConstants.SHARED_FOLDER_NAME);
    if (sharedFolder.exists()) {
        IFolder androidLinkedFolder = androidProject.getFolder(ProjectCreationConstants.SHARED_FOLDER_NAME);
        /* The variable workspaceLoc is required only for Eclipse 3.5.
         * For Eclipses after 3.5, the project specific path variable WORKSPACE_LOC
         * can be used instead.
         */
        String workspaceLoc = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();
        // use variables for shared folder path
        IPath sharedFolderPath = new Path(workspaceLoc + "/" + gaeProject.getName() + "/" //$NON-NLS-N$
                + ProjectCreationConstants.SHARED_FOLDER_NAME);
        androidLinkedFolder.createLink(sharedFolderPath, IResource.ALLOW_MISSING_LOCAL,
                new SubProgressMonitor(monitor, 1));
        entries = addEntryToClasspath(entries, JavaCore.newSourceEntry(androidLinkedFolder.getFullPath()));
    }

    // add .apt_generated to classpath
    IClasspathAttribute[] attributes = new IClasspathAttribute[] {
            JavaCore.newClasspathAttribute("optional", "true") }; //$NON-NLS-N$
    IFolder aptFolder = project.getFolder(ProjectCreationConstants.APT_FOLDER);
    IClasspathEntry entry = JavaCore.newSourceEntry(aptFolder.getFullPath(), ClasspathEntry.INCLUDE_ALL,
            ClasspathEntry.EXCLUDE_NONE, null, attributes);
    entries = addEntryToClasspath(entries, entry);

    javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 10));
}

From source file:com.google.gwt.eclipse.core.GWTProjectUtilities.java

License:Open Source License

/**
 * Returns the GWT-applicable source folder paths from a project (note: this
 * will not traverse into the project's dependencies, for this behavior, see
 * {@link #getGWTSourceFolderPathsFromProjectAndDependencies(IJavaProject, boolean)}
 * )./*w  ww. j av  a  2  s .  c o  m*/
 *
 * @param javaProject Reference to the project
 * @param sourceEntries The list to be filled with the entries corresponding
 *          to the source folder paths
 * @param includeTestSourceEntries Whether to include the entries for test
 *          source
 * @throws SdkException
 */
private static void fillGWTSourceFolderPathsFromProject(IJavaProject javaProject,
        Collection<? super IRuntimeClasspathEntry> sourceEntries, boolean includeTestSourceEntries)
        throws SdkException {

    assert (javaProject != null);

    if (GWTProjectsRuntime.isGWTRuntimeProject(javaProject)) {
        // TODO: Do we still need to handle this here since Sdk's report their
        // own runtime classpath entries?
        sourceEntries.addAll(
                GWTProjectsRuntime.getGWTRuntimeProjectSourceEntries(javaProject, includeTestSourceEntries));
    } else {
        try {
            for (IClasspathEntry curClasspathEntry : javaProject.getRawClasspath()) {
                if (curClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath sourcePath = curClasspathEntry.getPath();
                    // If including tests, include all source, or if not including tests, ensure
                    // it is not a test path
                    if (includeTestSourceEntries || !GWTProjectUtilities.isTestPath(sourcePath)) {
                        if (!isOptional(curClasspathEntry) || exists(sourcePath)) {
                            sourceEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(sourcePath));
                        }
                    }
                }
            }
            IFolder folder = javaProject.getProject().getFolder("super");
            if (folder.exists()) {
                sourceEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(folder.getFullPath()));
            }
        } catch (JavaModelException jme) {
            GWTPluginLog.logError(jme,
                    "Unable to retrieve raw classpath for project " + javaProject.getProject().getName());
        }
    }
}