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

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

Introduction

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

Prototype

int CPE_PROJECT

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_PROJECT.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a required project.

Usage

From source file:org.grails.ide.eclipse.commands.test.AbstractCommandTest.java

License:Open Source License

protected boolean isOnClasspath(IProject project, IProject maybeOnClasspath) throws JavaModelException {
    IJavaProject jProject = JavaCore.create(project);

    IClasspathEntry[] entries = jProject.getRawClasspath();
    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            if (entry.getPath().equals(maybeOnClasspath.getFullPath())) {
                return true;
            }/*from w  w w  .j  a  v  a  2  s  . co m*/
        }
    }
    return false;
}

From source file:org.grails.ide.eclipse.core.internal.classpath.SourceFolderJob.java

License:Open Source License

/**
  * /*from   ww  w .  j av  a 2 s. c  om*/
  * @return all grails source class path entries. List may be empty or
  *         partial complete if error encountered.
  */
public static List<IClasspathEntry> getGrailsSourceClasspathEntries(IJavaProject javaProject) {
    List<IClasspathEntry> oldEntries = new ArrayList<IClasspathEntry>();
    try {
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            // CPE_PROJECT is for in-place plugins that are present as
            // project entries, as opposed to CPE_SOURCE that are for source
            // folders
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                    || entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                for (IClasspathAttribute attr : entry.getExtraAttributes()) {
                    if (attr.getName().equals(GrailsClasspathContainer.PLUGIN_SOURCEFOLDER_ATTRIBUTE_NAME)) {
                        oldEntries.add(entry);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        GrailsCoreActivator.log(e);
    }
    return oldEntries;
}

From source file:org.grails.ide.eclipse.core.workspace.internal.GrailsProjectUtil.java

License:Open Source License

public static List<IProject> getDependentGrailsProjects(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    ArrayList<IProject> result = new ArrayList<IProject>();
    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IProject relatedProject = root.getProject(entry.getPath().lastSegment());
            if (GrailsNature.isGrailsPluginProject(relatedProject)) {
                result.add(relatedProject);
            }//from ww w .j a va  2s.com
        }
    }
    return result;
}

From source file:org.grails.ide.eclipse.editor.groovy.elements.GrailsProject.java

License:Open Source License

private Map<String, ClassNode> internalFindGrailsElementsForProject(PerProjectTypeCache typeCache,
        IJavaProject thisProject, GrailsElementKind kind) throws JavaModelException {
    Map<String, ClassNode> grailsElementMap = new HashMap<String, ClassNode>();
    IClasspathEntry[] rawEntries = thisProject.getRawClasspath();
    for (IClasspathEntry entry : rawEntries) {
        // this may not capture services in plugins because the source folder is linked
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                && entry.getPath().lastSegment().equals(kind.getSourceFolder())) { //$NON-NLS-1$
            IPackageFragmentRoot root = thisProject.findPackageFragmentRoot(entry.getPath());
            if (root == null)
                continue; // something is wrong with this project

            // all CUs that end in Service are services
            IJavaElement[] frags = root.getChildren();
            for (IJavaElement elt : frags) {
                if (elt instanceof IPackageFragment) {
                    IPackageFragment frag = (IPackageFragment) elt;
                    ICompilationUnit[] units = frag.getCompilationUnits();
                    for (ICompilationUnit unit : units) {
                        if (unit instanceof GroovyCompilationUnit
                                && unit.getElementName().endsWith(kind.getNameSuffix())) { //$NON-NLS-1$
                            IType graileElementType = getPrimaryType(unit);
                            if (graileElementType != null) {
                                grailsElementMap.put(getBeanName(graileElementType),
                                        typeCache.getClassNode(graileElementType.getFullyQualifiedName()));
                            }// w  w  w  . ja  v  a2  s  .com
                        }
                    }
                }
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            // trawl through dependent grails projects
            IProject project = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entry.getPath().lastSegment());
            if (GrailsNature.isGrailsPluginProject(project)) {
                IJavaProject otherProject = JavaCore.create(project);
                grailsElementMap.putAll(internalFindGrailsElementsForProject(typeCache, otherProject, kind));
            }
        }
    }
    return grailsElementMap;
}

From source file:org.grails.ide.eclipse.editor.groovy.elements.GrailsProject.java

License:Open Source License

private IType internalFindGrailsElementTypeForProject(String primaryTypeName, PerProjectTypeCache typeCache,
        IJavaProject thisProject, GrailsElementKind kind) throws JavaModelException {
    String unitName = primaryTypeName + ".groovy";
    IClasspathEntry[] rawEntries = thisProject.getRawClasspath();
    for (IClasspathEntry entry : rawEntries) {
        // this may not capture services in plugins because the source folder is linked
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                && entry.getPath().lastSegment().equals(kind.getSourceFolder())) { //$NON-NLS-1$
            IPackageFragmentRoot root = thisProject.findPackageFragmentRoot(entry.getPath());
            if (root == null)
                continue; // something is wrong with this project

            // all CUs that end in Service are services
            IJavaElement[] frags = root.getChildren();
            for (IJavaElement elt : frags) {
                if (elt instanceof IPackageFragment) {
                    IPackageFragment frag = (IPackageFragment) elt;
                    ICompilationUnit[] units = frag.getCompilationUnits();
                    for (ICompilationUnit unit : units) {
                        if (unit instanceof GroovyCompilationUnit && unit.getElementName().equals(unitName)) { //$NON-NLS-1$
                            IType grailsElementType = unit.getType(primaryTypeName);
                            if (grailsElementType.exists()) {
                                return grailsElementType;
                            }// w w w. j  a  va 2s . com
                        }
                    }
                }
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            // trawl through dependent grails projects
            IProject project = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entry.getPath().lastSegment());
            if (GrailsNature.isGrailsPluginProject(project)) {
                IJavaProject otherProject = JavaCore.create(project);
                IType type = internalFindGrailsElementTypeForProject(primaryTypeName, typeCache, otherProject,
                        kind);
                if (type != null) {
                    return type;
                }
            }
        }
    }
    return null;
}

From source file:org.grails.ide.eclipse.groovy.debug.core.evaluation.GroovyJDIEvaluator.java

License:Open Source License

private void addClasspathEntries(JDIGroovyClassLoader groovyLoader, IJavaProject currentProject,
        boolean includeAll) throws JavaModelException, CoreException {
    IClasspathEntry[] entries = currentProject.getResolvedClasspath(true);
    IPath workspaceLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation();
    for (int i = 0; i < entries.length; i++) {
        if (!includeAll && !entries[i].isExported()) {
            continue;
        }/*from   w ww  .  j  ava2 s.  c  o  m*/
        switch (entries[i].getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            try {
                groovyLoader.addURL(entries[i].getPath().toFile().toURL());
            } catch (MalformedURLException e) {
                throw new CoreException(new Status(IStatus.ERROR, GroovyDebugCoreActivator.PLUGIN_ID,
                        e.getLocalizedMessage(), e));
            }
            break;

        case IClasspathEntry.CPE_SOURCE:
            IPath outLocation = entries[i].getOutputLocation();
            if (outLocation != null) {
                // using non-default output location
                try {
                    groovyLoader.addURL(workspaceLocation.append(outLocation).toFile().toURL());
                } catch (MalformedURLException e) {
                    throw new CoreException(new Status(IStatus.ERROR, GroovyDebugCoreActivator.PLUGIN_ID,
                            e.getLocalizedMessage(), e));
                }
            }
            break;

        case IClasspathEntry.CPE_PROJECT:
            IProject otherProject = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entries[i].getPath().lastSegment());
            if (otherProject.isAccessible()) {
                IJavaProject otherJavaProject = JavaCore.create(otherProject);
                addClasspathEntries(groovyLoader, otherJavaProject, false);
            }
            break;

        default:
            break;
        }
    }

    // now add default out location
    IPath outLocation = currentProject.getOutputLocation();
    if (outLocation != null) {
        try {
            groovyLoader.addURL(workspaceLocation.append(outLocation).toFile().toURL());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.gw4e.eclipse.facade.TestResourceGeneration.java

License:Open Source License

public static void getProjectClassPath(IJavaProject project, List<File> dst) throws Exception {
    IRuntimeClasspathEntry[] rentries = JavaRuntime.computeUnresolvedRuntimeClasspath(project);
    for (IRuntimeClasspathEntry entry : rentries) {
        switch (entry.getType()) {
        case IClasspathEntry.CPE_SOURCE:
            break;
        case IClasspathEntry.CPE_PROJECT:
            break;
        case IClasspathEntry.CPE_LIBRARY:
            break;
        case IClasspathEntry.CPE_VARIABLE:
            // JRE like entries
            IRuntimeClasspathEntry[] variableEntries = JavaRuntime.resolveRuntimeClasspathEntry(entry, project);
            break;
        case IClasspathEntry.CPE_CONTAINER:
            IRuntimeClasspathEntry[] containerEntries = JavaRuntime.resolveRuntimeClasspathEntry(entry,
                    project);//from w w w. ja v  a  2 s.c  o  m
            for (IRuntimeClasspathEntry containerentry : containerEntries) {
                dst.add(new File(containerentry.getLocation()));
            }
            break;
        default:
            throw new Exception("unsupported classpath entry " + entry);
        }
    }
}

From source file:org.hammurapi.eclipse.plugin.HammurapiBuilder.java

License:Open Source License

/**
 * @author Daniel Berg jdt-dev@eclipse.org.
 * @param javaProject/*from   ww w.  ja  v  a2 s  .  c o m*/
 * @return
 */
private List getClasspathURLs(IJavaProject javaProject, boolean exportedOnly)
        throws JavaModelException, MalformedURLException, CoreException {
    HashSet urls = new HashSet();

    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    boolean defaultOutputAdded = false;

    for (int i = 0; i < entries.length; i++) {
        // Source entries are apparently always assumed to be exported - but don't
        // report themselves as such.
        if (!exportedOnly || entries[i].isExported()
                || entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            switch (entries[i].getEntryKind()) {
            case IClasspathEntry.CPE_SOURCE: {
                IPath outputLocation = null;

                if (isEclipse_2_1_Safe()) {
                    outputLocation = entries[i].getOutputLocation();
                }
                if (outputLocation == null) {
                    //
                    //  If the output location is null then the project's
                    //  default output location is being used.
                    //
                    if (!defaultOutputAdded) {
                        defaultOutputAdded = true;
                        outputLocation = javaProject.getOutputLocation();
                    }
                }

                if (outputLocation != null) {

                    // When the output location is the project itself, the project
                    // can't resolve the file - therefore just get the project's
                    // location. 

                    if (outputLocation.segmentCount() == 1) {
                        outputLocation = javaProject.getProject().getLocation();
                    } else {
                        // Output locations are always workspace relative. Do this mess
                        // to get a fully qualified location.
                        outputLocation = javaProject.getProject().getParent().getFile(outputLocation)
                                .getLocation();
                    }

                    urls.add(outputLocation.addTrailingSeparator().toFile().toURL());
                }

                break;
            }
            case IClasspathEntry.CPE_LIBRARY: {
                // Jars always come with a nice fully specified path.
                urls.add(new URL("file:/" + entries[i].getPath().toOSString()));

                break;
            }
            case IClasspathEntry.CPE_PROJECT: {
                IJavaProject dependentProject = (IJavaProject) (ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(entries[i].getPath().segment(0))).getAdapter(IJavaElement.class);

                urls.addAll(getClasspathURLs(dependentProject, true));

                break;
            }
            default: {
                String msg = "Encountered unexpected classpath entry : " + entries[i].getEntryKind();
                HammurapiPlugin.report2LogError(msg, null);
                Status status = new Status(IStatus.ERROR, "HammurapiPlugin", IStatus.ERROR, msg, null);
                throw new CoreException(status);
            }
            }
        }
    }

    return new ArrayList(urls);
}

From source file:org.jactr.eclipse.core.parser.ProjectSensitiveParserImportDelegate.java

License:Open Source License

@Override
public URL resolveURL(String url, URL baseURL) {
    try {//w  ww.j a v a  2s  .com
        IJavaProject javaProject = JavaCore.create(_project);
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(false);

        for (IClasspathEntry entry : entries)
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT
                    && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = entry.getPath();
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("checking " + entry + " at " + path);

                IProject tmpProject = ResourcesPlugin.getWorkspace().getRoot().getProject(path.lastSegment());
                IFolder modelsFolder = tmpProject.getFolder("models");
                if (modelsFolder == null || !modelsFolder.exists())
                    continue;

                IFile modelFile = modelsFolder.getFile(url);
                if (modelFile == null || !modelFile.exists())
                    continue;

                // CorePlugin.debug("Found a matching file at "
                // + modelFile.getFullPath());

                if (javaProject.isOnClasspath(modelFile)) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("Is on classpath, returning url");

                    URL rtn = modelFile.getLocation().toFile().toURI().toURL();

                    // CorePlugin.debug(String.format("On path at %s",
                    // rtn.toExternalForm()));

                    return rtn;
                } else if (LOGGER.isDebugEnabled())
                    LOGGER.debug("is not on classpath");
            }
    } catch (Exception e) {
        CorePlugin.error("Failed to extract location info for " + url, e);
    }
    return super.resolveURL(url, baseURL);
}

From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java

License:Open Source License

/**
 * Creates a ClassLoader using the project's build path.
 *
 * @param javaProject the Java project./*w  w  w .  j  av a2 s. c  om*/
 * @param parentClassLoader the parent class loader, may be null.
 *
 * @return a new ClassLoader based on the project's build path.
 *
 * @throws Exception if something goes wrong.
 */
public static ClassLoader getProjectClassLoader(IJavaProject javaProject, ClassLoader parentClassLoader)
        throws Exception {
    IProject project = javaProject.getProject();
    IWorkspaceRoot root = project.getWorkspace().getRoot();
    List<URL> urls = new ArrayList<>();
    urls.add(
            new File(project.getLocation() + "/" + javaProject.getOutputLocation().removeFirstSegments(1) + "/") //$NON-NLS-2$ //$NON-NLS-2$
                    .toURI().toURL());
    for (IClasspathEntry classpathEntry : javaProject.getResolvedClasspath(true)) {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IPath projectPath = classpathEntry.getPath();
            IProject otherProject = root.getProject(projectPath.segment(0));
            IJavaProject otherJavaProject = JavaCore.create(otherProject);
            urls.add(new File(otherProject.getLocation() + "/" //$NON-NLS-1$
                    + otherJavaProject.getOutputLocation().removeFirstSegments(1) + "/").toURI().toURL()); //$NON-NLS-1$
        } else if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            urls.add(new File(classpathEntry.getPath().toOSString()).toURI().toURL());
        }
    }
    if (parentClassLoader == null) {
        return new URLClassLoader(urls.toArray(new URL[urls.size()]));
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), parentClassLoader);
}