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

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

Introduction

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

Prototype

int getContentKind();

Source Link

Document

Returns the kind of files found in the package fragments identified by this classpath entry.

Usage

From source file:org.eclipse.jst.j2ee.internal.web.operations.ClasspathUtilities.java

License:Open Source License

static protected boolean entryExists(IClasspathEntry entry, IClasspathEntry[] col) {
    for (int i = 0; i < col.length; i++) {
        IClasspathEntry otherEntry = col[i];
        if (entry.getContentKind() != otherEntry.getContentKind())
            continue;

        if (entry.getEntryKind() != otherEntry.getEntryKind())
            continue;

        if (entry.isExported() != otherEntry.isExported())
            continue;

        if (!entry.getPath().equals(otherEntry.getPath()))
            continue;
        return true;
    }/* w  ww .  ja  v a  2  s. co m*/
    return false;
}

From source file:org.eclipse.pde.api.tools.tests.util.ProjectUtils.java

License:Open Source License

/**
 * Delegate equals method to cover the test cases where we want to insert an
 * updated element and one with the same path/type/kind is already there.
 * /*w  w  w .ja v  a 2 s  . c  o  m*/
 * @param e1
 * @param e2
 * @return
 */
private static boolean entriesEqual(IClasspathEntry e1, IClasspathEntry e2) {
    return e1.equals(e2) || (e1.getEntryKind() == e2.getEntryKind()
            && e1.getContentKind() == e2.getContentKind() && e1.getPath().equals(e2.getPath()));
}

From source file:org.eclipse.pde.internal.ui.properties.SelfHostingPropertyPage.java

License:Open Source License

private String[] getOutputFolders() {
    IProject project = (IProject) getElement().getAdapter(IProject.class);
    ArrayList<String> list = new ArrayList<String>();
    try {// w ww  .  j  a v  a 2 s .co  m
        if (project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject jProject = JavaCore.create(project);
            list.add(jProject.getOutputLocation().toString());
            IClasspathEntry[] entries = jProject.getRawClasspath();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                        && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    IPath path = entry.getOutputLocation();
                    if (path != null)
                        list.add(path.toString());
                }
            }
        }
    } catch (JavaModelException e) {
    } catch (CoreException e) {
    }
    return list.toArray(new String[list.size()]);
}

From source file:org.flowerplatform.editor.java.propertypage.remote.JavaProjectPropertyPageService.java

License:Open Source License

public Object getClasspathEntries(ServiceInvocationContext context, List<PathFragment> path) {
    @SuppressWarnings("unchecked")
    Pair<File, String> node = (Pair<File, String>) GenericTreeStatefulService.getNodeByPathFor(path, null);
    File projectFile = node.a;// w w w .  j  a  v a2  s.  c o  m
    File wd = ProjectsService.getInstance().getProjectToWorkingDirectoryAndIProjectMap().get(projectFile).a;

    IProject project = ProjectsService.getInstance().getProjectToWorkingDirectoryAndIProjectMap()
            .get(projectFile).b;
    IJavaProject javaProject = JavaCore.create(project);

    List<String> srcFolders = new ArrayList<String>();
    List<String> projects = new ArrayList<String>();
    List<String> libraries = new ArrayList<String>();

    try {
        if (!project.getFile(IJavaProject.CLASSPATH_FILE_NAME).exists()) {
            return null;
        }
        @SuppressWarnings("restriction")
        IClasspathEntry[][] entries = ((JavaProject) javaProject).readFileEntriesWithException(null);
        for (IClasspathEntry entry : entries[0]) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                srcFolders.add(entry.getPath()
                        .makeRelativeTo(project.getFolder(ProjectsService.LINK_TO_PROJECT).getFullPath())
                        .toFile().getPath());
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                File file = ProjectsService.getInstance().getFileFromProjectWrapperResource(
                        ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().lastSegment()));
                projects.add(CommonPlugin.getInstance().getPathRelativeToFile(file, wd));
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                    && entry.getContentKind() == IPackageFragmentRoot.K_BINARY) {
                IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(entry.getPath());
                File file = ProjectsService.getInstance().getFileFromProjectWrapperResource(resource);
                libraries.add(CommonPlugin.getInstance().getPathRelativeToFile(file, wd));
            }
        }

    } catch (CoreException | IOException e) {
        logger.error("Exception thrown while getting java classpath entries for {}", project.getName(), e);
        return null;
    }
    return new Object[] { srcFolders, projects, libraries };
}

From source file:org.jactr.eclipse.core.parser.ProjectSensitiveParserImportDelegate.java

License:Open Source License

@Override
public URL resolveURL(String url, URL baseURL) {
    try {/*w w  w.  j  a  va  2s  .c  om*/
        IJavaProject javaProject = JavaCore.create(_project);
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(false);

        for (IClasspathEntry entry : entries)
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT
                    && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = entry.getPath();
                if (LOGGER.isDebugEnabled())
                    LOGGER.debug("checking " + entry + " at " + path);

                IProject tmpProject = ResourcesPlugin.getWorkspace().getRoot().getProject(path.lastSegment());
                IFolder modelsFolder = tmpProject.getFolder("models");
                if (modelsFolder == null || !modelsFolder.exists())
                    continue;

                IFile modelFile = modelsFolder.getFile(url);
                if (modelFile == null || !modelFile.exists())
                    continue;

                // CorePlugin.debug("Found a matching file at "
                // + modelFile.getFullPath());

                if (javaProject.isOnClasspath(modelFile)) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("Is on classpath, returning url");

                    URL rtn = modelFile.getLocation().toFile().toURI().toURL();

                    // CorePlugin.debug(String.format("On path at %s",
                    // rtn.toExternalForm()));

                    return rtn;
                } else if (LOGGER.isDebugEnabled())
                    LOGGER.debug("is not on classpath");
            }
    } catch (Exception e) {
        CorePlugin.error("Failed to extract location info for " + url, e);
    }
    return super.resolveURL(url, baseURL);
}

From source file:org.jboss.tools.portlet.ui.internal.wizard.xpl.NewJavaClassWizardPageEx.java

License:Open Source License

/**
 * /*from  w  w  w . ja v  a  2  s .co m*/
 **/
private IFolder getDefaultJavaSourceFolder(IProject project) {

    if (project == null)
        return null;
    IPackageFragmentRoot[] sources = J2EEProjectUtilities.getSourceContainers(project);
    // Try and return the first source folder
    if (sources.length > 0) {
        try {
            return (IFolder) sources[0].getCorrespondingResource();
        } catch (Exception e) {
            return null;
        }
    }
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        return null;
    }
    try {
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(false);
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = entry.getPath();
                IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
                if (resource instanceof IFolder) {
                    return (IFolder) resource;
                }
            }
        }
    } catch (JavaModelException e) {
        // ignore
    }
    return null;
}

From source file:org.key_project.key4eclipse.resources.util.KeYResourcesUtil.java

License:Open Source License

/**
 * Returns a {@link LinkedList} with all Java source folders ais {@link IPath}.
 * @param project - the project to search the source folders for.
 * @return the {@link LinkedList} with the source folders
 *//*ww w .  ja v  a 2  s  .  com*/
public static LinkedList<IPath> getAllJavaSrcFolders(IProject project) {
    LinkedList<IPath> srcFolders = new LinkedList<IPath>();

    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] classpathEntries;
    try {
        classpathEntries = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < classpathEntries.length; i++) {
            IClasspathEntry entry = classpathEntries[i];
            if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = entry.getPath();
                srcFolders.add(path);
            }
        }
    } catch (JavaModelException e) {
        srcFolders = new LinkedList<IPath>();
    }
    return srcFolders;
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Returns all source {@link IPackageFragmentRoot}s.
 * @param javaProject The {@link IJavaProject} to read source {@link IPackageFragmentRoot}s from.
 * @return The found {@link IPackageFragmentRoot}s.
 * @throws JavaModelException Occurred Exception.
 *//*www.  j  a v  a2 s .  c o  m*/
public static List<IPackageFragmentRoot> getSourcePackageFragmentRoots(IJavaProject javaProject)
        throws JavaModelException {
    List<IPackageFragmentRoot> result = new LinkedList<IPackageFragmentRoot>();
    if (javaProject != null && javaProject.exists()) {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE
                    && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
                CollectionUtil.addAll(result, roots);
            }
        }
    }
    return result;
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Internal helper method that is used in {@link #getSourceLocations(IProject)}
 * to compute the source path. It is required to solve cycles in project dependencies.
 * @param project The given Project./* w w  w . ja v  a  2 s  .  c o m*/
 * @param alreadyHandledProjects The already handled {@link IProject} that don't need to be analysed again.
 * @return The found source locations in the file system.
 * @throws JavaModelException Occurred Exception.
 */
private static List<File> getSourceLocations(IProject project, Set<IProject> alreadyHandledProjects)
        throws JavaModelException {
    List<File> result = new LinkedList<File>();
    if (project != null) {
        Assert.isNotNull(alreadyHandledProjects);
        alreadyHandledProjects.add(project);
        IJavaProject javaProject = getJavaProject(project);
        if (javaProject != null && javaProject.exists()) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            for (IClasspathEntry entry : entries) {
                if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    List<File> location = getLocationFor(javaProject, entry, IPackageFragmentRoot.K_SOURCE,
                            alreadyHandledProjects);
                    if (location != null) {
                        result.addAll(location);
                    }
                }
            }
        }
    }
    return result;
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Internal helper method that is used in {@link #getSourceResources(IProject)}
 * to compute the source path. It is required to solve cycles in project dependencies.
 * @param project The given Project.// w  ww .j a  v  a 2 s  .  c  o  m
 * @param alreadyHandledProjects The already handled {@link IProject} that don't need to be analysed again.
 * @return The found source {@link IResource}s in the workspace.
 * @throws JavaModelException Occurred Exception.
 */
private static List<IResource> getSourceResources(IProject project, Set<IProject> alreadyHandledProjects)
        throws JavaModelException {
    List<IResource> result = new LinkedList<IResource>();
    if (project != null) {
        Assert.isNotNull(alreadyHandledProjects);
        alreadyHandledProjects.add(project);
        IJavaProject javaProject = getJavaProject(project);
        if (javaProject != null && javaProject.exists()) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            for (IClasspathEntry entry : entries) {
                if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    List<IResource> location = getResourceFor(javaProject, entry, IPackageFragmentRoot.K_SOURCE,
                            alreadyHandledProjects);
                    if (location != null) {
                        result.addAll(location);
                    }
                }
            }
        }
    }
    return result;
}