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

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

Introduction

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

Prototype

boolean isExported();

Source Link

Document

Returns whether this entry is exported to dependent projects.

Usage

From source file:org.eclim.plugin.jdt.util.ClasspathUtils.java

License:Open Source License

/**
 * Recursively collects classpath entries from the current and dependent
 * projects./*  w  ww.j a  v  a 2  s .c  om*/
 */
private static void collect(IJavaProject javaProject, List<String> paths, Set<IJavaProject> visited,
        boolean isFirstProject) throws Exception {
    if (visited.contains(javaProject)) {
        return;
    }
    visited.add(javaProject);

    try {
        IPath out = javaProject.getOutputLocation();
        paths.add(ProjectUtils.getFilePath(javaProject.getProject(), out.addTrailingSeparator().toOSString()));
    } catch (JavaModelException ignore) {
        // ignore... just signals that no output dir was configured.
    }

    IProject project = javaProject.getProject();
    String name = project.getName();

    IClasspathEntry[] entries = null;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException jme) {
        // this may or may not be a problem.
        logger.warn("Unable to retrieve resolved classpath for project: " + name, jme);
        return;
    }

    final List<IJavaProject> nextProjects = new ArrayList<IJavaProject>();
    for (IClasspathEntry entry : entries) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_CONTAINER:
        case IClasspathEntry.CPE_VARIABLE:
            String path = entry.getPath().toOSString().replace('\\', '/');
            if (path.startsWith("/" + name + "/")) {
                path = ProjectUtils.getFilePath(project, path);
            }
            paths.add(path);
            break;
        case IClasspathEntry.CPE_PROJECT:
            if (isFirstProject || entry.isExported()) {
                javaProject = JavaUtils.getJavaProject(entry.getPath().segment(0));
                if (javaProject != null) {
                    // breadth first, not depth first, to preserve dependency ordering
                    nextProjects.add(javaProject);
                }
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            IPath out = entry.getOutputLocation();
            if (out != null) {
                paths.add(ProjectUtils.getFilePath(javaProject.getProject(),
                        out.addTrailingSeparator().toOSString()));
            }
            break;
        }
    }
    // depth second
    for (final IJavaProject nextProject : nextProjects) {
        collect(nextProject, paths, visited, false);
    }
}

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

License:Open Source License

public static List<IClasspathEntry> resolveDependentProjectClasspath(IClasspathEntry projEntry,
        IProject requiredProj) {//w  ww.jav a 2 s . com
    // 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

/**
 * Firstly, add library to the Java build path if it's not there already,
 * then mark the entry as being on the aspect path
 * @param project/*from   w w w . j  a  va  2 s .  c  o m*/
 * @param path
 */
private static void addAttribute(IProject project, String jarPath, int eKind, IClasspathAttribute attribute) {
    IJavaProject jp = JavaCore.create(project);

    try {
        IClasspathEntry[] cp = jp.getRawClasspath();
        int cpIndex = getIndexInBuildPathEntry(cp, jarPath);
        if (cpIndex >= 0) { // already on classpath
            // add attribute to classpath entry
            // if it doesn't already exist
            IClasspathEntry pathAdd = cp[cpIndex];
            // only add attribute if this element is not already on the path
            if (isAspectPathAttribute(attribute) ? !isOnAspectpath(pathAdd) : !isOnInpath(pathAdd)) {
                IClasspathAttribute[] attributes = pathAdd.getExtraAttributes();
                IClasspathAttribute[] newattrib = new IClasspathAttribute[attributes.length + 1];
                System.arraycopy(attributes, 0, newattrib, 0, attributes.length);
                newattrib[attributes.length] = attribute;
                switch (pathAdd.getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                    pathAdd = JavaCore.newLibraryEntry(pathAdd.getPath(), pathAdd.getSourceAttachmentPath(),
                            pathAdd.getSourceAttachmentRootPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    pathAdd = JavaCore.newVariableEntry(pathAdd.getPath(), pathAdd.getSourceAttachmentPath(),
                            pathAdd.getSourceAttachmentRootPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_CONTAINER:
                    pathAdd = JavaCore.newContainerEntry(pathAdd.getPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_PROJECT:
                    pathAdd = JavaCore.newProjectEntry(pathAdd.getPath(), pathAdd.getAccessRules(), true,
                            newattrib, pathAdd.isExported());
                    break;
                }

                cp[cpIndex] = pathAdd;
                jp.setRawClasspath(cp, null);
            }
        } else {
            addEntryToJavaBuildPath(jp, attribute, jarPath, eKind);
        }
    } catch (JavaModelException e) {
    }
}

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

License:Open Source License

public static IClasspathEntry copyContainerEntry(IClasspathEntry containerEntry,
        IClasspathAttribute[] extraAttrs) {
    return JavaCore.newContainerEntry(containerEntry.getPath(), containerEntry.getAccessRules(), extraAttrs,
            containerEntry.isExported());
}

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];
    }// ww  w .  j  av  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.ui.tests.builder.ProjectDependenciesUtils.java

License:Open Source License

public static boolean projectHasAnExportedClasspathEntry(IProject project) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        return false;
    }/*from w w  w  .  j av  a 2 s. co  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) {
                    return true;
                }
            }
        }
    } catch (JavaModelException e) {
        AspectJTestPlugin.log(e);
    }
    return false;
}

From source file:org.eclipse.ajdt.ui.tests.builder.ProjectDependenciesUtils.java

License:Open Source License

public static boolean projectHasJarOnClasspath(IProject projectToHaveJar, IProject projectWhichExportedJar)
        throws JavaModelException {
    IJavaProject javaProject1 = JavaCore.create(projectToHaveJar);
    IJavaProject javaProject2 = JavaCore.create(projectWhichExportedJar);
    if (javaProject1 == null || javaProject2 == null) {
        return false;
    }/*from   ww w. j  av a  2s .c  o m*/

    IClasspathEntry[] cpEntry2 = javaProject2.getRawClasspath();
    for (int j = 0; j < cpEntry2.length; j++) {
        IClasspathEntry entry = cpEntry2[j];
        if (entry.isExported()) {
            // we don't want to export it in the new classpath.
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IClasspathEntry[] cpEntry1 = javaProject1.getRawClasspath();
                for (int i = 0; i < cpEntry1.length; i++) {
                    IClasspathEntry entry1 = cpEntry1[i];
                    if (entry1.getEntryKind() == entry.getEntryKind()
                            && entry1.getPath().equals(entry.getPath())) {
                        if (entry1.isExported()) {
                            // don't want it to be exported
                            return false;
                        }
                        return true;

                    }
                }
                return false;
            }
        }
    }
    return false;
}

From source file:org.eclipse.andmore.internal.build.BuildHelper.java

License:Open Source License

/**
 * Computes all the project output and dependencies that must go into building the apk.
 *
 * @param resMarker//from www  .  j  a  va2s .  c om
 * @throws CoreException
 */
private void gatherPaths(ResourceMarker resMarker) throws CoreException {
    IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();

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

    // get the output of the main project
    IPath path = javaProject.getOutputLocation();
    IResource outputResource = wsRoot.findMember(path);
    if (outputResource != null && outputResource.getType() == IResource.FOLDER) {
        mCompiledCodePaths.add(outputResource.getLocation().toOSString());
    }

    // we could use IJavaProject.getResolvedClasspath directly, but we actually
    // want to see the containers themselves.
    IClasspathEntry[] classpaths = javaProject.readRawClasspath();
    if (classpaths != null) {
        for (IClasspathEntry e : classpaths) {
            // ignore non exported entries, unless they're in the DEPEDENCIES container,
            // in which case we always want it (there may be some older projects that
            // have it as non exported).
            if (e.isExported() || (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && e.getPath().toString().equals(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES))) {
                handleCPE(e, javaProject, wsRoot, resMarker);
            }
        }
    }
}

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

License:Open Source License

/**
 * Fix the project classpath entries. The method ensures that:
 * <ul>//from   w  w w .  j  av  a 2  s . c  om
 * <li>The project does not reference any old android.zip/android.jar archive.</li>
 * <li>The project does not use its output folder as a sourc folder.</li>
 * <li>The project does not reference a desktop JRE</li>
 * <li>The project references the AndroidClasspathContainer.
 * </ul>
 * @param javaProject The project to fix.
 * @throws JavaModelException
 */
public static void fixProjectClasspathEntries(IJavaProject javaProject) throws JavaModelException {

    // get the project classpath
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IClasspathEntry[] oldEntries = entries;
    boolean forceRewriteOfCPE = false;

    // check if the JRE is set as library
    int jreIndex = ProjectHelper.findClasspathEntryByPath(entries, JavaRuntime.JRE_CONTAINER,
            IClasspathEntry.CPE_CONTAINER);
    if (jreIndex != -1) {
        // the project has a JRE included, we remove it
        entries = ProjectHelper.removeEntryFromClasspath(entries, jreIndex);
    }

    // get the output folder
    IPath outputFolder = javaProject.getOutputLocation();

    boolean foundFrameworkContainer = false;
    IClasspathEntry foundLibrariesContainer = null;
    IClasspathEntry foundDependenciesContainer = null;

    for (int i = 0; i < entries.length;) {
        // get the entry and kind
        IClasspathEntry entry = entries[i];
        int kind = entry.getEntryKind();

        if (kind == IClasspathEntry.CPE_SOURCE) {
            IPath path = entry.getPath();

            if (path.equals(outputFolder)) {
                entries = ProjectHelper.removeEntryFromClasspath(entries, i);

                // continue, to skip the i++;
                continue;
            }
        } else if (kind == IClasspathEntry.CPE_CONTAINER) {
            String path = entry.getPath().toString();
            if (AndmoreAndroidConstants.CONTAINER_FRAMEWORK.equals(path)) {
                foundFrameworkContainer = true;
            } else if (AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES.equals(path)) {
                foundLibrariesContainer = entry;
            } else if (AndmoreAndroidConstants.CONTAINER_DEPENDENCIES.equals(path)) {
                foundDependenciesContainer = entry;
            }
        }

        i++;
    }

    // look to see if we have the m2eclipse nature
    boolean m2eNature = false;
    try {
        m2eNature = javaProject.getProject().hasNature("org.eclipse.m2e.core.maven2Nature");
    } catch (CoreException e) {
        AndmoreAndroidPlugin.log(e, "Failed to query project %s for m2e nature",
                javaProject.getProject().getName());
    }

    // if the framework container is not there, we add it
    if (!foundFrameworkContainer) {
        // add the android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_FRAMEWORK)));
    }

    // same thing for the library container
    if (foundLibrariesContainer == null) {
        // add the exported libraries android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries, JavaCore
                .newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES), true));
    } else if (!m2eNature && !foundLibrariesContainer.isExported()) {
        // the container is present but it's not exported and since there's no m2e nature
        // we do want it to be exported.
        // keep all the other parameters the same.
        entries = ProjectHelper.replaceEntryInClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES),
                        foundLibrariesContainer.getAccessRules(), foundLibrariesContainer.getExtraAttributes(),
                        true));
        forceRewriteOfCPE = true;
    }

    // same thing for the dependencies container
    if (foundDependenciesContainer == null) {
        // add the android dependencies container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES), true));
    } else if (!m2eNature && !foundDependenciesContainer.isExported()) {
        // the container is present but it's not exported and since there's no m2e nature
        // we do want it to be exported.
        // keep all the other parameters the same.
        entries = ProjectHelper.replaceEntryInClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES),
                        foundDependenciesContainer.getAccessRules(),
                        foundDependenciesContainer.getExtraAttributes(), true));
        forceRewriteOfCPE = true;
    }

    // set the new list of entries to the project
    if (entries != oldEntries || forceRewriteOfCPE) {
        javaProject.setRawClasspath(entries, new NullProgressMonitor());
    }

    // If needed, check and fix compiler compliance and source compatibility
    ProjectHelper.checkAndFixCompilerCompliance(javaProject);
}

From source file:org.eclipse.birt.report.designer.internal.ui.ide.adapters.IDEReportClasspathResolver.java

License:Open Source License

private List<String> resolveClasspathEntries(IClasspathEntry[] classpathEntries, boolean needExported,
        IJavaProject project) {/* w  ww  .  ja  va2s . c  o  m*/
    ArrayList<String> newClassPath = new ArrayList<String>();
    IWorkspace space = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = space.getRoot();
    for (int i = 0; i < classpathEntries.length; i++) {
        IClasspathEntry curr = classpathEntries[i];
        if (!needExported && !curr.isExported() && curr.getEntryKind() != IClasspathEntry.CPE_VARIABLE) {
            continue;
        }
        IPath path = curr.getPath();
        //         if (curr.getEntryKind( ) == IClasspathEntry.CPE_VARIABLE)
        //         {
        //            path = JavaCore.getClasspathVariable( path.segment( 0 ) );
        //         }
        //         else
        //         {
        path = JavaCore.getResolvedClasspathEntry(curr).getPath();
        //         }

        if (project != null && curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            try {
                IClasspathContainer contianer = JavaCore.getClasspathContainer(path, project);
                if (contianer != null && contianer.getKind() == IClasspathContainer.K_APPLICATION) {
                    IClasspathEntry[] entrys = contianer.getClasspathEntries();
                    List<String> list = resolveClasspathEntries(entrys, needExported, project);
                    for (int j = 0; j < list.size(); j++) {
                        addToList(newClassPath, list.get(j));
                    }
                }
            } catch (JavaModelException e) {
                //do nothing
            }
            continue;
        }
        if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            path = curr.getOutputLocation();
        }
        if (path == null) {
            continue;
        }
        if (curr.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            if (root.findMember(path) instanceof IProject) {
                List<String> strs = getProjectClasspath((IProject) root.findMember(path), false, false);
                for (int j = 0; j < strs.size(); j++) {
                    addToList(newClassPath, strs.get(j));
                }
            }
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                || curr.getEntryKind() == IClasspathEntry.CPE_VARIABLE
                || curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

            boolean inWorkSpace = true;
            if (space == null || space.getRoot() == null) {
                inWorkSpace = false;
            }

            if (root.findMember(path) == null) {
                inWorkSpace = false;
            }

            if (inWorkSpace) {
                String absPath = getFullPath(path, root.findMember(path).getProject());

                //URL url = new URL( "file:///" + absPath );//$NON-NLS-1$//file:/
                //newClassPath.add( url.getPath( ) );
                newClassPath.add(absPath);
            } else {
                //                  newClassPath.add( curr.getPath( )
                //                        .toFile( )
                //                        .toURI( )
                //                        .toURL( ) );
                newClassPath.add(path.toFile().getAbsolutePath());
            }

        }

    }
    return newClassPath;
}