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.hibernate.eclipse.console.utils.ClassLoaderHelper.java

License:Open Source License

static public List<URL> getProjectClassPathURLs(IJavaProject project) {
    List<URL> pathElements = new ArrayList<URL>();

    try {/*ww  w .ja v a 2 s. c  o  m*/
        IClasspathEntry paths[] = project.getResolvedClasspath(true);

        if (paths != null) {
            for (int i = 0; i < paths.length; i++) {
                IClasspathEntry path = paths[i];
                if (path.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    IPath simplePath = path.getPath();
                    URL url = getRawLocationURL(simplePath);

                    pathElements.add(url);
                }
            }
        }

        IPath location = getProjectLocation(project.getProject());
        IPath outputPath = location.append(project.getOutputLocation().removeFirstSegments(1));

        pathElements.add(outputPath.toFile().toURI().toURL());
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    return pathElements;
}

From source file:org.jactr.eclipse.ui.wizards.templates.DefaultACTRContributorTemplateSection.java

License:Open Source License

/**
 * stupid cosmetic bit to reorder the classpath entries so that models/,
 * java/, conf/ appear first//from  w  ww . jav  a 2s . c o m
 * 
 * @throws CoreException
 */
private void sortEntries() throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);

    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    SortedMap<String, IClasspathEntry> sortedMap = new TreeMap<String, IClasspathEntry>(
            Collections.reverseOrder());

    for (IClasspathEntry entry : oldEntries) {
        String name = entry.getPath().lastSegment();
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE
                && entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY)
            name = "a" + name;
        sortedMap.put(name, entry);
    }

    oldEntries = sortedMap.values().toArray(oldEntries);
    javaProject.setRawClasspath(oldEntries, null);
}

From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java

License:Open Source License

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

From source file:org.jboss.tools.arquillian.core.internal.builder.ArquillianBuilder.java

License:Open Source License

private IProject[] getRequiredProjects(boolean includeBinaryPrerequisites) {
    JavaProject javaProject = (JavaProject) JavaCore.create(this.currentProject);
    IWorkspaceRoot workspaceRoot = this.currentProject.getWorkspace().getRoot();
    if (javaProject == null || workspaceRoot == null)
        return NO_PROJECTS;

    ArrayList projects = new ArrayList();
    ExternalFoldersManager externalFoldersManager = JavaModelManager.getExternalManager();
    try {/*from  www  .  jav a 2s . co  m*/
        IClasspathEntry[] entries = javaProject.getExpandedClasspath();
        for (int i = 0, l = entries.length; i < l; i++) {
            IClasspathEntry entry = entries[i];
            IPath path = entry.getPath();
            IProject p = null;
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_PROJECT:
                p = workspaceRoot.getProject(path.lastSegment()); // missing projects are considered too
                if (((ClasspathEntry) entry).isOptional() && !JavaProject.hasJavaNature(p)) // except if entry is optional
                    p = null;
                break;
            case IClasspathEntry.CPE_LIBRARY:
                if (includeBinaryPrerequisites && path.segmentCount() > 0) {
                    // some binary resources on the class path can come from projects that are not included in the project references
                    IResource resource = workspaceRoot.findMember(path.segment(0));
                    if (resource instanceof IProject) {
                        p = (IProject) resource;
                    } else {
                        resource = externalFoldersManager.getFolder(path);
                        if (resource != null)
                            p = resource.getProject();
                    }
                }
            }
            if (p != null && !projects.contains(p))
                projects.add(p);
        }
    } catch (JavaModelException e) {
        return NO_PROJECTS;
    }
    IProject[] result = new IProject[projects.size()];
    projects.toArray(result);
    return result;
}

From source file:org.jboss.tools.arquillian.core.internal.classpath.ArquillianClassLoader.java

License:Open Source License

private static Set<URL> getURLSet(IJavaProject jProject) {
    Set<URL> urls = new HashSet<URL>();
    Set<IJavaProject> dependentProjects = new HashSet<IJavaProject>();
    if (jProject == null) {
        return urls;
    }//from   w  ww  .j av a  2 s.  com
    try {
        IClasspathEntry[] entries = jProject.getRawClasspath();

        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                addSource(jProject, urls, entry);
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IClasspathEntry resLib = JavaCore.getResolvedClasspathEntry(entry);
                addLibrary(urls, resLib);
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                addProject(urls, entry, dependentProjects);
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                IClasspathEntry resLib = JavaCore.getResolvedClasspathEntry(entry);
                addLibrary(urls, resLib);
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (!entry.getPath().segment(0).toString().endsWith("JRE_CONTAINER")) { //$NON-NLS-1$
                    addContainer(jProject, urls, entry, dependentProjects);
                }
            }
        }

    } catch (Exception e) {
        ArquillianCoreActivator.log(e);
    }

    return urls;
}

From source file:org.jboss.tools.arquillian.core.internal.classpath.ArquillianClassLoader.java

License:Open Source License

private static void addContainer(IJavaProject jProject, Set<URL> urls, IClasspathEntry entry,
        Set<IJavaProject> dependentProjects) throws JavaModelException, MalformedURLException {
    IClasspathEntry[] resLibs = JavaCore.getClasspathContainer(entry.getPath(), jProject).getClasspathEntries();
    for (int i = 0; i < resLibs.length; i++) {
        if (resLibs[i] == null) {
            continue;
        }//  w  w  w  .  j  a va 2 s.co m
        if (resLibs[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            addProject(urls, resLibs[i], dependentProjects);
        } else if (resLibs[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            addContainer(jProject, urls, resLibs[i], dependentProjects);
        } else if (resLibs[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            addLibrary(urls, resLibs[i]);
        } else if (resLibs[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            addSource(jProject, urls, resLibs[i]);
        }
    }
}

From source file:org.jboss.tools.arquillian.core.internal.compiler.ArquillianNameEnvironment.java

License:Open Source License

private void computeClasspathLocations(IWorkspaceRoot root, JavaProject javaProject,
        SimpleLookupTable binaryLocationsPerProject) throws CoreException {

    /* Update cycle marker */
    IMarker cycleMarker = javaProject.getCycleMarker();
    if (cycleMarker != null) {
        int severity = JavaCore.ERROR.equals(javaProject.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true))
                ? IMarker.SEVERITY_ERROR
                : IMarker.SEVERITY_WARNING;
        if (severity != cycleMarker.getAttribute(IMarker.SEVERITY, severity))
            cycleMarker.setAttribute(IMarker.SEVERITY, severity);
    }//from  w  w  w . j a  v  a 2s  .c o m

    IClasspathEntry[] classpathEntries = javaProject.getExpandedClasspath();
    ArrayList sLocations = new ArrayList(classpathEntries.length);
    ArrayList bLocations = new ArrayList(classpathEntries.length);
    nextEntry: for (int i = 0, l = classpathEntries.length; i < l; i++) {
        ClasspathEntry entry = (ClasspathEntry) classpathEntries[i];
        IPath path = entry.getPath();
        Object target = JavaModel.getTarget(path, true);
        if (target == null)
            continue nextEntry;

        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (!(target instanceof IContainer))
                continue nextEntry;
            IPath outputPath = entry.getOutputLocation() != null ? entry.getOutputLocation()
                    : javaProject.getOutputLocation();
            IContainer outputFolder;
            if (outputPath.segmentCount() == 1) {
                outputFolder = javaProject.getProject();
            } else {
                outputFolder = root.getFolder(outputPath);
                if (!outputFolder.exists())
                    createOutputFolder(outputFolder);
            }
            sLocations.add(ClasspathLocation.forSourceFolder((IContainer) target, outputFolder,
                    entry.fullInclusionPatternChars(), entry.fullExclusionPatternChars()));
            continue nextEntry;

        case IClasspathEntry.CPE_PROJECT:
            if (!(target instanceof IProject))
                continue nextEntry;
            IProject prereqProject = (IProject) target;
            if (!JavaProject.hasJavaNature(prereqProject))
                continue nextEntry; // if project doesn't have java nature or is not accessible

            JavaProject prereqJavaProject = (JavaProject) JavaCore.create(prereqProject);
            IClasspathEntry[] prereqClasspathEntries = prereqJavaProject.getRawClasspath();
            ArrayList seen = new ArrayList();
            nextPrereqEntry: for (int j = 0, m = prereqClasspathEntries.length; j < m; j++) {
                IClasspathEntry prereqEntry = prereqClasspathEntries[j];
                if (prereqEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    Object prereqTarget = JavaModel.getTarget(prereqEntry.getPath(), true);
                    if (!(prereqTarget instanceof IContainer))
                        continue nextPrereqEntry;
                    IPath prereqOutputPath = prereqEntry.getOutputLocation() != null
                            ? prereqEntry.getOutputLocation()
                            : prereqJavaProject.getOutputLocation();
                    IContainer binaryFolder = prereqOutputPath.segmentCount() == 1 ? (IContainer) prereqProject
                            : (IContainer) root.getFolder(prereqOutputPath);
                    if (binaryFolder.exists() && !seen.contains(binaryFolder)) {
                        seen.add(binaryFolder);
                        ClasspathLocation bLocation = ClasspathLocation.forBinaryFolder(binaryFolder, true,
                                entry.getAccessRuleSet());
                        bLocations.add(bLocation);
                        if (binaryLocationsPerProject != null) { // normal builder mode
                            ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject
                                    .get(prereqProject);
                            if (existingLocations == null) {
                                existingLocations = new ClasspathLocation[] { bLocation };
                            } else {
                                int size = existingLocations.length;
                                System.arraycopy(existingLocations, 0,
                                        existingLocations = new ClasspathLocation[size + 1], 0, size);
                                existingLocations[size] = bLocation;
                            }
                            binaryLocationsPerProject.put(prereqProject, existingLocations);
                        }
                    }
                }
            }
            continue nextEntry;

        case IClasspathEntry.CPE_LIBRARY:
            if (target instanceof IResource) {
                IResource resource = (IResource) target;
                ClasspathLocation bLocation = null;
                if (resource instanceof IFile) {
                    AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                            .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                            && JavaCore.IGNORE.equals(
                                    javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true)))
                                            ? null
                                            : entry.getAccessRuleSet();
                    bLocation = ClasspathLocation.forLibrary((IFile) resource, accessRuleSet);
                } else if (resource instanceof IContainer) {
                    AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                            .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                            && JavaCore.IGNORE.equals(
                                    javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true)))
                                            ? null
                                            : entry.getAccessRuleSet();
                    bLocation = ClasspathLocation.forBinaryFolder((IContainer) target, false, accessRuleSet); // is library folder not output folder
                }
                bLocations.add(bLocation);
                if (binaryLocationsPerProject != null) { // normal builder mode
                    IProject p = resource.getProject(); // can be the project being built
                    ClasspathLocation[] existingLocations = (ClasspathLocation[]) binaryLocationsPerProject
                            .get(p);
                    if (existingLocations == null) {
                        existingLocations = new ClasspathLocation[] { bLocation };
                    } else {
                        int size = existingLocations.length;
                        System.arraycopy(existingLocations, 0,
                                existingLocations = new ClasspathLocation[size + 1], 0, size);
                        existingLocations[size] = bLocation;
                    }
                    binaryLocationsPerProject.put(p, existingLocations);
                }
            } else if (target instanceof File) {

                AccessRuleSet accessRuleSet = (JavaCore.IGNORE
                        .equals(javaProject.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
                        && JavaCore.IGNORE.equals(
                                javaProject.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true))) ? null
                                        : entry.getAccessRuleSet();
                bLocations.add(ClasspathLocation.forLibrary(path.toString(), accessRuleSet));
            }
            continue nextEntry;
        }
    }

    // now split the classpath locations... place the output folders ahead of the other .class file folders & jars
    ArrayList outputFolders = new ArrayList(1);
    this.sourceLocations = new ClasspathMultiDirectory[sLocations.size()];
    if (!sLocations.isEmpty()) {
        sLocations.toArray(this.sourceLocations);

        // collect the output folders, skipping duplicates
        next: for (int i = 0, l = this.sourceLocations.length; i < l; i++) {
            ClasspathMultiDirectory md = this.sourceLocations[i];
            IPath outputPath = md.binaryFolder.getFullPath();
            for (int j = 0; j < i; j++) { // compare against previously walked source folders
                if (outputPath.equals(this.sourceLocations[j].binaryFolder.getFullPath())) {
                    md.hasIndependentOutputFolder = this.sourceLocations[j].hasIndependentOutputFolder;
                    continue next;
                }
            }
            outputFolders.add(md);

            // also tag each source folder whose output folder is an independent folder & is not also a source folder
            for (int j = 0, m = this.sourceLocations.length; j < m; j++)
                if (outputPath.equals(this.sourceLocations[j].sourceFolder.getFullPath()))
                    continue next;
            md.hasIndependentOutputFolder = true;
        }
    }

    // combine the output folders with the binary folders & jars... place the output folders before other .class file folders & jars
    //        this.binaryLocations = new ClasspathLocation[outputFolders.size() + bLocations.size()];
    //        int index = 0;
    //        for (int i = 0, l = outputFolders.size(); i < l; i++)
    //            this.binaryLocations[index++] = (ClasspathLocation) outputFolders.get(i);
    //        for (int i = 0, l = bLocations.size(); i < l; i++)
    //            this.binaryLocations[index++] = (ClasspathLocation) bLocations.get(i);

    this.binaryLocations = new ClasspathLocation[bLocations.size()];
    int index = 0;
    //        for (int i = 0, l = outputFolders.size(); i < l; i++)
    //            this.binaryLocations[index++] = (ClasspathLocation) outputFolders.get(i);
    for (int i = 0, l = bLocations.size(); i < l; i++)
        this.binaryLocations[index++] = (ClasspathLocation) bLocations.get(i);

    this.baseBinaryLocations = new ClasspathLocation[binaryLocations.length];
    System.arraycopy(binaryLocations, 0, baseBinaryLocations, 0, binaryLocations.length);
    this.baseSourceLocations = new ClasspathMultiDirectory[sourceLocations.length];
    System.arraycopy(sourceLocations, 0, baseSourceLocations, 0, sourceLocations.length);

}

From source file:org.jboss.tools.common.jdt.core.buildpath.MaterializeLibraryJob.java

License:Open Source License

private LinkedHashSet<IClasspathEntry> copyClasspathEntries(IProgressMonitor monitor, final IPath rootPath)
        throws CoreException, JavaModelException {

    int jarSize = jars.size();

    monitor.beginTask(Messages.Materialize_Library, jarSize);

    if (libFolder instanceof IFolder) {
        mkdirs((IFolder) libFolder, monitor);
    }//from  ww w  .j a v a 2s  .  c  om

    IPath destination = libFolder.getLocation();

    LinkedHashSet<IClasspathEntry> newCpes = new LinkedHashSet<IClasspathEntry>(jarSize);

    IClasspathEntry[] cpEntries = containerToRemove.getClasspathEntries();

    for (IClasspathEntry entry : cpEntries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {

            IPath sourceFilePath = entry.getPath();
            String fileName = jars.get(sourceFilePath);
            if (fileName == null) {
                // Jar was not selected
                continue;
            }

            monitor.subTask(fileName);
            IPath destinationFilePath = destination.append(fileName);
            try {
                if ((javaProject.findPackageFragmentRoot(destinationFilePath) == null)// Not already defined
                        && copy(sourceFilePath, destinationFilePath)) {
                    // TODO handle duplicate files -> rename?
                    // FIXME use absolute / relative paths depending on the
                    // location of the destination folder (in project /
                    // elsewhere in the workspace)
                    // FIXME need a more elegant way to get a relative path
                    // with a leading /
                    IPath relativePath = new Path("/").append(libFolder.getFullPath()).append(fileName); //$NON-NLS-1$
                    IClasspathEntry newEntry = getNewClasspathEntry(entry, relativePath);
                    newCpes.add(newEntry);
                }
            } catch (IOException e) {
                IStatus status = new Status(IStatus.ERROR, JDTExtActivator.PLUGIN_ID,
                        NLS.bind(Messages.MaterializeLibraryJob_error_copying_file, fileName), e);
                throw new CoreException(status);
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            newCpes.add(entry);
        }
    }
    return newCpes;
}

From source file:org.jboss.tools.common.jdt.ui.buildpath.dialog.MaterializeLibraryDialog.java

License:Open Source License

private void initClasspathEntryPaths() {
    IClasspathEntry[] cpEntries = containerToMaterialize.getClasspathEntries();
    classpathEntryPaths = new LinkedHashMap<IClasspathEntry, String>(cpEntries.length);
    for (IClasspathEntry entry : cpEntries) {
        if ((entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath() != null)
                || (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT)) {
            IPath sourceFilePath = entry.getPath();
            String fileName = sourceFilePath.lastSegment();
            classpathEntryPaths.put(entry, fileName);
        }//from w  w  w  .j a  va  2  s  .c  om
    }
}

From source file:org.jboss.tools.common.jdt.ui.buildpath.dialog.MaterializeLibraryDialog.java

License:Open Source License

@SuppressWarnings("unchecked")
private boolean validateEntries() {
    Object[] selection = classpathEntriesViewer.getCheckedElements();
    selectedClasspathEntryPaths = new LinkedHashMap<IPath, String>(selection.length);
    for (Object o : selection) {
        Map.Entry<IClasspathEntry, String> entry = (Map.Entry<IClasspathEntry, String>) o;
        if (entry.getKey().getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            String name = entry.getValue();
            if (!checkValidName(name)) {
                setErrorMessage(name + " is not a valid file name");
                return false;
            }//from  w w  w  .j  av  a2 s.co  m
        }
        selectedClasspathEntryPaths.put(entry.getKey().getPath(), entry.getValue());
    }

    Set<String> duplicates = findDuplicates(selectedClasspathEntryPaths.values());
    if (!duplicates.isEmpty()) {
        setErrorMessage("Duplicate entries found : " + duplicates.toString());
        return false;
    }
    return true;
}