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:org.cloudfoundry.ide.eclipse.internal.server.core.CloudUtil.java

License:Open Source License

public static String getFramework(IProject project) {
    if (project != null) {
        IJavaProject javaProject = CloudFoundryProjectUtil.getJavaProject(project);
        if (javaProject != null) {
            if (CloudFoundryProjectUtil.hasNature(project, DeploymentConstants.GRAILS_NATURE)) {
                return CloudApplication.GRAILS;
            }/*from   w ww . j  av  a2 s.  c  o  m*/

            // in case user has Grails projects without the nature
            // attached
            if (project.isAccessible() && project.getFolder("grails-app").exists()
                    && project.getFile("application.properties").exists()) {
                return CloudApplication.GRAILS;
            }

            IClasspathEntry[] entries;
            boolean foundSpringLibrary = false;
            try {
                entries = javaProject.getRawClasspath();
                for (IClasspathEntry entry : entries) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        if (isLiftLibrary(entry)) {
                            return DeploymentConstants.LIFT;
                        }
                        if (isSpringLibrary(entry)) {
                            foundSpringLibrary = true;
                        }
                    } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                                javaProject);
                        if (container != null) {
                            for (IClasspathEntry childEntry : container.getClasspathEntries()) {
                                if (isLiftLibrary(childEntry)) {
                                    return DeploymentConstants.LIFT;
                                }
                                if (isSpringLibrary(childEntry)) {
                                    foundSpringLibrary = true;
                                }
                            }
                        }
                    }
                }
            } catch (JavaModelException e) {

                CloudFoundryPlugin.logError(new Status(IStatus.WARNING, CloudFoundryPlugin.PLUGIN_ID,
                        "Unexpected error during auto detection of application type", e));
            }

            if (CloudFoundryProjectUtil.isSpringProject(project)) {
                return CloudApplication.SPRING;
            }

            if (foundSpringLibrary) {
                return CloudApplication.SPRING;
            }
        }
    }
    return null;
}

From source file:org.cloudfoundry.ide.eclipse.internal.server.ui.standalone.JavaUIHelper.java

License:Open Source License

public IPackageFragment getDefaultPackageFragment() {
    IJavaProject javaProject = getJavaProject();

    if (getJavaProject() == null) {
        return null;
    }/* www .j  a v  a 2 s. co m*/

    IPackageFragmentRoot[] roots = null;
    try {

        IClasspathEntry[] entries = javaProject.getRawClasspath();

        for (IClasspathEntry entry : entries) {

            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                roots = javaProject.findPackageFragmentRoots(entry);
                if (roots != null) {
                    break;
                }
            }
        }

    } catch (JavaModelException e) {
        CloudFoundryPlugin.logError(e);
    }

    if (roots != null) {
        IPackageFragment fragment = null;
        for (IPackageFragmentRoot root : roots) {
            try {
                IJavaElement[] members = root.getChildren();
                if (members != null) {
                    for (IJavaElement element : members) {
                        if (element instanceof IPackageFragment) {
                            IPackageFragment frag = (IPackageFragment) element;
                            if (frag.isDefaultPackage()) {
                                fragment = frag;
                                break;
                            }
                        }
                    }
                }
                if (fragment != null) {
                    break;
                }
            } catch (JavaModelException e) {
                CloudFoundryPlugin.logError(e);
            }
        }
        return fragment;
    }
    return null;
}

From source file:org.cloudfoundry.ide.eclipse.internal.server.ui.wizards.CloudFoundryApplicationWizardPage.java

License:Open Source License

private static String getFramework(ApplicationModule module) {
    if (module != null && module.getLocalModule() != null) {
        IProject project = module.getLocalModule().getProject();
        if (project != null) {
            IJavaProject javaProject = CloudFoundryProjectUtil.getJavaProject(project);
            if (javaProject != null) {
                if (CloudFoundryProjectUtil.hasNature(project, GRAILS_NATURE)) {
                    return CloudApplication.GRAILS;
                }/*from  w ww  .j av a2 s  .  co m*/

                // in case user has Grails projects without the nature
                // attached
                if (project.isAccessible() && project.getFolder("grails-app").exists()
                        && project.getFile("application.properties").exists()) {
                    return CloudApplication.GRAILS;
                }

                IClasspathEntry[] entries;
                boolean foundSpringLibrary = false;
                try {
                    entries = javaProject.getRawClasspath();
                    for (IClasspathEntry entry : entries) {
                        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                            if (isLiftLibrary(entry)) {
                                return LIFT;
                            }
                            if (isSpringLibrary(entry)) {
                                foundSpringLibrary = true;
                            }
                        } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                                    javaProject);
                            if (container != null) {
                                for (IClasspathEntry childEntry : container.getClasspathEntries()) {
                                    if (isLiftLibrary(childEntry)) {
                                        return LIFT;
                                    }
                                    if (isSpringLibrary(childEntry)) {
                                        foundSpringLibrary = true;
                                    }
                                }
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    CloudFoundryServerUiPlugin.getDefault().getLog()
                            .log(new Status(IStatus.WARNING, CloudFoundryServerUiPlugin.PLUGIN_ID,
                                    "Unexpected error during auto detection of application type", e));
                }

                if (CloudFoundryProjectUtil.isSpringProject(project)) {
                    return CloudApplication.SPRING;
                }

                if (foundSpringLibrary) {
                    return CloudApplication.SPRING;
                }
            }
        }
    }

    return CloudApplication.JAVA_WEB;
}

From source file:org.cloudfoundry.ide.eclipse.server.core.internal.application.JavaWebApplicationDelegate.java

License:Open Source License

/**
 * Attempts to determine the framework based on the contents and nature of
 * the project. Returns null if no framework was determined.
 * @param project/*  www  .  j a  v  a2 s.  c  om*/
 * @return Framework type or null if framework was not determined.
 * @deprecated kept for reference as to how application type was being
 * determined from a Java project for legacy v1 CF servers. v2 Servers no
 * longer require a framework for an application, as frameworks have been
 * replaced with buildpacks.
 */
protected String getFramework(IProject project) {
    if (project != null) {
        IJavaProject javaProject = CloudFoundryProjectUtil.getJavaProject(project);
        if (javaProject != null) {
            if (CloudFoundryProjectUtil.hasNature(project, CloudFoundryConstants.GRAILS_NATURE)) {
                return CloudFoundryConstants.GRAILS;
            }

            // in case user has Grails projects without the nature
            // attached
            if (project.isAccessible() && project.getFolder("grails-app").exists() //$NON-NLS-1$
                    && project.getFile("application.properties").exists()) { //$NON-NLS-1$
                return CloudFoundryConstants.GRAILS;
            }

            IClasspathEntry[] entries;
            boolean foundSpringLibrary = false;
            try {
                entries = javaProject.getRawClasspath();
                for (IClasspathEntry entry : entries) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        if (isLiftLibrary(entry)) {
                            return CloudFoundryConstants.LIFT;
                        }
                        if (isSpringLibrary(entry)) {
                            foundSpringLibrary = true;
                        }
                    } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                                javaProject);
                        if (container != null) {
                            for (IClasspathEntry childEntry : container.getClasspathEntries()) {
                                if (isLiftLibrary(childEntry)) {
                                    return CloudFoundryConstants.LIFT;
                                }
                                if (isSpringLibrary(childEntry)) {
                                    foundSpringLibrary = true;
                                }
                            }
                        }
                    }
                }
            } catch (JavaModelException e) {
                // Log the error but don't throw it again as there may be
                // other ways to detect the framework
                CloudFoundryPlugin.log(new Status(IStatus.WARNING, CloudFoundryPlugin.PLUGIN_ID,
                        "Unexpected error during auto detection of application type", e)); //$NON-NLS-1$
            }

            if (CloudFoundryProjectUtil.isSpringProject(project)) {
                return CloudFoundryConstants.SPRING;
            }

            if (foundSpringLibrary) {
                return CloudFoundryConstants.SPRING;
            }
        }
    }
    return null;
}

From source file:org.cloudfoundry.ide.eclipse.server.standalone.internal.startcommand.JavaTypeUIAdapter.java

License:Open Source License

public IPackageFragment getDefaultPackageFragment(IJavaProject javaProject) {

    if (javaProject == null) {
        return null;
    }/*from w  w w  . java  2s  . c  om*/

    List<IPackageFragmentRoot> packFragRoots = new ArrayList<IPackageFragmentRoot>();
    try {

        IClasspathEntry[] entries = javaProject.getRawClasspath();

        for (IClasspathEntry entry : entries) {

            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
                if (roots != null) {
                    for (IPackageFragmentRoot rt : roots) {
                        if (!packFragRoots.contains(rt)) {
                            packFragRoots.add(rt);
                        }
                    }
                }
            }
        }

    } catch (JavaModelException e) {
        CloudFoundryPlugin.log(e);
    }

    IPackageFragment fragment = null;
    for (IPackageFragmentRoot root : packFragRoots) {
        try {
            IJavaElement[] members = root.getChildren();
            if (members != null) {
                for (IJavaElement element : members) {
                    if (element instanceof IPackageFragment) {
                        IPackageFragment frag = (IPackageFragment) element;
                        if (frag.isDefaultPackage()) {
                            fragment = frag;
                            break;
                        }
                    }
                }
            }
            if (fragment != null) {
                break;
            }
        } catch (JavaModelException e) {
            CloudFoundryPlugin.log(e);
        }
    }
    return fragment;
}

From source file:org.codehaus.groovy.eclipse.core.model.GroovyRuntime.java

License:Apache License

public static void removeLibraryFromClasspath(final IJavaProject javaProject, final IPath libraryPath)
        throws JavaModelException {
    final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    for (int i = 0; i < oldEntries.length; i++) {
        final IClasspathEntry entry = oldEntries[i];
        if (entry.getPath().equals(libraryPath)) {
            final IClasspathEntry[] newEntries = (IClasspathEntry[]) ArrayUtils.remove(oldEntries, i);
            javaProject.setRawClasspath(newEntries, null);
            return;
        }//from www  . j  a  v a  2 s.co m
    }
}

From source file:org.codehaus.groovy.eclipse.core.model.GroovyRuntime.java

License:Apache License

public static boolean hasClasspathContainer(final IJavaProject javaProject, final IPath libraryPath)
        throws CoreException {
    if (javaProject == null || !javaProject.getProject().isAccessible())
        return false;
    final IClasspathEntry[] entries = javaProject.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            if (ObjectUtils.equals(entry.getPath(), libraryPath) || libraryPath.isPrefixOf(entry.getPath())) {
                return true;
            }/*from   w ww .  j  a v  a 2  s .  co  m*/
        }
    }
    return false;
}

From source file:org.codehaus.groovy.eclipse.core.model.GroovyRuntime.java

License:Apache License

public static void removeClasspathContainer(IPath containerPath, IJavaProject javaProject) {
    try {//  ww  w  .  j  a v a2s .  co  m
        if (!hasGroovyClasspathContainer(javaProject)) {
            return;
        }

        IClasspathEntry[] entries = javaProject.getRawClasspath();
        int removeIndex = -1;
        for (int i = 0; i < entries.length; i++) {
            if (entries[i].getPath().equals(containerPath)) {
                removeIndex = i;
                break;
            }
        }
        IClasspathEntry[] newEntries = (IClasspathEntry[]) ArrayUtils.remove(entries, removeIndex);
        javaProject.setRawClasspath(newEntries, null);
    } catch (final CoreException ce) {
        GroovyCore.logException("Failed to add groovy classpath container:" + ce.getMessage(), ce);
        throw new RuntimeException(ce);
    }
}

From source file:org.codehaus.groovy.eclipse.core.model.GroovyRuntime.java

License:Apache License

/**
 * Adds a classpath entry to the end of a project's the classpath
 *
 * @param project/*  ww  w  . j  ava 2  s.c  o m*/
 *            The project to add the entry to.
 * @param newEntry
 *            The entry to add.
 * @throws JavaModelException
 */
public static void addClassPathEntry(IJavaProject project, IClasspathEntry newEntry) throws JavaModelException {
    IClasspathEntry[] newEntries = (IClasspathEntry[]) ArrayUtils.add(project.getRawClasspath(), newEntry);
    project.setRawClasspath(newEntries, null);
}

From source file:org.codehaus.groovy.eclipse.core.model.GroovyRuntime.java

License:Apache License

/**
 * Adds a classpath entry to the front of a project's the classpath
 *
 * @param project/*  ww  w.  j av a2s  .com*/
 *            The project to add the entry to.
 * @param newEntry
 *            The entry to add.
 * @throws JavaModelException
 */
public static void addClassPathEntryToFront(IJavaProject project, IClasspathEntry newEntry)
        throws JavaModelException {
    IClasspathEntry[] newEntries = (IClasspathEntry[]) ArrayUtils.add(project.getRawClasspath(), 0, newEntry);
    project.setRawClasspath(newEntries, null);
}