Example usage for org.eclipse.jdt.core IClasspathEntry getEntryKind

List of usage examples for org.eclipse.jdt.core IClasspathEntry getEntryKind

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry getEntryKind.

Prototype

int getEntryKind();

Source Link

Document

Returns the kind of this classpath entry.

Usage

From source file:com.liferay.ide.server.remote.ModuleTraverser.java

License:Open Source License

private static boolean isClassFolderEntry(final IClasspathEntry entry) {
    if (entry == null || entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) {
        return false;
    }/*  w  w w. j a v a2  s.  co m*/
    // does the path refer to a file or a folder?
    final IPath entryPath = entry.getPath();
    IPath entryLocation = entryPath;
    final IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath);
    if (resource != null) {
        entryLocation = resource.getLocation();
    }
    boolean isFile = true; // by default, assume a jar file
    if (entryLocation.toFile().isDirectory()) {
        isFile = false;
    }
    return !isFile;
}

From source file:com.liferay.ide.theme.core.facet.ThemePluginFacetInstall.java

License:Open Source License

protected void removeUnneededClasspathEntries() {
    IFacetedProjectWorkingCopy facetedProject = getFacetedProject();
    IJavaProject javaProject = JavaCore.create(facetedProject.getProject());

    try {/*  ww  w. jav a2  s.c  om*/
        IClasspathEntry[] existingClasspath = javaProject.getRawClasspath();
        List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>();

        for (IClasspathEntry entry : existingClasspath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                continue;
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                String path = entry.getPath().toPortableString();
                if (path.contains("org.eclipse.jdt.launching.JRE_CONTAINER") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.web.container") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.module.container")) //$NON-NLS-1$
                {
                    continue;
                }
            }

            newClasspath.add(entry);
        }

        javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[0]), null);

        IResource sourceFolder = javaProject.getProject()
                .findMember(IPluginFacetConstants.PORTLET_PLUGIN_SDK_SOURCE_FOLDER);

        if (sourceFolder.exists()) {
            sourceFolder.delete(true, null);
        }
    } catch (Exception e) {

    }
}

From source file:com.microsoft.javapkgbuild.Tasks.java

License:MIT License

public static void exportReferences(String projectName, String outputFileName) throws JavaModelException {
    try {//w w  w.j a v  a2  s . c  o m
        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        IProject project = workspaceRoot.getProject(projectName);
        IJavaProject javaProject = JavaCore.create(project);

        DocumentBuilderFactory xFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = xFactory.newDocumentBuilder();
        Document doc = builder.newDocument();

        Element mainRoot = doc.createElement("classpath");
        mainRoot.setAttribute("projectName", projectName);
        doc.appendChild(mainRoot);

        IClasspathEntry[] classPathList = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry cp : classPathList) {
            Element cpNode = doc.createElement("classpathentry");
            cpNode.setAttribute("path", cp.getPath().toOSString());
            cpNode.setAttribute("kind", getClassPathType(cp));
            cpNode.setAttribute("exported", Boolean.toString(cp.isExported()));

            IPath sourceFolder = cp.getSourceAttachmentPath();
            if (cp.getEntryKind() == IClasspathEntry.CPE_LIBRARY && sourceFolder != null)
                cpNode.setAttribute("sourcepath", sourceFolder.toOSString());

            mainRoot.appendChild(cpNode);
        }

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);

        FileOutputStream fos = new FileOutputStream(outputFileName);
        StreamResult outFile = new StreamResult(fos);
        transformer.transform(source, outFile);
        fos.close();

        System.out.println("Output file is: " + outputFileName);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

From source file:com.microsoft.javapkgbuild.Tasks.java

License:MIT License

private static String getClassPathType(IClasspathEntry cp) {
    switch (cp.getEntryKind()) {
    case IClasspathEntry.CPE_CONTAINER:
        return "con";
    case IClasspathEntry.CPE_LIBRARY:
        return "lib";
    case IClasspathEntry.CPE_PROJECT:
        return "proj";
    case IClasspathEntry.CPE_SOURCE:
        return "src";
    case IClasspathEntry.CPE_VARIABLE:
        return "var";
    default://  w  w  w  . j  ava2s.c  om
        return "unexpected";
    }
}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

private static IClasspathEntry getClasspathEntry(IPackageFragmentRoot root) throws JavaModelException {
    IClasspathEntry rawEntry = root.getRawClasspathEntry();
    int rawEntryKind = rawEntry.getEntryKind();
    switch (rawEntryKind) {
    case IClasspathEntry.CPE_LIBRARY:
    case IClasspathEntry.CPE_VARIABLE:
    case IClasspathEntry.CPE_CONTAINER: // should not happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=305037
        if (root.isArchive() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
            IClasspathEntry resolvedEntry = root.getResolvedClasspathEntry();
            if (resolvedEntry.getReferencingEntry() != null)
                return resolvedEntry;
            else/*from w w  w.j  a v a2  s  . c o m*/
                return rawEntry;
        }
    }
    return rawEntry;
}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) {
    try {//from  w  w  w .j  av a2  s.c  om
        IClasspathEntry rawEntry = root.getRawClasspathEntry();
        if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            IClasspathEntry entry = getClasspathEntry(root);
            if (entry.getReferencingEntry() != null) {
                return false; // not the variable entry itself, but a referenced entry
            }
            IPath path = rawEntry.getPath().makeRelative();

            if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) {
                int segements = path.segmentCount();
                if (segements > 0) {
                    fBuffer.append(path.segment(segements - 1));
                    if (segements > 1) {
                        int offset = fBuffer.length();
                        fBuffer.append(CONCAT_STRING);
                        fBuffer.append(path.removeLastSegments(1).toOSString());
                    }
                } else {
                    fBuffer.append(path.toString());
                }
            } else {
                fBuffer.append(path.toString());
            }
            int offset = fBuffer.length();
            fBuffer.append(CONCAT_STRING);
            if (root.isExternal())
                fBuffer.append(root.getPath().toOSString());
            else
                fBuffer.append(root.getPath().makeRelative().toString());
            return true;
        }
    } catch (JavaModelException e) {
        // problems with class path, ignore (bug 202792)
        return false;
    }
    return false;
}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

private void appendExternalArchiveLabel(IPackageFragmentRoot root, long flags) {
    IPath path;//  ww w. ja va  2 s  . c om
    IClasspathEntry classpathEntry = null;
    try {
        classpathEntry = getClasspathEntry(root);
        IPath rawPath = classpathEntry.getPath();
        if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER && !rawPath.isAbsolute())
            path = rawPath;
        else
            path = root.getPath();
    } catch (JavaModelException e) {
        path = root.getPath();
    }
    if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) {
        int segements = path.segmentCount();
        if (segements > 0) {
            fBuffer.append(path.segment(segements - 1));
            int offset = fBuffer.length();
            if (segements > 1 || path.getDevice() != null) {
                fBuffer.append(CONCAT_STRING);
                fBuffer.append(path.removeLastSegments(1).toOSString());
            }
            if (classpathEntry != null) {
                IClasspathEntry referencingEntry = classpathEntry.getReferencingEntry();
                if (referencingEntry != null) {
                    fBuffer.append(" (from ");
                    fBuffer.append(Name.CLASS_PATH.toString());
                    fBuffer.append(" of ");
                    fBuffer.append(referencingEntry.getPath().lastSegment());
                    fBuffer.append(")");
                }
            }
        } else {
            fBuffer.append(path.toOSString());
        }
    } else {
        fBuffer.append(path.toOSString());
    }
}

From source file:com.mountainminds.eclemma.core.ScopeUtils.java

License:Open Source License

/**
 * Remove all JRE runtime entries from the given set
 * // w ww  . jav  a  2s . co m
 * @param scope
 *          set to filter
 * @return filtered set without JRE runtime entries
 */
public static Set<IPackageFragmentRoot> filterJREEntries(Collection<IPackageFragmentRoot> scope)
        throws JavaModelException {
    final Set<IPackageFragmentRoot> filtered = new HashSet<IPackageFragmentRoot>();
    for (final IPackageFragmentRoot root : scope) {
        final IClasspathEntry entry = root.getRawClasspathEntry();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_VARIABLE:
            filtered.add(root);
            break;
        case IClasspathEntry.CPE_CONTAINER:
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                    root.getJavaProject());
            if (container != null && container.getKind() == IClasspathContainer.K_APPLICATION) {
                filtered.add(root);
            }
            break;
        }
    }
    return filtered;
}

From source file:com.nginious.http.plugin.ServerManager.java

License:Apache License

private boolean updateProjectWithPluginVersion(IProject project) {
    logger.log("ENTER ServerManager.updateProjectWithPluginVersion project={0}", project);

    try {/*www .jav a2 s  .com*/
        URL apiJar = NginiousPlugin.getApiJar();
        String filePath = apiJar.toString();
        filePath = filePath.substring(5);
        Path apiJarPath = new Path(filePath);

        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();
        boolean changed = false;

        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IPath path = entry.getPath();

                if (path.lastSegment() != null && path.lastSegment().endsWith("nginious-api.jar")) {
                    changed = true;
                    entry = JavaCore.newLibraryEntry(apiJarPath, null, null);
                }
            }

            newEntries.add(entry);
        }

        if (changed) {
            IProgressMonitor progress = new NullProgressMonitor();
            entries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            javaProject.setRawClasspath(entries, progress);
            javaProject.save(progress, true);
        }

        logger.log("EXIT ServerManager.updateProjectWithPluginVersion changed={0}", changed);
        return changed;
    } catch (JavaModelException e) {
        logger.log("ServerManager.updateProkectWithPluginVersion exception", e);
        return false;
    } catch (IOException e) {
        logger.log("ServerManager.updateProkectWithPluginVersion exception", e);
        return false;
    }
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerContentProvider.java

License:Open Source License

@Override
protected Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
    if (!project.getProject().isOpen())
        return NO_CHILDREN;

    List<Object> result = new ArrayList<Object>();

    IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
    for (int i = 0; i < roots.length; i++) {
        IPackageFragmentRoot root = roots[i];
        IClasspathEntry classpathEntry = root.getRawClasspathEntry();
        int entryKind = classpathEntry.getEntryKind();
        if (entryKind == IClasspathEntry.CPE_CONTAINER) {
            // all ClassPathContainers are added later
        } else if (fShowLibrariesNode
                && (entryKind == IClasspathEntry.CPE_LIBRARY || entryKind == IClasspathEntry.CPE_VARIABLE)) {
            IResource resource = root.getResource();
            if (resource != null && project.getResource().equals(resource.getParent())) {
                // show resource as child of project, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=141906
                result.add(resource);//from  w  w  w  . j  av a 2s.  c  o m
            } else {
                // skip: will add the referenced library node later
            }
        } else {
            if (isProjectPackageFragmentRoot(root)) {
                // filter out package fragments that correspond to projects and
                // replace them with the package fragments directly
                Object[] fragments = getPackageFragmentRootContent(root);
                for (int j = 0; j < fragments.length; j++) {
                    result.add(fragments[j]);
                }
            } else {
                result.add(root);
            }
        }
    }

    if (fShowLibrariesNode) {
        result.add(new LibraryContainer(project));
    }

    // separate loop to make sure all containers are on the classpath (even empty ones)
    IClasspathEntry[] rawClasspath = project.getRawClasspath();
    for (int i = 0; i < rawClasspath.length; i++) {
        IClasspathEntry classpathEntry = rawClasspath[i];
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            result.add(new ClassPathContainer(project, classpathEntry));
        }
    }
    Object[] resources = project.getNonJavaResources();
    for (int i = 0; i < resources.length; i++) {
        result.add(resources[i]);
    }
    return result.toArray();
}