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:com.android.ide.eclipse.adt.internal.project.AndroidClasspathContainerInitializer.java

License:Open Source License

@Override
public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project,
        IClasspathContainer containerSuggestion) throws CoreException {
    AdtPlugin plugin = AdtPlugin.getDefault();

    synchronized (Sdk.getLock()) {
        boolean sdkIsLoaded = plugin.getSdkLoadStatus() == LoadStatus.LOADED;

        // check if the project has a valid target.
        IAndroidTarget target = null;//from   w  ww  .j  a  v a  2s  .c o m
        if (sdkIsLoaded) {
            target = Sdk.getCurrent().getTarget(project.getProject());
        }
        if (sdkIsLoaded && target != null) {
            String[] paths = getTargetPaths(target);
            IPath android_lib = new Path(paths[CACHE_INDEX_JAR]);
            IClasspathEntry[] entries = containerSuggestion.getClasspathEntries();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    IPath entryPath = entry.getPath();

                    if (entryPath != null) {
                        if (entryPath.equals(android_lib)) {
                            IPath entrySrcPath = entry.getSourceAttachmentPath();
                            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                            if (entrySrcPath != null) {
                                ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target),
                                        entrySrcPath.toString());
                            } else {
                                ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target), null);
                            }
                            IClasspathAttribute[] extraAttributtes = entry.getExtraAttributes();
                            if (extraAttributtes.length == 0) {
                                ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, NULL_API_URL);
                            }
                            for (int j = 0; j < extraAttributtes.length; j++) {
                                IClasspathAttribute extraAttribute = extraAttributtes[j];
                                String value = extraAttribute.getValue();
                                if ((value == null || value.trim().length() == 0)
                                        && IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME
                                                .equals(extraAttribute.getName())) {
                                    value = NULL_API_URL;
                                }
                                if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME
                                        .equals(extraAttribute.getName())) {
                                    ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, value);

                                }
                            }
                        }
                    }
                }
            }
            rebindClasspathEntries(project.getJavaModel(), containerPath);
        }
    }
}

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

License:Open Source License

private static void rebindClasspathEntries(IJavaModel model, IPath containerPath) throws JavaModelException {
    ArrayList<IJavaProject> affectedProjects = new ArrayList<IJavaProject>();

    IJavaProject[] projects = model.getJavaProjects();
    for (int i = 0; i < projects.length; i++) {
        IJavaProject project = projects[i];
        IClasspathEntry[] entries = project.getRawClasspath();
        for (int k = 0; k < entries.length; k++) {
            IClasspathEntry curr = entries[k];
            if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER && containerPath.equals(curr.getPath())) {
                affectedProjects.add(project);
            }//w  w w .  j  av a 2  s .c  o  m
        }
    }
    if (!affectedProjects.isEmpty()) {
        IJavaProject[] affected = affectedProjects.toArray(new IJavaProject[affectedProjects.size()]);
        updateProjects(affected);
    }
}

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

License:Open Source License

/**
 * returns a list of source classpath for a specified project
 * @param javaProject//from  w  w  w.j  a v  a  2s .  co m
 * @return a list of path relative to the workspace root.
 */
@NonNull
public static List<IPath> getSourceClasspaths(IJavaProject javaProject) {
    List<IPath> sourceList = Lists.newArrayList();
    IClasspathEntry[] classpaths = javaProject.readRawClasspath();
    if (classpaths != null) {
        for (IClasspathEntry e : classpaths) {
            if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                sourceList.add(e.getPath());
            }
        }
    }

    return sourceList;
}

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

License:Open Source License

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

    if (AdtPlugin.getDefault() == null) { // This is totally weird, but I've seen it happen!
        return null;
    }/*w ww . java 2s.co  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:com.android.ide.eclipse.adt.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//  w  w w.  ja va2s  . 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(AdtConstants.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.
            AdtPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", entry.getPath());
        }
    }
}

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

License:Open Source License

/**
 * Look for a specific classpath entry by full path and return its index.
 * @param entries The entry array to search in.
 * @param entryPath The OS specific path of the entry.
 * @param entryKind The kind of the entry. Accepted values are 0
 * (no filter), IClasspathEntry.CPE_LIBRARY, IClasspathEntry.CPE_PROJECT,
 * IClasspathEntry.CPE_SOURCE, IClasspathEntry.CPE_VARIABLE,
 * and IClasspathEntry.CPE_CONTAINER//w ww .  j ava2  s .  c om
 * @return the index of the found classpath entry or -1.
 */
public static int findClasspathEntryByPath(IClasspathEntry[] entries, String entryPath, int entryKind) {
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];

        int kind = entry.getEntryKind();

        if (kind == entryKind || entryKind == 0) {
            // get the path
            IPath path = entry.getPath();

            String osPathString = path.toOSString();
            if (osPathString.equals(entryPath)) {
                return i;
            }
        }
    }

    // not found, return bad index.
    return -1;
}

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

License:Open Source License

/**
 * Look for a specific classpath entry for file name only and return its
 *  index.//from   ww  w.ja  v  a 2s .c o m
 * @param entries The entry array to search in.
 * @param entryName The filename of the entry.
 * @param entryKind The kind of the entry. Accepted values are 0
 * (no filter), IClasspathEntry.CPE_LIBRARY, IClasspathEntry.CPE_PROJECT,
 * IClasspathEntry.CPE_SOURCE, IClasspathEntry.CPE_VARIABLE,
 * and IClasspathEntry.CPE_CONTAINER
 * @param startIndex Index where to start the search
 * @return the index of the found classpath entry or -1.
 */
public static int findClasspathEntryByName(IClasspathEntry[] entries, String entryName, int entryKind,
        int startIndex) {
    if (startIndex < 0) {
        startIndex = 0;
    }
    for (int i = startIndex; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];

        int kind = entry.getEntryKind();

        if (kind == entryKind || entryKind == 0) {
            // get the path
            IPath path = entry.getPath();
            String name = path.segment(path.segmentCount() - 1);

            if (name.equals(entryName)) {
                return i;
            }
        }
    }

    // not found, return bad index.
    return -1;
}

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

License:Open Source License

/**
 * Fix the project classpath entries. The method ensures that:
 * <ul>//w ww . jav a  2 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 (AdtConstants.CONTAINER_FRAMEWORK.equals(path)) {
                foundFrameworkContainer = true;
            } else if (AdtConstants.CONTAINER_PRIVATE_LIBRARIES.equals(path)) {
                foundLibrariesContainer = entry;
            } else if (AdtConstants.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) {
        AdtPlugin.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(AdtConstants.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(AdtConstants.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(AdtConstants.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(AdtConstants.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(AdtConstants.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:com.android.ide.eclipse.adt.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 a  v  a  2 s  .c o  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.
                    AdtPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", e.getPath());
                }
            }
        }
    }

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

From source file:com.android.ide.eclipse.adt.internal.sourcelookup.AdtSourceLookupDirector.java

License:Open Source License

@Override
public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
    dispose();//w w w. jav  a  2  s.  c om
    setLaunchConfiguration(configuration);
    String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
    if (projectName != null && projectName.length() > 0) {
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        if (project != null && project.isOpen()) {
            ProjectState state = Sdk.getProjectState(project);
            if (state == null) {
                initDefaults();
                return;
            }
            IAndroidTarget target = state.getTarget();
            if (target == null) {
                initDefaults();
                return;
            }
            String path = target.getPath(IAndroidTarget.ANDROID_JAR);
            if (path == null) {
                initDefaults();
                return;
            }
            IJavaProject javaProject = JavaCore.create(project);
            if (javaProject != null && javaProject.isOpen()) {
                IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
                IClasspathEntry androidEntry = null;
                for (int i = 0; i < entries.length; i++) {
                    IClasspathEntry entry = entries[i];
                    if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                            && path.equals(entry.getPath().toString())) {
                        androidEntry = entry;
                        break;
                    }
                }
                if (androidEntry != null) {
                    IPath sourceAttachmentPath = androidEntry.getSourceAttachmentPath();
                    if (sourceAttachmentPath != null) {
                        String androidSrc = sourceAttachmentPath.toString();
                        if (androidSrc != null && androidSrc.trim().length() > 0) {
                            File srcFile = new File(androidSrc);
                            ISourceContainer adtContainer = null;
                            if (srcFile.isFile()) {
                                adtContainer = new ExternalArchiveSourceContainer(androidSrc, true);
                            }
                            if (srcFile.isDirectory()) {
                                adtContainer = new DirectorySourceContainer(srcFile, false);
                            }
                            if (adtContainer != null) {
                                ISourceContainer defaultContainer = new DefaultSourceContainer();
                                setSourceContainers(new ISourceContainer[] { adtContainer, defaultContainer });
                                initializeParticipants();
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
    initDefaults();
}