Example usage for org.eclipse.jdt.core IClasspathEntry CPE_LIBRARY

List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_LIBRARY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry CPE_LIBRARY.

Prototype

int CPE_LIBRARY

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_LIBRARY.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a library.

Usage

From source file:org.eclipse.pde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

/**
 * @return updated classpath or null if there were no changes
 *//*from  ww  w  .  j  ava  2 s .c om*/
private IClasspathEntry[] getUpdatedClasspath(IClasspathEntry[] cp, IJavaProject currentProject) {
    boolean exposed = false;
    int refIndex = -1;
    List<IClasspathEntry> result = new ArrayList<IClasspathEntry>();
    Set<Manifest> manifests = new HashSet<Manifest>();
    for (int i = 0; i < fData.getLibraryPaths().length; ++i) {
        try {
            manifests.add(new JarFile(fData.getLibraryPaths()[i]).getManifest());
        } catch (IOException e) {
            PDEPlugin.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();
            }
            JarFile jarFile = null;
            try {
                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) {
                PDEPlugin.log(e);
            } finally {
                if (jarFile != null) {
                    try {
                        jarFile.close();
                    } catch (IOException e) {
                        PDEPlugin.log(e);
                    }
                }
            }
            break;
        default:
            result.add(cpe);
            break;
        }
    }
    if (refIndex >= 0) {
        result.set(refIndex, JavaCore.newProjectEntry(currentProject.getPath(), exposed));
        return result.toArray(new IClasspathEntry[result.size()]);
    }
    return null;
}

From source file:org.eclipse.pde.internal.ui.wizards.tools.ConvertJarsAction.java

License:Open Source License

/**
 * @see IActionDelegate#selectionChanged(IAction, ISelection)
 */// w  w w . j a v  a2  s.  com
public void selectionChanged(IAction action, ISelection s) {
    boolean enabled = true;
    if (s instanceof IStructuredSelection) {
        selection = (IStructuredSelection) s;
        if (selection.size() == 0)
            return;
        Iterator<?> i = selection.iterator();
        while (i.hasNext()) {
            Object obj = i.next();
            if (obj instanceof IPackageFragmentRoot) {
                try {
                    IPackageFragmentRoot packageFragment = (IPackageFragmentRoot) obj;
                    if (packageFragment.getKind() == IPackageFragmentRoot.K_BINARY) {
                        if (PDE.hasPluginNature(packageFragment.getJavaProject().getProject())) {
                            if (packageFragment.getRawClasspathEntry()
                                    .getEntryKind() == IClasspathEntry.CPE_LIBRARY)
                                continue;
                        }
                    }
                } catch (JavaModelException e) {
                }
            }
            enabled = false;
            break;
        }
    } else {
        enabled = false;
        this.selection = null;
    }
    action.setEnabled(enabled);
}

From source file:org.eclipse.pde.internal.ui.wizards.tools.ConvertProjectToPluginOperation.java

License:Open Source License

private void loadClasspathEntries(IProject project, IProgressMonitor monitor) {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] currentClassPath = new IClasspathEntry[0];
    ArrayList<String> sources = new ArrayList<String>();
    ArrayList<String> libraries = new ArrayList<String>();
    try {//from w  w w . j  ava 2s. c  o m
        currentClassPath = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
    }
    for (int i = 0; i < currentClassPath.length; i++) {
        int contentType = currentClassPath[i].getEntryKind();
        if (contentType == IClasspathEntry.CPE_SOURCE) {
            String relativePath = getRelativePath(currentClassPath[i], project);
            if (relativePath.equals("")) { //$NON-NLS-1$
                sources.add("."); //$NON-NLS-1$
            } else {
                sources.add(relativePath + "/"); //$NON-NLS-1$
            }
        } else if (contentType == IClasspathEntry.CPE_LIBRARY) {
            String path = getRelativePath(currentClassPath[i], project);
            if (path.length() > 0)
                libraries.add(path);
            else
                libraries.add("."); //$NON-NLS-1$
        }
    }
    fSrcEntries = sources.toArray(new String[sources.size()]);
    fLibEntries = libraries.toArray(new String[libraries.size()]);

    IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1];
    System.arraycopy(currentClassPath, 0, classPath, 0, currentClassPath.length);
    classPath[classPath.length - 1] = ClasspathComputer.createContainerEntry();
    try {
        javaProject.setRawClasspath(classPath, monitor);
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.pde.nls.internal.ui.model.ResourceBundleModel.java

License:Open Source License

/**
 * @param monitor//  ww w.  j  a v a2s  .  c o m
 * @throws CoreException
 */
private void populateFromWorkspace(IProgressMonitor monitor) throws CoreException {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject[] projects = root.getProjects();
    for (IProject project : projects) {
        try {
            if (!project.isOpen())
                continue;

            IJavaProject javaProject = (IJavaProject) project.getNature(JAVA_NATURE);
            String pluginId = null;

            try {
                Class IFragmentModel = Class.forName("org.eclipse.pde.core.plugin.IFragmentModel");
                Class IPluginModelBase = Class.forName("org.eclipse.pde.core.plugin.IPluginModelBase");
                Class PluginRegistry = Class.forName("org.eclipse.pde.core.plugin.PluginRegistry");
                Class IPluginBase = Class.forName("org.eclipse.pde.core.plugin.IPluginBase");
                Class PluginFragmentModel = Class.forName("org.eclipse.core.runtime.model.PluginFragmentModel");

                // Plugin and fragment projects
                Class pluginModel = (Class) PluginRegistry.getMethod("findModel", IProject.class).invoke(null,
                        project);
                if (pluginModel != null) {
                    // Get plugin id
                    BundleDescription bd = (BundleDescription) IPluginModelBase
                            .getMethod("getBundleDescription").invoke(pluginModel);
                    pluginId = bd.getName();
                    // OSGi bundle name
                    if (pluginId == null) {
                        Object pluginBase = IPluginModelBase.getMethod("getPluginBase").invoke(pluginModel);
                        pluginId = (String) IPluginBase.getMethod("getId").invoke(pluginBase); // non-OSGi
                        // plug-in id
                    }

                    boolean isFragment = IFragmentModel.isInstance(pluginModel);
                    if (isFragment) {
                        Object pfm = IFragmentModel.getMethod("getFragment");
                        pluginId = (String) PluginFragmentModel.getMethod("getPluginId").invoke(pfm);
                    }

                    // Look for additional 'nl' resources
                    IFolder nl = project.getFolder("nl"); //$NON-NLS-1$
                    if (isFragment && nl.exists()) {
                        IResource[] members = nl.members();
                        for (IResource member : members) {
                            if (member instanceof IFolder) {
                                IFolder langFolder = (IFolder) member;
                                String language = langFolder.getName();

                                // Collect property files
                                IFile[] propertyFiles = collectPropertyFiles(langFolder);
                                for (IFile file : propertyFiles) {
                                    // Compute path name
                                    IPath path = file.getProjectRelativePath();
                                    String country = ""; //$NON-NLS-1$
                                    String packageName = null;
                                    int segmentCount = path.segmentCount();
                                    if (segmentCount > 1) {
                                        StringBuilder builder = new StringBuilder();

                                        // Segment 0: 'nl'
                                        // Segment 1: language code
                                        // Segment 2: (country code)
                                        int begin = 2;
                                        if (segmentCount > 2 && isCountry(path.segment(2))) {
                                            begin = 3;
                                            country = path.segment(2);
                                        }

                                        for (int i = begin; i < segmentCount - 1; i++) {
                                            if (i > begin)
                                                builder.append('.');
                                            builder.append(path.segment(i));
                                        }
                                        packageName = builder.toString();
                                    }

                                    String baseName = getBaseName(file.getName());

                                    ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                            packageName, baseName);
                                    addBundle(family, getLocale(language, country), file);
                                }
                            }
                        }
                    }

                    // Collect property files
                    if (isFragment || javaProject == null) {
                        IFile[] propertyFiles = collectPropertyFiles(project);
                        for (IFile file : propertyFiles) {
                            IPath path = file.getProjectRelativePath();
                            int segmentCount = path.segmentCount();

                            if (segmentCount > 0 && path.segment(0).equals("nl")) //$NON-NLS-1$
                                continue; // 'nl' resource have been
                            // processed
                            // above

                            // Guess package name
                            String packageName = null;
                            if (segmentCount > 1) {
                                StringBuilder builder = new StringBuilder();
                                for (int i = 0; i < segmentCount - 1; i++) {
                                    if (i > 0)
                                        builder.append('.');
                                    builder.append(path.segment(i));
                                }
                                packageName = builder.toString();
                            }

                            String baseName = getBaseName(file.getName());
                            String language = getLanguage(file.getName());
                            String country = getCountry(file.getName());

                            ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                    packageName, baseName);
                            addBundle(family, getLocale(language, country), file);
                        }
                    }

                }
            } catch (Throwable e) {
                // MessagesEditorPlugin.log(e);
            }

            // Look for resource bundles in Java packages (output folders,
            // e.g. 'bin', will be ignored)
            if (javaProject != null) {
                IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
                for (IClasspathEntry entry : classpathEntries) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        IPath path = entry.getPath();
                        IFolder folder = workspace.getRoot().getFolder(path);
                        IFile[] propertyFiles = collectPropertyFiles(folder);

                        for (IFile file : propertyFiles) {
                            String name = file.getName();
                            String baseName = getBaseName(name);
                            String language = getLanguage(name);
                            String country = getCountry(name);
                            IPackageFragment pf = javaProject
                                    .findPackageFragment(file.getParent().getFullPath());
                            String packageName = pf.getElementName();

                            ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                    packageName, baseName);

                            addBundle(family, getLocale(language, country), file);
                        }
                    } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPackageFragmentRoot[] findPackageFragmentRoots = javaProject
                                .findPackageFragmentRoots(entry);
                        for (IPackageFragmentRoot packageFragmentRoot : findPackageFragmentRoots) {
                            IJavaElement[] children = packageFragmentRoot.getChildren();
                            for (IJavaElement child : children) {
                                IPackageFragment pf = (IPackageFragment) child;
                                Object[] nonJavaResources = pf.getNonJavaResources();

                                for (Object resource : nonJavaResources) {
                                    if (resource instanceof IJarEntryResource) {
                                        IJarEntryResource jarEntryResource = (IJarEntryResource) resource;
                                        String name = jarEntryResource.getName();
                                        if (name.endsWith(PROPERTIES_SUFFIX)) {
                                            String baseName = getBaseName(name);
                                            String language = getLanguage(name);
                                            String country = getCountry(name);
                                            String packageName = pf.getElementName();

                                            ResourceBundleFamily family = getOrCreateFamily(project.getName(),
                                                    pluginId, packageName, baseName);

                                            addBundle(family, getLocale(language, country), jarEntryResource);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Collect non-Java resources
                Object[] nonJavaResources = javaProject.getNonJavaResources();
                ArrayList<IFile> files = new ArrayList<IFile>();
                for (Object resource : nonJavaResources) {
                    if (resource instanceof IContainer) {
                        IContainer container = (IContainer) resource;
                        collectPropertyFiles(container, files);
                    } else if (resource instanceof IFile) {
                        IFile file = (IFile) resource;
                        String name = file.getName();
                        if (isIgnoredFilename(name))
                            continue;
                        if (name.endsWith(PROPERTIES_SUFFIX)) {
                            files.add(file);
                        }
                    }
                }
                for (IFile file : files) {

                    // Convert path to package name format
                    IPath path = file.getProjectRelativePath();
                    String packageName = null;
                    int segmentCount = path.segmentCount();
                    if (segmentCount > 1) {
                        StringBuilder builder = new StringBuilder();
                        for (int i = 0; i < segmentCount - 1; i++) {
                            if (i > 0)
                                builder.append('.');
                            builder.append(path.segment(i));
                        }
                        packageName = builder.toString();
                    }

                    String baseName = getBaseName(file.getName());
                    String language = getLanguage(file.getName());
                    String country = getCountry(file.getName());

                    ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId, packageName,
                            baseName);
                    addBundle(family, getLocale(language, country), file);
                }

            }
        } catch (Exception e) {
            MessagesEditorPlugin.log(e);
        }
    }
}

From source file:org.eclipse.pde.ui.tests.imports.BaseImportTestCase.java

License:Open Source License

protected boolean checkSourceAttached(IJavaProject jProject) throws CoreException {
    IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots();
    for (int i = 0; i < roots.length; i++) {
        IClasspathEntry entry = roots[i].getRawClasspathEntry();
        if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY
                || entry.getEntryKind() != IClasspathEntry.CPE_CONTAINER
                || !entry.getPath().equals(PDECore.REQUIRED_PLUGINS_CONTAINER_PATH))
            continue;
        if (roots[i].getSourceAttachmentPath() == null)
            return false;
    }/*from   w w  w .jav a  2s .co  m*/
    return true;
}

From source file:org.eclipse.pde.ui.tests.imports.ImportAsBinaryTestCase.java

License:Open Source License

private boolean checkLibraryEntry(IJavaProject jProject) throws JavaModelException {
    // When self hosting the tests, import tests may fail if you have the imported project in the host
    if (Platform.inDevelopmentMode()) {
        return true;
    }/* w  w w.ja  va  2  s .  c  om*/

    IClasspathEntry[] entries = jProject.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY)
            return true;
    }
    return false;
}

From source file:org.eclipse.virgo.ide.ui.editors.BundleLibrarySection.java

License:Open Source License

private void updateJavaClasspathLibs(String[] oldPaths, String[] newPaths) {
    IProject project = ((IModel) getPage().getModel()).getUnderlyingResource().getProject();
    IJavaProject jproject = JavaCore.create(project);
    try {// ww  w .  j ava2s  .  c om
        IClasspathEntry[] entries = jproject.getRawClasspath();
        ArrayList toBeAdded = new ArrayList();
        int index = -1;
        entryLoop: for (int i = 0; i < entries.length; i++) {
            if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                if (index == -1) {
                    index = i;
                }
                // do not add the old paths (handling deletion/renaming)
                IPath path = entries[i].getPath().removeFirstSegments(1).removeTrailingSeparator();
                for (int j = 0; j < oldPaths.length; j++) {
                    if (oldPaths[j] != null && path.equals(new Path(oldPaths[j]).removeTrailingSeparator())) {
                        continue entryLoop;
                    }
                }
            } else if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (index == -1) {
                    index = i;
                }
            }
            toBeAdded.add(entries[i]);
        }
        if (index == -1) {
            index = entries.length;
        }

        // add paths
        for (String element : newPaths) {
            if (element == null) {
                continue;
            }
            IClasspathEntry entry = JavaCore.newLibraryEntry(project.getFullPath().append(element), null, null,
                    true);
            if (!toBeAdded.contains(entry)) {
                toBeAdded.add(index++, entry);
            }
        }

        if (toBeAdded.size() == entries.length) {
            return;
        }

        IClasspathEntry[] updated = (IClasspathEntry[]) toBeAdded
                .toArray(new IClasspathEntry[toBeAdded.size()]);
        jproject.setRawClasspath(updated, null);
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.vjet.eclipse.javalaunch.utils.EclipseResourceUtils.java

License:Open Source License

/**
 * Searches through the tree of dependencies and adds to a list of projects.
 * @param javaProject - The Java project to search
 * @param transitiveClosureProjectList - The list to store the projects
 * @throws JavaModelException/*www.j  a  va2 s  . c  om*/
 */
public static void getTransitiveClosureDependencies(IJavaProject javaProject,
        Map<String, IJavaProject> transitiveClosureProjectList, Map<String, IPath> transitiveLibrarySet)
        throws JavaModelException {

    IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(true);

    if (classPathEntries != null) {

        for (IClasspathEntry classPathEntry : classPathEntries) {

            if (classPathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IResource classPathProject = ResourcesPlugin.getWorkspace().getRoot()
                        .findMember(classPathEntry.getPath());
                if (classPathProject != null) {
                    if (transitiveClosureProjectList.containsKey(classPathProject.getName()) == false) {

                        IJavaProject subJavaProject = getJavaProject(classPathProject);
                        transitiveClosureProjectList.put(classPathProject.getName(), subJavaProject);

                        getTransitiveClosureDependencies(subJavaProject, transitiveClosureProjectList,
                                transitiveLibrarySet);
                    }
                }
            } else if (classPathEntry != null && classPathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                if (classPathEntry.getSourceAttachmentPath() != null) {
                    String key = classPathEntry.getSourceAttachmentPath().toString();
                    transitiveLibrarySet.put(key, classPathEntry.getSourceAttachmentPath());
                }
            }
        }
    }

}

From source file:org.eclipse.wb.tests.designer.core.PdeProjectConversionUtils.java

License:Open Source License

private void loadClasspathEntries(IProject project, IProgressMonitor monitor) {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] currentClassPath = new IClasspathEntry[0];
    List<String> sources = Lists.newArrayList();
    List<String> libraries = Lists.newArrayList();
    try {/*from   w w w .j ava  2  s.  c o m*/
        currentClassPath = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
    }
    for (int i = 0; i < currentClassPath.length; i++) {
        int contentType = currentClassPath[i].getEntryKind();
        if (contentType == IClasspathEntry.CPE_SOURCE) {
            String relativePath = getRelativePath(currentClassPath[i], project);
            if (relativePath.equals("")) { //$NON-NLS-1$
                sources.add("."); //$NON-NLS-1$
            } else {
                sources.add(relativePath + "/"); //$NON-NLS-1$
            }
        } else if (contentType == IClasspathEntry.CPE_LIBRARY) {
            String path = getRelativePath(currentClassPath[i], project);
            if (path.length() > 0) {
                libraries.add(path);
            } else {
                libraries.add("."); //$NON-NLS-1$
            }
        }
    }
    fSrcEntries = sources.toArray(new String[sources.size()]);
    fLibEntries = libraries.toArray(new String[libraries.size()]);
    //
    IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1];
    System.arraycopy(currentClassPath, 0, classPath, 0, currentClassPath.length);
    try {
        classPath[classPath.length - 1] = createContainerEntry();
        javaProject.setRawClasspath(classPath, monitor);
    } catch (Throwable e) {
    }
}

From source file:org.eclipse.xtend.ide.macro.JdtBasedProcessorProvider.java

License:Open Source License

protected void collectClasspathURLs(final IJavaProject projectToUse, final LinkedHashSet<URL> result,
        final boolean includeOutputFolder, final Set<IJavaProject> visited) throws JavaModelException {
    try {//from   w  w w.  ja  v  a 2 s  .c om
        if (((!projectToUse.getProject().isAccessible()) || (!visited.add(projectToUse)))) {
            return;
        }
        if (includeOutputFolder) {
            IPath path = projectToUse.getOutputLocation().addTrailingSeparator();
            String _string = URI.createPlatformResourceURI(path.toString(), true).toString();
            URL url = new URL(_string);
            result.add(url);
        }
        final IClasspathEntry[] resolvedClasspath = projectToUse.getResolvedClasspath(true);
        for (final IClasspathEntry entry : resolvedClasspath) {
            {
                URL url_1 = null;
                int _entryKind = entry.getEntryKind();
                switch (_entryKind) {
                case IClasspathEntry.CPE_SOURCE:
                    if (includeOutputFolder) {
                        final IPath path_1 = entry.getOutputLocation();
                        if ((path_1 != null)) {
                            String _string_1 = URI
                                    .createPlatformResourceURI(path_1.addTrailingSeparator().toString(), true)
                                    .toString();
                            URL _uRL = new URL(_string_1);
                            url_1 = _uRL;
                        }
                    }
                    break;
                case IClasspathEntry.CPE_PROJECT:
                    IPath path_2 = entry.getPath();
                    final IResource project = this.getWorkspaceRoot(projectToUse).findMember(path_2);
                    final IJavaProject referencedProject = JavaCore.create(project.getProject());
                    this.collectClasspathURLs(referencedProject, result, true, visited);
                    break;
                case IClasspathEntry.CPE_LIBRARY:
                    IPath path_3 = entry.getPath();
                    final IResource library = this.getWorkspaceRoot(projectToUse).findMember(path_3);
                    URL _xifexpression = null;
                    if ((library != null)) {
                        URL _xblockexpression = null;
                        {
                            final java.net.URI locationUri = library.getLocationURI();
                            URL _xifexpression_1 = null;
                            String _scheme = null;
                            if (locationUri != null) {
                                _scheme = locationUri.getScheme();
                            }
                            boolean _equals = Objects.equal(EFS.SCHEME_FILE, _scheme);
                            if (_equals) {
                                java.net.URI _rawLocationURI = library.getRawLocationURI();
                                URL _uRL_1 = null;
                                if (_rawLocationURI != null) {
                                    _uRL_1 = _rawLocationURI.toURL();
                                }
                                _xifexpression_1 = _uRL_1;
                            } else {
                                _xifexpression_1 = null;
                            }
                            _xblockexpression = _xifexpression_1;
                        }
                        _xifexpression = _xblockexpression;
                    } else {
                        _xifexpression = path_3.toFile().toURI().toURL();
                    }
                    url_1 = _xifexpression;
                    break;
                default: {
                    IPath path_4 = entry.getPath();
                    url_1 = path_4.toFile().toURI().toURL();
                }
                    break;
                }
                if ((url_1 != null)) {
                    result.add(url_1);
                }
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}