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.javadude.dependencies.actions.DOTReportAction.java

License:Open Source License

public void run(IAction action) {
    // create DOT definition
    String NL = System.getProperty("line.separator");
    String dot = "digraph dependencies {" + NL;
    try {//from  w w w . j a  va  2  s . co  m
        for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
            if (project.isOpen() && project.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject javaProject = JavaCore.create(project);
                for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                        dot += '\t' + fixName(project.getName()) + " -> "
                                + fixName(entry.getPath().lastSegment()) + ';' + NL;
                    }
                }
            }
        }
        dot += "}";
        try {
            FileWriter fileWriter = new FileWriter("/dependencies.dot");
            fileWriter.write(dot);
            fileWriter.close();
        } catch (IOException e) {
            DependenciesPlugin.error(111, "Error writing dot file /dependencies.dot", e);
        }
    } catch (CoreException e) {
        DependenciesPlugin.error(222, "Error determining dependencies", e);
    }
}

From source file:com.javadude.dependencies.commands.DeleteCommand.java

License:Open Source License

@Override
public void execute() {
    // delete the target dep from the project
    try {/*from ww w .j  ava2  s  .c o m*/
        IClasspathEntry[] rawClasspath = getDependency().getSource().getRawClasspath();
        List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>();
        for (IClasspathEntry entry : rawClasspath) {
            if (entry.getEntryKind() != IClasspathEntry.CPE_PROJECT
                    || !entry.getPath().equals(getDependency().getTarget().getPath())) {
                newClasspath.add(entry);
            } else {
                setDeletedPosition(newClasspath.size());
                setDeletedEntry(entry);
            }
        }
        rawClasspath = newClasspath.toArray(new IClasspathEntry[rawClasspath.length - 1]);
        getDependency().getSource().setRawClasspath(rawClasspath, null);
    } catch (JavaModelException e) {
        throw new RuntimeException("Trouble deleting!", e);
    }
}

From source file:com.javadude.dependencies.editparts.WorkspaceRootEditPart.java

License:Open Source License

@SuppressWarnings("rawtypes")
@Override/*from w  w  w .  j  a v a2 s .co  m*/
protected List getModelChildren() {
    List<String> geronimoLibs = new ArrayList<String>();

    // return the java projects
    IWorkspaceRoot root = (IWorkspaceRoot) getModel();
    List<IJavaProject> javaProjects = new ArrayList<IJavaProject>();
    Map<IPath, IJavaProject> projectFinder = new HashMap<IPath, IJavaProject>();
    for (int i = 0; i < root.getProjects().length; i++) {
        IProject project = root.getProjects()[i];
        try {
            if (project.isOpen() && project.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject javaProject = JavaCore.create(project);
                javaProjects.add(javaProject);
                projectFinder.put(javaProject.getPath(), javaProject);
            }
        } catch (CoreException e) {
            DependenciesPlugin.error(142, "Could not check project nature", e);
        }
    }

    // need to make this more efficient -- should only process deltas
    // set up dependencies
    DependencyManager.clear();

    for (Iterator iter = javaProjects.iterator(); iter.hasNext();) {
        IJavaProject project = (IJavaProject) iter.next();
        try {
            IClasspathEntry[] rawClasspath = project.getRawClasspath();
            for (int i = 0; i < rawClasspath.length; i++) {
                IClasspathEntry entry = rawClasspath[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                    IPath path = entry.getPath();
                    IJavaProject targetProject = projectFinder.get(path);
                    Dependency dependency = new Dependency(project, targetProject, entry.isExported());
                    DependencyManager.add(dependency);
                }
            }
        } catch (JavaModelException e) {
            DependenciesPlugin.error(342, "Could not get classpath", e);
        }
    }

    List<Object> results = new ArrayList<Object>(javaProjects);
    results.addAll(geronimoLibs);
    return results;
}

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

License:Open Source License

private static String getSourcepathEntry(IJavaProject project) {

    StringBuilder sourcepath = new StringBuilder();
    IClasspathEntry[] paths;/*from  ww w .  j a  v  a  2  s .co  m*/

    try {
        paths = project.getResolvedClasspath(true);
    } catch (JavaModelException e) {
        VJP.logError("Could not retrieve project classpaths.", e);
        return "";
    }

    for (IClasspathEntry entry : paths) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            sourcepath.append(getAbsolutePath(project, entry.getPath()));
            sourcepath.append(Config.LIST_SEPARATOR);
        } else if (entry.getSourceAttachmentPath() != null) {
            IPath path = entry.getSourceAttachmentPath();
            if (path.getFileExtension() == null) { //null for a directory
                sourcepath.append(path);
                sourcepath.append(Config.LIST_SEPARATOR);
            }
        }
    }
    if (sourcepath.length() > 0)
        sourcepath.setLength(sourcepath.length() - 1); //remove that trailing separator
    // VJP.logInfo(sourcepath.toString());
    return sourcepath.toString();
}

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

License:BSD License

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

    try {/*  ww w .j a  v  a  2 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.//from   w  ww.  ja  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 www .ja  va 2  s  .co 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);
        }
    }
}

From source file:com.liferay.ide.core.model.internal.GenericResourceBundlePathService.java

License:Open Source License

/**
 * @param project/*from  w w w  .  j a va 2  s  .c om*/
 * @return
 */
final List<Path> computeRoots(IProject project) {
    List<Path> roots = new ArrayList<Path>();

    if (project != null) {
        IClasspathEntry[] cpEntries = CoreUtil.getClasspathEntries(project);

        for (IClasspathEntry iClasspathEntry : cpEntries) {
            if (IClasspathEntry.CPE_SOURCE == iClasspathEntry.getEntryKind()) {
                IPath entryPath = WORKSPACE_ROOT.getFolder(iClasspathEntry.getPath()).getLocation();
                String fullPath = entryPath.toOSString();
                Path sapphirePath = new Path(fullPath);
                roots.add(sapphirePath);
            }
        }
    }

    return roots;
}

From source file:com.liferay.ide.core.util.CoreUtil.java

License:Open Source License

public final static List<IFolder> getSourceFolders(final IJavaProject project) {
    final List<IFolder> folders = new ArrayList<IFolder>();

    if (project != null) {
        final IClasspathEntry[] entries = project.readRawClasspath();

        for (final IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().segmentCount() > 0) {
                IContainer container = null;

                if (entry.getPath().segmentCount() == 1) {
                    container = ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().segment(0));
                } else {
                    container = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath());
                }/*from   w w w  . j a  va 2 s  . c o  m*/

                if (!folders.contains(container) && container instanceof IFolder) {
                    folders.add((IFolder) container);
                }
            }
        }
    }

    return folders;
}

From source file:com.liferay.ide.layouttpl.core.facet.LayoutTplPluginFacetInstall.java

License:Open Source License

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

    try {//from   w  ww  .j  a  va  2s.c o  m
        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) {
        // no need to report errors
    }
}