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

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

Introduction

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

Prototype

int getEntryKind();

Source Link

Document

Returns the kind of this classpath entry.

Usage

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  .  j a v a2  s .c  o m
 * <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.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
 *///from ww 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 handleSources(IClasspathEntry entry) throws JavaModelException {
    String projectRoot = ExportUtil.getProjectRoot(project);
    String defaultClassDir = project.getOutputLocation().toString();
    String defaultClassDirAbsolute = ExportUtil.resolve(project.getOutputLocation());

    if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE
            && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
        // found source path
        IPath srcDirPath = entry.getPath();
        IPath classDirPath = entry.getOutputLocation();
        String srcDir = handleLinkedResource(srcDirPath);
        ExportUtil.removeProjectRoot((srcDirPath != null) ? srcDirPath.toString() : projectRoot,
                project.getProject());//from  w ww .ja va 2  s. c  om
        String classDir = ExportUtil.removeProjectRoot(
                (classDirPath != null) ? classDirPath.toString() : defaultClassDir, project.getProject());
        srcDirs.add(srcDir);
        classDirs.add(classDir);
        String classDirAbsolute = (classDirPath != null) ? ExportUtil.resolve(classDirPath)
                : defaultClassDirAbsolute;
        rawClassPathEntries.add(classDir);
        rawClassPathEntriesAbsolute.add(classDirAbsolute);
        IPath[] inclusions = entry.getInclusionPatterns();
        List<String> inclusionList = new ArrayList<>();
        for (int j = 0; j < inclusions.length; j++) {
            if (inclusions[j] != null) {
                inclusionList.add(ExportUtil.removeProjectRoot(inclusions[j].toString(), project.getProject()));
            }
        }
        inclusionLists.add(inclusionList);
        IPath[] exclusions = entry.getExclusionPatterns();
        List<String> exclusionList = new ArrayList<>();
        for (int j = 0; j < exclusions.length; j++) {
            if (exclusions[j] != null) {
                exclusionList.add(ExportUtil.removeProjectRoot(exclusions[j].toString(), project.getProject()));
            }
        }
        exclusionLists.add(exclusionList);
    }
}

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

License:Open Source License

private void handleJars(IClasspathEntry entry) {
    if (entry.getContentKind() == IPackageFragmentRoot.K_BINARY
            && entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
        String jarFile = entry.getPath().toString();
        StringBuffer jarFileBuffer = new StringBuffer();
        StringBuffer jarFileAbsoluteBuffer = new StringBuffer();
        String jarFileAbsolute = ExportUtil.resolve(entry.getPath());
        if (jarFileAbsolute == null) {
            jarFileAbsolute = jarFile; // jarFile was already absolute
            if (handleSubProjectClassesDirectory(jarFile, jarFileBuffer, jarFileAbsoluteBuffer)) {
                jarFile = jarFileBuffer.toString();
                jarFileAbsolute = jarFileAbsoluteBuffer.toString();
            }//from   w w  w. j  a  v a2  s.  c  om
        }
        String jarFileOld = jarFile;
        jarFile = ExportUtil.removeProjectRoot(jarFile, project.getProject());
        if (jarFile.equals(jarFileOld)) {
            if (handleSubProjectClassesDirectory(jarFile, jarFileBuffer, jarFileAbsoluteBuffer)) {
                jarFile = jarFileBuffer.toString();
                jarFileAbsolute = jarFileAbsoluteBuffer.toString();
            }
        }
        rawClassPathEntries.add(jarFile);
        rawClassPathEntriesAbsolute.add(jarFileAbsolute);
    }
}

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

License:Open Source License

private void handleVariables(IClasspathEntry entry) {
    if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE
            && entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
        // found variable
        String e = entry.getPath().toString();
        int index = e.indexOf('/');
        if (index == -1) {
            index = e.indexOf('\\');
        }/*  w  w w . j a v  a2  s  . com*/
        String variable = e;
        String path = IAntCoreConstants.EMPTY_STRING;
        if (index != -1) {
            variable = e.substring(0, index);
            path = e.substring(index);
        }
        IPath value = JavaCore.getClasspathVariable(variable);
        if (value != null) {
            String projectRoot = ExportUtil.getProjectRoot(project);
            String relativePath = ExportUtil.getRelativePath(value.toString(), projectRoot);
            variable2valueMap.put(variable, relativePath);
        } else if (variable2valueMap.get(variable) == null) {
            // only add empty value, if variable is new
            variable2valueMap.put(variable, IAntCoreConstants.EMPTY_STRING);
        }
        rawClassPathEntriesAbsolute.add(value + path);
        rawClassPathEntries.add("${" + variable + "}" + path); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

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  w ww.  j  a  v a  2 s  .co  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.ant.internal.ui.datatransfer.EclipseClasspath.java

License:Open Source License

private void handleProjects(IClasspathEntry entry) {
    if (entry.getContentKind() == IPackageFragmentRoot.K_SOURCE
            && entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
        // found required project on build path
        String subProjectRoot = entry.getPath().toString();
        IJavaProject subProject = ExportUtil.getJavaProject(subProjectRoot);
        if (subProject == null) {
            // project was not loaded in workspace
            AntUIPlugin.log("project is not loaded in workspace: " + subProjectRoot, null); //$NON-NLS-1$
            return;
        }//from w w w.  j a v  a  2 s  .c  o  m
        // only add an indicator that this is a project reference
        String classpathRef = "${" + subProject.getProject().getName() + ".classpath}"; //$NON-NLS-1$ //$NON-NLS-2$
        srcDirs.add(classpathRef);
        classDirs.add(classpathRef);
        rawClassPathEntries.add(classpathRef);
        rawClassPathEntriesAbsolute.add(classpathRef);
        inclusionLists.add(new ArrayList<String>());
        exclusionLists.add(new ArrayList<String>());
    }
}

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

License:Open Source License

private static void addClasspathProjects(List<IJavaProject> projects, IClasspathEntry[] entries) {
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry classpathEntry = entries[i];
        if (classpathEntry.getContentKind() == IPackageFragmentRoot.K_SOURCE
                && classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            // found required project on build path
            String subProjectRoot = classpathEntry.getPath().toString();
            IJavaProject subProject = getJavaProject(subProjectRoot);
            // is project available in workspace
            if (subProject != null) {
                projects.add(subProject);
            }/*w  w w  . j  ava2 s  .c  om*/
        }
    }
}

From source file:org.eclipse.birt.report.debug.internal.ui.launcher.util.WorkspaceClassPathFinder.java

License:Open Source License

private List getExistingEntries(IClasspathEntry[] classpathEntries, IProject project) {
    ArrayList newClassPath = new ArrayList();
    for (int i = 0; i < classpathEntries.length; i++) {
        IClasspathEntry curr = classpathEntries[i];
        if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            try {
                boolean inWorkSpace = true;
                IWorkspace space = ResourcesPlugin.getWorkspace();
                if (space == null || space.getRoot() == null) {
                    inWorkSpace = false;
                }//from  w  ww . j a v  a  2s  . c om

                IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                IPath path = curr.getPath();
                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);
                } else {
                    newClassPath.add(curr.getPath().toFile().toURL());
                }

            } catch (MalformedURLException e) {
                // DO nothing
            }
        }
    }
    return newClassPath;
}

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

License:Open Source License

@Override
public boolean performApply() {
    String value = ""; //$NON-NLS-1$
    List list = fLibrariesList.getElements();

    for (int i = 0; i < list.size(); i++) {
        StringBuffer entryScript = new StringBuffer();
        IDECPListElement element = (IDECPListElement) list.get(i);
        int type = getType(element);
        if (type == UNKNOW_TYPE) {
            continue;
        }/* w ww  .j  av  a2  s . com*/
        IClasspathEntry entry = element.getClasspathEntry();
        if (entry == null) {
            continue;
        }
        entryScript.append(entry.getEntryKind());
        entryScript.append(TYPE_SEPARATOR);
        entryScript.append(type);
        entryScript.append(TYPE_SEPARATOR);
        IResource resource = element.getResource();
        IPath path;
        if (resource == null) {
            path = element.getPath();
        } else {
            path = resource.getFullPath();
        }
        entryScript.append(path);
        if (i != list.size() - 1) {
            entryScript.append(ENTRY_SEPARATOR);
        }
        value = value + entryScript.toString();
    }

    setValue(PREF_CLASSPATH, value);
    return super.performApply();
}