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.auidt.internal.project.ProjectHelper.java

License:Open Source License

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

    // 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;
    boolean foundLibrariesContainer = 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) {
            String path = entry.getPath().toString();
            if (AdtConstants.CONTAINER_FRAMEWORK.equals(path)) {
                foundFrameworkContainer = true;
            }
            if (AdtConstants.CONTAINER_LIBRARIES.equals(path)) {
                foundLibrariesContainer = true;
            }
        }

        i++;
    }

    // same thing for the library container
    if (foundLibrariesContainer == false) {
        // add the android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_LIBRARIES)));
    }

    // if the framework container is not there, we add it
    if (foundFrameworkContainer == false) {
        // add the android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_FRAMEWORK)));
    }

    // 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.sourcelookup.AdtSourceLookupDirector.java

License:Open Source License

@Override
public void initializeDefaults(ILaunchConfiguration configuration) throws CoreException {
    dispose();/* w  ww  .ja va  2s .  c  o  m*/
    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);
            String path = AdtPlugin.getAuidtJar();

            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.common.project.BaseProjectHelper.java

License:Open Source License

/**
 * returns a list of source classpath for a specified project
 * @param javaProject/* w  w  w  . j a  v  a2 s . c o m*/
 * @return a list of path relative to the workspace root.
 */
public static ArrayList<IPath> getSourceClasspaths(IJavaProject javaProject) {
    ArrayList<IPath> sourceList = new ArrayList<IPath>();
    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.editors.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 w w  w  . jav a2 s.  co m
private final URL[] getExternalJars() {
    // get a java project from it
    IJavaProject javaProject = JavaCore.create(mJavaProject.getProject());

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

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

                // 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;
                        try {
                            oslibraryList.add(new File(resource.getLocation().toOSString()).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.toURL());
                            } catch (MalformedURLException mue) {
                                // pass
                            }
                        }
                    }
                }
            }
        }
    }

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

From source file:com.android.ide.eclipse.mock.Mocks.java

License:Open Source License

/**
 * Creates a mock implementation of an {@link IClasspathEntry}, which supports
 * {@link IClasspathEntry#getEntryKind} and {@link IClasspathEntry#getPath}.
 *///from ww w .j av  a  2 s.  c  o  m
public static IClasspathEntry createClasspathEntry(IPath path, int kind) {
    IClasspathEntry entry = createNiceMock(IClasspathEntry.class);
    expect(entry.getEntryKind()).andReturn(kind).anyTimes();
    expect(entry.getPath()).andReturn(path).anyTimes();
    replay(entry);
    return entry;
}

From source file:com.annotatedsql.classlibrary.ClassPathContainerPage.java

License:Open Source License

public void initialize(IJavaProject project, IClasspathEntry[] currentEntries) {
    for (int i = 0; i < currentEntries.length; i++) {
        IClasspathEntry curr = currentEntries[i];
        if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            fUsedPaths.add(curr.getPath());
        }/*w  ww . j  a  va 2  s  .c  om*/
    }
}

From source file:com.centurylink.mdw.plugin.project.assembly.ProjectConfigurator.java

License:Apache License

public void addJavaProjectSourceFolder(String sourceFolder, String outputFolder, IProgressMonitor monitor)
        throws CoreException, JavaModelException {
    if (!isJavaCapable())
        return;//w  w  w .ja v a 2  s. com

    IFolder folder = project.getSourceProject().getFolder(sourceFolder);
    if (!folder.exists())
        folder.create(true, true, monitor);

    // projects with newly-added Java Nature contain root source entry,
    // which must be removed
    IClasspathEntry rootEntry = JavaCore.newSourceEntry(project.getSourceProject().getFullPath());
    IPath outputPath = outputFolder == null ? null
            : project.getSourceProject().getFolder(outputFolder).getFullPath();
    IClasspathEntry newEntry = JavaCore.newSourceEntry(folder.getFullPath(), ClasspathEntry.EXCLUDE_NONE,
            outputPath);

    boolean includesContainer = false;
    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    IClasspathEntry toRemove = null;
    for (IClasspathEntry existingEntry : project.getSourceJavaProject().getRawClasspath()) {
        if (newEntry.equals(existingEntry))
            return; // already on classpath
        else if (newEntry.getPath().equals(existingEntry.getPath())
                && newEntry.getEntryKind() == existingEntry.getEntryKind())
            toRemove = existingEntry; // to be replaced with new entry

        if (!rootEntry.equals(existingEntry))
            classpathEntries.add(existingEntry);

        if (existingEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                && String.valueOf(existingEntry.getPath()).indexOf("JRE_CONTAINER") >= 0)
            includesContainer = true;
    }
    if (toRemove != null)
        classpathEntries.remove(toRemove);
    classpathEntries.add(newEntry);
    if (!includesContainer) {
        IClasspathEntry jre = getJreContainerClasspathEntry(project.getJavaVersion());
        if (jre == null)
            jre = JavaRuntime.getDefaultJREContainerEntry(); // fallback to
                                                             // any
                                                             // available
        classpathEntries.add(jre);
    }

    project.getSourceJavaProject().setRawClasspath(classpathEntries.toArray(new IClasspathEntry[0]), monitor);
    J2EEComponentClasspathUpdater.getInstance().queueUpdate(project.getSourceProject());
}

From source file:com.cisco.yangide.core.indexing.DeltaProcessor.java

License:Open Source License

private OutputsInfo outputsInfo(IResource res) {
    try {//from   w ww .ja  v a2s  .c om
        JavaProject proj = (JavaProject) JavaCore.create(res.getProject());
        if (proj != null) {
            IPath projectOutput = proj.getOutputLocation();
            int traverseMode = IGNORE;
            if (proj.getProject().getFullPath().equals(projectOutput)) { // case of
                // proj==bin==src
                return new OutputsInfo(new IPath[] { projectOutput }, new int[] { SOURCE }, 1);
            }
            IClasspathEntry[] classpath = proj.getResolvedClasspath();
            IPath[] outputs = new IPath[classpath.length + 1];
            int[] traverseModes = new int[classpath.length + 1];
            int outputCount = 1;
            outputs[0] = projectOutput;
            traverseModes[0] = traverseMode;
            for (int i = 0, length = classpath.length; i < length; i++) {
                IClasspathEntry entry = classpath[i];
                IPath entryPath = entry.getPath();
                IPath output = entry.getOutputLocation();
                if (output != null) {
                    outputs[outputCount] = output;
                    // check case of src==bin
                    if (entryPath.equals(output)) {
                        traverseModes[outputCount++] = (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE)
                                ? SOURCE
                                : BINARY;
                    } else {
                        traverseModes[outputCount++] = IGNORE;
                    }
                }

                // check case of src==bin
                if (entryPath.equals(projectOutput)) {
                    traverseModes[0] = (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) ? SOURCE : BINARY;
                }
            }
            return new OutputsInfo(outputs, traverseModes, outputCount);
        }
    } catch (JavaModelException e) {
        // java project doesn't exist: ignore
    }
    return null;
}

From source file:com.cisco.yangide.core.indexing.IndexAllProject.java

License:Open Source License

@Override
public boolean execute(IProgressMonitor progressMonitor) {
    System.err.println("[I] Project: " + project.getName());

    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled()) {
        return true;
    }//from w ww  .  j a  v a 2 s . co  m

    if (!this.project.isAccessible()) {
        return true;
    }
    final HashSet<IPath> ignoredPath = new HashSet<IPath>();
    final HashSet<IPath> externalJarsPath = new HashSet<IPath>();
    try {
        JavaProject proj = (JavaProject) JavaCore.create(project);
        final HashSet<String> projectScope = new HashSet<>();
        projectScope.add(project.getName());

        if (proj != null) {
            IClasspathEntry[] classpath = proj.getResolvedClasspath();
            for (int i = 0, length = classpath.length; i < length; i++) {
                IClasspathEntry entry = classpath[i];
                IPath entryPath = entry.getPath();
                IPath output = entry.getOutputLocation();
                if (output != null && !entryPath.equals(output)) {
                    ignoredPath.add(output);
                }

                // index dependencies projects
                if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                    IProject prj = ResourcesPlugin.getWorkspace().getRoot()
                            .getProject(entry.getPath().lastSegment());
                    if (prj != null && prj.exists()) {
                        this.manager.indexAll(prj);
                        projectScope.add(prj.getName());
                    }
                }
            }
            IPackageFragmentRoot[] roots = proj.getAllPackageFragmentRoots();
            for (int i = 0, length = roots.length; i < length; i++) {
                IPath entryPath = roots[i].getPath();
                if (entryPath != null && entryPath.toFile().exists()
                        && entryPath.lastSegment().toLowerCase().endsWith(".jar")) {
                    externalJarsPath.add(entryPath);
                }
            }
            // Update project information with set of project dependencies
            YangProjectInfo yangProjectInfo = (YangProjectInfo) YangCorePlugin.create(project)
                    .getElementInfo(null);
            yangProjectInfo.setProjectScope(projectScope);
            // fill indirect scope
            HashSet<String> indirectScope = new HashSet<String>();
            indirectScope.add(project.getName());
            for (IJavaProject jproj : JavaCore.create(ResourcesPlugin.getWorkspace().getRoot())
                    .getJavaProjects()) {
                if (jproj != proj) {
                    for (String name : jproj.getRequiredProjectNames()) {
                        if (name.equals(project.getName())) {
                            indirectScope.add(jproj.getProject().getName());
                        }
                    }
                }
            }
            yangProjectInfo.setIndirectScope(indirectScope);
        }
    } catch (JavaModelException | YangModelException e) {
        // java project doesn't exist: ignore
    }

    for (IPath iPath : externalJarsPath) {
        try (JarFile jarFile = new JarFile(iPath.toFile())) {
            ZipEntry entry = jarFile.getEntry("META-INF/yang/");
            if (entry != null) {
                this.manager.addJarFile(project, iPath);
            }
        } catch (IOException e) {
            YangCorePlugin.log(e);
        }
    }
    try {
        final HashSet<IFile> indexedFiles = new HashSet<IFile>();
        project.accept(new IResourceProxyVisitor() {
            @Override
            public boolean visit(IResourceProxy proxy) {
                if (IndexAllProject.this.isCancelled) {
                    return false;
                }
                if (!ignoredPath.isEmpty() && ignoredPath.contains(proxy.requestFullPath())) {
                    return false;
                }
                if (proxy.getType() == IResource.FILE) {
                    if (CoreUtil.isYangLikeFileName(proxy.getName())) {
                        IFile file = (IFile) proxy.requestResource();
                        indexedFiles.add(file);
                    }
                    return false;
                }
                return true;
            }
        }, IResource.NONE);

        for (IFile iFile : indexedFiles) {
            this.manager.addSource(iFile);
        }
    } catch (CoreException e) {
        this.manager.removeIndexFamily(project);
        return false;
    }
    return true;
}

From source file:com.cisco.yangide.core.model.YangProject.java

License:Open Source License

@Override
protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm,
        Map<IOpenable, OpenableElementInfo> newElements, IResource underlyingResource)
        throws YangModelException {
    final HashSet<IResource> resources = new HashSet<IResource>();
    final HashSet<IPath> externalJarsPath = new HashSet<IPath>();

    IJavaProject javaProject = JavaCore.create(project);
    try {/*from  w w  w  .  ja va 2 s .c  o  m*/
        project.accept(new IResourceVisitor() {
            @Override
            public boolean visit(IResource resource) throws CoreException {
                if (CoreUtil.isYangLikeFileName(resource.getName())) {
                    resources.add(resource.getParent());
                }
                return true;
            }
        });

        if (javaProject.isOpen()) {
            IClasspathEntry[] classpath = javaProject.getResolvedClasspath(true);
            for (int i = 0, length = classpath.length; i < length; i++) {
                IClasspathEntry entry = classpath[i];
                IPath entryPath = entry.getPath();
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    externalJarsPath.add(entryPath);
                }
            }
        }
    } catch (CoreException e) {
        throw new YangModelException(e);
    }
    ArrayList<IOpenable> result = new ArrayList<IOpenable>();
    for (IResource resource : resources) {
        if (resource.getType() == IResource.FOLDER) {
            result.add(new YangFolder(resource, this));
        }
    }

    for (IPath iPath : externalJarsPath) {
        try (JarFile jarFile = new JarFile(iPath.toFile())) {
            ZipEntry entry = jarFile.getEntry("META-INF/yang/");
            if (entry != null) {
                result.add(new YangJarFile(iPath, this));
            }
        } catch (IOException e) {
            YangCorePlugin.log(e);
        }
    }
    info.setChildren(result.toArray(new IOpenable[result.size()]));
    return javaProject.isOpen();
}