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:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

public static List<IClasspathEntry> resolveDependentProjectClasspath(IClasspathEntry projEntry,
        IProject requiredProj) {//from   w w w. j  av  a2 s .co m
    // add all output locations and exported classpath entities
    // AspectJ compiler doesn't understand the concept of a java project
    List<IClasspathEntry> actualEntries = new ArrayList<IClasspathEntry>();

    try {
        JavaProject requiredJavaProj = (JavaProject) JavaCore.create(requiredProj);
        // bug 288395 Do not use the default mechanism for resolving classpath here
        // this will look into jar files at the Classpath header in the jar's manifest
        // and include jar files that are potentially missing, but have no effect on
        // the build.
        Object resolvedClasspath = requiredJavaProj.resolveClasspath(requiredJavaProj.getRawClasspath(), true,
                false);
        IClasspathEntry[] requiredEntries = extractRequiredEntries(resolvedClasspath);
        for (int i = 0; i < requiredEntries.length; i++) {
            IClasspathEntry requiredEntry = requiredEntries[i];
            if (requiredEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

                // always add source entries even if not explicitly exported
                // don't add the source folder itself, but instead add the outfolder
                IPath outputLocation = requiredEntry.getOutputLocation();
                if (outputLocation != null) {
                    IAccessRule[] rules = projEntry.getAccessRules();
                    IClasspathAttribute[] attributes = projEntry.getExtraAttributes();

                    // only add the out folder if it already exists
                    if (requiredProj.getFolder(outputLocation.removeFirstSegments(1)).exists()) {
                        IClasspathEntry outFolder = JavaCore.newLibraryEntry(outputLocation,
                                requiredEntry.getPath(), requiredProj.getFullPath(), rules, attributes,
                                projEntry.isExported());
                        actualEntries.add(outFolder);
                    }
                }
            } else if (requiredEntry.isExported()) {
                // must recur through this entry and add entries that it contains
                actualEntries.addAll(resolveClasspath(requiredEntry, requiredProj));

            }
        } // for (int i = 0; i < requiredEntries.length; i++)

        IPath outputLocation = requiredJavaProj.getOutputLocation();
        // Output location may not exist.  Do not put output location of required project
        // on path unless it exists
        boolean exists = false;
        // bug 244330 check to see if the project folder is also the output folder
        if (outputLocation.segmentCount() == 1) {
            exists = true;
        } else {
            if (requiredProj.getWorkspace().getRoot().getFolder(outputLocation).exists()) {
                exists = true;
            }
        }

        if (exists) {
            IClasspathEntry outFolder = JavaCore.newLibraryEntry(outputLocation, null,
                    requiredProj.getFullPath());
            actualEntries.add(outFolder);
        }
    } catch (JavaModelException e) {
    }
    return actualEntries;
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

private static String toEntryKind(String entryStr) {
    int entry = 0;
    if (entryStr.equals("SOURCE")) { //$NON-NLS-1$
        entry = IClasspathEntry.CPE_SOURCE;
    } else if (entryStr.equals("LIBRARY")) { //$NON-NLS-1$
        entry = IClasspathEntry.CPE_LIBRARY;
    } else if (entryStr.equals("PROJECT")) { //$NON-NLS-1$
        entry = IClasspathEntry.CPE_PROJECT;
    } else if (entryStr.equals("VARIABLE")) { //$NON-NLS-1$
        entry = IClasspathEntry.CPE_VARIABLE;
    } else if (entryStr.equals("CONTAINER")) { //$NON-NLS-1$
        entry = IClasspathEntry.CPE_CONTAINER;
    }//w ww  .ja  va 2s.c  o  m
    return new Integer(entry).toString();
}

From source file:org.eclipse.ajdt.core.BuildConfig.java

License:Open Source License

/**
 * Returns all of the currently included source files in a project
 * This list is cached and reset every build (or on request by calling flushIncludedSourceFileCache)
 * @param project// w ww  .j a  va  2  s.c  o  m
 * @return a list of IFiles
 */
public static Set<IFile> getIncludedSourceFiles(IProject project) {
    if (projectsToIncludedSourceFiles.get(project) instanceof List) {
        return projectsToIncludedSourceFiles.get(project);
    }
    Set<IFile> sourceFiles = new HashSet<IFile>();
    try {
        IJavaProject jp = JavaCore.create(project);
        IClasspathEntry[] cpes = jp.getRawClasspath();
        for (int i = 0; i < cpes.length; i++) {
            if ((cpes[i] instanceof ClasspathEntry) && (cpes[i].getEntryKind() == IClasspathEntry.CPE_SOURCE)) {
                ClasspathEntry cp = (ClasspathEntry) cpes[i];
                char[][] incl = cp.fullInclusionPatternChars();
                char[][] excl = cp.fullExclusionPatternChars();
                IPath path = cpes[i].getPath();
                IResource res = project.findMember(path.removeFirstSegments(1));
                if ((res != null) && (res instanceof IContainer)) {
                    List<IFile> l = allFiles((IContainer) res);
                    for (IFile file : l) {
                        if (!Util.isExcluded(file, incl, excl)) {
                            sourceFiles.add(file);
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
    }
    projectsToIncludedSourceFiles.put(project, sourceFiles);
    return sourceFiles;
}

From source file:org.eclipse.ajdt.core.BuildConfig.java

License:Open Source License

/**
 * Experimental version of above that uses a set, not a list
 * @param project//  w w  w . j ava  2s .  c om
 * @return
 */
public static Set<IFile> getIncludedSourceFilesSet(IProject project) {
    if (projectsToIncludedSourceFiles.get(project) instanceof List) {
        return projectsToIncludedSourceFiles.get(project);
    }
    Set<IFile> sourceFiles = new HashSet<IFile>();
    try {
        IJavaProject jp = JavaCore.create(project);
        IClasspathEntry[] cpes = jp.getRawClasspath();
        for (int i = 0; i < cpes.length; i++) {
            if ((cpes[i] instanceof ClasspathEntry) && (cpes[i].getEntryKind() == IClasspathEntry.CPE_SOURCE)) {
                ClasspathEntry cp = (ClasspathEntry) cpes[i];
                char[][] incl = cp.fullInclusionPatternChars();
                char[][] excl = cp.fullExclusionPatternChars();
                IPath path = cpes[i].getPath();
                IResource res = project.findMember(path.removeFirstSegments(1));
                if ((res != null) && (res instanceof IContainer)) {
                    List<IFile> l = allFiles((IContainer) res);
                    for (IFile file : l) {
                        if (!Util.isExcluded(file, incl, excl)) {
                            sourceFiles.add(file);
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
    }
    projectsToIncludedSourceFiles.put(project, sourceFiles);
    return sourceFiles;
}

From source file:org.eclipse.ajdt.core.builder.AJBuilder.java

License:Open Source License

/**
 * Returns the CPE_SOURCE classpath entries for the given IJavaProject
 * //from   www .jav  a2s.c o  m
 * @param IJavaProject
 */
private IClasspathEntry[] getSrcClasspathEntry(IJavaProject javaProject) throws JavaModelException {
    List<IClasspathEntry> srcEntries = new ArrayList<IClasspathEntry>();
    if (javaProject == null) {
        return new IClasspathEntry[0];
    }
    IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
    for (int j = 0; j < cpEntry.length; j++) {
        IClasspathEntry entry = cpEntry[j];
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            srcEntries.add(entry);
        }
    }
    return (IClasspathEntry[]) srcEntries.toArray(new IClasspathEntry[srcEntries.size()]);
}

From source file:org.eclipse.ajdt.core.buildpath.BuildConfigurationUtils.java

License:Open Source License

public static void saveBuildConfiguration(IFile ifile) {
    File file = ifile.getLocation().toFile();
    IProject project = ifile.getProject();
    try {/* w  w w  . j  av  a 2s .c  o m*/
        IJavaProject jp = JavaCore.create(project);
        IClasspathEntry[] entries = jp.getRawClasspath();
        List srcIncludes = new ArrayList();
        List srcExcludes = new ArrayList();
        List srcInclusionpatterns = new ArrayList();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath srcpath = entry.getPath();
                srcpath = srcpath.removeFirstSegments(1);
                String path = srcpath.toString().trim();
                if (!path.endsWith("/")) { //$NON-NLS-1$
                    path = path + "/"; //$NON-NLS-1$
                }
                srcIncludes.add(path);
                IPath[] inclusions = entry.getInclusionPatterns();
                for (int j = 0; j < inclusions.length; j++) {
                    srcInclusionpatterns.add((path.length() > 1 ? path : "") + inclusions[j]); //$NON-NLS-1$   
                }
                IPath[] exclusions = entry.getExclusionPatterns();
                for (int j = 0; j < exclusions.length; j++) {
                    srcExcludes.add((path.length() > 1 ? path : "") + exclusions[j]); //$NON-NLS-1$
                }
            }
        }
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            printProperties(bw, "src.includes", srcIncludes); //$NON-NLS-1$
            printProperties(bw, "src.excludes", srcExcludes); //$NON-NLS-1$
            printProperties(bw, "src.inclusionpatterns", srcInclusionpatterns); //$NON-NLS-1$   
        } catch (IOException e) {
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                }
            }
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.buildpath.BuildConfigurationUtils.java

License:Open Source License

public static void applyBuildConfiguration(IFile ifile) {
    File file = ifile.getLocation().toFile();
    BufferedReader br = null;/*from w w  w  . jav  a 2  s  .  com*/
    try {
        IJavaProject project = JavaCore.create(ifile.getProject());
        List classpathEntries = new ArrayList();
        IClasspathEntry[] originalEntries = project.getRawClasspath();
        for (int i = 0; i < originalEntries.length; i++) {
            IClasspathEntry entry = originalEntries[i];
            if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                classpathEntries.add(entry);
            }
        }
        List srcFolders = new ArrayList();
        Map srcFoldersToIncludes = new HashMap();
        Map srcFoldersToExcludes = new HashMap();
        br = new BufferedReader(new FileReader(file));
        Properties properties = new Properties();
        properties.load(ifile.getContents());
        Enumeration iter = properties.keys();

        // first stage - find any source folders
        while (iter.hasMoreElements()) {
            String name = iter.nextElement().toString();
            String value = properties.get(name).toString();
            String[] values = value.split(","); //$NON-NLS-1$
            if (name.equals("src.includes")) { //$NON-NLS-1$
                for (int i = 0; i < values.length; i++) {
                    String inc = values[i];
                    if (inc.equals("/")) { //$NON-NLS-1$
                        srcFolders.add(inc);
                    } else if (inc.indexOf("/") == inc.length() - 1) { //$NON-NLS-1$
                        if (project.getProject().getFolder(inc) != null
                                && project.getProject().getFolder(inc).exists()) {
                            srcFolders.add(inc);
                        }
                    }
                }
            }
        }

        // second stage - identify include and exclude filters
        iter = properties.keys();
        if (srcFolders.isEmpty()) {
            srcFolders.add(""); //$NON-NLS-1$
        }
        while (iter.hasMoreElements()) {
            String name = iter.nextElement().toString();
            String value = properties.get(name).toString();
            String[] values = value.split(","); //$NON-NLS-1$
            if (name.equals("src.inclusionpatterns")) { //$NON-NLS-1$
                for (int i = 0; i < values.length; i++) {
                    String inc = values[i];
                    for (Iterator iterator = srcFolders.iterator(); iterator.hasNext();) {
                        String srcFolder = (String) iterator.next();
                        if (inc.startsWith(srcFolder)) {
                            List incs = (List) srcFoldersToIncludes.get(srcFolder);
                            if (incs == null) {
                                incs = new ArrayList();
                            }
                            incs.add(inc);
                            srcFoldersToIncludes.put(srcFolder, incs);
                        }
                    }
                }
            } else if (name.equals("src.excludes")) { //$NON-NLS-1$
                for (int i = 0; i < values.length; i++) {
                    String exc = values[i];
                    for (Iterator iterator = srcFolders.iterator(); iterator.hasNext();) {
                        String srcFolder = (String) iterator.next();
                        if (srcFolder.equals("/") || exc.startsWith(srcFolder)) { //$NON-NLS-1$
                            List excs = (List) srcFoldersToExcludes.get(srcFolder);
                            if (excs == null) {
                                excs = new ArrayList();
                            }
                            excs.add(exc);
                            srcFoldersToExcludes.put(srcFolder, excs);
                        }
                    }
                }
            }
        }

        // third stage - create classpath entries
        IClasspathEntry[] entries = new IClasspathEntry[srcFolders.size() + classpathEntries.size()];
        for (int i = 0; i < entries.length; i++) {
            if (srcFolders.size() > i) {
                String srcFolder = (String) srcFolders.get(i);
                IPath path = project.getPath().append(stripSlash(srcFolder));
                List exclusions = (List) srcFoldersToExcludes.get(srcFolder);
                if (exclusions == null) {
                    exclusions = Collections.EMPTY_LIST;
                }
                List inclusions = (List) srcFoldersToIncludes.get(srcFolder);
                if (inclusions == null) {
                    inclusions = Collections.EMPTY_LIST;
                }
                IPath[] exclusionPatterns = new IPath[exclusions.size()];
                for (int j = 0; j < exclusionPatterns.length; j++) {
                    String exclusionPathStr = (String) exclusions.get(j);
                    if (exclusionPathStr.startsWith(srcFolder)) {
                        exclusionPathStr = exclusionPathStr.substring(srcFolder.length());
                    }
                    IPath exclusionPath = new Path(exclusionPathStr);
                    exclusionPatterns[j] = exclusionPath;

                }
                IPath[] inclusionPatterns = new IPath[inclusions.size()];
                for (int j = 0; j < inclusionPatterns.length; j++) {
                    String inclusionPathStr = (String) inclusions.get(j);
                    if (inclusionPathStr.startsWith(srcFolder)) {
                        inclusionPathStr = inclusionPathStr.substring(srcFolder.length());
                    }
                    IPath inclusionPath = new Path(inclusionPathStr);
                    inclusionPatterns[j] = inclusionPath;

                }
                IClasspathEntry classpathEntry = JavaCore.newSourceEntry(path, exclusionPatterns);
                //new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, IClasspathEntry.CPE_SOURCE, path, ClasspathEntry.INCLUDE_ALL, exclusionPatterns, null, null, null, true, ClasspathEntry.NO_ACCESS_RULES, false, ClasspathEntry.NO_EXTRA_ATTRIBUTES);
                entries[i] = classpathEntry;
            } else {
                entries[i] = (IClasspathEntry) classpathEntries.get(i - srcFolders.size());
            }
        }
        project.setRawClasspath(entries, null);
    } catch (FileNotFoundException e) {
    } catch (JavaModelException e) {
    } catch (IOException e) {
    } catch (CoreException e) {
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.eclipse.ajdt.core.CoreUtils.java

License:Open Source License

public static IPath[] getOutputFolders(IJavaProject project) throws CoreException {
    List<IPath> paths = new ArrayList<IPath>();
    paths.add(project.getOutputLocation());
    IClasspathEntry[] cpe = project.getRawClasspath();
    for (int i = 0; i < cpe.length; i++) {
        if (cpe[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath output = cpe[i].getOutputLocation();
            if (output != null) {
                paths.add(output);//w  w w .  j a  v  a  2  s  . co m
            }
        }
    }
    return (IPath[]) paths.toArray(new IPath[paths.size()]);
}

From source file:org.eclipse.ajdt.core.javaelements.AJCompilationUnitManager.java

License:Open Source License

private void addProjectToList(IProject project, List l) {
    if (AspectJPlugin.isAJProject(project)) {
        try {//ww w  .  j av  a  2s .  c o  m
            IJavaProject jp = JavaCore.create(project);
            IClasspathEntry[] cpes = jp.getRawClasspath();
            for (int i = 0; i < cpes.length; i++) {
                IClasspathEntry entry = cpes[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath p = entry.getPath();
                    if (p.segmentCount() == 1)
                        addAllAJFilesInFolder(project, l);
                    else
                        addAllAJFilesInFolder(project.getFolder(p.removeFirstSegments(1)), l);
                }
            }
        } catch (JavaModelException e) {
        }
    }
}

From source file:org.eclipse.ajdt.core.tests.AJDTCoreTestCase.java

License:Open Source License

private IPackageFragmentRoot createDefaultSourceFolder(IJavaProject javaProject) throws CoreException {
    IProject project = javaProject.getProject();
    IFolder folder = project.getFolder("src");
    if (!folder.exists())
        ensureExists(folder);/*from   w w w  .j a v  a  2s  .  co m*/

    // if already exists, do nothing
    final IClasspathEntry[] entries = javaProject.getResolvedClasspath(false);
    final IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(folder.getFullPath())) {
            return root;
        }
    }

    // else, remove old source folders and add this new one
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    List<IClasspathEntry> oldEntriesList = new ArrayList<IClasspathEntry>();
    oldEntriesList.add(JavaCore.newSourceEntry(root.getPath()));
    for (IClasspathEntry entry : oldEntries) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            oldEntriesList.add(entry);
        }
    }

    IClasspathEntry[] newEntries = oldEntriesList.toArray(new IClasspathEntry[0]);
    javaProject.setRawClasspath(newEntries, null);
    return root;
}