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

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

Introduction

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

Prototype

int CPE_CONTAINER

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

Click Source Link

Document

Entry kind constant describing a classpath entry representing a name classpath container.

Usage

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

License:Open Source License

private static IClasspathContainer allocateContainer(IJavaProject javaProject, List<IClasspathEntry> entries,
        IPath id, String description) {

    if (AndmoreAndroidPlugin.getDefault() == null) { // This is totally weird, but I've seen it happen!
        return null;
    }/*from  ww  w  . j  a v  a2s  .c  o  m*/

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

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

            if (kind == IClasspathEntry.CPE_CONTAINER) {
                String path = entry.getPath().toString();
                String idString = id.toString();
                if (idString.equals(path)) {
                    foundContainer = true;
                    break;
                }
            }
        }

        // if there isn't any, add it.
        if (foundContainer == false) {
            // add the android container to the array
            rawClasspath = ProjectHelper.addEntryToClasspath(rawClasspath,
                    JavaCore.newContainerEntry(id, 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;
    }

    return new AndroidClasspathContainer(entries.toArray(new IClasspathEntry[entries.size()]), id, description,
            IClasspathContainer.K_APPLICATION);
}

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

License:Open Source License

/**
 * Processes a {@link IClasspathEntry} and add it to one of the list if applicable.
 * @param entry the entry to process/*from w ww. ja  va  2  s .co  m*/
 * @param javaProject the {@link IJavaProject} from which this entry came.
 * @param wsRoot the {@link IWorkspaceRoot}
 * @param projects the project list to add to
 * @param jarFiles the jar list to add to
 * @param includeJarFiles whether to include jar files or just projects. This is useful when
 *           calling on an Android project (value should be <code>false</code>)
 */
private static void processCPE(IClasspathEntry entry, IJavaProject javaProject, IWorkspaceRoot wsRoot,
        Set<IProject> projects, Set<File> jarFiles, boolean includeJarFiles) {

    // if this is a classpath variable reference, we resolve it.
    if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
        entry = JavaCore.getResolvedClasspathEntry(entry);
    }

    if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
        IProject refProject = wsRoot.getProject(entry.getPath().lastSegment());
        try {
            // ignore if it's an Android project, or if it's not a Java Project
            if (refProject.hasNature(JavaCore.NATURE_ID)
                    && refProject.hasNature(AndmoreAndroidConstants.NATURE_DEFAULT) == false) {
                // add this project to the list
                projects.add(refProject);

                // also get the dependency from this project.
                getDependencyListFromClasspath(refProject, projects, jarFiles, true /*includeJarFiles*/);
            }
        } catch (CoreException exception) {
            // can't query the project nature? ignore
        }
    } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
        if (includeJarFiles) {
            handleClasspathLibrary(entry, wsRoot, jarFiles);
        }
    } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
        // get the container and its content
        try {
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject);
            // ignore the system and default_system types as they represent
            // libraries that are part of the runtime.
            if (container != null && container.getKind() == IClasspathContainer.K_APPLICATION) {
                IClasspathEntry[] entries = container.getClasspathEntries();
                for (IClasspathEntry cpe : entries) {
                    processCPE(cpe, javaProject, wsRoot, projects, jarFiles, includeJarFiles);
                }
            }
        } catch (JavaModelException jme) {
            // can't resolve the container? ignore it.
            AndmoreAndroidPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", entry.getPath());
        }
    }
}

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

License:Open Source License

/**
 * Fix the project classpath entries. The method ensures that:
 * <ul>//w w w  .  ja v  a 2s .  com
 * <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.andmore.internal.project.ProjectHelperTest.java

License:Open Source License

@Test
@Ignore//from  ww w  . j  a v a 2  s.c o  m
public final void testFixProjectClasspathEntriesFromOldContainer() throws Exception {
    // create a project with a path to an android .zip
    IJavaProject javaProject = Mocks.createProject(
            new IClasspathEntry[] { Mocks.createClasspathEntry(new Path("Project/src"), //$NON-NLS-1$
                    IClasspathEntry.CPE_SOURCE),
                    Mocks.createClasspathEntry(new Path(OLD_CONTAINER_ID), IClasspathEntry.CPE_CONTAINER), },
            new Path("Project/bin"));

    ProjectHelper.fixProjectClasspathEntries(javaProject);

    IClasspathEntry[] fixedEntries = javaProject.getRawClasspath();
    assertEquals(5, fixedEntries.length);
    assertEquals("Project/src", fixedEntries[0].getPath().toString());
    assertEquals(OLD_CONTAINER_ID, fixedEntries[1].getPath().toString());
    assertEquals(CONTAINER_ID, fixedEntries[2].getPath().toString());
}

From source file:org.eclipse.andmore.internal.resources.manager.ProjectClassLoader.java

License:Open Source License

/**
 * Returns an array of external jar files used by the project.
 * @return an array of OS-specific absolute file paths
 *//*w  w  w. j av a2  s. co  m*/
private final URL[] getExternalJars() {
    // get a java project from it
    IJavaProject javaProject = JavaCore.create(mJavaProject.getProject());

    ArrayList<URL> oslibraryList = new ArrayList<URL>();
    IClasspathEntry[] classpaths = javaProject.readRawClasspath();
    if (classpaths != null) {
        for (IClasspathEntry e : classpaths) {
            if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                    || e.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                // if this is a classpath variable reference, we resolve it.
                if (e.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                    e = JavaCore.getResolvedClasspathEntry(e);
                }

                handleClassPathEntry(e, oslibraryList);
            } else if (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                // get the container.
                try {
                    IClasspathContainer container = JavaCore.getClasspathContainer(e.getPath(), javaProject);
                    // ignore the system and default_system types as they represent
                    // libraries that are part of the runtime.
                    if (container != null && container.getKind() == IClasspathContainer.K_APPLICATION) {
                        IClasspathEntry[] entries = container.getClasspathEntries();
                        for (IClasspathEntry entry : entries) {
                            // TODO: Xav -- is this necessary?
                            if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                                entry = JavaCore.getResolvedClasspathEntry(entry);
                            }

                            handleClassPathEntry(entry, oslibraryList);
                        }
                    }
                } catch (JavaModelException jme) {
                    // can't resolve the container? ignore it.
                    AndmoreAndroidPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", e.getPath());
                }
            }
        }
    }

    return oslibraryList.toArray(new URL[oslibraryList.size()]);
}

From source file:org.eclipse.ant.internal.ui.datatransfer.EclipseClasspath.java

License:Open Source License

private void handleLibraries(IClasspathEntry entry) throws JavaModelException {
    if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE
            && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
        // found library
        IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
        if (container == null) {
            // jar missing (project not compile clean)
            return;
        }//from   www  . j a  v  a  2s.  c  o  m
        String jar = entry.getPath().toString();
        String refName;
        if (jar.startsWith(JavaRuntime.JRE_CONTAINER)) {
            // JRE System Library
            refName = "${jre.container}"; //$NON-NLS-1$
        } else if (jar.startsWith(JavaCore.USER_LIBRARY_CONTAINER_ID)) {
            // User Library
            String libraryName = container.getDescription();
            refName = "${" + libraryName + ".userclasspath}"; //$NON-NLS-1$ //$NON-NLS-2$
            if (container.getKind() == IClasspathContainer.K_SYSTEM) {
                refName = "${" + libraryName + ".bootclasspath}"; //$NON-NLS-1$ //$NON-NLS-2$
            }
        } else {
            // Library dependencies: e.g. Plug-in Dependencies
            String libraryName = container.getDescription();
            refName = "${" + libraryName + ".libraryclasspath}"; //$NON-NLS-1$ //$NON-NLS-2$
        }
        userLibraryCache.put(refName, container);
        srcDirs.add(refName);
        classDirs.add(refName);
        rawClassPathEntries.add(refName);
        rawClassPathEntriesAbsolute.add(refName);
        inclusionLists.add(new ArrayList<String>());
        exclusionLists.add(new ArrayList<String>());
    }
}

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

License:Open Source License

public boolean isEntryKind(int kind) {
    return kind == IClasspathEntry.CPE_LIBRARY || kind == IClasspathEntry.CPE_PROJECT
            || kind == IClasspathEntry.CPE_VARIABLE || kind == IClasspathEntry.CPE_CONTAINER;
}

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

License:Open Source License

private IClasspathEntry newClasspathEntry() {

    IClasspathAttribute[] extraAttributes = new IClasspathAttribute[0];
    switch (fEntryKind) {
    case IClasspathEntry.CPE_SOURCE:
        return JavaCore.newSourceEntry(fPath, null, null, null, extraAttributes);
    case IClasspathEntry.CPE_LIBRARY: {
        return JavaCore.newLibraryEntry(fPath, null, null, null, extraAttributes, isExported());
    }//from w  ww  . j  av a 2s .c  o  m
    case IClasspathEntry.CPE_PROJECT: {
        return JavaCore.newProjectEntry(fPath, null, false, extraAttributes, isExported());
    }
    case IClasspathEntry.CPE_CONTAINER: {
        return JavaCore.newContainerEntry(fPath, null, extraAttributes, isExported());
    }
    case IClasspathEntry.CPE_VARIABLE: {
        return JavaCore.newVariableEntry(fPath, null, null, null, extraAttributes, isExported());
    }
    default:
        return null;
    }
}

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) {/* ww  w.  java2  s.  c om*/
    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;
}

From source file:org.eclipse.buckminster.jdt.internal.BMClasspathInitializer.java

License:Open Source License

@Override
public void resourceChanged(IResourceChangeEvent event) {
    IPath path = BMClasspathContainer.PATH;
    try {/* w  w w . j  a v  a 2 s. c om*/
        IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
        for (IJavaProject javaProject : model.getJavaProjects()) {
            for (IClasspathEntry rawEntry : javaProject.readRawClasspath()) {
                if (rawEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    IPath entryPath = rawEntry.getPath();
                    if (path.isPrefixOf(entryPath)) {
                        this.initialize(entryPath, javaProject);
                        return;
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (CoreException e) {
        e.printStackTrace();
    }
}