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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path of this classpath entry.

Usage

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);
            }//  ww w  .java2s. com
        }
    }
    if (!affectedProjects.isEmpty()) {
        IJavaProject[] affected = affectedProjects.toArray(new IJavaProject[affectedProjects.size()]);
        updateProjects(affected);
    }
}

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

License:Open Source License

@Override
public void setSelection(final IClasspathEntry cpentry) {
    final IPath path = cpentry == null ? null : cpentry.getPath();

    if (path == null || path.segmentCount() == 1) {
        if (this.mOwnerProject != null) {
            this.mLibsProjectName = this.mOwnerProject.getName();
        }//from ww  w  . j a  v  a2  s .  com
    } else {
        this.mLibsProjectName = path.segment(1);
    }
}

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/*  www . j a  v a  2s. c om*/
 * @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 List<IClasspathEntry> convertJarsToClasspathEntries(final IProject iProject,
        Set<File> jarFiles) {
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(jarFiles.size());

    // 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;/*w  w  w .  ja  va2  s  .c om*/

    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 entries;
}

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  w  w  . ja v  a  2  s .  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:com.android.ide.eclipse.adt.internal.project.LibraryClasspathContainerInitializer.java

License:Open Source License

/**
 * Finds all the dependencies of a given project and add them to a project list and
 * a jar list.//from   w w w.  j a v  a2s.  c  o  m
 * Only classpath entries that are exported are added, and only Java project (not Android
 * project) are added.
 *
 * @param project the project to query
 * @param projects the referenced 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 getDependencyListFromClasspath(IProject project, Set<IProject> projects, Set<File> jarFiles,
        boolean includeJarFiles) {
    IJavaProject javaProject = JavaCore.create(project);
    IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();

    // 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 entries that are not exported
            if (!e.getPath().toString().equals(CONTAINER_DEPENDENCIES) && e.isExported()) {
                processCPE(e, javaProject, wsRoot, projects, jarFiles, includeJarFiles);
            }
        }
    }
}

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//from w w w .  j  ava 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(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.LibraryClasspathContainerInitializer.java

License:Open Source License

private static void handleClasspathLibrary(IClasspathEntry e, IWorkspaceRoot wsRoot, Set<File> jarFiles) {
    // get the IPath
    IPath path = e.getPath();

    IResource resource = wsRoot.findMember(path);

    if (SdkConstants.EXT_JAR.equalsIgnoreCase(path.getFileExtension())) {
        // case of a jar file (which could be relative to the workspace or a full path)
        if (resource != null && resource.exists() && resource.getType() == IResource.FILE) {
            jarFiles.add(new CPEFile(resource.getLocation().toFile(), e));
        } else {//from   www .j  av  a2  s.c  o m
            // if the jar path doesn't match a workspace resource,
            // then we get an OSString and check if this links to a valid file.
            String osFullPath = path.toOSString();

            File f = new CPEFile(osFullPath, e);
            if (f.isFile()) {
                jarFiles.add(f);
            }
        }
    }
}

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

License:Open Source License

/**
 * Replaces the given ClasspathEntry in the classpath entries.
 *
 * If the classpath does not yet exists (Check is based on entry path), then it is added.
 *
 * @param entries The class path entries to read. The same array (replace) or a copy (add)
 *                will be returned./*from w w  w. jav  a  2  s . com*/
 * @param newEntry The new class path entry to add.
 * @return The same array (replace) or a copy (add) will be returned.
 *
 * @see IClasspathEntry#getPath()
 */
public static IClasspathEntry[] replaceEntryInClasspath(IClasspathEntry[] entries, IClasspathEntry newEntry) {

    IPath path = newEntry.getPath();
    for (int i = 0, count = entries.length; i < count; i++) {
        if (path.equals(entries[i].getPath())) {
            entries[i] = newEntry;
            return entries;
        }
    }

    return addEntryToClasspath(entries, newEntry);
}

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  w  w. j  a  va2s.co  m
 * @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;
}