Example usage for org.eclipse.jdt.core IJavaProject getRawClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject getRawClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getRawClasspath.

Prototype

IClasspathEntry[] getRawClasspath() throws JavaModelException;

Source Link

Document

Returns the raw classpath for the project, as a list of classpath entries.

Usage

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 {//from www .  ja v  a  2 s  .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;//  w  w  w.j ava 2s .c om
    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

private static IClasspathEntry[] getExportedEntries(IProject project) {
    List<IClasspathEntry> exportedEntries = new ArrayList<IClasspathEntry>();

    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        return new IClasspathEntry[0];
    }//from  ww w  .j  a  v a 2s .c o  m

    try {
        IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
        for (int j = 0; j < cpEntry.length; j++) {
            IClasspathEntry entry = cpEntry[j];
            if (entry.isExported()) {
                // we don't want to export it in the new classpath.
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    IClasspathEntry nonExportedEntry = JavaCore.newLibraryEntry(entry.getPath(), null, null);
                    exportedEntries.add(nonExportedEntry);
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return (IClasspathEntry[]) exportedEntries.toArray(new IClasspathEntry[exportedEntries.size()]);
}

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

License:Open Source License

/**
 * Get the output locations for the project
 * //from   www.j a  v  a  2s .  c  o m
 * @param project
 * @return list of IPath objects
 */
public static List<IPath> getOutputLocationPaths(IProject project) {
    List<IPath> outputLocations = new ArrayList<IPath>();
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null)
        return outputLocations;

    try {
        // Have been unable to create a user scenario where the following
        // for
        // loop adds something to outputLocations, therefore always
        // fall through to the following if loop. However, if a project has
        // more than one output folder, then this for loop should pick them
        // up.
        // Needs testing.......
        IClasspathEntry[] cpEntry = javaProject.getRawClasspath();
        for (int j = 0; j < cpEntry.length; j++) {
            IClasspathEntry entry = cpEntry[j];
            int contentKind = entry.getContentKind();
            if (contentKind == ClasspathEntry.K_OUTPUT) {
                if (entry.getOutputLocation() != null) {
                    outputLocations.add(entry.getOutputLocation());
                }
            }
        }
        // If we haven't added anything from reading the .classpath
        // file, then use the default output location
        if (outputLocations.size() == 0) {
            outputLocations.add(javaProject.getOutputLocation());
        }
    } catch (JavaModelException e) {
    }
    return outputLocations;
}

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);/*from   w  w w . java 2s  .  com*/
            }
        }
    }
    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 {/* w  w  w.ja v  a 2 s  .  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);/*ww  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;
}

From source file:org.eclipse.ajdt.core.tests.builder.AJBuilderTest2.java

License:Open Source License

/**
 * Part of Bug 91420 - if a library has been added to the classpath then we
 * need to do a rebuild./* w w w  .j  av a 2  s  .c o  m*/
 */
public void testChangeInRequiredLibsCausesReBuild() throws Exception {
    IProject project = createPredefinedProject("bug91420"); //$NON-NLS-1$
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] origClasspath = javaProject.getRawClasspath();

    TestLogger testLog = new TestLogger();
    AspectJPlugin.getDefault().setAJLogger(testLog);
    // add a library to the classpath
    addLibraryToClasspath(project, "testJar.jar"); //$NON-NLS-1$
    joinBackgroudActivities();

    // check it's been added to the classpath
    assertTrue("library should have been added to classpath", //$NON-NLS-1$
            projectHasLibraryOnClasspath(javaProject, "testJar.jar")); //$NON-NLS-1$

    // check that a build has in fact occured
    List log = testLog.getMostRecentEntries(3);
    // print log to the screen for test case development purposes
    testLog.printLog();

    assertTrue("classpath has changed (new required library) so should have spent time in AJDE", //$NON-NLS-1$
            testLog.containsMessage("Total time spent in AJDE")); //$NON-NLS-1$
    assertTrue("classpath has changed (new required library) so should have spent time in AJBuilder.build()", //$NON-NLS-1$
            testLog.containsMessage("Total time spent in AJBuilder.build()")); //$NON-NLS-1$

    // reset the changes
    javaProject.setRawClasspath(origClasspath, null);
    joinBackgroudActivities();

    assertFalse("library should no longer be on the classpath", //$NON-NLS-1$
            projectHasLibraryOnClasspath(javaProject, "testJar.jar")); //$NON-NLS-1$
}

From source file:org.eclipse.ajdt.core.tests.builder.AJBuilderTest2.java

License:Open Source License

/**
 * Part of Bug 91420 - if there has been a change in required projects then
 * need to to a rebuild//from www .  java 2 s  .  c  om
 */
public void testChangeInRequiredProjectsCausesReBuild() throws Exception {
    IProject project = createPredefinedProject("bug91420"); //$NON-NLS-1$
    IProject project2 = createPredefinedProject("bug101481"); //$NON-NLS-1$
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] origClasspath = javaProject.getRawClasspath();

    TestLogger testLog = new TestLogger();
    AspectJPlugin.getDefault().setAJLogger(testLog);
    addProjectDependency(project, project2);
    joinBackgroudActivities();

    // check the dependency is there
    assertTrue("project bug91420 should have a project dependency on project bug101481", //$NON-NLS-1$
            projectHasProjectDependency(javaProject, project2));

    List log = testLog.getMostRecentEntries(3);
    // print log to the screen for test case development purposes
    testLog.printLog();

    assertTrue("classpath has changed (new project dependency) so should have spent time in AJDE", //$NON-NLS-1$
            testLog.containsMessage("Total time spent in AJDE")); //$NON-NLS-1$  
    assertTrue("classpath has changed (new project dependency) so should have spent time in AJBuilder.build()", //$NON-NLS-1$
            testLog.containsMessage("Total time spent in AJBuilder.build()")); //$NON-NLS-1$ 

    // reset the changes
    javaProject.setRawClasspath(origClasspath, null);
    joinBackgroudActivities();
    assertFalse("project dependencies should have been removed", //$NON-NLS-1$
            projectHasProjectDependency(javaProject, project2));
}

From source file:org.eclipse.ajdt.core.tests.builder.AJBuilderTest2.java

License:Open Source License

private void addLibraryToClasspath(IProject project, String libName) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(project);

    IClasspathEntry[] originalCP = javaProject.getRawClasspath();
    IClasspathEntry ajrtLIB = JavaCore.newLibraryEntry(project.getLocation().append(libName).makeAbsolute(), // library
            // location
            null, // no source
            null // no source
    );//from   ww w.j  a  v a2  s  .  co m
    int originalCPLength = originalCP.length;
    IClasspathEntry[] newCP = new IClasspathEntry[originalCPLength + 1];
    System.arraycopy(originalCP, 0, newCP, 0, originalCPLength);
    newCP[originalCPLength] = ajrtLIB;
    javaProject.setRawClasspath(newCP, null);
}