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.che.plugin.maven.server.core.classpath.ClasspathEntryHelper.java

License:Open Source License

private void setClasspathEntry(IClasspathEntry entry) {
    this.kind = entry.getEntryKind();
    this.path = entry.getPath();
    this.exported = entry.isExported();
    this.outputLocation = entry.getOutputLocation();

    this.accessRules = new ArrayList<>();
    for (IAccessRule rule : entry.getAccessRules()) {
        this.accessRules.add(rule);
    }//w w  w . j  a va  2s  . c  om

    this.attributes = new HashMap<>();
    for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
        attributes.put(attribute.getName(), attribute.getValue());
    }

    this.sourcePath = entry.getSourceAttachmentPath();
    this.sourceRootPath = entry.getSourceAttachmentRootPath();
    setInclusionPatterns(entry.getInclusionPatterns());
    setExclusionPatterns(entry.getExclusionPatterns());
    this.combineAccessRules = entry.combineAccessRules();

    String groupId = attributes.get(ClasspathManager.GROUP_ID_ATTRIBUTE);
    String artifactId = attributes.get(ClasspathManager.ARTIFACT_ID_ATTRIBUTE);
    String version = attributes.get(ClasspathManager.VERSION_ATTRIBUTE);
    String packaging = attributes.get(ClasspathManager.PACKAGING_ATTRIBUTE);
    String classifier = attributes.get(ClasspathManager.CLASSIFIER_ATTRIBUTE);
    if (groupId != null && artifactId != null && version != null) {
        this.artifactKey = new MavenArtifactKey(groupId, artifactId, version, packaging, classifier);
    }
}

From source file:org.eclipse.datatools.connectivity.jdt.internal.DriverClasspathContainerPage.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_CONTAINER) {
            fUsedPaths.add(curr.getPath());
        }/*  w w w.  j  a  va  2  s. c  o m*/
    }
}

From source file:org.eclipse.datatools.enablement.jdt.classpath.internal.DriverClasspathContainerPage.java

License:Open Source License

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

From source file:org.eclipse.dltk.freemarker.core.util.ClassUtils.java

License:Open Source License

/**
 * Add URL form the JavaProject./*from w  w  w.  j av a2 s .  co m*/
 * 
 * @param javaProject
 * @param urls
 * @param root
 * @param rootLocation
 * @param javaProjectsAlreadyDone
 * @throws JavaModelException
 * @throws CoreException
 */
private static void addPath(IJavaProject javaProject, List<URL> urls, IWorkspaceRoot root, IPath rootLocation,
        List<IJavaProject> javaProjectsAlreadyDone) throws JavaModelException, CoreException {
    if (javaProjectsAlreadyDone.contains(javaProject))
        return;

    javaProjectsAlreadyDone.add(javaProject);
    String projectName = javaProject.getElementName();
    IClasspathEntry javacp[] = javaProject.getResolvedClasspath(true);

    // Add bin folder
    IPath outputLocation = javaProject.getOutputLocation();
    addPath(urls, rootLocation.append(outputLocation));

    // Loop for .classpath
    IClasspathEntry entry = null;
    IPath entryPath = null;
    for (int i = 0; i < javacp.length; i++) {
        // load bin folder of referenced projects into classpath
        entry = javacp[i];
        entryPath = entry.getPath();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            // add jars
            if (projectName.equals(entryPath.segment(0))) {
                // Jar belongs to the Java Project, add base dir
                addPath(urls, rootLocation.append(entryPath));
            } else {
                // External Jar (ex : C:/Program Files/xxx.jar)
                addPath(urls, entryPath);
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            // add the source folders of the project
            addPath(urls, rootLocation.append(entryPath));
            break;
        case IClasspathEntry.CPE_PROJECT:
            // add bin folder from referenced project
            IProject referencedProject = root.getProject(entryPath.segment(0));
            if (referencedProject != null && referencedProject.exists()
                    && referencedProject.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject referencedJavaProject = JavaCore.create(referencedProject);
                addPath(referencedJavaProject, urls, root, rootLocation, javaProjectsAlreadyDone);
            }
            break;
        default:
            addPath(urls, entryPath);
            break;
        }
    }
}

From source file:org.eclipse.draw2d.preview.OpenFigureViewerHandler.java

License:Open Source License

private IFigure getFigure(IWorkbenchWindow activeWorkbenchWindow, Object item) {
    IType figureClass = null;/*from w w  w.  ja  v a 2 s  .c  o m*/
    try {
        if (item instanceof ICompilationUnit) {
            figureClass = ((ICompilationUnit) item).getTypes()[0];
        } else if (item instanceof IType) {
            figureClass = (IType) item;
        } else if (item instanceof IClassFile) {
            figureClass = ((IClassFile) item).getType();
        } else if (item instanceof IFile) {
            ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom((IFile) item);
            figureClass = compilationUnit.getTypes()[0];
        }
        boolean isFigure = false;
        for (IType type : figureClass.newSupertypeHierarchy(null).getAllInterfaces()) {
            if (type.getFullyQualifiedName().equals(IFigure.class.getName())) {
                isFigure = true;
            }
        }
        if (!isFigure) {
            MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.notADraw2DFigure_title,
                    Messages.notADraw2DFigure_message);
            return null;
        }
        List<URL> urls = new ArrayList<URL>();
        URL outputURL = toAbsoluteURL(figureClass.getJavaProject().getOutputLocation());
        urls.add(outputURL);
        for (IClasspathEntry entry : figureClass.getJavaProject().getResolvedClasspath(false)) {
            URL toAdd = null;
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                toAdd = entry.getPath().toFile().toURI().toURL();
                break;
            case IClasspathEntry.CPE_PROJECT:
                IProject project = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(entry.getPath().toString());
                IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
                toAdd = toAbsoluteURL(javaProject.getOutputLocation());
                break;
            }
            if (toAdd != null) {
                urls.add(toAdd);
            }
        }
        ClassLoader cl = new URLClassLoader(urls.toArray(new URL[urls.size()]),
                this.getClass().getClassLoader());
        Class<? extends IFigure> figureClazz = (Class<? extends IFigure>) cl
                .loadClass(figureClass.getFullyQualifiedName());
        for (Constructor<?> cons : figureClazz.getConstructors()) {
            cons.setAccessible(true);
        }
        IFigure figureObject = figureClazz.newInstance();
        return figureObject;
    } catch (JavaModelException ex) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.javaError_title,
                Messages.javaError_message);
        return null;
    } catch (MalformedURLException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        return null;
    } catch (CoreException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        Activator.getDefault().getLog().log(e.getStatus());
        return null;
    } catch (ClassNotFoundException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.internalError_title,
                Messages.internalError_message);
        return null;
    } catch (InstantiationException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.couldNotInstantiate_title,
                Messages.couldNotInstantiate_message);
        return null;
    } catch (IllegalAccessException e) {
        MessageDialog.openError(activeWorkbenchWindow.getShell(), Messages.couldNotInstantiate_title,
                Messages.couldNotInstantiate_message);
        return null;
    } catch (Error e) {
        ErrorDialog.openError(activeWorkbenchWindow.getShell(), Messages.errorInFigure_title,
                Messages.errorInFigure_message, new Status(IStatus.ERROR,
                        figureClass.getJavaProject().getElementName(), Messages.errorInFigure_message, e));
        return null;
    }
}

From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java

License:Open Source License

private void appendExternalArchiveLabel(IPackageFragmentRoot root, long flags) {
    IPath path;/* w w  w.  ja va  2s. co m*/
    IClasspathEntry classpathEntry = null;
    try {
        classpathEntry = JavaModelUtil.getClasspathEntry(root);
        IPath rawPath = classpathEntry.getPath();
        if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_CONTAINER && !rawPath.isAbsolute())
            path = rawPath;
        else
            path = root.getPath();
    } catch (JavaModelException e) {
        path = root.getPath();
    }
    if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
        int segements = path.segmentCount();
        if (segements > 0) {
            fBuffer.append(path.segment(segements - 1));
            int offset = fBuffer.length();
            if (segements > 1 || path.getDevice() != null) {
                fBuffer.append(JavaElementLabels.CONCAT_STRING);
                fBuffer.append(path.removeLastSegments(1).toOSString());
            }
            if (classpathEntry != null) {
                IClasspathEntry referencingEntry = classpathEntry.getReferencingEntry();
                if (referencingEntry != null) {
                    fBuffer.append(messages.JavaElementLabels_onClassPathOf(Name.CLASS_PATH.toString(),
                            referencingEntry.getPath().lastSegment()));
                }
            }
            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            }
        } else {
            fBuffer.append(path.toOSString());
        }
    } else {
        fBuffer.append(path.toOSString());
    }
}

From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java

License:Open Source License

private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) {
    try {/*from w w  w . j a  v a2s .  co  m*/
        IClasspathEntry rawEntry = root.getRawClasspathEntry();
        if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            IClasspathEntry entry = JavaModelUtil.getClasspathEntry(root);
            if (entry.getReferencingEntry() != null) {
                return false; // not the variable entry itself, but a
                              // referenced entry
            }
            IPath path = rawEntry.getPath().makeRelative();

            if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
                int segements = path.segmentCount();
                if (segements > 0) {
                    fBuffer.append(path.segment(segements - 1));
                    if (segements > 1) {
                        int offset = fBuffer.length();
                        fBuffer.append(JavaElementLabels.CONCAT_STRING);
                        fBuffer.append(path.removeLastSegments(1).toOSString());
                        if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                            fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
                        }
                    }
                } else {
                    fBuffer.append(path.toString());
                }
            } else {
                fBuffer.append(path.toString());
            }
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            if (root.isExternal())
                fBuffer.append(root.getPath().toOSString());
            else
                fBuffer.append(root.getPath().makeRelative().toString());

            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            }
            return true;
        }
    } catch (JavaModelException e) {
        // problems with class path, ignore (bug 202792)
        return false;
    }
    return false;
}

From source file:org.eclipse.e4.tools.ui.designer.session.JavaHelper.java

License:Open Source License

/**
 * Get output folders.<br>//  ww w  .j  a va2s.  c om
 * 
 * @param project
 * @return a List of IFolders
 */
public static List<IFolder> getOutputFolders(IJavaProject project) throws CoreException {
    List<IFolder> folders = new UniqueEList<IFolder>();
    if (project == null || project.exists() == false) {
        return folders;
    }
    // Default Output Location
    IFolder folder = findFolder(project.getOutputLocation());
    if (folder != null) {
        folders.add(folder);
    }
    // Lookup in source folders
    for (IClasspathEntry entry : project.getResolvedClasspath(true)) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IFolder innerFolder = findFolder(entry.getOutputLocation());
            if (innerFolder != null) {
                folders.add(innerFolder);
            }
        }
    }
    return folders;
}

From source file:org.eclipse.e4.tools.ui.designer.session.JavaHelper.java

License:Open Source License

/**
 * Get source folders.<br>//ww w  . j a va2 s.  c om
 * 
 * @param project
 * @return a List of IFolders
 */
public static List<IFolder> getSourceFolders(IJavaProject project) throws CoreException {
    List<IFolder> folders = new UniqueEList<IFolder>();
    if (project == null || project.exists() == false) {
        return folders;
    }
    // Lookup in source folders
    for (IClasspathEntry entry : project.getResolvedClasspath(true)) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IFolder innerFolder = findFolder(entry.getPath());
            if (innerFolder != null) {
                folders.add(innerFolder);
            }
        }
    }
    return folders;
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ClassLoaderHelper.java

License:Open Source License

private static URL findResourceURL(IJavaProject javaProject, Set<IJavaProject> visited, boolean isFirstProject,
        String name) {//  w w  w .j  av a 2 s.  c o  m
    if (visited.contains(javaProject))
        return null;
    visited.add(javaProject);
    try {
        IPath outPath = javaProject.getProject().getLocation().removeLastSegments(1)
                .append(javaProject.getOutputLocation());
        outPath = outPath.addTrailingSeparator();
        {
            URL url = toURL(outPath.append(name));
            if (url != null) {
                return url;
            }
        }
        for (IPackageFragmentRoot fragment : javaProject.getPackageFragmentRoots()) {
            if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
                URL url = toURL(fragment.getResource().getLocation().append(name));
                if (url != null) {
                    return url;
                }
            }
        }
        // urls.add(out);
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY: {
                // TODO
                IClasspathEntry resolveEntry = JavaCore.getResolvedClasspathEntry(entry);
                File file = resolveEntry.getPath().toFile();
                IPath path = resolveEntry.getPath();
                if (!file.exists()) {
                    String projectName = path.segment(0);
                    IProject project = javaProject.getProject().getWorkspace().getRoot()
                            .getProject(projectName);
                    path = project.getLocation().append(path.removeFirstSegments(1));
                }
                String spec = "jar:file:" + path.toString() + "!/" + name;
                try {
                    URL url2 = new URL(spec);
                    url2.getContent();
                    return url2;
                } catch (Exception e) {
                }
            }
                break;
            case IClasspathEntry.CPE_CONTAINER:

                break;
            case IClasspathEntry.CPE_VARIABLE: {
                {
                    // TODO
                    URL url = toURL(outPath.append(name));
                    if (url != null) {
                        return url;
                    }
                }
            }
                break;
            case IClasspathEntry.CPE_PROJECT: {
                if (isFirstProject || entry.isExported()) {
                    URL url = findResourceURL(getJavaProject(entry), visited, false, name);
                    if (url != null) {
                        return url;
                    }
                }
                break;
            }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return null;
}