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.switchyard.tools.ui.JavaUtil.java

License:Open Source License

/**
 * Creates a ClassLoader using the project's build path.
 * /*from  ww  w. j av a 2  s  .  c  o  m*/
 * @param javaProject the Java project.
 * @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<URL>();
    urls.add(
            new File(project.getLocation() + "/" + javaProject.getOutputLocation().removeFirstSegments(1) + "/") //$NON-NLS-1$ //$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()]));
    } else {
        return new URLClassLoader(urls.toArray(new URL[urls.size()]), parentClassLoader);
    }
}

From source file:org.teavm.eclipse.debugger.TeaVMSourcePathComputerDelegate.java

License:Apache License

@Override
public ISourceContainer[] computeSourceContainers(ILaunchConfiguration config, IProgressMonitor monitor)
        throws CoreException {
    List<ISourceContainer> sourceContainers = new ArrayList<>();
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for (IProject project : projects) {
        if (!project.isOpen()) {
            continue;
        }//from   www  .  j av a2s.  c  o  m
        if (project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(project);
            for (IPackageFragmentRoot fragmentRoot : javaProject.getAllPackageFragmentRoots()) {
                if (fragmentRoot.getResource() instanceof IFolder) {
                    sourceContainers.add(new FolderSourceContainer((IFolder) fragmentRoot.getResource(), true));
                }
            }
            for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
                switch (entry.getEntryKind()) {
                case IClasspathEntry.CPE_CONTAINER:
                    sourceContainers.add(new ClasspathContainerSourceContainer(entry.getPath()));
                    break;
                case IClasspathEntry.CPE_LIBRARY:
                    sourceContainers.add(new ExternalArchiveSourceContainer(entry.getPath().toString(), true));
                    if (entry.getSourceAttachmentPath() != null) {
                        System.out.println(entry.getSourceAttachmentPath());
                        sourceContainers.add(new ExternalArchiveSourceContainer(
                                entry.getSourceAttachmentPath().toString(), true));
                        sourceContainers
                                .add(new DirectorySourceContainer(entry.getSourceAttachmentPath(), true));
                    }
                    break;
                case IClasspathEntry.CPE_SOURCE:
                    sourceContainers.add(new DirectorySourceContainer(entry.getPath(), true));
                    break;
                }
            }
        }
    }
    IRuntimeClasspathEntry[] entries = JavaRuntime.computeUnresolvedSourceLookupPath(config);
    IRuntimeClasspathEntry[] resolved = JavaRuntime.resolveSourceLookupPath(entries, config);
    sourceContainers.addAll(Arrays.asList(JavaRuntime.getSourceContainers(resolved)));
    return sourceContainers.toArray(new ISourceContainer[sourceContainers.size()]);
}

From source file:org.teavm.eclipse.TeaVMProjectBuilder.java

License:Apache License

private void prepareClassPath() throws CoreException {
    classPath = new URL[0];
    sourceContainers = new IContainer[0];
    sourceProviders = new SourceFileProvider[0];
    IProject project = getProject();/* w ww.  j  a va  2s .  c  o m*/
    if (!project.hasNature(JavaCore.NATURE_ID)) {
        return;
    }
    IJavaProject javaProject = JavaCore.create(project);
    PathCollector collector = new PathCollector();
    SourcePathCollector srcCollector = new SourcePathCollector();
    SourcePathCollector binCollector = new SourcePathCollector();
    SourceFileCollector sourceFileCollector = new SourceFileCollector();
    IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();
    try {
        if (javaProject.getOutputLocation() != null) {
            IContainer container = (IContainer) workspaceRoot.findMember(javaProject.getOutputLocation());
            collector.addPath(container.getLocation());
            binCollector.addContainer(container);
        }
    } catch (MalformedURLException e) {
        TeaVMEclipsePlugin.logError(e);
    }
    Queue<IJavaProject> projectQueue = new ArrayDeque<>();
    projectQueue.add(javaProject);
    Set<IJavaProject> visitedProjects = new HashSet<>();
    while (!projectQueue.isEmpty()) {
        javaProject = projectQueue.remove();
        if (!visitedProjects.add(javaProject)) {
            continue;
        }
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                try {
                    collector.addPath(entry.getPath());
                } catch (MalformedURLException e) {
                    TeaVMEclipsePlugin.logError(e);
                }
                if (entry.getSourceAttachmentPath() != null) {
                    sourceFileCollector.addFile(entry.getSourceAttachmentPath());
                }
                break;
            case IClasspathEntry.CPE_SOURCE:
                if (entry.getOutputLocation() != null) {
                    try {
                        IResource res = workspaceRoot.findMember(entry.getOutputLocation());
                        if (res != null) {
                            collector.addPath(res.getLocation());
                        }
                    } catch (MalformedURLException e) {
                        TeaVMEclipsePlugin.logError(e);
                    }
                }
                IContainer srcContainer = (IContainer) workspaceRoot.findMember(entry.getPath());
                if (srcContainer != null) {
                    srcCollector.addContainer(srcContainer);
                    sourceFileCollector.addFile(srcContainer.getLocation());
                    try {
                        collector.addPath(srcContainer.getLocation());
                    } catch (MalformedURLException e) {
                        TeaVMEclipsePlugin.logError(e);
                    }
                }
                break;
            case IClasspathEntry.CPE_PROJECT: {
                IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
                IProject depProject = resource.getProject();
                if (!depProject.hasNature(JavaCore.NATURE_ID)) {
                    break;
                }
                IJavaProject depJavaProject = JavaCore.create(depProject);
                if (depJavaProject.getOutputLocation() != null) {
                    try {
                        IContainer container = (IContainer) workspaceRoot
                                .findMember(depJavaProject.getOutputLocation());
                        if (container != null) {
                            collector.addPath(container.getLocation());
                            binCollector.addContainer(container);
                        }
                    } catch (MalformedURLException e) {
                        TeaVMEclipsePlugin.logError(e);
                    }
                }
                projectQueue.add(depJavaProject);
                break;
            }
            }
        }
    }
    classPath = collector.getUrls();
    sourceContainers = srcCollector.getContainers();
    classFileContainers = binCollector.getContainers();
    sourceProviders = sourceFileCollector.getProviders();
}

From source file:org.wso2.developerstudio.eclipse.utils.jdt.JavaUtils.java

License:Open Source License

public static IPackageFragmentRoot[] getReferencedLibrariesForProject(IProject project)
        throws JavaModelException {
    IJavaProject p = JavaCore.create(project);
    IPackageFragmentRoot[] packageFragmentRoots = null;
    if (p != null) {
        packageFragmentRoots = p.getPackageFragmentRoots();
    }/* w w  w  .  j a  va  2 s .c om*/

    ArrayList<IPackageFragmentRoot> jarClassPaths = new ArrayList<IPackageFragmentRoot>();
    if (packageFragmentRoots != null) {
        for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
            if (isWebApp && packageFragmentRoot.isArchive()) {
                if (packageFragmentRoot.getRawClasspathEntry().getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    jarClassPaths.add(packageFragmentRoot);
                }

            } else if (!isWebApp) {
                if (packageFragmentRoot.getRawClasspathEntry().getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    jarClassPaths.add(packageFragmentRoot);
                }
            }
        }
    }
    return jarClassPaths.toArray(new IPackageFragmentRoot[] {});
}

From source file:org.wso2.developerstudio.eclipse.utils.jdt.JavaUtils.java

License:Open Source License

public static IPackageFragmentRoot[] getReferencedVariableLibrariesForProject(IProject project)
        throws JavaModelException {
    IJavaProject p = JavaCore.create(project);
    IPackageFragmentRoot[] packageFragmentRoots = p.getPackageFragmentRoots();
    ArrayList<IPackageFragmentRoot> jarClassPaths = new ArrayList<IPackageFragmentRoot>();
    for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
        if (isWebApp && packageFragmentRoot.isArchive()) {
            IClasspathEntry rawClasspathEntry = packageFragmentRoot.getRawClasspathEntry();
            IClasspathEntry resolvedClasspathEntry = JavaCore.getResolvedClasspathEntry(rawClasspathEntry);
            if (rawClasspathEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE
                    && resolvedClasspathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                jarClassPaths.add(packageFragmentRoot);
            }//w ww.j a  va  2s . c o  m
        } else if (!isWebApp) {
            IClasspathEntry rawClasspathEntry = packageFragmentRoot.getRawClasspathEntry();
            IClasspathEntry resolvedClasspathEntry = JavaCore.getResolvedClasspathEntry(rawClasspathEntry);
            if (rawClasspathEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE
                    && resolvedClasspathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                jarClassPaths.add(packageFragmentRoot);
            }
        }
    }
    return jarClassPaths.toArray(new IPackageFragmentRoot[] {});
}

From source file:pt.iscte.dcti.expressionsview.ProjectClassLoader.java

License:Open Source License

public static boolean existsInLibrary(IProject project, String name) {
    try {/*w  ww  .  ja va 2  s.  c  om*/
        IJavaProject javaProj = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
        IClasspathEntry[] classpath = javaProj.getRawClasspath();
        for (IClasspathEntry entry : classpath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IPath p = entry.getPath();
                File f = p.append(name.replace('.', '/') + ".class").toFile();
                if (f.exists())
                    return true;
            }
        }
    } catch (CoreException e1) {
        e1.printStackTrace();
    }
    return false;
}

From source file:pt.iscte.dcti.expressionsview.ProjectClassLoader.java

License:Open Source License

public Class<?> loadClass(String name) throws ClassNotFoundException {
    if (name.startsWith("java.") || name.startsWith("sun."))
        return getParent().loadClass(name);

    if (map.containsKey(name))
        return map.get(name);

    byte[] classData = null;
    try {//from   w  w  w  .  jav  a  2  s  .  c  o  m
        File file = project.getLocation().append("bin").append(name.replace('.', '/') + ".class").toFile();
        if (file.exists())
            classData = getBytesFromFile(file);
        else {

            IJavaProject javaProj = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
            try {
                IClasspathEntry[] classpath = javaProj.getRawClasspath();
                for (IClasspathEntry entry : classpath) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPath p = entry.getPath();
                        File f = p.append(name.replace('.', '/') + ".class").toFile();
                        if (f.exists()) {
                            classData = getBytesFromFile(f);
                        }
                    }
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }

        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (classData == null)
        throw new ClassNotFoundException(name);

    Class<?> clazz = defineClass(name, classData, 0, classData.length);

    if (!map.containsKey(name))
        map.put(name, clazz);

    return clazz;
}

From source file:repast.simphony.agents.designer.core.AgentBuilderPlugin.java

License:Open Source License

/**
 * Adds classpath entries to the supported list.
 * //from  ww  w .j  av  a 2 s.c o m
 * @param projectName
 * @param pathEntries
 */
public static void getResolvedClasspath(String projectName, List pathEntries) {

    IJavaProject javaProject = AgentBuilderPlugin.getJavaProject(projectName);
    IPath path = null;
    try {
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            path = null;
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                path = getWorkspaceRoot().getLocation()
                        .append(JavaCore.getResolvedClasspathEntry(entry).getPath());
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                path = JavaCore.getResolvedClasspathEntry(entry).getPath();
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                path = entry.getPath().makeAbsolute();
                if (!path.toFile().getAbsoluteFile().exists()) {
                    IPath location = getWorkspaceRoot().getProject(entry.getPath().segment(0))
                            .getFile(entry.getPath().removeFirstSegments(1)).getLocation();
                    if (location != null) {
                        File tmpFile = location.toFile();
                        if (tmpFile.exists())
                            path = location;
                    }
                }
            }
            if (path != null && !pathEntries.contains(path))
                pathEntries.add(path);

            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject requiredProject = getWorkspaceProject(entry.getPath());
                // recurse into projects
                if (requiredProject != null)
                    getResolvedClasspath(requiredProject, pathEntries);
            }
        }
        IPath outputPath = javaProject.getOutputLocation();
        if (outputPath.segmentCount() == 1)
            outputPath = javaProject.getResource().getLocation();
        else
            outputPath = javaProject.getProject().getFile(outputPath.removeFirstSegments(1)).getLocation();
        if (outputPath != null && !pathEntries.contains(outputPath))
            pathEntries.add(outputPath);
    } catch (JavaModelException e) {
        AgentBuilderPlugin.log(e);
    }
}

From source file:runjettyrun.tabs.action.AddClassFolderAction.java

License:Open Source License

/**
 * Adds all exported entries defined by <code>proj</code> to the list
 * <code>runtimeEntries</code>.
 *
 * @param proj/*from  ww  w .  ja va 2  s .  co  m*/
 * @param runtimeEntries
 * @throws JavaModelException
 */
protected void collectExportedEntries(IJavaProject proj, List<IRuntimeClasspathEntry> runtimeEntries)
        throws CoreException {
    IClasspathEntry[] entries = proj.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.isExported()) {
            IRuntimeClasspathEntry rte = null;
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_CONTAINER:
                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), proj);
                int kind = 0;
                switch (container.getKind()) {
                case IClasspathContainer.K_APPLICATION:
                    kind = IRuntimeClasspathEntry.USER_CLASSES;
                    break;
                case IClasspathContainer.K_SYSTEM:
                    kind = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES;
                    break;
                case IClasspathContainer.K_DEFAULT_SYSTEM:
                    kind = IRuntimeClasspathEntry.STANDARD_CLASSES;
                    break;
                }
                rte = JavaRuntime.newRuntimeContainerClasspathEntry(entry.getPath(), kind, proj);
                break;
            case IClasspathEntry.CPE_LIBRARY:
                rte = JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath());
                rte.setSourceAttachmentPath(entry.getSourceAttachmentPath());
                rte.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath());
                break;
            case IClasspathEntry.CPE_PROJECT:
                String name = entry.getPath().segment(0);
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
                if (p.exists()) {
                    IJavaProject jp = JavaCore.create(p);
                    if (jp.exists()) {
                        rte = JavaRuntime.newProjectRuntimeClasspathEntry(jp);
                    }
                }
                break;
            case IClasspathEntry.CPE_VARIABLE:
                rte = JavaRuntime.newVariableRuntimeClasspathEntry(entry.getPath());
                break;
            default:
                break;
            }
            if (rte != null) {
                if (!runtimeEntries.contains(rte)) {
                    runtimeEntries.add(rte);
                }
            }
        }
    }
}

From source file:x10dt.search.core.pdb.X10FactGenerator.java

License:Open Source License

private void processEntries(final CompilerOptionsBuilder cmpOptBuilder, final IWorkspaceRoot wsRoot,
        final IClasspathEntry[] entries, final IJavaProject javaProject, final IResource contextResource,
        final boolean isInRuntime) throws JavaModelException, AnalysisException {
    for (final IClasspathEntry pathEntry : entries) {
        switch (pathEntry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (pathEntry.getPath().isRoot()) {
                cmpOptBuilder.addToSourcePath(pathEntry.getPath().toFile());
            } else {
                cmpOptBuilder.addToSourcePath(wsRoot.getLocation().append(pathEntry.getPath()).toFile());
            }//from  w ww  .  j  ava2 s.  c o m
            if (pathEntry.getPath().segmentCount() > 1) {
                processSourceFolder(cmpOptBuilder, wsRoot.getFolder(pathEntry.getPath()), contextResource);
            }
            break;

        case IClasspathEntry.CPE_LIBRARY:
            try {
                final IPackageFragmentRoot pkgRoot = javaProject.findPackageFragmentRoot(pathEntry.getPath());
                if ((pkgRoot != null) && pkgRoot.exists()) {
                    final File localFile;
                    if (pkgRoot.isExternal()) {
                        localFile = pathEntry.getPath().toFile();
                    } else {
                        localFile = pkgRoot.getResource().getLocation().toFile();
                    }
                    cmpOptBuilder.addToClassPath(localFile.getAbsolutePath());
                    if (isInRuntime) {
                        cmpOptBuilder.addToSourcePath(localFile);
                    }
                    final ZipFile zipFile;
                    if (JAR_EXT.equals(pathEntry.getPath().getFileExtension())) {
                        zipFile = new JarFile(localFile);
                    } else {
                        zipFile = new ZipFile(localFile);
                    }
                    processLibrary(cmpOptBuilder, zipFile, localFile, contextResource, isInRuntime);
                }
            } catch (IOException except) {
                throw new AnalysisException(NLS.bind(Messages.XFG_JarReadingError, pathEntry.getPath()),
                        except);
            }
            break;

        case IClasspathEntry.CPE_CONTAINER:
            final IClasspathContainer cpContainer = JavaCore.getClasspathContainer(pathEntry.getPath(),
                    javaProject);
            processEntries(cmpOptBuilder, wsRoot, cpContainer.getClasspathEntries(), javaProject,
                    contextResource, true);
            break;

        case IClasspathEntry.CPE_PROJECT:
            final IResource projectResource = ResourcesPlugin.getWorkspace().getRoot()
                    .findMember(pathEntry.getPath());
            if ((projectResource != null) && projectResource.isAccessible()) {
                final IJavaProject newJavaProject = JavaCore.create((IProject) projectResource);
                processEntries(cmpOptBuilder, wsRoot, newJavaProject.getRawClasspath(), newJavaProject,
                        contextResource, false);
            }
            break;

        case IClasspathEntry.CPE_VARIABLE:
            processEntries(cmpOptBuilder, wsRoot,
                    new IClasspathEntry[] { JavaCore.getResolvedClasspathEntry(pathEntry) }, javaProject,
                    contextResource, false);
            break;
        }
    }
}