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:cn.ieclipse.aorm.eclipse.helpers.IntentReflectionHelper.java

License:Apache License

/**
 * Get all classpathentries for the given project.
 * /*from   w w  w. j a va  2 s .c om*/
 * @param javaProject
 *            project to get the classpath for.
 * @return classpathentries
 */
private String[] getJavaClasspath(IJavaProject javaProject) throws CoreException {
    List<String> classPath = new ArrayList<String>();
    String[] defaultClassPath = JavaRuntime.computeDefaultRuntimeClassPath(javaProject);
    classPath.addAll(Arrays.asList(defaultClassPath));

    // add CPE_CONTAINER classpathes
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(entry.getPath(),
                    javaProject);
            if (classpathContainer != null) {
                IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries();
                for (IClasspathEntry cEntry : classpathEntries) {
                    classPath.add(cEntry.getPath().toOSString());
                }
            }
        }
    }
    return classPath.toArray(new String[] {});
}

From source file:com.android.ide.eclipse.adt.build.BaseBuilder.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  a v a2 s. co  m
protected final String[] getExternalJars() {
    // get the current project
    IProject project = getProject();

    // get a java project from it
    IJavaProject javaProject = JavaCore.create(project);

    IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();

    ArrayList<String> oslibraryList = new ArrayList<String>();
    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);
                }

                // get the IPath
                IPath path = e.getPath();

                // check the name ends with .jar
                if (AndroidConstants.EXT_JAR.equalsIgnoreCase(path.getFileExtension())) {
                    boolean local = false;
                    IResource resource = wsRoot.findMember(path);
                    if (resource != null && resource.exists() && resource.getType() == IResource.FILE) {
                        local = true;
                        oslibraryList.add(resource.getLocation().toOSString());
                    }

                    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()) {
                            oslibraryList.add(osFullPath);
                        } else {
                            String message = String.format(Messages.Couldnt_Locate_s_Error, path);
                            AdtPlugin.printBuildToConsole(AdtConstants.BUILD_VERBOSE, project, message);

                            // Also put a warning marker on the project
                            markProject(AdtConstants.MARKER_ADT, message, IMarker.SEVERITY_WARNING);
                        }
                    }
                }
            }
        }
    }

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

From source file:com.android.ide.eclipse.adt.build.ResourceManagerBuilder.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  w  w.  j  av a  2s  .  co m*/
protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException {
    // Get the project.
    IProject project = getProject();

    // Clear the project of the generic markers
    BaseBuilder.removeMarkersFromProject(project, AdtConstants.MARKER_ADT);

    // check for existing target marker, in which case we abort.
    // (this means: no SDK, no target, or unresolvable target.)
    abortOnBadSetup(project);

    // Check the compiler compliance level, displaying the error message
    // since this is the first builder.
    int res = ProjectHelper.checkCompilerCompliance(project);
    String errorMessage = null;
    switch (res) {
    case ProjectHelper.COMPILER_COMPLIANCE_LEVEL:
        errorMessage = Messages.Requires_Compiler_Compliance_5;
    case ProjectHelper.COMPILER_COMPLIANCE_SOURCE:
        errorMessage = Messages.Requires_Source_Compatibility_5;
    case ProjectHelper.COMPILER_COMPLIANCE_CODEGEN_TARGET:
        errorMessage = Messages.Requires_Class_Compatibility_5;
    }

    if (errorMessage != null) {
        BaseProjectHelper.addMarker(project, AdtConstants.MARKER_ADT, errorMessage, IMarker.SEVERITY_ERROR);
        AdtPlugin.printErrorToConsole(project, errorMessage);

        // interrupt the build. The next builders will not run.
        stopBuild(errorMessage);
    }

    // Check that the SDK directory has been setup.
    String osSdkFolder = AdtPlugin.getOsSdkFolder();

    if (osSdkFolder == null || osSdkFolder.length() == 0) {
        AdtPlugin.printErrorToConsole(project, Messages.No_SDK_Setup_Error);
        markProject(AdtConstants.MARKER_ADT, Messages.No_SDK_Setup_Error, IMarker.SEVERITY_ERROR);

        // This interrupts the build. The next builders will not run.
        stopBuild(Messages.No_SDK_Setup_Error);
    }

    // check the project has a target
    IAndroidTarget projectTarget = Sdk.getCurrent().getTarget(project);
    if (projectTarget == null) {
        // no target. marker has been set by the container initializer: exit silently.
        // This interrupts the build. The next builders will not run.
        stopBuild("Project has no target");
    }

    // check the 'gen' source folder is present
    boolean hasGenSrcFolder = false; // whether the project has a 'gen' source folder setup
    IJavaProject javaProject = JavaCore.create(project);

    IClasspathEntry[] classpaths = javaProject.readRawClasspath();
    if (classpaths != null) {
        for (IClasspathEntry e : classpaths) {
            if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = e.getPath();
                if (path.segmentCount() == 2 && path.segment(1).equals(SdkConstants.FD_GEN_SOURCES)) {
                    hasGenSrcFolder = true;
                    break;
                }
            }
        }
    }

    boolean genFolderPresent = false; // whether the gen folder actually exists
    IResource resource = project.findMember(SdkConstants.FD_GEN_SOURCES);
    genFolderPresent = resource != null && resource.exists();

    if (hasGenSrcFolder == false && genFolderPresent) {
        // No source folder setup for 'gen' in the project, but there's already a
        // 'gen' resource (file or folder).
        String message;
        if (resource.getType() == IResource.FOLDER) {
            // folder exists already! This is an error. If the folder had been created
            // by the NewProjectWizard, it'd be a source folder.
            message = String.format(
                    "%1$s already exists but is not a source folder. Convert to a source folder or rename it.",
                    resource.getFullPath().toString());
        } else {
            // resource exists but is not a folder.
            message = String.format(
                    "Resource %1$s is in the way. ADT needs a source folder called 'gen' to work. Rename or delete resource.",
                    resource.getFullPath().toString());
        }

        AdtPlugin.printErrorToConsole(project, message);
        markProject(AdtConstants.MARKER_ADT, message, IMarker.SEVERITY_ERROR);

        // This interrupts the build. The next builders will not run.
        stopBuild(message);
    } else if (hasGenSrcFolder == false || genFolderPresent == false) {
        // either there is no 'gen' source folder in the project (older SDK),
        // or the folder does not exist (was deleted, or was a fresh svn checkout maybe.)

        // In case we are migrating from an older SDK, we go through the current source
        // folders and delete the generated Java files.
        ArrayList<IPath> sourceFolders = BaseProjectHelper.getSourceClasspaths(javaProject);
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        for (IPath path : sourceFolders) {
            IResource member = root.findMember(path);
            if (member != null) {
                removeDerivedResources(member, monitor);
            }
        }

        // create the new source folder, if needed
        IFolder genFolder = project.getFolder(SdkConstants.FD_GEN_SOURCES);
        if (genFolderPresent == false) {
            AdtPlugin.printBuildToConsole(AdtConstants.BUILD_VERBOSE, project,
                    "Creating 'gen' source folder for generated Java files");
            genFolder.create(true /* force */, true /* local */, new SubProgressMonitor(monitor, 10));
            genFolder.setDerived(true);
        }

        // add it to the source folder list, if needed only (or it will throw)
        if (hasGenSrcFolder == false) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            entries = ProjectHelper.addEntryToClasspath(entries,
                    JavaCore.newSourceEntry(genFolder.getFullPath()));
            javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 10));
        }

        // refresh the whole project
        project.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 10));
    }

    // Check the preference to be sure we are supposed to refresh
    // the folders.
    if (AdtPlugin.getAutoResRefresh()) {
        AdtPlugin.printBuildToConsole(AdtConstants.BUILD_VERBOSE, project, Messages.Refreshing_Res);

        // refresh the res folder.
        IFolder resFolder = project.getFolder(AndroidConstants.WS_RESOURCES);
        resFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor);

        // Also refresh the assets folder to make sure the ApkBuilder
        // will now it's changed and will force a new resource packaging.
        IFolder assetsFolder = project.getFolder(AndroidConstants.WS_ASSETS);
        assetsFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor);
    }

    return null;
}

From source file:com.android.ide.eclipse.adt.internal.build.BuildHelper.java

License:Open Source License

/**
 * Computes all the project output and dependencies that must go into building the apk.
 *
 * @param resMarker/*from w  ww .  j a v a 2 s.  c  o  m*/
 * @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 they're in the DEPEDENCIES 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_DEPENDENCIES))) {
                handleCPE(e, javaProject, wsRoot, resMarker);
            }
        }
    }
}

From source file:com.android.ide.eclipse.adt.internal.build.BuildHelper.java

License:Open Source License

private void handleCPE(IClasspathEntry entry, IJavaProject javaProject, IWorkspaceRoot wsRoot,
        ResourceMarker resMarker) {//  w w w.j  a  v a  2 s  .  c  o m

    // 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) {
                IJavaProject refJavaProject = JavaCore.create(refProject);

                // get the output folder
                IPath path = refJavaProject.getOutputLocation();
                IResource outputResource = wsRoot.findMember(path);
                if (outputResource != null && outputResource.getType() == IResource.FOLDER) {
                    mCompiledCodePaths.add(outputResource.getLocation().toOSString());
                }
            }
        } catch (CoreException exception) {
            // can't query the project nature? ignore
        }

    } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
        handleClasspathLibrary(entry, wsRoot, resMarker);
    } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
        // get the container
        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) {
                    handleCPE(cpe, javaProject, wsRoot, resMarker);
                }
            }
        } 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.build.PostCompilerHelper.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 a v a2s . c  o  m
private final String[] getExternalJars() {
    // get a java project from it
    IJavaProject javaProject = JavaCore.create(mProject);

    IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();

    ArrayList<String> oslibraryList = new ArrayList<String>();
    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);
                }

                // get the IPath
                IPath path = e.getPath();

                // check the name ends with .jar
                if (AndroidConstants.EXT_JAR.equalsIgnoreCase(path.getFileExtension())) {
                    IResource resource = wsRoot.findMember(path);
                    if (resource != null && resource.exists() && resource.getType() == IResource.FILE) {
                        oslibraryList.add(resource.getLocation().toOSString());
                    } else {
                        // 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()) {
                            oslibraryList.add(osFullPath);
                        } else {
                            String message = String.format(Messages.Couldnt_Locate_s_Error, path);
                            AdtPlugin.printBuildToConsole(BuildVerbosity.VERBOSE, mProject, message);

                            // Also put a warning marker on the project
                            BaseProjectHelper.markResource(mProject, AndroidConstants.MARKER_PACKAGING, message,
                                    IMarker.SEVERITY_WARNING);
                        }
                    }
                }
            }
        }
    }

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

From source file:com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.java

License:Open Source License

/**
 * Computes and return the {@link IPackageFragmentRoot}s corresponding to the source
 * folders of the specified project.//from  ww w .  ja va2 s . c om
 *
 * @param project the project
 * @param includeContainers True to include containers
 * @param skipGenFolder True to skip the "gen" folder
 * @return an array of IPackageFragmentRoot.
 */
private IPackageFragmentRoot[] getPackageFragmentRoots(IProject project, boolean includeContainers,
        boolean skipGenFolder) {
    ArrayList<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();
    try {
        IJavaProject javaProject = JavaCore.create(project);
        IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (skipGenFolder) {
                IResource resource = roots[i].getResource();
                if (resource != null && resource.getName().equals(FD_GEN_SOURCES)) {
                    continue;
                }
            }
            IClasspathEntry entry = roots[i].getRawClasspathEntry();
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                    || (includeContainers && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER)) {
                result.add(roots[i]);
            }
        }
    } catch (JavaModelException e) {
    }

    return result.toArray(new IPackageFragmentRoot[result.size()]);
}

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.model.UiClassAttributeNode.java

License:Open Source License

/**
 * Computes and return the {@link IPackageFragmentRoot}s corresponding to the source folders of
 * the specified project./*from ww w.  j av a  2  s  .  c o  m*/
 * @param project the project
 * @param include_containers True to include containers
 * @return an array of IPackageFragmentRoot.
 */
private IPackageFragmentRoot[] getPackageFragmentRoots(IProject project, boolean include_containers) {
    ArrayList<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();
    try {
        IJavaProject javaProject = JavaCore.create(project);
        IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            IClasspathEntry entry = roots[i].getRawClasspathEntry();
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                    || (include_containers && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER)) {
                result.add(roots[i]);
            }
        }
    } catch (JavaModelException e) {
    }

    return result.toArray(new IPackageFragmentRoot[result.size()]);
}

From source file:com.android.ide.eclipse.adt.internal.editors.manifest.model.UiPackageAttributeNode.java

License:Open Source License

/**
 * Utility method that computes and returns the list of {@link IPackageFragmentRoot}
 * corresponding to the source folder of the specified project.
 *
 * @param project the project//ww  w  .j a  va 2  s.  co  m
 * @return an array of IPackageFragmentRoot. Can be empty but not null.
 */
private IPackageFragmentRoot[] getPackageFragmentRoots(IProject project) {
    ArrayList<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();
    try {
        IJavaProject javaProject = JavaCore.create(project);
        IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            IClasspathEntry entry = roots[i].getRawClasspathEntry();
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                result.add(roots[i]);
            }
        }
    } catch (JavaModelException e) {
    }

    return result.toArray(new IPackageFragmentRoot[result.size()]);
}

From source file:com.android.ide.eclipse.adt.internal.lint.EclipseLintClient.java

License:Open Source License

@Override
@NonNull/* w  w  w . j a  v a2 s . c  o  m*/
protected ClassPathInfo getClassPath(@NonNull Project project) {
    ClassPathInfo info;
    if (mProjectInfo == null) {
        mProjectInfo = Maps.newHashMap();
        info = null;
    } else {
        info = mProjectInfo.get(project);
    }

    if (info == null) {
        List<File> sources = null;
        List<File> classes = null;
        List<File> libraries = null;

        IProject p = getProject(project);
        if (p != null) {
            try {
                IJavaProject javaProject = BaseProjectHelper.getJavaProject(p);

                // Output path
                File file = workspacePathToFile(javaProject.getOutputLocation());
                classes = Collections.singletonList(file);

                // Source path
                IClasspathEntry[] entries = javaProject.getRawClasspath();
                sources = new ArrayList<File>(entries.length);
                libraries = new ArrayList<File>(entries.length);
                for (int i = 0; i < entries.length; i++) {
                    IClasspathEntry entry = entries[i];
                    int kind = entry.getEntryKind();

                    if (kind == IClasspathEntry.CPE_VARIABLE) {
                        entry = JavaCore.getResolvedClasspathEntry(entry);
                        if (entry == null) {
                            // It's possible that the variable is no longer valid; ignore
                            continue;
                        }
                        kind = entry.getEntryKind();
                    }

                    if (kind == IClasspathEntry.CPE_SOURCE) {
                        sources.add(workspacePathToFile(entry.getPath()));
                    } else if (kind == IClasspathEntry.CPE_LIBRARY) {
                        libraries.add(entry.getPath().toFile());
                    }
                    // Note that we ignore IClasspathEntry.CPE_CONTAINER:
                    // Normal Android Eclipse projects supply both
                    //   AdtConstants.CONTAINER_FRAMEWORK
                    // and
                    //   AdtConstants.CONTAINER_LIBRARIES
                    // here. We ignore the framework classes for obvious reasons,
                    // but we also ignore the library container because lint will
                    // process the libraries differently. When Eclipse builds a
                    // project, it gets the .jar output of the library projects
                    // from this container, which means it doesn't have to process
                    // the library sources. Lint on the other hand wants to process
                    // the source code, so instead it actually looks at the
                    // project.properties file to find the libraries, and then it
                    // iterates over all the library projects in turn and analyzes
                    // those separately (but passing the main project for context,
                    // such that the including project's manifest declarations
                    // are used for data like minSdkVersion level).
                    //
                    // Note that this container will also contain *other*
                    // libraries (Java libraries, not library projects) that we
                    // *should* include. However, we can't distinguish these
                    // class path entries from the library project jars,
                    // so instead of looking at these, we simply listFiles() in
                    // the libs/ folder after processing the classpath info
                }

                // Add in libraries
                File libs = new File(project.getDir(), FD_NATIVE_LIBS);
                if (libs.isDirectory()) {
                    File[] jars = libs.listFiles();
                    if (jars != null) {
                        for (File jar : jars) {
                            if (SdkUtils.endsWith(jar.getPath(), DOT_JAR)) {
                                libraries.add(jar);
                            }
                        }
                    }
                }
            } catch (CoreException e) {
                AdtPlugin.log(e, null);
            }
        }

        if (sources == null) {
            sources = super.getClassPath(project).getSourceFolders();
        }
        if (classes == null) {
            classes = super.getClassPath(project).getClassFolders();
        }
        if (libraries == null) {
            libraries = super.getClassPath(project).getLibraries();
        }

        // No test folders in Eclipse:
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=224708
        List<File> tests = Collections.emptyList();

        info = new ClassPathInfo(sources, classes, libraries, tests);
        mProjectInfo.put(project, info);
    }

    return info;
}