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:com.google.gwt.eclipse.core.runtime.GWTJarsRuntimeTest.java

License:Open Source License

public void testGetClasspathEntries() throws Exception {
    IClasspathEntry[] cpEntries = runtime.getClasspathEntries();

    // Look for the gwt-specific classpath entries
    List<IClasspathEntry> gwtCpEntries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry cpEntry : cpEntries) {
        if (isGWTJar(runtime, cpEntry.getPath().lastSegment())) {
            gwtCpEntries.add(cpEntry);/*from www .  j a  v  a 2  s .  c  om*/
        }
    }

    // Make sure that there are two of them
    assertEquals(3, gwtCpEntries.size());

    for (int i = 0; i < gwtCpEntries.size(); i++) {
        IClasspathEntry gwtClasspathEntry = gwtCpEntries.get(i);
        assertEquals(IClasspathEntry.CPE_LIBRARY, gwtClasspathEntry.getEntryKind());
        assertEquals(IPackageFragmentRoot.K_BINARY, gwtClasspathEntry.getContentKind());

        // Verify that our classpath entries point at the GWT javadoc.
        IClasspathAttribute[] extraAttributes = gwtClasspathEntry.getExtraAttributes();
        assertTrue("No extra attributes seen for classpath entry: " + gwtClasspathEntry,
                extraAttributes.length > 0);
        assertEquals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, extraAttributes[0].getName());

        /*
         * Entries should have their javadoc location point at a directory with "index.html".
         * Strangely, the values of these classpath attributes are specified as "file://" urls.
         */
        File jdLocation = new File(new URL(extraAttributes[0].getValue()).getFile());
        assertTrue("Javadoc file does not exist", jdLocation.exists());
        List<String> files1 = Arrays.asList(jdLocation.list());
        assertTrue("Javadoc file is not an index.html file.", files1.contains("index.html"));
    }
}

From source file:com.google.gwt.eclipse.core.runtime.GWTJarsRuntimeTest.java

License:Open Source License

public void testGetValidationClasspathEntries() throws Exception {
    IClasspathEntry[] cpEntries = runtime.getClasspathEntries();

    // Look for the validation-specific classpath entries
    List<IClasspathEntry> validationCpEntries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry cpEntry : cpEntries) {
        if (cpEntry.getPath().lastSegment().startsWith(GWTRuntime.VALIDATION_API_JAR_PREFIX)) {
            validationCpEntries.add(cpEntry);
        }//from  w  ww  .j  ava2s  .com
    }

    if (validationCpEntries.size() == 0) {
        String sdkVersion = runtime.getVersion();

        // Can't be an internal version, because it would have the
        // validation jars
        assertFalse(SdkUtils.isInternal(sdkVersion));

        // Sdk version must be pre-GWT 2.3.0
        assertTrue(SdkUtils.compareVersionStrings(sdkVersion, "2.3.0") < 0);

        return;
    }

    // Make sure that there are at least two of them
    assertEquals(2, validationCpEntries.size());

    IClasspathEntry sourcesEntry = null;
    IClasspathEntry binaryEntry = null;

    for (IClasspathEntry validationClasspathEntry : validationCpEntries) {
        // Verify the entry types
        assertEquals(IClasspathEntry.CPE_LIBRARY, validationClasspathEntry.getEntryKind());
        assertEquals(IPackageFragmentRoot.K_BINARY, validationClasspathEntry.getContentKind());

        if (validationClasspathEntry.getPath().lastSegment().contains("sources")) {
            sourcesEntry = validationClasspathEntry;
        } else {
            binaryEntry = validationClasspathEntry;
        }
    }

    // Verify that the sources and binary entries correspond to each other
    assertTrue(Util.findSourcesJarForClassesJar(binaryEntry.getPath()).equals(sourcesEntry.getPath()));

    // Verify that the source attachment path has been set for the binary
    // entry
    assertTrue(binaryEntry.getSourceAttachmentPath().equals(sourcesEntry.getPath()));
}

From source file:com.google.gwt.eclipse.core.runtime.GWTRuntimeContainerInitializerTest.java

License:Open Source License

/**
 * TODO: We need to revisit this test. Since we don't allow container updates
 * right now, it is not clear that the test is sufficiently strong.
 *//* w  ww  .j a va  2s  . c  om*/
public void testRequestClasspathContainerUpdate() throws CoreException {
    IJavaProject testProject = getTestProject();

    IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(defaultRuntimePath, testProject);

    final List<IClasspathEntry> newClasspathEntries = new ArrayList<IClasspathEntry>();
    IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries();
    for (IClasspathEntry classpathEntry : classpathEntries) {

        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IClasspathAttribute[] extraAttributes = classpathEntry.getExtraAttributes();
            List<IClasspathAttribute> newAttributes = new ArrayList<IClasspathAttribute>();
            for (IClasspathAttribute extraAttribute : extraAttributes) {
                String attributeName = extraAttribute.getName();
                if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attributeName)) {
                    String attributeValue = extraAttribute.getValue() + "modified";
                    extraAttribute = JavaCore.newClasspathAttribute(attributeName, attributeValue);
                }

                newAttributes.add(extraAttribute);
            }

            IPath sourceAttachmentPath = new Path("/sourceAttachmentPath");
            IPath sourceAttachmentRootPath = new Path("sourceAttachmentRootPath");

            classpathEntry = JavaCore.newLibraryEntry(classpathEntry.getPath(), sourceAttachmentPath,
                    sourceAttachmentRootPath, classpathEntry.getAccessRules(),
                    newAttributes.toArray(new IClasspathAttribute[0]), classpathEntry.isExported());
        }

        newClasspathEntries.add(classpathEntry);
    }

    // Update the classpath container
    initializer.requestClasspathContainerUpdate(defaultRuntimePath, testProject,
            new ClasspathContainerAdapter(classpathContainer) {
                @Override
                public IClasspathEntry[] getClasspathEntries() {
                    return newClasspathEntries.toArray(new IClasspathEntry[0]);
                }
            });

    // Check that the modifications took effect
    IClasspathContainer updatedContainer = JavaCore.getClasspathContainer(defaultRuntimePath, testProject);
    for (IClasspathEntry classpathEntry : updatedContainer.getClasspathEntries()) {
        if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) {
            // Ignore all non-library entries
            continue;
        }

        for (IClasspathAttribute attribute : classpathEntry.getExtraAttributes()) {
            if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attribute.getName())) {
                String value = attribute.getValue();
                assertTrue(value.endsWith("modified"));
            }
        }

        IPath sourceAttachmentPath = classpathEntry.getSourceAttachmentPath();
        assertEquals(new Path("/sourceAttachmentPath"), sourceAttachmentPath);

        IPath sourceAttachmentRootPath = classpathEntry.getSourceAttachmentRootPath();
        assertEquals(new Path("sourceAttachmentRootPath"), sourceAttachmentRootPath);
    }
}

From source file:com.google.inject.tools.ideplugin.eclipse.EclipseJavaProject.java

License:Apache License

private List<String> expandClasspath(IClasspathEntry[] entries, String projectName, String projectLocation)
        throws Exception {
    final List<String> args = new ArrayList<String>();
    IResource presource;//from  www. j a v  a2 s.c o m
    String resourceLocation;
    String path;
    for (IClasspathEntry entry : entries) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_CONTAINER:
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
            args.addAll(expandClasspath(container.getClasspathEntries(), projectName, projectLocation));
            break;
        case IClasspathEntry.CPE_SOURCE:
            IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            path = resource.getLocation().makeAbsolute().toOSString();
            if (path.startsWith("/" + projectName)) {
                args.add(path.replaceFirst("/" + projectName, projectLocation));
            } else {
                args.add(path);
            }
            break;
        case IClasspathEntry.CPE_LIBRARY:
            path = entry.getPath().makeAbsolute().toOSString();
            if (path.startsWith("/" + projectName)) {
                args.add(path.replaceFirst("/" + projectName, projectLocation));
            } else {
                args.add(path);
            }
            break;
        case IClasspathEntry.CPE_PROJECT:
            presource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            resourceLocation = presource.getLocation().makeAbsolute().toOSString();
            String outputLocation = resourceLocation;
            args.add(outputLocation.replaceFirst(presource.getName(), resourceLocation));
            break;
        case IClasspathEntry.CPE_VARIABLE:
            break;
        default:
            //never happens
        }
    }
    return args;
}

From source file:com.ibm.wala.ide.util.JavaEclipseProjectPath.java

License:Open Source License

@Override
protected void resolveClasspathEntry(IJavaProject project, IClasspathEntry entry, ILoader loader,
        boolean includeSource, boolean cpeFromMainProject) {
    entry = JavaCore.getResolvedClasspathEntry(entry);
    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_SOURCE: {
        resolveSourcePathEntry(includeSource ? JavaSourceLoader.SOURCE : Loader.APPLICATION, includeSource,
                cpeFromMainProject, entry.getPath(), entry.getOutputLocation(), entry.getExclusionPatterns(),
                "java");
        break;// w  w  w.  j a v a2  s  .c  om
    }
    case IClasspathEntry.CPE_LIBRARY: {
        resolveLibraryPathEntry(loader, entry.getPath());
        break;
    }
    case IClasspathEntry.CPE_PROJECT: {
        resolveProjectPathEntry(loader, includeSource, entry.getPath());
        break;
    }
    case IClasspathEntry.CPE_CONTAINER: {
        try {
            IClasspathContainer cont = JavaCore.getClasspathContainer(entry.getPath(), project);
            IClasspathEntry[] entries = cont.getClasspathEntries();
            resolveClasspathEntries(project, Arrays.asList(entries),
                    cont.getKind() == IClasspathContainer.K_APPLICATION ? loader : Loader.PRIMORDIAL,
                    includeSource, false);
        } catch (CoreException e) {
            System.err.println(e);
            Assertions.UNREACHABLE();
        }
    }
    }
}

From source file:com.ifedorenko.m2e.sourcelookup.internal.JavaProjectSources.java

License:Open Source License

static ISourceContainer getProjectSourceContainer(IJavaProject javaProject) {
    List<ISourceContainer> containers = new ArrayList<ISourceContainer>();

    boolean hasSources = false;

    try {// w  ww  .  j  av  a  2 s . c  o m
        for (IClasspathEntry cpe : javaProject.getRawClasspath()) {
            switch (cpe.getEntryKind()) {
            case IClasspathEntry.CPE_SOURCE:
                hasSources = true;
                break;
            case IClasspathEntry.CPE_LIBRARY:
                IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
                IResource lib = workspaceRoot.findMember(cpe.getPath());
                IPackageFragmentRoot fragmentRoot;
                if (lib != null) {
                    fragmentRoot = javaProject.getPackageFragmentRoot(lib);
                } else {
                    fragmentRoot = javaProject.getPackageFragmentRoot(cpe.getPath().toOSString());
                }
                containers.add(new PackageFragmentRootSourceContainer(fragmentRoot));
                break;
            }
        }
    } catch (JavaModelException e) {
        // ignore... maybe log
    }

    if (hasSources) {
        containers.add(0, new JavaProjectSourceContainer(javaProject));
    }

    if (containers.isEmpty()) {
        return null;
    }

    if (containers.size() == 1) {
        return containers.get(0);
    }

    return new CompositeSourceContainer(containers);
}

From source file:com.javapathfinder.vjp.DefaultProperties.java

License:Open Source License

/**
 * append all relevant paths from the target project settings to the vm.classpath 
 *///  w  ww .  j  av a2  s  .co  m
private static void appendProjectClassPaths(IJavaProject project, StringBuilder cp) {
    try {
        // we need to maintain order
        LinkedHashSet<IPath> paths = new LinkedHashSet<IPath>();

        // append the default output folder
        IPath defOutputFolder = project.getOutputLocation();
        if (defOutputFolder != null) {
            paths.add(defOutputFolder);
        }
        // look for libraries and source root specific output folders
        for (IClasspathEntry e : project.getResolvedClasspath(true)) {
            IPath ePath = null;

            switch (e.getContentKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                ePath = e.getPath();
                break;
            case IClasspathEntry.CPE_SOURCE:
                ePath = e.getOutputLocation();
                break;
            }

            if (ePath != null && !paths.contains(ePath)) {

                paths.add(ePath);
            }
        }

        for (IPath path : paths) {
            String absPath = getAbsolutePath(project, path).toOSString();

            //        if (cp.length() > 0) {
            //          cp.append(Config.LIST_SEPARATOR);
            //        }
            // only add classes that are found in bin
            if (absPath.contains("/bin"))
                cp.append(absPath);
        }
        System.out.println("cp is" + cp);
    } catch (JavaModelException jme) {
        VJP.logError("Could not append Project classpath", jme);
    }
}

From source file:com.jstar.eclipse.objects.JavaFile.java

License:BSD License

public String getProjectClasspath() {
    StringBuilder projectClassPath = new StringBuilder();

    try {//  w  w w .ja  va2 s. c  o  m
        for (IClasspathEntry entry : getJavaProject().getProject().getResolvedClasspath(true)) {
            final int entryKind = entry.getEntryKind();
            if (entryKind == IClasspathEntry.CPE_SOURCE) {
                projectClassPath
                        .append(getJavaProject().getWorkspaceLocation().append(entry.getPath()).toOSString());
                projectClassPath.append(File.pathSeparator);
            }

            if (entryKind == IClasspathEntry.CPE_LIBRARY) {
                projectClassPath.append(getAbsolutePath(entry));
                projectClassPath.append(File.pathSeparator);

            }

            //TODO: IClasspathEntry.CPE_PROJECT
        }

        return projectClassPath.toString();
    } catch (CoreException ce) {
        ce.printStackTrace(ConsoleService.getInstance().getConsoleStream());
        return "";
    }
}

From source file:com.laex.j2objc.preferences.ClasspathPropertyPage.java

License:Open Source License

/**
 * Load project referenced classpaths./*w w  w  .j a  v  a  2 s.  c  o  m*/
 */
private void loadProjectReferencedClasspaths() {
    try {
        IClasspathEntry[] refClasspath = ProjectUtil.getJavaProject(getElement()).getResolvedClasspath(true);
        for (IClasspathEntry o : refClasspath) {

            IClasspathEntry entry = JavaCore.getResolvedClasspathEntry((IClasspathEntry) o);
            String path = null;

            // We need to figure out the path for different types of
            // classpath entries.
            // the types of entries are: CPE_LIBRARY, CPE_PROJECT,
            // CPE_SOURCE, CPE_VARIABLE, CPE_CONTAINER
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:

                // With CPE Library: some jar files could reside in system,
                // like JDK jar files
                // some jar files may reside in workspace, within other
                // project.
                // Check if the library resides in workspace project
                IResource file = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
                // null means, this library does not reside in workspace but
                // instead in the system
                if (file == null) {
                    // get the apropriate path
                    path = entry.getPath().makeAbsolute().toOSString();
                } else {
                    // if resdies in workspace, get the appropriate path
                    if (file.exists()) {
                        path = file.getLocation().makeAbsolute().toOSString();
                    }
                }

                break;

            case IClasspathEntry.CPE_PROJECT:
            case IClasspathEntry.CPE_SOURCE:
                // project and source
                IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
                if (res.exists()) {
                    path = res.getFullPath().makeAbsolute().toOSString();
                }
                break;
            }

            // some path may be folders. In that case, we have to make sure
            // we store the full absolute path to the folders
            boolean isArchive = path.endsWith("jar") || path.endsWith("zip");
            if (!isArchive) {
                IPath ipath = new Path(path);
                // We can only get a folder if the segment count is greater
                // 2 i.e. if the path has at least two segments
                if (ipath.segmentCount() >= 2) {
                    IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(ipath);
                    if (folder.exists()) {
                        classpathRef.add(folder.getLocation().makeAbsolute().toOSString());
                    }
                }

                // If the segment count is 1, then it is probably a project
                if (ipath.segmentCount() == 1) {
                    IProject refPrj = ResourcesPlugin.getWorkspace().getRoot().getProject(path);
                    // if this is a project, then we assume it has a SRC
                    // folder; and therefore append SRC at the end
                    // this is required because j2objc compiler needs the
                    // path till the src folder to compile properly

                    classpathRef.add(refPrj.getLocation().append("src").makeAbsolute().toOSString());
                }

            } else {
                // add whatever archive path we get from the results
                classpathRef.add(path);
            }

        }

        checkboxTableViewer.setInput(classpathRef);
        checkboxTableViewer.refresh();

    } catch (JavaModelException e) {
        LogUtil.logException(e);
    } catch (CoreException e) {
        LogUtil.logException(e);
    }
}

From source file:com.legstar.eclipse.plugin.schemagen.wizards.JavaToXsdWizardPage.java

License:Open Source License

/**
 * Given classpath entries from a java project, populate a list of
 * collections./*from  ww w . j ava  2  s .  c o  m*/
 * 
 * @param selectedPathElementsLocations the output path locations
 * @param classPathEntries the java project class path entries
 * @param javaProject the java project
 * @throws JavaModelException if invalid classpath
 */
private void addPathElements(final List<String> selectedPathElementsLocations,
        final IClasspathEntry[] classPathEntries, final IJavaProject javaProject) throws JavaModelException {

    IClasspathEntry jreEntry = JavaRuntime.getDefaultJREContainerEntry();
    IPath projectPath = javaProject.getProject().getLocation();

    for (int i = 0; i < classPathEntries.length; i++) {
        IClasspathEntry classpathEntry = classPathEntries[i];
        String pathElementLocation = null;
        switch (classpathEntry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            pathElementLocation = classpathEntry.getPath().toOSString();
            break;
        case IClasspathEntry.CPE_CONTAINER:
            /* No need for the default jre */
            if (classpathEntry.equals(jreEntry)) {
                break;
            }
            /* Resolve container into class path entries */
            IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(classpathEntry.getPath(),
                    javaProject);
            addPathElements(selectedPathElementsLocations, classpathContainer.getClasspathEntries(),
                    javaProject);
            break;
        case IClasspathEntry.CPE_VARIABLE:
            pathElementLocation = JavaCore.getResolvedVariablePath(classpathEntry.getPath()).toOSString();
            break;
        case IClasspathEntry.CPE_SOURCE:
            /*
             * If source has no specific output, use the project default
             * one
             */
            IPath outputLocation = classpathEntry.getOutputLocation();
            if (outputLocation == null) {
                outputLocation = javaProject.getOutputLocation();
            }
            pathElementLocation = projectPath.append(outputLocation.removeFirstSegments(1)).toOSString();
            break;
        default:
            break;
        }

        if (pathElementLocation != null && !selectedPathElementsLocations.contains(pathElementLocation)) {
            selectedPathElementsLocations.add(pathElementLocation);
        }
    }
}