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.siteview.mde.internal.ui.util.TemplateFileGenerator.java

License:Open Source License

/**
 * Returns the folder with Java files in the target project. The default
 * implementation looks for source folders in the classpath of the target
 * folders and picks the first one encountered. Subclasses may override this
 * behaviour.// w w  w  .  j  a v  a 2s. com
 * 
 * @param monitor
 *            progress monitor to use
 * @return source folder that will be used to generate Java files or
 *         <samp>null </samp> if none found.
 */

protected IFolder getSourceFolder(IProgressMonitor monitor) throws CoreException {
    IFolder sourceFolder = null;

    try {
        IJavaProject javaProject = JavaCore.create(fProject);
        IClasspathEntry[] classpath = javaProject.getRawClasspath();
        for (int i = 0; i < classpath.length; i++) {
            IClasspathEntry entry = classpath[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = entry.getPath().removeFirstSegments(1);
                if (path.segmentCount() > 0)
                    sourceFolder = fProject.getFolder(path);
                break;
            }
        }
    } catch (JavaModelException e) {
    }
    return sourceFolder;
}

From source file:com.siteview.mde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

private static void updateRequiredPlugins(IJavaProject javaProject, IProgressMonitor monitor,
        IMonitorModelBase model) throws CoreException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List classpath = new ArrayList();
    List requiredProjects = new ArrayList();
    for (int i = 0; i < entries.length; i++) {
        if (isPluginProjectEntry(entries[i])) {
            requiredProjects.add(entries[i]);
        } else {/*w w  w.  j a va  2s  . c o m*/
            classpath.add(entries[i]);
        }
    }
    if (requiredProjects.size() <= 0)
        return;
    IFile file = PDEProject.getManifest(javaProject.getProject());
    try {
        // TODO format manifest
        Manifest manifest = new Manifest(file.getContents());
        String value = manifest.getMainAttributes().getValue(Constants.REQUIRE_BUNDLE);
        StringBuffer sb = value != null ? new StringBuffer(value) : new StringBuffer();
        if (sb.length() > 0)
            sb.append(","); //$NON-NLS-1$
        for (int i = 0; i < requiredProjects.size(); i++) {
            IClasspathEntry entry = (IClasspathEntry) requiredProjects.get(i);
            if (i > 0)
                sb.append(","); //$NON-NLS-1$
            sb.append(entry.getPath().segment(0));
            if (entry.isExported())
                sb.append(";visibility:=reexport"); // TODO is there a //$NON-NLS-1$
            // constant?
        }
        manifest.getMainAttributes().putValue(Constants.REQUIRE_BUNDLE, sb.toString());
        ByteArrayOutputStream content = new ByteArrayOutputStream();
        manifest.write(content);
        file.setContents(new ByteArrayInputStream(content.toByteArray()), true, false, monitor);
        // now update .classpath
        javaProject.setRawClasspath(
                (IClasspathEntry[]) classpath.toArray(new IClasspathEntry[classpath.size()]), monitor);
        //         ClasspathComputer.setClasspath(javaProject.getProject(), model);
    } catch (IOException e) {
    } catch (CoreException e) {
    }
}

From source file:com.siteview.mde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

private static boolean isPluginProjectEntry(IClasspathEntry entry) {
    if (IClasspathEntry.CPE_PROJECT != entry.getEntryKind())
        return false;
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IProject other = workspaceRoot.getProject(entry.getPath().segment(0));
    if (!MDE.hasPluginNature(other))
        return false;
    if (PDEProject.getFragmentXml(other).exists())
        return false;
    try {// w  ww. j av a  2 s  .co m
        InputStream is = PDEProject.getManifest(other).getContents();
        try {
            Manifest mf = new Manifest(is);
            if (mf.getMainAttributes().getValue(Constants.FRAGMENT_HOST) != null)
                return false;
        } finally {
            is.close();
        }
    } catch (IOException e) {
        // assume "not a fragment"
    } catch (CoreException e) {
        // assume "not a fragment"
    }
    return true;
}

From source file:com.siteview.mde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

/**
 * @return updated classpath or null if there were no changes
 *///from   w w w  .  j a v a2  s.  c  o m
private IClasspathEntry[] getUpdatedClasspath(IClasspathEntry[] cp, IJavaProject currentProject) {
    boolean exposed = false;
    int refIndex = -1;
    List result = new ArrayList();
    Set manifests = new HashSet();
    for (int i = 0; i < fData.getLibraryPaths().length; ++i) {
        try {
            manifests.add(new JarFile(fData.getLibraryPaths()[i]).getManifest());
        } catch (IOException e) {
            MDEPlugin.log(e);
        }
    }
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (int i = 0; i < cp.length; ++i) {
        IClasspathEntry cpe = cp[i];
        switch (cpe.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            String path = null;
            IPath location = root.getFile(cpe.getPath()).getLocation();
            if (location != null) {
                path = location.toString();
            }
            //try maybe path is absolute
            if (path == null) {
                path = cpe.getPath().toString();
            }
            try {
                JarFile jarFile = new JarFile(path);
                if (manifests.contains(jarFile.getManifest())) {
                    if (refIndex < 0) {
                        // allocate slot
                        refIndex = result.size();
                        result.add(null);
                    }
                    exposed |= cpe.isExported();
                } else {
                    result.add(cpe);
                }
            } catch (IOException e) {
                MDEPlugin.log(e);
            }
            break;
        default:
            result.add(cpe);
            break;
        }
    }
    if (refIndex >= 0) {
        result.set(refIndex, JavaCore.newProjectEntry(currentProject.getPath(), exposed));
        return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
    }
    return null;
}

From source file:com.siteview.mde.internal.ui.wizards.plugin.NewProjectCreationOperation.java

License:Open Source License

private void addAllSourcePackages(IProject project, Set list) {
    try {/*from  w w w . j  a va2 s. co  m*/
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] classpath = javaProject.getRawClasspath();
        for (int i = 0; i < classpath.length; i++) {
            IClasspathEntry entry = classpath[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = entry.getPath().removeFirstSegments(1);
                if (path.segmentCount() > 0) {
                    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(project.getFolder(path));
                    IJavaElement[] children = root.getChildren();
                    for (int j = 0; j < children.length; j++) {
                        IPackageFragment frag = (IPackageFragment) children[j];
                        if (frag.getChildren().length > 0 || frag.getNonJavaResources().length > 0)
                            list.add(children[j].getElementName());
                    }
                }
            }
        }
    } catch (JavaModelException e) {
    }
}

From source file:com.siteview.mde.internal.ui.wizards.tools.ConvertJarsAction.java

License:Open Source License

/**
 * @see IActionDelegate#run(IAction)//from w ww . ja v  a 2 s .  c  o  m
 */
public void run(IAction action) {
    Map filesMap = new HashMap();
    Set projectSelection = new HashSet();
    Iterator i = selection.toList().iterator();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    while (i.hasNext()) {
        IPackageFragmentRoot pfr = (IPackageFragmentRoot) i.next();
        try {
            projectSelection.add(pfr.getJavaProject().getProject());
            IClasspathEntry rawClasspathEntry = pfr.getRawClasspathEntry();
            IPath path = rawClasspathEntry.getPath();
            IFile iFile = root.getFile(path);
            if (iFile.exists()) {
                JarFile jFile = new JarFile(iFile.getLocation().toString());
                if (!filesMap.containsKey(jFile.getManifest())) {
                    filesMap.put(jFile.getManifest(), iFile);
                }
            } else {
                String pathStr = path.toString();
                JarFile file = new JarFile(pathStr);
                if (!filesMap.containsKey(file.getManifest())) {
                    filesMap.put(file.getManifest(), new File(file.getName()));
                }
            }
        } catch (Exception e) {
            MDEPlugin.logException(e);
        }
    }
    NewLibraryPluginProjectWizard wizard = new NewLibraryPluginProjectWizard(filesMap.values(),
            projectSelection);
    wizard.init(workbench, selection);
    WizardDialog dialog = new WizardDialog(Display.getDefault().getActiveShell(), wizard);
    dialog.open();
}

From source file:com.siteview.mde.internal.ui.wizards.tools.ConvertProjectToPluginOperation.java

License:Open Source License

private String getRelativePath(IClasspathEntry cpe, IProject project) {
    IPath path = project.getFile(cpe.getPath()).getProjectRelativePath();
    return path.removeFirstSegments(1).toString();
}

From source file:com.siteview.mde.ui.templates.AbstractTemplateSection.java

License:Open Source License

/**
 * Returns the folder with Java files in the target project. The default
 * implementation looks for source folders in the classpath of the target
 * folders and picks the first one encountered. Subclasses may override this
 * behaviour./*  w  w w .  j  av a 2s.co m*/
 * 
 * @param monitor
 *            progress monitor to use
 * @return source folder that will be used to generate Java files or
 *         <samp>null </samp> if none found.
 */

protected IFolder getSourceFolder(IProgressMonitor monitor) {
    IFolder sourceFolder = null;

    try {
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] classpath = javaProject.getRawClasspath();
        for (int i = 0; i < classpath.length; i++) {
            IClasspathEntry entry = classpath[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = entry.getPath().removeFirstSegments(1);
                if (path.segmentCount() > 0)
                    sourceFolder = project.getFolder(path);
                break;
            }
        }
    } catch (JavaModelException e) {
        MDEPlugin.logException(e);
    }
    return sourceFolder;
}

From source file:com.sympedia.genfw.util.ClasspathHelper.java

License:Open Source License

public static void collectClasspathURLs(IJavaProject javaProject, List<URL> urls) {
    try {//from  w w  w  .  j  a  v a  2 s .c  om
        collectClasspathUrlOutput(javaProject.getOutputLocation(), urls);

        IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : resolvedClasspath) {
            try {
                switch (entry.getEntryKind()) {
                case IClasspathEntry.CPE_SOURCE:
                    collectClasspathUrlOutput(entry.getOutputLocation(), urls);
                    break;

                case IClasspathEntry.CPE_LIBRARY:
                    File libFile = new File(entry.getPath().toString());
                    URL url = libFile.toURL();
                    if (!urls.contains(url)) {
                        //              System.out.println("LIB: " + url);
                        urls.add(url);
                    }
                    break;

                case IClasspathEntry.CPE_PROJECT:
                    String projectName = entry.getPath().segment(0);
                    IJavaProject requiredProject = getJavaProject(projectName);
                    collectClasspathURLs(requiredProject, urls);
                    break;

                default:
                    throw new RuntimeException();
                }
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.tasktop.dropwizard.launcher.DropwizardRuntimeClasspathProvider.java

License:Open Source License

private void addMavenClasspathEntries(Set<IRuntimeClasspathEntry> resolved,
        IRuntimeClasspathEntry runtimeClasspathEntry, ILaunchConfiguration configuration, int scope,
        IProgressMonitor monitor) throws CoreException {
    IJavaProject javaProject = JavaRuntime.getJavaProject(configuration);
    MavenJdtPlugin plugin = MavenJdtPlugin.getDefault();
    IClasspathManager buildpathManager = plugin.getBuildpathManager();
    IClasspathEntry[] cp = buildpathManager.getClasspath(javaProject.getProject(), scope, false, monitor);
    for (IClasspathEntry entry : cp) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_PROJECT:
            addProjectEntries(resolved, entry.getPath(), scope, getArtifactClassifier(entry), configuration,
                    monitor);//from   ww w  .  ja va2s . com
            break;
        case IClasspathEntry.CPE_LIBRARY:
            resolved.add(JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath()));
            break;
        default:
            break;
        }
    }
}