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.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;/*from  ww  w .  java 2 s  .com*/
    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);
            }/* w w w .jav  a  2s . c  o  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.internal.core.java.SMAPBuilder.java

License:Open Source License

private void buildAll() throws CoreException {
    // Performance: only visit the bin directories.
    IJavaProject project = JavaCore.create(getProject());
    IClasspathEntry[] cp = project.getResolvedClasspath(true);

    Set<IPath> binDirectories = new HashSet<IPath>(cp.length);
    for (IClasspathEntry entry : cp) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = entry.getOutputLocation();
            if (outputLocation != null) {
                binDirectories.add(outputLocation);
            }/*  ww w.  ja  va 2s .c  o  m*/
        }
    }

    IPath outputLocation = project.getOutputLocation();
    if (outputLocation != null) {
        binDirectories.add(outputLocation);
    }

    ResourceVisitor visitor = new ResourceVisitor();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (IPath path : binDirectories) {
        IResource resource = root.findMember(path);
        if (resource != null) {
            resource.accept(visitor);
        }
    }
}

From source file:org.eclipse.edt.ide.deployment.services.generators.ServiceUriMappingGenerator.java

License:Open Source License

private static String createClasspath(IJavaProject javaProject)
        throws JavaModelException, MalformedURLException, URISyntaxException {
    StringBuffer classpath = new StringBuffer();
    String path;/*w w  w .  ja  va2s.c  o m*/
    IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);
    for (int i = 0; i < resolvedClasspath.length; i++) {
        IClasspathEntry entry = resolvedClasspath[i];
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            path = entry.getPath().toPortableString();
        } else {
            if (entry.getOutputLocation() != null) {
                path = javaProject.getProject().getLocation().removeLastSegments(1)
                        .append(entry.getOutputLocation()).toPortableString();
            } else {
                path = javaProject.getProject().getLocation().removeLastSegments(1)
                        .append(javaProject.getOutputLocation()).toPortableString();
            }
        }
        classpath.append(path);
        classpath.append(";");
    }
    return classpath.toString();
}

From source file:org.eclipse.edt.ide.ui.internal.deployment.ui.DeploymentUtilities.java

License:Open Source License

public static void getJavaSourceFolders(IProject sourceProject, List<IResource> resources)
        throws CoreException, JavaModelException {

    //for the source project get all of it's Java source folders
    //and include java folders in this project classpath
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IJavaProject javaProject = JavaCore.create(sourceProject);
    if (PlatformUI.isWorkbenchRunning() && javaProject.exists() && javaProject.isOpen()
            || !PlatformUI.isWorkbenchRunning() && javaProject.exists()) {
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        if (entries != null && entries.length > 0) {
            for (int idx = entries.length - 1; idx >= 0; idx--) {
                if (entries[idx].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IResource element = root.findMember(entries[idx].getPath());

                    if (element != null && element.exists()) {
                        resources.add(element);
                    }// w w  w.j a va2s  .c o m
                }
            }
        }
    }
}

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.ja v a  2s.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 {//from w w  w .  ja  v a2  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();
    }
}

From source file:org.eclipse.fx.ide.css.cssext.ui.adapter.ClasspathManager.java

License:Open Source License

private List<URI> scanClasspath(IProject project) {
    getLogger().debug("scanClasspath(" + project + ")");
    List<ClassPathSearchUtil.Entry> allFiles = new ArrayList<>();
    try {/*  ww w  .jav a  2 s. co m*/
        final IContainer workspace = project.getParent();
        final IJavaProject javaProject = JavaCore.create(project);
        final IWorkspaceRoot root = workspace.getWorkspace().getRoot();

        long locCount = 0;
        IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);

        allFiles.addAll(ClassPathSearchUtil.checkEntries(root, resolvedClasspath));
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    //         System.err.println("found : " + entries);

    //         HashSet externalFolders = ExternalFoldersManager.getExternalFolders(resolvedClasspath);
    ////         System.err.println("externalFolders " + externalFolders);
    //         for (IClasspathEntry e : resolvedClasspath) {
    ////            System.err.println(e);
    //            locCount++;
    //            switch (e.getEntryKind()) {
    //            case IClasspathEntry.CPE_SOURCE: {
    //               final IResource resource = workspace.findMember(e.getPath());
    //               final List<ClassPathSearchUtil.Entry> result = ClassPathSearchUtil.checkResource(resource);
    //               allFiles.addAll(result);
    //            }
    //            break;
    //            case IClasspathEntry.CPE_LIBRARY: {
    //               final IPath path = e.getPath();
    //               java.net.URI lU = null;
    //               String libUri = "file://" + path.toFile().getAbsolutePath(); // = external representation
    ////               System.err.println("cp path: " + path);
    //               
    //               IFolder folder = ExternalFoldersManager.getExternalFoldersManager().getFolder(path);
    ////               System.err.println("external Folder: " + folder);
    //               if (folder != null) {
    ////                  System.err.println(" .location = " + folder.getLocation());
    ////                  System.err.println(" .locationURI = " + folder.getLocationURI());
    ////                  System.err.println(" .fullPath = " + folder.getFullPath());
    //                  libUri = folder.getLocationURI().toString();
    //                  //libUri = "platform://resource" + folder.getFullPath();
    //                  lU = folder.getLocationURI();
    //               }
    //               
    ////               System.err.println("Using libUri = " + libUri);
    //               
    //               if ("jar".equals(e.getPath().getFileExtension())) {
    //                  IFile f = root.getFile(path);
    //                  if (f.exists()) {
    //                     List<ClassPathSearchUtil.Entry> result = ClassPathSearchUtil.checkJar(f.getLocation().toFile().getAbsolutePath(), f.getLocationURI().toString());
    //                     allFiles.addAll(result);
    //                  }
    //                  else {
    //                     List<ClassPathSearchUtil.Entry> result = ClassPathSearchUtil.checkJar(path.toFile().getAbsolutePath(), libUri);
    //                     allFiles.addAll(result);
    //                  }
    //               }
    //               else {
    //                  
    //                  IPath binPath = path.append("bin");
    //                  if (binPath.toFile().exists()) {
    //                     // try bin path
    //                     List<ClassPathSearchUtil.Entry> result = ClassPathSearchUtil.checkFolder(binPath.toFile().getAbsolutePath(), libUri + "/bin");
    //                     allFiles.addAll(result);
    //                  }
    //                  else {
    //                     // try whole folder
    //                     List<ClassPathSearchUtil.Entry> result = ClassPathSearchUtil.checkFolder(path.toFile().getAbsolutePath(), libUri);
    //                     allFiles.addAll(result);
    //                  }
    //                  
    //               }
    //            }
    //            break;
    //            case IClasspathEntry.CPE_PROJECT: {
    //               final IPath path = e.getPath();
    //               final IProject p = (IProject) project.getParent().findMember(path);
    //               final IJavaProject jp = JavaCore.create(p);
    //               final IResource output = project.getParent().findMember(jp.getOutputLocation());
    //               final List<ClassPathSearchUtil.Entry> result = ClassPathSearchUtil.checkResource(output);
    //               allFiles.addAll(result);
    //            }
    //            break;
    //               
    //            default: {
    //               getLogger().warning("entry not handled! " + e);
    //            }
    //               
    //            }
    //            
    //            
    //            
    //         }
    //         getLogger().debug("Checked " + locCount + " entries and found " + allFiles.size() + " cssext definitions: " + allFiles);
    //      }
    //      catch (JavaModelException e) {
    //         e.printStackTrace();
    //      }

    List<URI> extensions = new ArrayList<>();
    for (ClassPathSearchUtil.Entry entry : allFiles) {
        // load model
        final URI uri = entry.toURI();
        extensions.add(uri);
    }
    getLogger().debug(" => " + extensions);
    return extensions;
}

From source file:org.eclipse.imp.asl.compiler.ASLCompiler.java

License:Open Source License

private IPath findSrcFolderRelativePath(IFile file, IJavaProject javaProject) throws JavaModelException {
    final IPath folderPath = file.getFullPath().removeLastSegments(1);
    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);

    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            final IPath entryPath = entry.getPath();
            if (entryPath.isPrefixOf(folderPath)) {
                int segCount = entryPath.matchingFirstSegments(folderPath);
                return folderPath.removeFirstSegments(segCount);
            }/* w  ww .  j a  v a 2 s .  c om*/
        }
    }
    return null;
}

From source file:org.eclipse.imp.java.hosted.ProjectUtils.java

License:Open Source License

public void addExtenderForJavaHostedProjects(Language lang) {
    ModelFactory.getInstance().installExtender(new IFactoryExtender() {
        public void extend(ISourceProject project) {
            initializeBuildPathFromJavaProject(project);
        }/* w  ww.ja  v a2 s.c o m*/

        public void extend(ICompilationUnit unit) {
        }

        /**
         * Read the IJavaProject classpath configuration and populate the ISourceProject's
         * build path accordingly.
         */
        public void initializeBuildPathFromJavaProject(ISourceProject project) {
            IJavaProject javaProject = JavaCore.create(project.getRawProject());
            if (javaProject.exists()) {
                try {
                    IClasspathEntry[] cpEntries = javaProject.getResolvedClasspath(true);
                    List<IPathEntry> buildPath = new ArrayList<IPathEntry>(cpEntries.length);
                    for (int i = 0; i < cpEntries.length; i++) {
                        IClasspathEntry entry = cpEntries[i];
                        IPathEntry.PathEntryType type;
                        IPath path = entry.getPath();

                        switch (entry.getEntryKind()) {
                        case IClasspathEntry.CPE_CONTAINER:
                            type = PathEntryType.CONTAINER;
                            break;
                        case IClasspathEntry.CPE_LIBRARY:
                            type = PathEntryType.ARCHIVE;
                            break;
                        case IClasspathEntry.CPE_PROJECT:
                            type = PathEntryType.PROJECT;
                            break;
                        case IClasspathEntry.CPE_SOURCE:
                            type = PathEntryType.SOURCE_FOLDER;
                            break;
                        default:
                            // case IClasspathEntry.CPE_VARIABLE:
                            throw new IllegalArgumentException("Encountered variable class-path entry: "
                                    + entry.getPath().toPortableString());
                        }
                        IPathEntry pathEntry = ModelFactory.createPathEntry(type, path);
                        buildPath.add(pathEntry);
                    }
                    project.setBuildPath(buildPath);
                } catch (JavaModelException e) {
                    ErrorHandler.reportError(e.getMessage(), e);
                }
            }
        }
    }, lang);
}