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.centurylink.mdw.plugin.project.assembly.ProjectConfigurator.java

License:Apache License

public void removeJavaProjectSourceFolder(String sourceFolder, IProgressMonitor monitor)
        throws CoreException, JavaModelException {
    if (!isJavaCapable())
        return;//  w w w.  j a  v  a  2  s.  co  m

    IPath sourcePath = project.getSourceProject().getFolder(sourceFolder).getFullPath();
    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    IClasspathEntry toRemove = null;
    for (IClasspathEntry existingEntry : project.getSourceJavaProject().getRawClasspath()) {
        if (sourcePath.equals(existingEntry.getPath()))
            toRemove = existingEntry;
        else
            classpathEntries.add(existingEntry);
    }
    if (toRemove != null) {
        project.getSourceJavaProject().setRawClasspath(classpathEntries.toArray(new IClasspathEntry[0]),
                monitor);
        J2EEComponentClasspathUpdater.getInstance().queueUpdate(project.getSourceProject());
    }
}

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

License:Apache License

private void addJarsToClasspath(IJavaProject javaProject, IFolder libFolder, IProgressMonitor monitor)
        throws JavaModelException {
    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry existingEntry : javaProject.getRawClasspath())
        classpathEntries.add(existingEntry);

    File libDir = new File(libFolder.getRawLocation().toOSString());
    if (libDir.exists() && libDir.isDirectory()) {
        File[] jarFiles = libDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar") && !name.endsWith("_src.jar");
            }//  w w w .ja v a2s .  c  o m
        });

        for (File jarFile : jarFiles) {
            IPath path = libFolder.getFile(jarFile.getName()).getFullPath();
            IClasspathEntry newEntry = JavaCore.newLibraryEntry(path, null, null);
            boolean already = false;
            for (IClasspathEntry existing : javaProject.getRawClasspath()) {
                if (existing.getPath().equals(newEntry.getPath())) {
                    already = true;
                    break;
                }
            }
            if (!already)
                classpathEntries.add(newEntry);
        }

        javaProject.setRawClasspath(classpathEntries.toArray(new IClasspathEntry[0]), monitor);
        J2EEComponentClasspathUpdater.getInstance().queueUpdateModule(javaProject.getProject());
    }
}

From source file:com.cisco.surf.jenkow.ide.config.UserLibConfigurator.java

License:Open Source License

private boolean hasEntry(List<IClasspathEntry> list, IPath path) {
    for (IClasspathEntry iClasspathEntry : list) {
        if (iClasspathEntry.getPath().equals(path))
            return true;
    }/*  w w  w . j  a v a  2  s.c  om*/
    return false;
}

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

License:Open Source License

private OutputsInfo outputsInfo(IResource res) {
    try {/*from w  w w .  j  a v  a  2  s .  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  w  w  .  j av  a 2 s. c o 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   www . java2  s . co  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();
}

From source file:com.cisco.yangide.ui.wizards.YangProjectWizard.java

License:Open Source License

@Override
public boolean performFinish() {
    boolean res = super.performFinish();
    if (!res) {//  www. j  a v  a  2 s  .c o m
        return false;
    }
    final boolean doCreateDemoFile = yangPage.createExampleFile();
    final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(getModel().getArtifactId());
    final String yangRoot = yangPage.getRootDir();
    final IFolder folder = project.getFolder(yangRoot);

    final String yangVersion = yangPage.getYangVersion();

    final List<CodeGeneratorConfig> generators = yangPage.getCodeGenerators();

    Job updateJob = new Job("Yang Project update") {
        @Override
        public IStatus run(IProgressMonitor monitor) {
            try {
                createFolder(folder);

                IFile pomFile = project.getFile("pom.xml");
                Model model = MavenPlugin.getMavenModelManager().readMavenModel(pomFile);
                updateModel(model, yangVersion, generators, yangRoot);

                pomFile.delete(true, new NullProgressMonitor());
                MavenPlugin.getMavenModelManager().createMavenModel(pomFile, model);
                MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(project,
                        new NullProgressMonitor());

                if (doCreateDemoFile) {
                    InputStream demoFileContents = null;
                    try {
                        Path demoPath = new Path("resources/yang/acme-system.yang");
                        demoFileContents = FileLocator.openStream(YangUIPlugin.getDefault().getBundle(),
                                demoPath, false);

                        folder.getFile("acme-system.yang").create(demoFileContents, true, null);
                    } finally {
                        if (demoFileContents != null) {
                            demoFileContents.close();
                        }
                    }
                }
                // Add yang folder to java classpath
                IJavaProject javaProject = JavaCore.create(project);
                List<IClasspathEntry> classpath = new ArrayList<>(Arrays.asList(javaProject.getRawClasspath()));
                IClasspathEntry yangSrc = JavaCore.newSourceEntry(folder.getFullPath());
                boolean hasSame = false;
                for (IClasspathEntry ee : classpath) {
                    if (ee.getPath().equals(yangSrc.getPath())) {
                        hasSame = true;
                        break;
                    }
                }
                if (!hasSame) {
                    classpath.add(yangSrc);
                    javaProject.setRawClasspath(classpath.toArray(new IClasspathEntry[0]),
                            new NullProgressMonitor());
                }

            } catch (CoreException e) {
                YangUIPlugin.log(e.getMessage(), e);
            } catch (IOException e) {
                YangUIPlugin.log(e.getMessage(), e);
            }
            return Status.OK_STATUS;
        }
    };
    updateJob.setRule(MavenPlugin.getProjectConfigurationManager().getRule());
    updateJob.schedule();
    return true;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.DeltaProcessingState.java

License:Open Source License

private HashMap[] getRootInfos(boolean usePreviousSession) {
    HashMap newRoots = new HashMap();
    HashMap newOtherRoots = new HashMap();
    HashMap newSourceAttachments = new HashMap();
    HashMap newProjectDependencies = new HashMap();

    IJavaModel model = manager.getJavaModel();
    IJavaProject[] projects;/*from   w  ww .j  a  v a  2 s .  c  o  m*/
    try {
        projects = model.getJavaProjects();
    } catch (JavaModelException e) {
        // nothing can be done
        return null;
    }
    for (int i = 0, length = projects.length; i < length; i++) {
        JavaProject project = (JavaProject) projects[i];
        IClasspathEntry[] classpath;
        try {
            //            if (usePreviousSession) {
            //               PerProjectInfo perProjectInfo = project.getPerProjectInfo();
            //               project.resolveClasspath(perProjectInfo, true/*use previous session values*/, false/*don't add classpath change*/);
            //               classpath = perProjectInfo.resolvedClasspath;
            //            } else {
            classpath = project.getResolvedClasspath();
            //            }
        } catch (JavaModelException e) {
            // continue with next project
            continue;
        }
        for (int j = 0, classpathLength = classpath.length; j < classpathLength; j++) {
            IClasspathEntry entry = classpath[j];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IJavaProject key = model.getJavaProject(entry.getPath().segment(0)); // TODO (jerome) reuse handle
                IJavaProject[] dependents = (IJavaProject[]) newProjectDependencies.get(key);
                if (dependents == null) {
                    dependents = new IJavaProject[] { project };
                } else {
                    int dependentsLength = dependents.length;
                    System.arraycopy(dependents, 0, dependents = new IJavaProject[dependentsLength + 1], 0,
                            dependentsLength);
                    dependents[dependentsLength] = project;
                }
                newProjectDependencies.put(key, dependents);
                continue;
            }

            // root path
            IPath path = entry.getPath();
            if (newRoots.get(path) == null) {
                newRoots.put(path,
                        new DeltaProcessor.RootInfo(project, path,
                                ((ClasspathEntry) entry).fullInclusionPatternChars(),
                                ((ClasspathEntry) entry).fullExclusionPatternChars(), entry.getEntryKind()));
            } else {
                ArrayList rootList = (ArrayList) newOtherRoots.get(path);
                if (rootList == null) {
                    rootList = new ArrayList();
                    newOtherRoots.put(path, rootList);
                }
                rootList.add(new DeltaProcessor.RootInfo(project, path,
                        ((ClasspathEntry) entry).fullInclusionPatternChars(),
                        ((ClasspathEntry) entry).fullExclusionPatternChars(), entry.getEntryKind()));
            }

            // source attachment path
            if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY)
                continue;
            String propertyString = null;
            //            try {
            //               propertyString = Util.getSourceAttachmentProperty(path);
            //            } catch (JavaModelException e) {
            //               e.printStackTrace();
            //            }
            IPath sourceAttachmentPath;
            if (propertyString != null) {
                int index = propertyString.lastIndexOf(PackageFragmentRoot.ATTACHMENT_PROPERTY_DELIMITER);
                sourceAttachmentPath = (index < 0) ? new Path(propertyString)
                        : new Path(propertyString.substring(0, index));
            } else {
                sourceAttachmentPath = entry.getSourceAttachmentPath();
            }
            if (sourceAttachmentPath != null) {
                newSourceAttachments.put(sourceAttachmentPath, path);
            }
        }
    }
    return new HashMap[] { newRoots, newOtherRoots, newSourceAttachments, newProjectDependencies };
}

From source file:com.codenvy.ide.ext.java.server.internal.core.DeltaProcessor.java

License:Open Source License

private void addPackageFragmentRoot(OpenableElementInfo parent, IPackageFragmentRoot child)
        throws JavaModelException {

    IJavaElement[] roots = parent.getChildren();
    if (roots.length > 0) {
        IClasspathEntry[] resolvedClasspath = ((JavaProject) child.getJavaProject()).getResolvedClasspath();
        IPath currentEntryPath = child.getResolvedClasspathEntry().getPath();
        int indexToInsert = -1;
        int lastComparedIndex = -1;
        int i = 0, j = 0;
        for (; i < roots.length && j < resolvedClasspath.length;) {

            IClasspathEntry classpathEntry = resolvedClasspath[j];
            if (lastComparedIndex != j && currentEntryPath.equals(classpathEntry.getPath())) {
                indexToInsert = i;//from   w  w w.j  a  v  a 2  s . c o  m
                break;
            }
            lastComparedIndex = j;

            IClasspathEntry rootEntry = ((IPackageFragmentRoot) roots[i]).getResolvedClasspathEntry();
            if (rootEntry.getPath().equals(classpathEntry.getPath()))
                i++;
            else
                j++;
        }

        for (; i < roots.length; i++) {
            // If the new root is already among the children, no need to proceed further. Just return.
            if (roots[i].equals(child)) {
                return;
            }
            // If we start seeing root's classpath entry different from the child's entry, then the child can't
            // be present further down the roots array.
            if (!((IPackageFragmentRoot) roots[i]).getResolvedClasspathEntry().getPath()
                    .equals(currentEntryPath))
                break;
        }

        if (indexToInsert >= 0) {
            int newSize = roots.length + 1;
            IPackageFragmentRoot[] newChildren = new IPackageFragmentRoot[newSize];

            if (indexToInsert > 0)
                System.arraycopy(roots, 0, newChildren, 0, indexToInsert);

            newChildren[indexToInsert] = child;
            System.arraycopy(roots, indexToInsert, newChildren, indexToInsert + 1,
                    (newSize - indexToInsert - 1));
            parent.setChildren(newChildren);
            return;
        }
    }
    parent.addChild(child);
}

From source file:com.codenvy.ide.ext.java.server.internal.core.JavaProjectElementInfo.java

License:Open Source License

private boolean isClasspathEntryOrOutputLocation(IPath path, IPath location,
        IClasspathEntry[] resolvedClasspath, IPath projectOutput) {
    if (projectOutput.equals(path))
        return true;
    for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
        IClasspathEntry entry = resolvedClasspath[i];
        IPath entryPath;/*ww w .j a  v a2 s.c o m*/
        if ((entryPath = entry.getPath()).equals(path) || entryPath.equals(location)) {
            return true;
        }
        IPath output;
        if ((output = entry.getOutputLocation()) != null && output.equals(path)) {
            return true;
        }
    }
    return false;
}