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.ajdt.ui.tests.builder.ProjectDependenciesUtils.java

License:Open Source License

public static boolean projectHasOutJarOnClasspath(IProject projectToHaveJar, IProject projectWhichHasOutJar,
        String outJar) throws JavaModelException {
    IJavaProject javaProject1 = JavaCore.create(projectToHaveJar);
    if (javaProject1 == null) {
        return false;
    }//from  ww  w .j  a va2s.  c om
    System.out.println("TEST: outjar = " + outJar); //$NON-NLS-1$
    StringBuffer sb = new StringBuffer(outJar);
    int indexOfProject = sb.lastIndexOf(projectWhichHasOutJar.getName());
    IPath newPath;
    if (indexOfProject < 0) {
        newPath = new Path(outJar);
    } else {
        newPath = new Path(sb.substring(indexOfProject));
    }

    IClasspathEntry[] cpEntry = javaProject1.getRawClasspath();
    for (int j = 0; j < cpEntry.length; j++) {
        IClasspathEntry entry = cpEntry[j];
        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            if (entry.getPath().equals(new Path(outJar)) || entry.getPath().equals(newPath.makeAbsolute())) {
                return true;
            }
        }
    }
    return false;
}

From source file:org.eclipse.ajdt.ui.tests.builder.ProjectDependenciesUtils.java

License:Open Source License

public static int numberOfTimesOutJarOnClasspath(IProject projectToHaveJar, IProject projectWhichHasOutJar,
        String outJar) throws JavaModelException {
    IJavaProject javaProject1 = JavaCore.create(projectToHaveJar);
    if (javaProject1 == null) {
        return -1;
    }// w  ww .  j  ava  2s .c  o m
    int counter = 0;
    System.out.println("TEST: outjar = " + outJar); //$NON-NLS-1$
    StringBuffer sb = new StringBuffer(outJar);
    int indexOfProject = sb.lastIndexOf(projectWhichHasOutJar.getName());
    String jar = sb.substring(indexOfProject);
    IPath newPath = new Path(jar);

    IClasspathEntry[] cpEntry = javaProject1.getRawClasspath();
    for (int j = 0; j < cpEntry.length; j++) {
        IClasspathEntry entry = cpEntry[j];
        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            if (entry.getPath().equals(new Path(outJar)) || entry.getPath().equals(newPath.makeAbsolute())) {
                counter++;
            }
        }
    }
    return counter;
}

From source file:org.eclipse.ajdt.ui.tests.builder.ProjectDependenciesUtils.java

License:Open Source License

public static void removeProjectDependency(IJavaProject projectWhichHasDependency, IProject projectDependedOn) {
    try {//from  w  ww.  j a  va 2 s  .  c  o  m
        IClasspathEntry[] cpEntry = projectWhichHasDependency.getRawClasspath();
        List newEntries = new ArrayList();

        for (int j = 0; j < cpEntry.length; j++) {
            IClasspathEntry entry = cpEntry[j];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                if (!entry.getPath().equals(projectDependedOn.getFullPath())
                        && !entry.getPath().equals(projectDependedOn.getFullPath().makeAbsolute())) {
                    newEntries.add(entry);
                }
            } else {
                newEntries.add(entry);
            }
        }
        IClasspathEntry[] newCP = (IClasspathEntry[]) newEntries
                .toArray(new IClasspathEntry[newEntries.size()]);
        projectWhichHasDependency.setRawClasspath(newCP, null);
        waitForJobsToComplete();
        waitForJobsToComplete();
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:org.eclipse.andmore.android.command.ConvertADTProject.java

License:Open Source License

@SuppressWarnings("restriction")
private void updateClasspathEntries(IJavaProject androidProject) throws JavaModelException {
    ArrayList<IClasspathEntry> newclasspathEntries = new ArrayList<IClasspathEntry>();

    IClasspathEntry[] classpathEntries = androidProject.getRawClasspath();
    for (IClasspathEntry classpathEntry : classpathEntries) {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            String classpathId = classpathEntry.getPath().toString();
            if (classpathId.equals(AndmoreAndroidConstants.ADT_CONTAINER_DEPENDENCIES)) {
                newclasspathEntries/*w  w w. ja  va2  s  . c  o  m*/
                        .add(createNewAndmoreContainer(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES));
            } else if (classpathId.equals(AndmoreAndroidConstants.ADT_CONTAINER_FRAMEWORK)) {
                newclasspathEntries.add(createNewAndmoreContainer(AndmoreAndroidConstants.CONTAINER_FRAMEWORK));
            } else if (classpathId.equals(AndmoreAndroidConstants.ADT_CONTAINER_PRIVATE_LIBRARIES)) {
                newclasspathEntries
                        .add(createNewAndmoreContainer(AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES));
            } else {
                newclasspathEntries.add(classpathEntry);
            }

        } else {
            newclasspathEntries.add(classpathEntry);
        }
    }

    IClasspathEntry[] andmoreClasspathEntries = new IClasspathEntry[newclasspathEntries.size()];
    newclasspathEntries.toArray(andmoreClasspathEntries);
    androidProject.setRawClasspath(andmoreClasspathEntries, true, new NullProgressMonitor());

}

From source file:org.eclipse.andmore.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   www  .j  a v a2  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(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES))) {
                handleCPE(e, javaProject, wsRoot, resMarker);
            }
        }
    }
}

From source file:org.eclipse.andmore.internal.build.BuildHelper.java

License:Open Source License

private void handleCPE(IClasspathEntry entry, IJavaProject javaProject, IWorkspaceRoot wsRoot,
        ResourceMarker resMarker) {//from  www . j a v a  2  s .  co 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(AndmoreAndroidConstants.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.
            AndmoreAndroidPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", entry.getPath());
        }
    }
}

From source file:org.eclipse.andmore.internal.lint.EclipseLintClient.java

License:Open Source License

@Override
@NonNull//from ww w  .  j  a  va2s. 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) {
                AndmoreAndroidPlugin.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();
        }

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

    return info;
}

From source file:org.eclipse.andmore.internal.project.AndroidClasspathContainerInitializer.java

License:Open Source License

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

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

        // check if the project has a valid target.
        IAndroidTarget target = null;/*from w w  w.  j  av  a 2  s. c  om*/
        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:org.eclipse.andmore.internal.project.LibraryClasspathContainerInitializer.java

License:Open Source License

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

    if (AndmoreAndroidPlugin.getDefault() == null) { // This is totally weird, but I've seen it happen!
        return null;
    }//from  w  ww  .  jav 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:org.eclipse.andmore.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  ww . j av a2 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(AndmoreAndroidConstants.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.
            AndmoreAndroidPlugin.log(jme, "Failed to resolve ClasspathContainer: %s", entry.getPath());
        }
    }
}