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.ProjectHelper.java

License:Open Source License

/**
 * Look for a specific classpath entry for file name only and return its
 *  index./*from w  w w  . j a  v a2 s .com*/
 * @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>/*from w ww  . j  av 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
 *//* ww w . j  av  a  2 s . c  om*/
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.resources.manager.ProjectClassLoader.java

License:Open Source License

private void handleClassPathEntry(IClasspathEntry e, ArrayList<URL> oslibraryList) {
    // get the IPath
    IPath path = e.getPath();

    // check the name ends with .jar
    if (SdkConstants.EXT_JAR.equalsIgnoreCase(path.getFileExtension())) {
        boolean local = false;
        IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
        if (resource != null && resource.exists() && resource.getType() == IResource.FILE) {
            local = true;/*  w  w w.  j  av  a2  s. com*/
            try {
                oslibraryList.add(new File(resource.getLocation().toOSString()).toURI().toURL());
            } catch (MalformedURLException mue) {
                // pass
            }
        }

        if (local == false) {
            // 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 File(osFullPath);
            if (f.exists()) {
                try {
                    oslibraryList.add(f.toURI().toURL());
                } catch (MalformedURLException mue) {
                    // pass
                }
            }
        }
    }
}

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();//from w ww . j  a  v  a2s .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();
}

From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.BuildFileCreator.java

License:Open Source License

/**
 * Outputs a sourceSets block to the Android task that locates all of the various source
 * subdirectories in the project./*from   ww  w  . j a v  a2  s  . c o  m*/
 */
private void createAndroidSourceSets() {
    IFolderWrapper projectFolder = new IFolderWrapper(mModule.getProject());
    IAbstractFile mManifestFile = AndroidManifest.getManifest(projectFolder);
    if (mManifestFile == null) {
        return;
    }
    List<String> srcDirs = new ArrayList<String>();
    for (IClasspathEntry entry : mModule.getJavaProject().readRawClasspath()) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE
                || SdkConstants.FD_GEN_SOURCES.equals(entry.getPath().lastSegment())) {
            continue;
        }
        IPath path = entry.getPath().removeFirstSegments(1);
        srcDirs.add("'" + path.toOSString() + "'"); //$NON-NLS-1$
    }

    String srcPaths = Joiner.on(",").join(srcDirs);

    mBuildFile.append("    sourceSets {\n"); //$NON-NLS-1$
    mBuildFile.append("        main {\n"); //$NON-NLS-1$
    mBuildFile.append("            manifest.srcFile '" + SdkConstants.FN_ANDROID_MANIFEST_XML + "'\n"); //$NON-NLS-1$
    mBuildFile.append("            java.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$
    mBuildFile.append("            resources.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$
    mBuildFile.append("            aidl.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$
    mBuildFile.append("            renderscript.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$
    mBuildFile.append("            res.srcDirs = ['res']\n"); //$NON-NLS-1$
    mBuildFile.append("            assets.srcDirs = ['assets']\n"); //$NON-NLS-1$
    mBuildFile.append("        }\n"); //$NON-NLS-1$
    mBuildFile.append("\n"); //$NON-NLS-1$
    mBuildFile.append("        // Move the tests to tests/java, tests/res, etc...\n"); //$NON-NLS-1$
    mBuildFile.append("        instrumentTest.setRoot('tests')\n"); //$NON-NLS-1$
    if (srcDirs.contains("'src'")) {
        mBuildFile.append("\n"); //$NON-NLS-1$
        mBuildFile.append("        // Move the build types to build-types/<type>\n"); //$NON-NLS-1$
        mBuildFile.append(
                "        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...\n"); //$NON-NLS-1$
        mBuildFile.append(
                "        // This moves them out of them default location under src/<type>/... which would\n"); //$NON-NLS-1$
        mBuildFile.append("        // conflict with src/ being used by the main source set.\n"); //$NON-NLS-1$
        mBuildFile.append("        // Adding new build types or product flavors should be accompanied\n"); //$NON-NLS-1$
        mBuildFile.append("        // by a similar customization.\n"); //$NON-NLS-1$
        mBuildFile.append("        debug.setRoot('build-types/debug')\n"); //$NON-NLS-1$
        mBuildFile.append("        release.setRoot('build-types/release')\n"); //$NON-NLS-1$
    }
    mBuildFile.append("    }\n"); //$NON-NLS-1$
}

From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.BuildFileCreator.java

License:Open Source License

/**
 * Outputs a sourceSets block for non-Android projects to locate the source directories.
 *//*from   w  w  w  .  ja va  2 s . co m*/
private void createJavaSourceSets() {
    List<String> dirs = new ArrayList<String>();
    for (IClasspathEntry entry : mModule.getJavaProject().readRawClasspath()) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            continue;
        }
        IPath path = entry.getPath().removeFirstSegments(1);
        dirs.add("'" + path.toOSString() + "'"); //$NON-NLS-1$
    }

    String srcPaths = Joiner.on(",").join(dirs);

    mBuildFile.append("sourceSets {\n"); //$NON-NLS-1$
    mBuildFile.append("    main.java.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$
    mBuildFile.append("    main.resources.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$
    mBuildFile.append("    test.java.srcDirs = ['tests/java']\n"); //$NON-NLS-1$
    mBuildFile.append("    test.resources.srcDirs = ['tests/resources']\n"); //$NON-NLS-1$
    mBuildFile.append("}\n"); //$NON-NLS-1$
}

From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.ProjectSetupBuilder.java

License:Open Source License

@NonNull
private static List<IJavaProject> getReferencedProjects(IJavaProject javaProject)
        throws JavaModelException, InternalException {

    List<IJavaProject> projects = Lists.newArrayList();

    IClasspathEntry entries[] = javaProject.getRawClasspath();
    for (IClasspathEntry classpathEntry : entries) {
        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);
            } else {
                throw new InternalException(String.format(
                        "Project '%s' is missing project dependency '%s' in Eclipse workspace.\n"
                                + "Make sure all dependencies are opened.",
                        javaProject.getProject().getName(), classpathEntry.getPath().toString()));
            }//  www .j a va2  s  .  c  o m
        }
    }

    return projects;
}

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

License:Open Source License

/**
 * Fix the project classpath entries. The method ensures that:
 * <ul>/*from   w ww  . jav 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;

    // 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 foundContainer = false;

    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) {
            if (AndroidClasspathContainerInitializer.checkPath(entry.getPath())) {
                foundContainer = true;
            }
        }

        i++;
    }

    // if the framework container is not there, we add it
    if (foundContainer == false) {
        // add the android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                AndroidClasspathContainerInitializer.getContainerEntry());
    }

    // set the new list of entries to the project
    if (entries != oldEntries) {
        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.auidt.internal.build.BuildHelper.java

License:Open Source License

/**
 * Computes all the project output and dependencies that must go into building the apk.
 *
 * @param resMarker//  w ww .ja  va 2s  .  com
 * @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 it's the LIBRARIES 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(AdtConstants.CONTAINER_LIBRARIES))) {
                handleCPE(e, javaProject, wsRoot, resMarker);
            }
        }
    }
}