Example usage for org.eclipse.jdt.core IJavaProject getResolvedClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject getResolvedClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getResolvedClasspath.

Prototype

IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry) throws JavaModelException;

Source Link

Document

This is a helper method returning the resolved classpath for the project as a list of simple (non-variable, non-container) classpath entries.

Usage

From source file:org.switchyard.tools.cxf.Activator.java

License:Open Source License

static ClassLoader getProjectBuildClassLoader(IJavaProject javaProject) throws Exception {
    IProject project = javaProject.getProject();
    IWorkspaceRoot root = project.getWorkspace().getRoot();
    List<URL> urls = new ArrayList<URL>();
    urls.add(//from  www .j a v a  2  s  .c om
            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) {
            IPath path = classpathEntry.getPath();
            if (!BLACKLIST.matcher(path.lastSegment()).find()) {
                urls.add(new File(path.toOSString()).toURI().toURL());
            }
        }
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), Activator.class.getClassLoader());
}

From source file:org.switchyard.tools.cxf.Java2WSDLOperation.java

License:Open Source License

private ClassLoader getProjectBuildClassLoader(IType interfaceType) throws Exception {
    IJavaProject javaProject = interfaceType.getJavaProject();
    IProject project = javaProject.getProject();
    IWorkspaceRoot root = project.getWorkspace().getRoot();
    List<URL> urls = new ArrayList<URL>();
    urls.add(/*w  w w. j a va2  s.  c  o m*/
            new File(project.getLocation() + "/" + javaProject.getOutputLocation().removeFirstSegments(1) + "/")
                    .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() + "/"
                    + otherJavaProject.getOutputLocation().removeFirstSegments(1) + "/").toURI().toURL());
        } else if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            urls.add(new File(classpathEntry.getPath().toOSString()).toURI().toURL());
        }
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
}

From source file:org.switchyard.tools.ui.JavaUtil.java

License:Open Source License

/**
 * Creates a ClassLoader using the project's build path.
 * /*w w  w . j av  a  2 s  .  c  om*/
 * @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.talend.librariesmanager.ui.actions.RemoveExternalJarAction.java

License:Open Source License

@Override
public void run() {
    BusyIndicator.showWhile(Display.getDefault(), new Runnable() {

        @Override//w  ww . ja  v  a2s.c om
        public void run() {
            for (ModuleNeeded module : modules) {
                try {
                    LibManagerUiPlugin.getDefault().getLibrariesService()
                            .undeployLibrary(module.getModuleName());
                } catch (Exception e) {
                    ExceptionHandler.process(e);
                }
            }
            if (GlobalServiceRegister.getDefault().isServiceRegistered(IRunProcessService.class)) {
                IRunProcessService processService = (IRunProcessService) GlobalServiceRegister.getDefault()
                        .getService(IRunProcessService.class);
                ITalendProcessJavaProject talendProcessJavaProject = processService
                        .getTalendProcessJavaProject();
                if (talendProcessJavaProject != null) {
                    IJavaProject javaProject = talendProcessJavaProject.getJavaProject();
                    List<IClasspathEntry> projectLibraries = new ArrayList<IClasspathEntry>();
                    try {
                        IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);
                        projectLibraries.addAll(Arrays.asList(resolvedClasspath));
                        for (ModuleNeeded module : modules) {
                            IClasspathEntry foundEntry = null;
                            for (IClasspathEntry entry : resolvedClasspath) {
                                if (entry.getPath().toPortableString().contains(module.getModuleName())) {
                                    foundEntry = entry;
                                    break;
                                }
                            }
                            if (foundEntry != null) {
                                projectLibraries.remove(foundEntry);
                            }
                        }
                        javaProject.setRawClasspath(
                                projectLibraries.toArray(new IClasspathEntry[projectLibraries.size()]), null);
                        setEnabled(false);
                    } catch (JavaModelException e) {
                        ExceptionHandler.process(e);
                    }
                }
            }

        }
    });
}

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;
        }/* w  w w.j  ava2  s  .  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 Set<IProject> getRelatedProjects() throws CoreException {
    Set<IProject> projects = new HashSet<>();
    Set<IProject> visited = new HashSet<>();
    Queue<IProject> queue = new ArrayDeque<>();
    queue.add(getProject());//from   w w  w  .  ja v  a2s.  c  om
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    while (!queue.isEmpty()) {
        IProject project = queue.remove();
        if (!visited.add(project) || !project.hasNature(JavaCore.NATURE_ID)) {
            continue;
        }
        projects.add(project);
        IJavaProject javaProject = JavaCore.create(project);
        for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                project = (IProject) root.findMember(entry.getPath());
                queue.add(project);
            }
        }
    }
    return projects;
}

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();/*from   w  w w  .  ja v a  2s .  com*/
    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:repast.simphony.agents.designer.core.AgentBuilderPlugin.java

License:Open Source License

/**
 * TODO//from  w  w  w  . j  a v a  2  s.c  o  m
 * 
 * @param project
 * @return
 * @throws JavaModelException
 */
public static IPath[] getClasspathSourceContainers(IProject project) throws JavaModelException {
    IJavaProject javaProject = AgentBuilderPlugin.getJavaProject(project);
    List folders = new ArrayList();
    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            folders.add(entry.getPath());
        }
    }
    return (IPath[]) folders.toArray(new Path[folders.size()]);
}

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 a v  a  2s.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:tod.plugin.TODPluginUtils.java

License:Open Source License

/**
 * Searches for declarations of the given name and kind in the whole workspace. 
 *//*from   w w  w  . j  av a2 s . co m*/
public static List searchDeclarations(List<IJavaProject> aJavaProject, String aName, int aKind)
        throws CoreException {
    // Recursively add referenced projects (because Eclipse doesn't recursively include non-exported referenced projects) 
    Set<IJavaProject> theProjects = new HashSet<IJavaProject>();
    Stack<IJavaProject> theWorkingList = new ArrayStack<IJavaProject>();
    for (IJavaProject theProject : aJavaProject)
        theWorkingList.push(theProject);

    while (!theWorkingList.isEmpty()) {
        IJavaProject theProject = theWorkingList.pop();
        theProjects.add(theProject);

        IJavaModel theJavaModel = theProject.getJavaModel();
        IClasspathEntry[] theEntries = theProject.getResolvedClasspath(true);
        for (IClasspathEntry theEntry : theEntries)
            if (theEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IJavaProject theReferencedProject = theJavaModel
                        .getJavaProject(theEntry.getPath().lastSegment());
                if (theReferencedProject.exists() && !theProjects.contains(theReferencedProject))
                    theWorkingList.push(theReferencedProject);
            }
    }

    SearchPattern thePattern = SearchPattern.createPattern(aName.replace('$', '.'), aKind,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);

    IJavaSearchScope theScope = SearchEngine
            .createJavaSearchScope(theProjects.toArray(new IJavaElement[theProjects.size()]), true);

    SearchEngine theSearchEngine = new SearchEngine();
    SimpleResultCollector theCollector = new SimpleResultCollector();

    theSearchEngine.search(thePattern, PARTICIPANTS, theScope, theCollector, null);

    return theCollector.getResults();
}