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

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

Introduction

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

Prototype

int CPE_LIBRARY

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

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a library.

Usage

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) {//w  ww .  j  a v  a  2  s.c  o  m
    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;/*  w  w  w . j ava2  s.c om*/
    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.e4.xwt.tools.ui.designer.jdt.ProjectHelper.java

License:Open Source License

protected static boolean containsJar(IJavaProject javaProject, IClasspathEntry entry, String jarName) {
    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_VARIABLE:
        IClasspathEntry resolvedEntry = JavaCore.getJavaCore().getResolvedClasspathEntry(entry);
        IPath resolvedPath = resolvedEntry.getPath();
        String string = resolvedPath.toString();
        if (string.indexOf(jarName) != -1) {
            return true;
        }//  w  ww  . j a  v  a 2 s.  c o m
        break;
    case IClasspathEntry.CPE_CONTAINER:
        try {
            IPath path = entry.getPath();
            IClasspathContainer classpathContainer = JavaCore.getJavaCore().getClasspathContainer(path,
                    javaProject);
            if (classpathContainer != null) {
                classpathContainer.getClasspathEntries();
                IClasspathEntry[] oldclasspath = classpathContainer.getClasspathEntries();
                for (int i = 0; i < oldclasspath.length; i++) {
                    if (containsJar(javaProject, oldclasspath[i], jarName)) {
                        return true;
                    }
                }
            }
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    case IClasspathEntry.CPE_SOURCE:
    case IClasspathEntry.CPE_LIBRARY:
        IPath path = entry.getPath();
        String value = path.toString();
        if (value.indexOf(jarName) != -1) {
            return true;
        }
    }

    return false;
}

From source file:org.eclipse.e4.xwt.ui.utils.LibraryBuilder.java

License:Open Source License

private void updateJavaClasspathLibs(String[] oldPaths, String[] newPaths) {
    IJavaProject jproject = JavaCore.create(project);
    try {/*from  w  w  w .  j  av a  2  s. c  o m*/
        IClasspathEntry[] entries = jproject.getRawClasspath();
        ArrayList toBeAdded = new ArrayList();
        int index = -1;
        entryLoop: for (int i = 0; i < entries.length; i++) {
            if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                if (index == -1)
                    index = i;
                // do not add the old paths (handling deletion/renaming)
                IPath path = entries[i].getPath().removeFirstSegments(1).removeTrailingSeparator();
                for (int j = 0; j < oldPaths.length; j++)
                    if (oldPaths[j] != null && path.equals(new Path(oldPaths[j]).removeTrailingSeparator()))
                        continue entryLoop;
            } else if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                if (index == -1)
                    index = i;
            toBeAdded.add(entries[i]);
        }
        if (index == -1)
            index = entries.length;

        // add paths
        for (int i = 0; i < newPaths.length; i++) {
            if (newPaths[i] == null)
                continue;
            IClasspathEntry entry = JavaCore.newLibraryEntry(project.getFullPath().append(newPaths[i]), null,
                    null, true);
            if (!toBeAdded.contains(entry))
                toBeAdded.add(index++, entry);
        }

        if (toBeAdded.size() == entries.length)
            return;

        IClasspathEntry[] updated = (IClasspathEntry[]) toBeAdded
                .toArray(new IClasspathEntry[toBeAdded.size()]);
        jproject.setRawClasspath(updated, null);
    } catch (JavaModelException e) {
    }
}

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 w ww  . java2s. co  m
            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);
                            }/*www.  j  a  v a 2 s .com*/
                            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.emf.codegen.ecore.gwt.GWTBuilder.java

License:Open Source License

@SuppressWarnings("rawtypes")
@Override//  w w w .  ja  v  a 2  s .c  o  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.quickfix.XcoreClasspathUpdater.java

License:Open Source License

protected boolean addJarToClasspath(IJavaProject javaProject, IPath location, IProgressMonitor monitor)
        throws JavaModelException {
    IClasspathEntry newClasspthEntry = JavaCore.newLibraryEntry(location, null, null);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    IClasspathEntry[] newRawClasspath = new IClasspathEntry[rawClasspath.length + 1];
    for (int i = 0; i < rawClasspath.length; ++i) {
        IClasspathEntry entry = rawClasspath[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(location)) {
            return false;
        }//from   www  .j  a va 2  s  .  c  o  m
        newRawClasspath[i + 1] = entry;
    }
    newRawClasspath[0] = newClasspthEntry;
    javaProject.setRawClasspath(newRawClasspath, monitor);
    return true;
}

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 {/*from  ww w . j a va 2  s  . 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;
}

From source file:org.eclipse.emf.java.presentation.JavaEditor.java

License:Open Source License

public void setupClassLoader(IProject project) {
    JavaPackageResourceImpl javaPackageResource = (JavaPackageResourceImpl) editingDomain
            .loadResource(JavaUtil.JAVA_PACKAGE_RESOURCE);

    IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();

    List<URL> libraryURLs = new UniqueEList<URL>();
    List<String> sourceURIs = new ArrayList<String>();
    try {/* ww w .j a  v a  2  s .  com*/
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] classpath = javaProject.getResolvedClasspath(true);
        if (classpath != null) {
            for (int i = 0; i < classpath.length; ++i) {
                IClasspathEntry classpathEntry = classpath[i];
                switch (classpathEntry.getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                case IClasspathEntry.CPE_CONTAINER: {
                    libraryURLs.add(new URL(URI.createFileURI(classpathEntry.getPath().toString()).toString()));
                    break;
                }
                case IClasspathEntry.CPE_SOURCE: {
                    sourceURIs.add(
                            URI.createPlatformResourceURI(classpathEntry.getPath().toString(), true) + "/");
                    break;
                }
                case IClasspathEntry.CPE_PROJECT: {
                    IProject referencedProject = workspaceRoot.getProject(classpathEntry.getPath().segment(0));
                    IJavaProject referencedJavaProject = JavaCore.create(referencedProject);
                    IContainer container = workspaceRoot.getFolder(referencedJavaProject.getOutputLocation());
                    libraryURLs.add(
                            new URL(URI.createFileURI(container.getLocation().toString() + "/").toString()));

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

        javaPackageResource.setClassLoader(new URLClassLoader(libraryURLs.toArray(new URL[libraryURLs.size()]),
                new URLClassLoader(new URL[0], null)));
        javaPackageResource.getSourceURIs().addAll(sourceURIs);
    } catch (MalformedURLException exception) {
        exception.printStackTrace();
    } catch (JavaModelException exception) {
        exception.printStackTrace();
    } catch (CoreException exception) {
        exception.printStackTrace();
    }
}