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.eclipse.dltk.freemarker.core.util.ClassUtils.java

License:Open Source License

/**
 * Add URL form the JavaProject./*from w ww  .  ja v  a2  s.  co m*/
 * 
 * @param javaProject
 * @param urls
 * @param root
 * @param rootLocation
 * @param javaProjectsAlreadyDone
 * @throws JavaModelException
 * @throws CoreException
 */
private static void addPath(IJavaProject javaProject, List<URL> urls, IWorkspaceRoot root, IPath rootLocation,
        List<IJavaProject> javaProjectsAlreadyDone) throws JavaModelException, CoreException {
    if (javaProjectsAlreadyDone.contains(javaProject))
        return;

    javaProjectsAlreadyDone.add(javaProject);
    String projectName = javaProject.getElementName();
    IClasspathEntry javacp[] = javaProject.getResolvedClasspath(true);

    // Add bin folder
    IPath outputLocation = javaProject.getOutputLocation();
    addPath(urls, rootLocation.append(outputLocation));

    // Loop for .classpath
    IClasspathEntry entry = null;
    IPath entryPath = null;
    for (int i = 0; i < javacp.length; i++) {
        // load bin folder of referenced projects into classpath
        entry = javacp[i];
        entryPath = entry.getPath();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            // add jars
            if (projectName.equals(entryPath.segment(0))) {
                // Jar belongs to the Java Project, add base dir
                addPath(urls, rootLocation.append(entryPath));
            } else {
                // External Jar (ex : C:/Program Files/xxx.jar)
                addPath(urls, entryPath);
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            // add the source folders of the project
            addPath(urls, rootLocation.append(entryPath));
            break;
        case IClasspathEntry.CPE_PROJECT:
            // add bin folder from referenced project
            IProject referencedProject = root.getProject(entryPath.segment(0));
            if (referencedProject != null && referencedProject.exists()
                    && referencedProject.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject referencedJavaProject = JavaCore.create(referencedProject);
                addPath(referencedJavaProject, urls, root, rootLocation, javaProjectsAlreadyDone);
            }
            break;
        default:
            addPath(urls, entryPath);
            break;
        }
    }
}

From source file:org.eclipse.draw2d.preview.OpenFigureViewerHandler.java

License:Open Source License

private IFigure getFigure(IWorkbenchWindow activeWorkbenchWindow, Object item) {
    IType figureClass = null;//from   w w  w  . j  a  va  2s .co  m
    try {
        if (item instanceof ICompilationUnit) {
            figureClass = ((ICompilationUnit) item).getTypes()[0];
        } else if (item instanceof IType) {
            figureClass = (IType) item;
        } else if (item instanceof IClassFile) {
            figureClass = ((IClassFile) item).getType();
        } else if (item instanceof IFile) {
            ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom((IFile) item);
            figureClass = compilationUnit.getTypes()[0];
        }
        boolean isFigure = false;
        for (IType type : figureClass.newSupertypeHierarchy(null).getAllInterfaces()) {
            if (type.getFullyQualifiedName().equals(IFigure.class.getName())) {
                isFigure = true;
            }
        }
        if (!isFigure) {
            MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.notADraw2DFigure_title,
                    Messages.notADraw2DFigure_message);
            return null;
        }
        List<URL> urls = new ArrayList<URL>();
        URL outputURL = toAbsoluteURL(figureClass.getJavaProject().getOutputLocation());
        urls.add(outputURL);
        for (IClasspathEntry entry : figureClass.getJavaProject().getResolvedClasspath(false)) {
            URL toAdd = null;
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                toAdd = entry.getPath().toFile().toURI().toURL();
                break;
            case IClasspathEntry.CPE_PROJECT:
                IProject project = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(entry.getPath().toString());
                IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
                toAdd = toAbsoluteURL(javaProject.getOutputLocation());
                break;
            }
            if (toAdd != null) {
                urls.add(toAdd);
            }
        }
        ClassLoader cl = new URLClassLoader(urls.toArray(new URL[urls.size()]),
                this.getClass().getClassLoader());
        Class<? extends IFigure> figureClazz = (Class<? extends IFigure>) cl
                .loadClass(figureClass.getFullyQualifiedName());
        for (Constructor<?> cons : figureClazz.getConstructors()) {
            cons.setAccessible(true);
        }
        IFigure figureObject = figureClazz.newInstance();
        return figureObject;
    } catch (JavaModelException ex) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.javaError_title,
                Messages.javaError_message);
        return null;
    } catch (MalformedURLException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        return null;
    } catch (CoreException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        Activator.getDefault().getLog().log(e.getStatus());
        return null;
    } catch (ClassNotFoundException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        return null;
    } catch (InstantiationException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.couldNotInstantiate_title,
                Messages.couldNotInstantiate_message);
        return null;
    } catch (IllegalAccessException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.couldNotInstantiate_title,
                Messages.couldNotInstantiate_message);
        return null;
    } catch (Error e) {
        ErrorDialog.openError(activeWorkbenchWindow.getShell(), Messages.errorInFigure_title,
                Messages.errorInFigure_message, new Status(IStatus.ERROR,
                        figureClass.getJavaProject().getElementName(), Messages.errorInFigure_message, e));
        return null;
    }
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ClassLoaderHelper.java

License:Open Source License

private static URL findResourceURL(IJavaProject javaProject, Set<IJavaProject> visited, boolean isFirstProject,
        String name) {/*from   www  .  j  av  a  2 s .com*/
    if (visited.contains(javaProject))
        return null;
    visited.add(javaProject);
    try {
        IPath outPath = javaProject.getProject().getLocation().removeLastSegments(1)
                .append(javaProject.getOutputLocation());
        outPath = outPath.addTrailingSeparator();
        {
            URL url = toURL(outPath.append(name));
            if (url != null) {
                return url;
            }
        }
        for (IPackageFragmentRoot fragment : javaProject.getPackageFragmentRoots()) {
            if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
                URL url = toURL(fragment.getResource().getLocation().append(name));
                if (url != null) {
                    return url;
                }
            }
        }
        // urls.add(out);
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY: {
                // TODO
                IClasspathEntry resolveEntry = JavaCore.getResolvedClasspathEntry(entry);
                File file = resolveEntry.getPath().toFile();
                IPath path = resolveEntry.getPath();
                if (!file.exists()) {
                    String projectName = path.segment(0);
                    IProject project = javaProject.getProject().getWorkspace().getRoot()
                            .getProject(projectName);
                    path = project.getLocation().append(path.removeFirstSegments(1));
                }
                String spec = "jar:file:" + path.toString() + "!/" + name;
                try {
                    URL url2 = new URL(spec);
                    url2.getContent();
                    return url2;
                } catch (Exception e) {
                }
            }
                break;
            case IClasspathEntry.CPE_CONTAINER:

                break;
            case IClasspathEntry.CPE_VARIABLE: {
                {
                    // TODO
                    URL url = toURL(outPath.append(name));
                    if (url != null) {
                        return url;
                    }
                }
            }
                break;
            case IClasspathEntry.CPE_PROJECT: {
                if (isFirstProject || entry.isExported()) {
                    URL url = findResourceURL(getJavaProject(entry), visited, false, name);
                    if (url != null) {
                        return url;
                    }
                }
                break;
            }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ClassLoaderHelper.java

License:Open Source License

private static void collectClasspathURLs(IJavaProject javaProject, List<URL> urls, Set<IJavaProject> visited,
        boolean isFirstProject) {
    if (visited.contains(javaProject))
        return;//  www .  j  a v a 2s  .c  o m
    visited.add(javaProject);
    try {
        IPath outPath = javaProject.getProject().getWorkspace().getRoot().getFullPath()
                .append(javaProject.getOutputLocation());
        outPath = outPath.addTrailingSeparator();
        URL out = createFileURL(outPath);
        urls.add(out);
        IClasspathEntry[] entries = null;
        entries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
            case IClasspathEntry.CPE_CONTAINER:
            case IClasspathEntry.CPE_VARIABLE:
                collectClasspathEntryURL(entry, urls);
                break;
            case IClasspathEntry.CPE_PROJECT: {
                if (isFirstProject || entry.isExported())
                    collectClasspathURLs(getJavaProject(entry), urls, visited, false);
                break;
            }
            }
        }
    } catch (JavaModelException e) {
        return;
    }
}

From source file:org.eclipse.edt.debug.core.java.filters.ClasspathEntryFilter.java

License:Open Source License

protected void processEntries(IClasspathEntry[] entries, IJavaProject project, Map<String, Object> classMap)
        throws CoreException {
    for (IClasspathEntry entry : entries) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            processLibraryEntry(entry, classMap);
            break;

        case IClasspathEntry.CPE_CONTAINER:
            processContainerEntry(entry, project, classMap);
            break;

        case IClasspathEntry.CPE_SOURCE:
            processSourceEntry(entry, classMap);
            break;

        case IClasspathEntry.CPE_PROJECT:
            IProject depProject = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entry.getPath().lastSegment());
            if (depProject.isAccessible() && depProject.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject jp = JavaCore.create(depProject);
                processEntries(jp.getResolvedClasspath(true), jp, classMap);
            }//from  ww  w.j  a v a  2  s  .c  om
            break;

        default:
            EDTDebugCorePlugin.log(new Status(IStatus.WARNING, EDTDebugCorePlugin.PLUGIN_ID,
                    NLS.bind(EDTDebugCoreMessages.TypeFilterClasspathEntryNotSupported,
                            new Object[] { entry.getEntryKind(), getId() })));
            break;
        }
    }
}

From source file:org.eclipse.edt.debug.core.java.filters.WorkspaceProjectClassFilter.java

License:Open Source License

@Override
protected IClasspathEntry[] getCommonClasspathEntries() {
    String[] projects = getProjectNames();
    if (projects != null && projects.length > 0) {
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
        for (String project : projects) {
            IProject proj = root.getProject(project);
            try {
                if (proj.isAccessible() && proj.hasNature(JavaCore.NATURE_ID)) {
                    for (IClasspathEntry entry : JavaCore.create(proj).getResolvedClasspath(true)) {
                        switch (entry.getEntryKind()) {
                        case IClasspathEntry.CPE_LIBRARY:
                            if (includeLibraries()) {
                                list.add(entry);
                            }/* w w  w  .ja  v a  2 s . co m*/
                            break;

                        case IClasspathEntry.CPE_PROJECT:
                            if (includeReferencedProjects()) {
                                list.add(entry);
                            }
                            break;

                        case IClasspathEntry.CPE_CONTAINER:
                            if (includeContainers()) {
                                list.add(entry);
                            }
                            break;

                        case IClasspathEntry.CPE_SOURCE:
                            if (includeSource()) {
                                list.add(entry);
                            }
                            break;
                        }
                    }
                }
            } catch (CoreException ce) {
                EDTDebugCorePlugin.log(ce);
            }
        }
        return list.toArray(new IClasspathEntry[list.size()]);
    }
    return null;
}

From source file:org.eclipse.edt.ide.deployment.rui.operation.CopyJavaRuntimeResourcesOperation.java

License:Open Source License

/**
 * Finds all the classpath entries on the Java build path, including referenced projects, that are of type {@link IClasspathEntry#CPE_CONTAINER}.
 * @throws CoreException/*from  w w w  .j a va  2 s. c o  m*/
 */
private void getClasspathContainers(IJavaProject project, Set<IJavaProject> seen, Set<IClasspathEntry> entries)
        throws CoreException {
    if (seen.contains(project)) {
        return;
    }

    seen.add(project);

    for (IClasspathEntry entry : project.getRawClasspath()) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_CONTAINER:
            entries.add(entry);
            break;

        case IClasspathEntry.CPE_PROJECT:
            IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().lastSegment());
            if (p.isAccessible() && p.hasNature(JavaCore.NATURE_ID)) {
                getClasspathContainers(JavaCore.create(p), seen, entries);
            }
            break;
        }
    }
}

From source file:org.eclipse.edt.ide.eunit.internal.actions.GenTestDriverAction.java

License:Open Source License

protected WorkspaceModifyOperation getSetJavaBuildPathOperation(final IProject javaDriverProject,
        final IProject dependentProj) {
    WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
        @Override//  w ww. j a  va2s  . com
        protected void execute(IProgressMonitor monitor)
                throws CoreException, InvocationTargetException, InterruptedException {
            if (javaDriverProject.hasNature(JavaCore.NATURE_ID)) {
                monitor.subTask("Set java build path depends on project " + dependentProj.getName());
                IJavaProject javaProject = JavaCore.create(javaDriverProject);

                IClasspathEntry[] classpath = javaProject.getRawClasspath();

                boolean javaProjBuildPathAlreadySet = false;
                //check to see if the java build path already set for the same dependent project
                for (int p = 0; p < classpath.length && !javaProjBuildPathAlreadySet; p++) {
                    if (classpath[p].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                        IPath dependentProjPath = classpath[p].getPath();
                        if (dependentProj.getFullPath().equals(dependentProjPath))
                            javaProjBuildPathAlreadySet = true;
                    }
                }

                //if not set, set it
                if (!javaProjBuildPathAlreadySet) {
                    List<IClasspathEntry> additions = new ArrayList<IClasspathEntry>();

                    IClasspathEntry newClsPathEntry = JavaCore.newProjectEntry(dependentProj.getFullPath());
                    additions.add(newClsPathEntry);

                    if (additions.size() > 0) {
                        IClasspathEntry[] newEntries = new IClasspathEntry[classpath.length + additions.size()];
                        System.arraycopy(classpath, 0, newEntries, 0, classpath.length);
                        for (int i = 0; i < additions.size(); i++) {
                            newEntries[classpath.length + i] = additions.get(i);
                        }
                        javaProject.setRawClasspath(newEntries, null);
                    }
                }
                monitor.worked(1);
            }
        }
    };

    return op;
}

From source file:org.eclipse.emf.codegen.ecore.gwt.GWTBuilder.java

License:Open Source License

@SuppressWarnings("rawtypes")
@Override/* w  w  w .j a  va  2  s.  co m*/
protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
    Set<IProject> result = new HashSet<IProject>();
    final IProject project = getProject();
    if (project.exists()) {
        IWorkspaceRoot root = project.getWorkspace().getRoot();
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathContainer gaeClasspathContainer = JavaCore.getClasspathContainer(
                new Path("com.google.appengine.eclipse.core.GAE_CONTAINER"), javaProject);
        if (gaeClasspathContainer != null) {
            for (IClasspathEntry classpathEntry : gaeClasspathContainer.getClasspathEntries()) {
                if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                        && classpathEntry.getContentKind() == IPackageFragmentRoot.K_BINARY) {
                    IPath path = classpathEntry.getPath();
                    int segmentCount = path.segmentCount();
                    if (segmentCount >= 4) {
                        if (path.segment(segmentCount - 2).equals("user")
                                || path.segment(segmentCount - 3).equals("user")) {
                            copy(URI.createFileURI(path.toOSString()), URI.createPlatformResourceURI(
                                    project.getName() + "/war/WEB-INF/lib/" + path.lastSegment(), true));
                        }
                    }
                }
            }
        }

        IClasspathContainer gwtClasspathContainer = JavaCore
                .getClasspathContainer(new Path("com.google.gwt.eclipse.core.GWT_CONTAINER"), javaProject);
        if (gwtClasspathContainer != null) {
            for (IClasspathEntry classpathEntry : gwtClasspathContainer.getClasspathEntries()) {
                if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                        && classpathEntry.getContentKind() == IPackageFragmentRoot.K_BINARY) {
                    IPath path = classpathEntry.getPath();
                    int segmentCount = path.segmentCount();
                    if (segmentCount >= 2) {
                        path = path.removeLastSegments(1).append("gwt-servlet.jar");
                        URI fileURI = URI.createFileURI(path.toOSString());
                        if (URIConverter.INSTANCE.exists(fileURI, null)) {
                            copy(fileURI, URI.createPlatformResourceURI(
                                    project.getName() + "/war/WEB-INF/lib/" + path.lastSegment(), true));
                        }
                    }
                }
            }
        }
        IClasspathContainer pdeClasspathContainer = JavaCore
                .getClasspathContainer(new Path("org.eclipse.pde.core.requiredPlugins"), javaProject);
        if (pdeClasspathContainer != null) {
            for (IClasspathEntry classpathEntry : pdeClasspathContainer.getClasspathEntries()) {
                if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                    IProject requiredProject = root.getProject(classpathEntry.getPath().segment(0));
                    IJavaProject requiredJavaProject = JavaCore.create(requiredProject);
                    IPath outputLocation = requiredJavaProject.getOutputLocation();
                    final int depth = outputLocation.segmentCount();
                    IFolder folder = root.getFolder(outputLocation);
                    folder.accept(new IResourceVisitor() {
                        public boolean visit(IResource resource) throws CoreException {
                            if (resource.getType() == IResource.FILE) {
                                IPath fullPath = resource.getFullPath();
                                copy(URI.createPlatformResourceURI(fullPath.toString(), true),
                                        URI.createPlatformResourceURI(project.getName()
                                                + "/war/WEB-INF/classes/" + fullPath.removeFirstSegments(depth),
                                                true));
                            }
                            return true;
                        }
                    }, IResource.DEPTH_INFINITE, 0);
                    result.add(requiredProject);
                } else if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                        && classpathEntry.getContentKind() == IPackageFragmentRoot.K_BINARY) {
                    IPath path = classpathEntry.getPath();
                    copy(URI.createFileURI(path.toOSString()), URI.createPlatformResourceURI(
                            project.getName() + "/war/WEB-INF/lib/" + path.lastSegment(), true));
                }
            }
        }
    }
    return result.toArray(new IProject[result.size()]);
}

From source file:org.eclipse.emf.ecore.xcore.ui.XcoreJavaProjectProvider.java

License:Open Source License

public ClassLoader getClassLoader(ResourceSet resourceSet) {
    IJavaProject project = getJavaProject(resourceSet);
    if (project != null) {
        IProject iProject = project.getProject();
        IWorkspaceRoot workspaceRoot = iProject.getWorkspace().getRoot();
        List<URL> libraryURLs = new UniqueEList<URL>();
        try {//  w  w  w .  j  av a2s .  c o  m
            getAllReferencedProjects(libraryURLs, new IProject[] { iProject });
            IClasspathEntry[] classpath = project.getResolvedClasspath(true);
            if (classpath != null) {
                String projectName = iProject.getName();
                for (int i = 0; i < classpath.length; ++i) {
                    IClasspathEntry classpathEntry = classpath[i];
                    switch (classpathEntry.getEntryKind()) {
                    case IClasspathEntry.CPE_LIBRARY:
                    case IClasspathEntry.CPE_CONTAINER: {
                        IPath path = classpathEntry.getPath();
                        if (path.segment(0).equals(projectName)) {
                            path = iProject.getLocation().append(path.removeFirstSegments(1));
                        }
                        libraryURLs.add(new URL(URI.createFileURI(path.toString()).toString()));
                        break;
                    }
                    case IClasspathEntry.CPE_PROJECT: {
                        IPath path = classpathEntry.getPath();
                        IProject referencedProject = workspaceRoot.getProject(path.segment(0));
                        IJavaProject referencedJavaProject = JavaCore.create(referencedProject);
                        IContainer container = workspaceRoot
                                .getFolder(referencedJavaProject.getOutputLocation());
                        libraryURLs.add(new URL(
                                URI.createFileURI(container.getLocation().toString() + "/").toString()));

                        IProjectDescription description = referencedProject.getDescription();
                        getAllReferencedProjects(libraryURLs, description.getReferencedProjects());
                        getAllReferencedProjects(libraryURLs, description.getDynamicReferences());
                        break;
                    }
                    case IClasspathEntry.CPE_SOURCE:
                    case IClasspathEntry.CPE_VARIABLE:
                    default: {
                        break;
                    }
                    }
                }
            }

            return new URLClassLoader(libraryURLs.toArray(new URL[libraryURLs.size()]),
                    getClass().getClassLoader());
        } catch (MalformedURLException exception) {
            exception.printStackTrace();
        } catch (JavaModelException exception) {
            exception.printStackTrace();
        } catch (CoreException exception) {
            exception.printStackTrace();
        }
    }
    for (Resource resource : resourceSet.getResources()) {
        URI uri = resource.getURI();
        if (uri.isPlatformPlugin()) {
            final Bundle bundle = Platform.getBundle(uri.segments()[1]);
            return new ClassLoader() {
                @Override
                public Enumeration<URL> findResources(String name) throws IOException {
                    return bundle.getResources(name);
                }

                @Override
                public URL findResource(String name) {
                    return bundle.getResource(name);
                }

                @Override
                public URL getResource(String name) {
                    return findResource(name);
                }

                @Override
                public Class<?> findClass(String name) throws ClassNotFoundException {
                    return bundle.loadClass(name);
                }

                @Override
                protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
                    Class<?> clazz = findClass(name);
                    if (resolve) {
                        resolveClass(clazz);
                    }
                    return clazz;
                }
            };
        }
    }
    return null;
}