Example usage for org.eclipse.jdt.core JavaCore newProjectEntry

List of usage examples for org.eclipse.jdt.core JavaCore newProjectEntry

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore newProjectEntry.

Prototype

public static IClasspathEntry newProjectEntry(IPath path, boolean isExported) 

Source Link

Document

Creates and returns a new classpath entry of kind CPE_PROJECT for the project identified by the given absolute path.

Usage

From source file:byke.tests.workspaceutils.JavaProject.java

License:Open Source License

@Override
public void addReferencedProject(IProject reference, IProgressMonitor monitor) throws CoreException {
    super.addReferencedProject(reference, monitor);
    addClasspathEntry(JavaCore.newProjectEntry(reference.getFullPath(), true));
}

From source file:com.android.ide.eclipse.adt.internal.project.LibraryClasspathContainerInitializer.java

License:Open Source License

private static IClasspathContainer allocateLibraryContainer(IJavaProject javaProject) {
    final IProject iProject = javaProject.getProject();

    // check if the project has a valid target.
    ProjectState state = Sdk.getProjectState(iProject);
    if (state == null) {
        // getProjectState should already have logged an error. Just bail out.
        return null;
    }/*  w w w . ja v  a  2  s  . c o m*/

    /*
     * At this point we're going to gather a list of all that need to go in the
     * dependency container.
     * - Library project outputs (direct and indirect)
     * - Java project output (those can be indirectly referenced through library projects
     *   or other other Java projects)
     * - Jar files:
     *    + inside this project's libs/
     *    + inside the library projects' libs/
     *    + inside the referenced Java projects' classpath
     */
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();

    // list of java project dependencies and jar files that will be built while
    // going through the library projects.
    Set<File> jarFiles = new HashSet<File>();
    Set<IProject> refProjects = new HashSet<IProject>();

    // process all the libraries

    List<IProject> libProjects = state.getFullLibraryProjects();
    for (IProject libProject : libProjects) {
        // process all of the library project's dependencies
        getDependencyListFromClasspath(libProject, refProjects, jarFiles, true);
    }

    // now process this projects' referenced projects only.
    processReferencedProjects(iProject, refProjects, jarFiles);

    // and the content of its libs folder
    getJarListFromLibsFolder(iProject, jarFiles);

    // now add a classpath entry for each Java project (this is a set so dups are already
    // removed)
    for (IProject p : refProjects) {
        entries.add(JavaCore.newProjectEntry(p.getFullPath(), true /*isExported*/));
    }

    entries.addAll(convertJarsToClasspathEntries(iProject, jarFiles));

    return allocateContainer(javaProject, entries, new Path(AdtConstants.CONTAINER_PRIVATE_LIBRARIES),
            "Android Private Libraries");
}

From source file:com.android.ide.eclipse.auidt.internal.project.LibraryClasspathContainerInitializer.java

License:Open Source License

private static IClasspathContainer allocateLibraryContainer(IJavaProject javaProject) {
    final IProject iProject = javaProject.getProject();

    AdtPlugin plugin = AdtPlugin.getDefault();
    if (plugin == null) { // This is totally weird, but I've seen it happen!
        return null;
    }/*from   w w w  .  j  av a 2 s  .  c  o  m*/

    // First check that the project has a library-type container.
    try {
        IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
        IClasspathEntry[] oldRawClasspath = rawClasspath;

        boolean foundLibrariesContainer = false;
        for (IClasspathEntry entry : rawClasspath) {
            // get the entry and kind
            int kind = entry.getEntryKind();

            if (kind == IClasspathEntry.CPE_CONTAINER) {
                String path = entry.getPath().toString();
                if (AdtConstants.CONTAINER_LIBRARIES.equals(path)) {
                    foundLibrariesContainer = true;
                    break;
                }
            }
        }

        // if there isn't any, add it.
        if (foundLibrariesContainer == false) {
            // add the android container to the array
            rawClasspath = ProjectHelper.addEntryToClasspath(rawClasspath, JavaCore
                    .newContainerEntry(new Path(AdtConstants.CONTAINER_LIBRARIES), true /*isExported*/));
        }

        // set the new list of entries to the project
        if (rawClasspath != oldRawClasspath) {
            javaProject.setRawClasspath(rawClasspath, new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
        // This really shouldn't happen, but if it does, simply return null (the calling
        // method will fails as well)
        return null;
    }

    // check if the project has a valid target.
    ProjectState state = Sdk.getProjectState(iProject);
    if (state == null) {
        // getProjectState should already have logged an error. Just bail out.
        return null;
    }

    /*
     * At this point we're going to gather a list of all that need to go in the
     * dependency container.
     * - Library project outputs (direct and indirect)
     * - Java project output (those can be indirectly referenced through library projects
     *   or other other Java projects)
     * - Jar files:
     *    + inside this project's libs/
     *    + inside the library projects' libs/
     *    + inside the referenced Java projects' classpath
     */

    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();

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

    // list of java project dependencies and jar files that will be built while
    // going through the library projects.
    Set<File> jarFiles = new HashSet<File>();
    Set<IProject> refProjects = new HashSet<IProject>();

    // process all the libraries

    List<IProject> libProjects = state.getFullLibraryProjects();
    for (IProject libProject : libProjects) {
        // get the project output
        IFolder outputFolder = BaseProjectHelper.getAndroidOutputFolder(libProject);

        if (outputFolder != null) { // can happen when closing/deleting a library)
            IFile jarIFile = outputFolder.getFile(libProject.getName().toLowerCase() + AdtConstants.DOT_JAR);

            // get the source folder for the library project
            List<IPath> srcs = BaseProjectHelper.getSourceClasspaths(libProject);
            // find the first non-derived source folder.
            IPath sourceFolder = null;
            for (IPath src : srcs) {
                IFolder srcFolder = workspaceRoot.getFolder(src);
                if (srcFolder.isDerived() == false) {
                    sourceFolder = src;
                    break;
                }
            }

            // we can directly add a CPE for this jar as there's no risk of a duplicate.
            IClasspathEntry entry = JavaCore.newLibraryEntry(jarIFile.getLocation(), sourceFolder, // source attachment path
                    null, // default source attachment root path.
                    true /*isExported*/);

            entries.add(entry);

            // process all of the library project's dependencies
            getDependencyListFromClasspath(libProject, refProjects, jarFiles, true);
            // and the content of its libs folder.
            getJarListFromLibsFolder(libProject, jarFiles);
        }
    }

    // now process this projects' referenced projects only.
    processReferencedProjects(iProject, refProjects, jarFiles);
    // and the content of its libs folder
    getJarListFromLibsFolder(iProject, jarFiles);

    // annotations support for older version of android
    if (state.getTarget() != null && state.getTarget().getVersion().getApiLevel() <= 15) {
        File annotationsJar = new File(Sdk.getCurrent().getSdkLocation(), SdkConstants.FD_TOOLS + File.separator
                + SdkConstants.FD_SUPPORT + File.separator + SdkConstants.FN_ANNOTATIONS_JAR);

        jarFiles.add(annotationsJar);
    }

    // now add a classpath entry for each Java project (this is a set so dups are already
    // removed)
    for (IProject p : refProjects) {
        entries.add(JavaCore.newProjectEntry(p.getFullPath(), true /*isExported*/));
    }

    // and process the jar files list, but first sanitize it to remove dups.
    JarListSanitizer sanitizer = new JarListSanitizer(
            iProject.getFolder(SdkConstants.FD_OUTPUT).getLocation().toFile(),
            new AndroidPrintStream(iProject, null /*prefix*/, AdtPlugin.getOutStream()));

    String errorMessage = null;

    try {
        List<File> sanitizedList = sanitizer.sanitize(jarFiles);

        for (File jarFile : sanitizedList) {
            if (jarFile instanceof CPEFile) {
                CPEFile cpeFile = (CPEFile) jarFile;
                IClasspathEntry e = cpeFile.getClasspathEntry();

                entries.add(JavaCore.newLibraryEntry(e.getPath(), e.getSourceAttachmentPath(),
                        e.getSourceAttachmentRootPath(), e.getAccessRules(), e.getExtraAttributes(),
                        true /*isExported*/));
            } else {
                String jarPath = jarFile.getAbsolutePath();

                IPath sourceAttachmentPath = null;
                IClasspathAttribute javaDocAttribute = null;

                File jarProperties = new File(jarPath + DOT_PROPERTIES);
                if (jarProperties.isFile()) {
                    Properties p = new Properties();
                    InputStream is = null;
                    try {
                        p.load(is = new FileInputStream(jarProperties));

                        String value = p.getProperty(ATTR_SRC);
                        if (value != null) {
                            File srcPath = getFile(jarFile, value);

                            if (srcPath.exists()) {
                                sourceAttachmentPath = new Path(srcPath.getAbsolutePath());
                            }
                        }

                        value = p.getProperty(ATTR_DOC);
                        if (value != null) {
                            File docPath = getFile(jarFile, value);
                            if (docPath.exists()) {
                                try {
                                    javaDocAttribute = JavaCore.newClasspathAttribute(
                                            IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
                                            docPath.toURI().toURL().toString());
                                } catch (MalformedURLException e) {
                                    AdtPlugin.log(e, "Failed to process 'doc' attribute for %s",
                                            jarProperties.getAbsolutePath());
                                }
                            }
                        }

                    } catch (FileNotFoundException e) {
                        // shouldn't happen since we check upfront
                    } catch (IOException e) {
                        AdtPlugin.log(e, "Failed to read %s", jarProperties.getAbsolutePath());
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException e) {
                                // ignore
                            }
                        }
                    }
                }

                if (javaDocAttribute != null) {
                    entries.add(JavaCore.newLibraryEntry(new Path(jarPath), sourceAttachmentPath,
                            null /*sourceAttachmentRootPath*/, new IAccessRule[0],
                            new IClasspathAttribute[] { javaDocAttribute }, true /*isExported*/));
                } else {
                    entries.add(JavaCore.newLibraryEntry(new Path(jarPath), sourceAttachmentPath,
                            null /*sourceAttachmentRootPath*/, true /*isExported*/));
                }
            }
        }
    } catch (DifferentLibException e) {
        errorMessage = e.getMessage();
        AdtPlugin.printErrorToConsole(iProject, (Object[]) e.getDetails());
    } catch (Sha1Exception e) {
        errorMessage = e.getMessage();
    }

    processError(iProject, errorMessage, AdtConstants.MARKER_DEPENDENCY, true /*outputToConsole*/);

    return new AndroidClasspathContainer(entries.toArray(new IClasspathEntry[entries.size()]),
            new Path(AdtConstants.CONTAINER_LIBRARIES), "Android Dependencies",
            IClasspathContainer.K_APPLICATION);
}

From source file:com.google.gwt.eclipse.core.compile.GWTCompileRunnerTest.java

License:Open Source License

/**
 * Tests computing the classpath for dependent projects.
 *//*from  w  w  w .j  a va 2s  .co m*/
public void testComputeClasspathForDependentProject() throws CoreException {
    addAndCreateSourceEntry(javaProjectA, SRC_DIR_NAME, null);
    addAndCreateSourceEntry(javaProjectB, SRC_DIR_NAME, null);

    // Make project A dependent on project B
    JavaProjectUtilities.addRawClassPathEntry(javaProjectA,
            JavaCore.newProjectEntry(javaProjectB.getProject().getFullPath(), true));

    // Get the computed classpath
    List<File> actualCp = getListOfFiles(GWTCompileRunner.computeClasspath(javaProjectA));

    // Ensure the paths and ordering are all the same
    List<File> expectedCp = new ArrayList<File>();

    // Source of project A
    expectedCp.add(getFile(javaProjectA.getProject(), SRC_DIR_NAME));

    // Source of project B
    expectedCp.add(getFile(javaProjectB.getProject(), SRC_DIR_NAME));

    // Output of project A
    expectedCp.add(getOutputOfProject(javaProjectA));

    // Output of project B
    expectedCp.add(getOutputOfProject(javaProjectB));

    assertEquals(expectedCp, actualCp);
}

From source file:com.google.gwt.eclipse.core.compile.GWTCompileRunnerTest.java

License:Open Source License

/**
 * Tests computing the classpath for a project with a dependency project that
 * all have multiple source directories and each having a specific output
 * directory./*from   ww w .jav  a 2s .c o  m*/
 */
public void testComputeClasspathForProjectsWithMultipleSourcesAndSpecificOutputs() throws CoreException {
    final String sourceOutDirName = "srcOut";
    final String secondarySourceOutDirName = "secondarySrcOut";

    // Create source dirs and specific outputs for A
    addAndCreateSourceEntry(javaProjectA, SRC_DIR_NAME, sourceOutDirName);
    addAndCreateSourceEntry(javaProjectA, SECONDARY_SRC_DIR_NAME, secondarySourceOutDirName);

    // Create source dirs and specific outputs for B
    addAndCreateSourceEntry(javaProjectB, SRC_DIR_NAME, sourceOutDirName);
    addAndCreateSourceEntry(javaProjectB, SECONDARY_SRC_DIR_NAME, secondarySourceOutDirName);

    // Add A depends on B
    JavaProjectUtilities.addRawClassPathEntry(javaProjectA,
            JavaCore.newProjectEntry(javaProjectB.getProject().getFullPath(), true));

    // Get the computed classpath
    List<File> actualCp = getListOfFiles(GWTCompileRunner.computeClasspath(javaProjectA));

    // Ensure the paths and ordering are all the same
    List<File> expectedCp = new ArrayList<File>();

    // Check that it contains both source dirs for A
    expectedCp.add(getFile(javaProjectA.getProject(), SRC_DIR_NAME));
    expectedCp.add(getFile(javaProjectA.getProject(), SECONDARY_SRC_DIR_NAME));

    // Check that it contains both source dirs for B
    expectedCp.add(getFile(javaProjectB.getProject(), SRC_DIR_NAME));
    expectedCp.add(getFile(javaProjectB.getProject(), SECONDARY_SRC_DIR_NAME));

    // Check that it contains both output dirs for A
    IPath projPath = javaProjectA.getProject().getFullPath();
    expectedCp.add(ResourceUtils.resolveToAbsoluteFileSystemPath(projPath.append(sourceOutDirName)).toFile());
    expectedCp.add(
            ResourceUtils.resolveToAbsoluteFileSystemPath(projPath.append(secondarySourceOutDirName)).toFile());

    // Check that the default output directory for A is there
    expectedCp.add(getOutputOfProject(javaProjectA));

    // Check that it contains both output dirs for B
    IPath projBPath = javaProjectB.getProject().getFullPath();
    expectedCp.add(ResourceUtils.resolveToAbsoluteFileSystemPath(projBPath.append(sourceOutDirName)).toFile());
    expectedCp.add(ResourceUtils.resolveToAbsoluteFileSystemPath(projBPath.append(secondarySourceOutDirName))
            .toFile());

    // Check that the default output directory for B is there
    expectedCp.add(getOutputOfProject(javaProjectB));

    assertEquals(expectedCp, actualCp);
}

From source file:com.siteview.mde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

/**
 * @return updated classpath or null if there were no changes
 *///from   w w w .  j a  va2s . c  om
private IClasspathEntry[] getUpdatedClasspath(IClasspathEntry[] cp, IJavaProject currentProject) {
    boolean exposed = false;
    int refIndex = -1;
    List result = new ArrayList();
    Set manifests = new HashSet();
    for (int i = 0; i < fData.getLibraryPaths().length; ++i) {
        try {
            manifests.add(new JarFile(fData.getLibraryPaths()[i]).getManifest());
        } catch (IOException e) {
            MDEPlugin.log(e);
        }
    }
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (int i = 0; i < cp.length; ++i) {
        IClasspathEntry cpe = cp[i];
        switch (cpe.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            String path = null;
            IPath location = root.getFile(cpe.getPath()).getLocation();
            if (location != null) {
                path = location.toString();
            }
            //try maybe path is absolute
            if (path == null) {
                path = cpe.getPath().toString();
            }
            try {
                JarFile jarFile = new JarFile(path);
                if (manifests.contains(jarFile.getManifest())) {
                    if (refIndex < 0) {
                        // allocate slot
                        refIndex = result.size();
                        result.add(null);
                    }
                    exposed |= cpe.isExported();
                } else {
                    result.add(cpe);
                }
            } catch (IOException e) {
                MDEPlugin.log(e);
            }
            break;
        default:
            result.add(cpe);
            break;
        }
    }
    if (refIndex >= 0) {
        result.set(refIndex, JavaCore.newProjectEntry(currentProject.getPath(), exposed));
        return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
    }
    return null;
}

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

License:Open Source License

static protected IClasspathEntry[] resolveDependentProjects(String[] dependencies) {
    IClasspathEntry[] sourceClasspathEntries = new IClasspathEntry[dependencies.length];
    int i = 0;/*from   w w  w  .  java  2 s .com*/
    for (String project : dependencies) {
        sourceClasspathEntries[i++] = JavaCore.newProjectEntry(new Path(IConstants.BACK_SLASH_MARK + project),
                true);
    }
    return sourceClasspathEntries;
}

From source file:org.azzyzt.jee.tools.project.ProjectUtil.java

License:EUPL

public static void appendProjectToClassPath(IJavaProject jprj, IJavaProject jprjAdded) throws CoreException {
    try {/*w w w . j  a  v a2s  .c  o  m*/
        IClasspathEntry[] entries = jprj.getRawClasspath();
        IClasspathEntry newEntry = JavaCore.newProjectEntry(jprjAdded.getPath(), true);
        for (IClasspathEntry entry : entries) {
            if (newEntry.equals(entry)) {
                return;
            }
        }
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);
        newEntries[newEntries.length - 1] = newEntry;
        jprj.setRawClasspath(newEntries, null);
    } catch (JavaModelException e) {
        throw Util.createCoreException("Can't add " + jprjAdded.getProject().getName() + " to class path of "
                + jprj.getProject().getName(), e);
    }
}

From source file:org.eclim.plugin.jdt.project.JavaProjectManager.java

License:Open Source License

/**
 * Creates or updates the projects dependencies on other projects.
 *
 * @param project The project./*from  ww w .  j  av a2s.  c om*/
 * @param depends The comma seperated list of project names.
 */
protected IClasspathEntry[] createOrUpdateDependencies(IJavaProject project, String depends) throws Exception {
    if (depends != null) {
        String[] dependPaths = StringUtils.split(depends, ',');
        IClasspathEntry[] entries = new IClasspathEntry[dependPaths.length];
        for (int ii = 0; ii < dependPaths.length; ii++) {
            IProject theProject = ProjectUtils.getProject(dependPaths[ii]);
            if (!theProject.exists()) {
                throw new IllegalArgumentException(
                        Services.getMessage("project.depends.not.found", dependPaths[ii]));
            }
            IJavaProject otherProject = JavaCore.create(theProject);
            entries[ii] = JavaCore.newProjectEntry(otherProject.getPath(), true);
        }
        return entries;
    }
    return new IClasspathEntry[0];
}

From source file:org.eclipse.andmore.internal.project.LibraryClasspathContainerInitializer.java

License:Open Source License

private static IClasspathContainer allocateLibraryContainer(IJavaProject javaProject) {
    final IProject iProject = javaProject.getProject();

    // check if the project has a valid target.
    ProjectState state = Sdk.getProjectState(iProject);
    if (state == null) {
        // getProjectState should already have logged an error. Just bail out.
        return null;
    }/*from  w w w .  ja  v a 2  s.c  o  m*/

    /*
     * At this point we're going to gather a list of all that need to go in the
     * dependency container.
     * - Library project outputs (direct and indirect)
     * - Java project output (those can be indirectly referenced through library projects
     *   or other other Java projects)
     * - Jar files:
     *    + inside this project's libs/
     *    + inside the library projects' libs/
     *    + inside the referenced Java projects' classpath
     */
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();

    // list of java project dependencies and jar files that will be built while
    // going through the library projects.
    Set<File> jarFiles = new HashSet<File>();
    Set<IProject> refProjects = new HashSet<IProject>();

    // process all the libraries

    List<IProject> libProjects = state.getFullLibraryProjects();
    for (IProject libProject : libProjects) {
        // process all of the library project's dependencies
        getDependencyListFromClasspath(libProject, refProjects, jarFiles, true);
    }

    // now process this projects' referenced projects only.
    processReferencedProjects(iProject, refProjects, jarFiles);

    // and the content of its libs folder
    getJarListFromLibsFolder(iProject, jarFiles);

    // now add a classpath entry for each Java project (this is a set so dups are already
    // removed)
    for (IProject p : refProjects) {
        entries.add(JavaCore.newProjectEntry(p.getFullPath(), true /*isExported*/));
    }

    entries.addAll(convertJarsToClasspathEntries(iProject, jarFiles));

    return allocateContainer(javaProject, entries,
            new Path(AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES), "Android Private Libraries");
}