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

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

Introduction

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

Prototype

int CPE_SOURCE

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a folder containing package fragments with source code to be compiled.

Usage

From source file:net.rim.ejde.internal.util.ImportUtils.java

License:Open Source License

/**
 * Applies the Java Path exclusion patterns to a given project and returns the list of filtered IClasspathEntry
 *
 * @param eclipseProject/*from   w  w  w  . j  a  v a  2s  .  c o  m*/
 * @param legacyProject
 * @param originalClasspathEntries
 * @return
 */
static public List<IClasspathEntry> applyExclusionPatterns(IProject eclipseProject, Project legacyProject,
        List<IClasspathEntry> originalClasspathEntries) {
    if (null == eclipseProject) {
        throw new IllegalArgumentException("Can't process undefined Eclipse project!");
    }

    if (null == legacyProject) {
        throw new IllegalArgumentException("Can't process undefined legacy project!");
    }

    if (null == originalClasspathEntries) {
        throw new IllegalArgumentException("Can't process undefined Eclipse classpath entries!");
    }

    // TODO: call this when importing projects, rather than from the
    // Compilation Participant
    List<WorkspaceFile> excludedWorkspaceFiles = getFilesToBeExcluded(legacyProject);

    if (excludedWorkspaceFiles.isEmpty() && originalClasspathEntries.isEmpty()) {
        return originalClasspathEntries;
    }

    List<IClasspathEntry> excludedClasspathEntries = new ArrayList<IClasspathEntry>();
    HashMap<IPath, IClasspathEntry> filterMap = new HashMap<IPath, IClasspathEntry>();
    String projectNamePattern = IPath.SEPARATOR + eclipseProject.getName() + IPath.SEPARATOR;
    List<IPath> exclusionPatterns;
    IPath classpathEntryPath, exclusionPatternPath;
    boolean forProject;
    String lastSegment;
    IFolder folder;
    IPath srcLocation;
    IClasspathEntry newEntry;
    IPath[] excludedPaths;
    File file;
    String workspaceFilePath;
    String packageId;

    for (IClasspathEntry entry : originalClasspathEntries) {
        exclusionPatterns = new ArrayList<IPath>();

        classpathEntryPath = entry.getPath();

        if (IClasspathEntry.CPE_SOURCE == entry.getEntryKind()) {
            lastSegment = classpathEntryPath.lastSegment();

            if (lastSegment.equalsIgnoreCase(
                    ImportUtils.getImportPref(ResourceBuilder.LOCALE_INTERFACES_FOLDER_NAME))) {
                continue;
            }

            folder = eclipseProject.getFolder(lastSegment);

            if (folder.isDerived() || !folder.exists()) {
                continue;
            }

            forProject = classpathEntryPath.toString().startsWith(projectNamePattern);

            if (forProject) {
                srcLocation = folder.getLocation();

                if (srcLocation == null || srcLocation.isEmpty()) {
                    return originalClasspathEntries;
                }

                for (WorkspaceFile workspaceFile : excludedWorkspaceFiles) {
                    workspaceFilePath = workspaceFile.toString();
                    file = workspaceFile.getFile();

                    if (null != file && file.exists() && file.isFile()) {
                        // Fix for IDT 149988 - Check type of source folder and file type to prevent duplication for exclusion
                        // patterns
                        if (lastSegment.equalsIgnoreCase(
                                ImportUtils.getImportPref(LegacyImportHelper.PROJECT_SRC_FOLDER_NAME_KEY))) {
                            if (!workspaceFile.getIsJava()) {
                                continue;
                            }
                        } else {
                            if (workspaceFile.getIsJava()) {
                                continue;
                            }
                        }

                        if (workspaceFile.getIsJava() || workspaceFile.getIsResourceHeader()
                                || workspaceFile.getIsResource()) {
                            packageId = IConstants.EMPTY_STRING;
                            try {
                                packageId = PackageUtils.getFilePackageString(file, legacyProject);
                            } catch (CoreException e) {
                                _log.error(e.getMessage());
                                packageId = IConstants.EMPTY_STRING;
                            }
                            workspaceFilePath = File.separator + packageId + File.separator + workspaceFilePath;
                        }
                        exclusionPatternPath = getExclusionPattern(workspaceFile, lastSegment, eclipseProject,
                                legacyProject);

                        if (!exclusionPatternPath.isEmpty()) {
                            exclusionPatterns.add(exclusionPatternPath);
                        }
                    }
                }
            }

            if (exclusionPatterns.isEmpty()) {
                excludedPaths = new IPath[] {};
            } else {
                excludedPaths = exclusionPatterns.toArray(new IPath[exclusionPatterns.size()]);
            }

            newEntry = JavaCore.newSourceEntry(classpathEntryPath, entry.getInclusionPatterns(), excludedPaths,
                    entry.getOutputLocation(), entry.getExtraAttributes());
            filterMap.put(classpathEntryPath, newEntry);
        } else {// IClasspathEntry of type other than CPE_SOURCE
            filterMap.put(classpathEntryPath, entry);
        }
    }

    IPath elementPath;

    for (IClasspathEntry element : originalClasspathEntries) {
        elementPath = element.getPath();
        newEntry = filterMap.get(elementPath);
        if (null != newEntry) {
            excludedClasspathEntries.add(newEntry);
        }
    }

    return excludedClasspathEntries;
}

From source file:net.rim.ejde.internal.util.LegacyModelUtil.java

License:Open Source License

static public void setSource(Project proj, IJavaProject eclipseJavaProject, String source) {
    if (null == proj)// Don't process for a non existing legacy project
        return;//from  w  w w.  ja va  2s.co  m

    if (StringUtils.isBlank(source))// Don't process for a non existing
        // source folder
        return;

    if (null == eclipseJavaProject)// Don't process for a non existing
        // Eclipse equivalent
        return;

    try {
        IClasspathEntry[] classpathEntries = eclipseJavaProject.getRawClasspath();

        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot workspaceRoot = workspace.getRoot();

        IPath classpathEntryPath;
        String classpathEntryLastSegment;
        IFolder folder;

        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (IClasspathEntry.CPE_SOURCE == classpathEntry.getEntryKind()) {
                classpathEntryPath = classpathEntry.getPath();
                classpathEntryLastSegment = classpathEntryPath.lastSegment();

                if (source.equalsIgnoreCase(classpathEntryLastSegment)) {// if
                    // the
                    // string
                    // can't
                    // be
                    // matched
                    // to
                    // an
                    // existing
                    // classpath
                    // entry
                    // why
                    // should
                    // we
                    // add
                    // it
                    // to
                    // the
                    // legacy
                    // metadata?!
                    if (ImportUtils.getImportPref(ResourceBuilder.LOCALE_INTERFACES_FOLDER_NAME)
                            .equalsIgnoreCase(classpathEntryLastSegment)) {
                        return;
                    }
                    if (!classpathEntryPath.toOSString().equals(IConstants.EMPTY_STRING)) {

                        folder = workspaceRoot.getFolder(classpathEntryPath);

                        if (folder.isDerived())// Don't process for
                            // Eclipse
                            // derived directories
                            return;
                    }

                }
            }
        }
    } catch (JavaModelException e) {
        log.error(e.getMessage(), e);
    }

    String udata = proj.getUserData();

    if (StringUtils.isNotBlank(udata)) {
        int idx1 = udata.indexOf(DELIM_SECTION);
        if (idx1 >= 0) {
            int idx2 = udata.indexOf(DELIM_SECTION, idx1 + 1);
            String udata_new = (idx1 > 0 ? udata.substring(0, idx1) : EMPTY_STRING) + DELIM_SECTION + source
                    + (idx2 > idx1 ? udata.substring(idx2) : EMPTY_STRING);
            if (!udata.equals(udata_new)) {
                proj.setUserData(udata_new);
            }
        }
    } else {
        proj.setUserData(DELIM_SECTION + source);
    }
}

From source file:net.rim.ejde.internal.util.LegacyModelUtil.java

License:Open Source License

static public void syncSources(Project proj, IJavaProject eclipseJavaProject) {
    if (null == proj)// Don't process for a non existing legacy project
        return;//from  w ww  . j  a v a  2  s.  com

    if (null == eclipseJavaProject)// Don't process for a non existing
        // Eclipse equivalent
        return;

    String sources = "";
    StringBuffer buf = new StringBuffer();

    try {
        IClasspathEntry[] classpathEntries = eclipseJavaProject.getRawClasspath();

        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot workspaceRoot = workspace.getRoot();

        IPath classpathEntryPath;
        String classpathEntryLastSegment;
        IFolder folder;

        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (IClasspathEntry.CPE_SOURCE == classpathEntry.getEntryKind()) {
                classpathEntryPath = classpathEntry.getPath();
                classpathEntryLastSegment = classpathEntryPath.lastSegment();

                if (ImportUtils.getImportPref(ResourceBuilder.LOCALE_INTERFACES_FOLDER_NAME)
                        .equalsIgnoreCase(classpathEntryLastSegment)) {
                    continue;
                }

                if (classpathEntryPath.toOSString().equals(IConstants.EMPTY_STRING)) {
                    continue;
                }

                folder = workspaceRoot.getFolder(classpathEntryPath);

                if (folder.isDerived()) {// Don't process for Eclipse
                    // derived directories
                    continue;
                }

                buf.append(DELIM_SECTION + classpathEntryLastSegment);
            }
        }
    } catch (JavaModelException e) {
        log.error(e.getMessage(), e);
    }

    sources = buf.toString();

    String udata = proj.getUserData();

    if (StringUtils.isNotBlank(udata)) {

        int idx1 = udata.indexOf(DELIM_SECTION);

        if (idx1 >= 0) {
            int idx2 = udata.indexOf(DELIM_SECTION, idx1 + 1);
            String udata_new = (idx1 > 0 ? udata.substring(0, idx1) : EMPTY_STRING) + DELIM_SECTION + sources
                    + (idx2 > idx1 ? udata.substring(idx2) : EMPTY_STRING);
            if (!udata.equals(udata_new)) {
                proj.setUserData(udata_new);
            }
        }
    } else {
        proj.setUserData(sources);
    }
}

From source file:net.rim.ejde.internal.util.PackageUtils.java

License:Open Source License

/**
 * Gets all the source folders of the given <code>iJavaProject</code>.
 *
 * @param iJavaProject/* w w  w  . j  a  va2s  .  c o m*/
 * @return
 */
public static ArrayList<IContainer> getAllSrcFolders(IJavaProject iJavaProject) {
    ArrayList<IContainer> result = new ArrayList<IContainer>();
    IProject iProject = iJavaProject.getProject();
    IClasspathEntry[] classPathEntries = null;
    IFolder srcFolder = null;
    IPath srcFolderPath = null;
    IPath relativeSrcFolderPath = null;
    try {
        classPathEntries = iJavaProject.getRawClasspath();
        for (IClasspathEntry classPathEntry : classPathEntries) {
            if (classPathEntry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            srcFolderPath = classPathEntry.getPath();
            if (srcFolderPath.segment(0).equals(iProject.getName())) {
                // remove the project name from the path
                relativeSrcFolderPath = srcFolderPath.removeFirstSegments(1);
            }
            if (relativeSrcFolderPath == null || relativeSrcFolderPath.isEmpty()) {
                result.add(iProject);
            } else {
                srcFolder = iProject.getFolder(relativeSrcFolderPath);
                if (srcFolder.exists()) {
                    result.add(srcFolder);
                }
            }
        }
    } catch (Throwable e) {
        _logger.error(e.getMessage(), e);
    }
    return result;
}

From source file:net.rim.ejde.internal.util.ProjectUtils.java

License:Open Source License

/**
 * Gets a file that exists in an Eclipse project.
 * <p>/*from   ww w  .  ja va2s. c  o m*/
 * TODO: Someone can probably optimize this method better. Like using some of the IWorkspaceRoot.find*() methods...
 *
 * @param project
 *            the Eclipse project the file belongs to
 * @param file
 *            the File which is in the Eclipse project
 * @return the Eclipse resource file associated with the file
 */
public static IResource getResource(IProject project, File file) {
    IJavaProject javaProject = JavaCore.create(project);
    IPath filePath = new Path(file.getAbsolutePath());
    try {
        IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);

        IFile input = null;
        // Look for a source folder
        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

                // Try to resolve the source container
                IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();
                IResource resource = workspaceRoot.findMember(classpathEntry.getPath());
                if (resource instanceof IContainer) {
                    IContainer sourceContainer = (IContainer) resource;
                    File sourceContainerFile = resource.getLocation().toFile();
                    IPath sourceFolderPath = new Path(sourceContainerFile.getAbsolutePath());

                    // See if the file path is within this source folder
                    // path
                    if (sourceFolderPath.isPrefixOf(filePath)) {
                        int segmentCount = sourceFolderPath.segmentCount();
                        IPath relativePath = filePath.removeFirstSegments(segmentCount);
                        input = sourceContainer.getFile(relativePath);
                        break;
                    }
                }
            }
        }
        return input;
    } catch (JavaModelException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:net.sf.eclipse.tomcat.TomcatBootstrap.java

License:Open Source License

private void getClassPathEntries(IClasspathEntry[] entries, IJavaProject prj, ArrayList data,
        List selectedPaths, ArrayList visitedProjects, IPath outputPath) {
    for (IClasspathEntry entrie : entries) {
        IClasspathEntry entry = entrie;/*  w w w. j av  a2 s  .  co  m*/
        IPath path = entry.getPath();
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            path = entry.getOutputLocation();
            if (path == null) {
                continue;
            }
        }
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            String prjName = entry.getPath().lastSegment();
            if (!visitedProjects.contains(prjName)) {
                visitedProjects.add(prjName);
                getClassPathEntries(prj.getJavaModel().getJavaProject(prjName), data, selectedPaths,
                        visitedProjects);
            }
            continue;
        } else if (!selectedPaths.contains(path.toFile().toString().replace('\\', '/'))) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && !entry.getPath().toString().equals("org.eclipse.jdt.launching.JRE_CONTAINER")) {

                // entires in container are only processed individually
                // if container itself is not selected

                IClasspathContainer container;
                try {
                    container = JavaCore.getClasspathContainer(path, prj);
                } catch (JavaModelException e1) {
                    TomcatLauncherPlugin.log(e1);
                    container = null;
                }

                if (container != null) {
                    getClassPathEntries(container.getClasspathEntries(), prj, data, selectedPaths,
                            visitedProjects, outputPath);
                }
            }
            continue;
        }

        IClasspathEntry[] tmpEntry = null;
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            try {
                tmpEntry = JavaCore.getClasspathContainer(path, prj).getClasspathEntries();
            } catch (JavaModelException e1) {
                TomcatLauncherPlugin.log(e1);
                continue;
            }
        } else {
            tmpEntry = new IClasspathEntry[1];
            tmpEntry[0] = JavaCore.getResolvedClasspathEntry(entry);
        }

        for (IClasspathEntry element : tmpEntry) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IResource res = prj.getProject().getWorkspace().getRoot().findMember(element.getPath());
                if (res != null) {
                    add(data, res);
                } else {
                    add(data, element.getPath());
                }
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath srcPath = entry.getOutputLocation();
                if (srcPath != null && !srcPath.equals(outputPath)) {
                    add(data, prj.getProject().getWorkspace().getRoot().findMember(srcPath));
                }
            } else {
                TomcatLauncherPlugin.log(">>> " + element);
                if (element.getPath() != null) {
                    add(data, element.getPath());
                }
            }
        }
    }
}

From source file:net.sf.eclipse.tomcat.TomcatProject.java

License:Open Source License

public void clearDefaultSourceEntries() throws CoreException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List cp = new ArrayList(entries.length + 1);
    for (IClasspathEntry entrie : entries) {
        if (entrie.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            cp.add(entrie);//w  w w  .  j  a v  a  2s  .c  o  m
        }
    }
    javaProject.setRawClasspath((IClasspathEntry[]) cp.toArray(new IClasspathEntry[cp.size()]), null);
}

From source file:net.sf.eclipse.tomcat.TomcatProjectWebclasspathPropertyPage.java

License:Open Source License

private void getClassPathEntries(IClasspathEntry[] entries, IJavaProject prj, ArrayList data,
        IPath outputPath) {//from ww  w  . j ava  2 s. c o m
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            String prjName = entry.getPath().lastSegment();
            if (!visitedProjects.contains(prjName)) {
                visitedProjects.add(prjName);
                getClassPathEntries(getJavaProject().getJavaModel().getJavaProject(prjName), data);
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            add(data, entry.getPath());
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath path = entry.getOutputLocation();
            if (path != null && !path.equals(outputPath)) {
                add(data, path);
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            if (!entry.getPath().toString().equals("org.eclipse.jdt.launching.JRE_CONTAINER")) {
                // Add container itself, as TomcatBootstrap can actually process them
                // at the moment
                // Basically, users will be able to choose b/w the whole container
                // or some artifacts enclosed by it
                add(data, entry.getPath());

                // Expand container and add its content as individual
                // elements
                IClasspathContainer container;
                try {
                    container = JavaCore.getClasspathContainer(entry.getPath(), prj);
                } catch (JavaModelException e) {
                    TomcatLauncherPlugin.log("failed to obtain classpath container '" + entry.getPath() + "'"
                            + " for project '" + prj.getProject().getName() + "'");
                    TomcatLauncherPlugin.log(e);
                    container = null;
                }

                if (container != null) {
                    getClassPathEntries(container.getClasspathEntries(), prj, data, outputPath);
                }
            }
        } else {
            add(data, entry.getPath());
        }
    }
}

From source file:net.sf.eclipsecs.core.builder.ProjectClassLoader.java

License:Open Source License

/**
 * Adds the contents of a project to list of URLs.
 * //from   w  w  w  .  ja v  a  2s.  c o m
 * @param project the project
 * @param cpURLs the resulting list
 * @param isReferenced true if a referenced project is processed
 */
private static void addToClassPath(IProject project, List<URL> cpURLs, boolean isReferenced,
        Collection<IProject> processedProjects) {

    try {

        // this project has already been added
        if (processedProjects.contains(project)) {
            return;
        } else {
            processedProjects.add(project);
        }

        // get the java project
        IJavaProject javaProject = JavaCore.create(project);

        // get the resolved classpath of the project
        IClasspathEntry[] cpEntries = javaProject.getResolvedClasspath(true);

        // iterate over classpath to create classpath urls
        int size = cpEntries.length;
        for (int i = 0; i < size; i++) {

            int entryKind = cpEntries[i].getEntryKind();

            // handle a source path
            if (IClasspathEntry.CPE_SOURCE == entryKind) {

                handleSourcePath(project, cpURLs, cpEntries[i], javaProject);
            }
            // handle a project reference
            else if (IClasspathEntry.CPE_PROJECT == entryKind) {

                handleRefProject(cpURLs, cpEntries[i], processedProjects);
            }
            // handle a library entry
            else if (IClasspathEntry.CPE_LIBRARY == entryKind) {

                handleLibrary(project, cpURLs, cpEntries[i]);
            }
            // cannot happen since we use a resolved classpath
            else {

                // log as exception
                CheckstylePluginException ex = new CheckstylePluginException(
                        NLS.bind(Messages.errorUnknownClasspathEntry, cpEntries[i].getPath()));
                CheckstyleLog.log(ex);
            }
        }
    } catch (JavaModelException jme) {
        CheckstyleLog.log(jme);
    }
}

From source file:net.sf.eclipsecs.core.projectconfig.filters.NonSrcDirsFilter.java

License:Open Source License

/**
 * Gets all source paths of a project.//from   w  w w  .  java2  s . c  o m
 * 
 * @param project the project
 * @return the list of source paths
 */
private List<IPath> getSourceDirPaths(IProject project) {

    List<IPath> sourceDirs = new ArrayList<IPath>();

    try {
        if (project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(project);
            IClasspathEntry[] cp = javaProject.getResolvedClasspath(true);
            for (int i = 0; i < cp.length; i++) {
                if (cp[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    sourceDirs.add(cp[i].getPath());
                }
            }
        }
    } catch (JavaModelException e) {
        CheckstyleLog.log(e);
    } catch (CoreException e) {
        CheckstyleLog.log(e);
    }

    return sourceDirs;
}