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.che.plugin.java.testing.ProjectClasspathProvider.java

License:Open Source License

/**
 * Builds classpath for the java project.
 *
 * @param javaProject java project// w w w . j  a  v a  2s .com
 * @return set of resources which are included to the classpath
 */
public Set<String> getProjectClassPath(IJavaProject javaProject) {
    try {
        IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(false);
        Set<String> result = new HashSet<>();
        for (IClasspathEntry classpathEntry : resolvedClasspath) {
            switch (classpathEntry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                IPath path = classpathEntry.getPath();
                result.add(path.toOSString());
                break;

            case IClasspathEntry.CPE_SOURCE:
                IPath outputLocation = classpathEntry.getOutputLocation();
                if (outputLocation != null) {
                    result.add(workspacePath + outputLocation.toOSString());
                }
                break;

            case IClasspathEntry.CPE_PROJECT:
                IPath projectPath = classpathEntry.getPath();
                JavaModel javaModel = JavaModelManager.getJavaModelManager().getJavaModel();
                IJavaProject project = javaModel.getJavaProject(projectPath.toOSString());
                result.addAll(getProjectClassPath(project));
                break;
            }
        }
        return result;
    } catch (JavaModelException e) {
        LOG.debug(e.getMessage(), e);
    }

    return Collections.emptySet();
}

From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProviderTest.java

License:Open Source License

@Test
public void classpathProviderShouldProvideClasspathPathsWithExternalDependencies() throws Exception {
    IClasspathEntry classpathEntry = mock(IClasspathEntry.class);
    when(classpathEntry.getEntryKind()).thenReturn(IClasspathEntry.CPE_SOURCE);
    IPath path = new Path("/testProject/target/classes");
    when(classpathEntry.getOutputLocation()).thenReturn(path);

    IClasspathEntry jarClasspathEntry = mock(IClasspathEntry.class);
    when(jarClasspathEntry.getEntryKind()).thenReturn(IClasspathEntry.CPE_LIBRARY);
    IPath jarPath = new Path("/absolute/path/to/jar.file");
    when(jarClasspathEntry.getPath()).thenReturn(jarPath);

    IClasspathEntry[] entries = new IClasspathEntry[] { classpathEntry, jarClasspathEntry };
    when(javaProject.getResolvedClasspath(false)).thenReturn(entries);

    Set<String> classPath = classpathProvider.getProjectClassPath(javaProject);

    assertThat(classPath).isNotNull().isNotEmpty().contains(PROJECTS_PATH + "/testProject/target/classes",
            "/absolute/path/to/jar.file");
}

From source file:org.eclipse.che.plugin.java.testing.ProjectClasspathProviderTest.java

License:Open Source License

@Test
public void classpathProviderShouldProvideClasspathPathsWithAnotherProject() throws Exception {
    JavaModel model = mock(JavaModel.class);
    Field javaModel = JavaModelManager.class.getDeclaredField("javaModel");
    javaModel.setAccessible(true);//from   ww w .j a v  a  2s.c o  m
    javaModel.set(JavaModelManager.getJavaModelManager(), model);
    IClasspathEntry entry = mockClasspathEntry(IClasspathEntry.CPE_SOURCE, "/anotherProject/src",
            "/anotherProject/target/classes");

    IJavaProject anotherProject = mock(IJavaProject.class);
    when(anotherProject.getResolvedClasspath(false)).thenReturn(new IClasspathEntry[] { entry });
    when(model.getJavaProject("/anotherProject")).thenReturn(anotherProject);

    IClasspathEntry classpathEntry = mockClasspathEntry(IClasspathEntry.CPE_SOURCE, "",
            "/testProject/target/classes");
    IClasspathEntry jarClasspathEntry = mockClasspathEntry(IClasspathEntry.CPE_LIBRARY,
            "/absolute/path/to/jar.file", null);
    IClasspathEntry projectEntry = mockClasspathEntry(IClasspathEntry.CPE_PROJECT, "/anotherProject", null);

    IClasspathEntry[] entries = new IClasspathEntry[] { classpathEntry, jarClasspathEntry, projectEntry };
    when(javaProject.getResolvedClasspath(false)).thenReturn(entries);

    Set<String> classPath = classpathProvider.getProjectClassPath(javaProject);

    assertThat(classPath).isNotNull().isNotEmpty().contains(PROJECTS_PATH + "/testProject/target/classes",
            "/absolute/path/to/jar.file", PROJECTS_PATH + "/anotherProject/target/classes");
}

From source file:org.eclipse.che.plugin.maven.server.core.classpath.ClasspathEntryHelper.java

License:Open Source License

public IClasspathEntry toClasspathEntry() {
    Map<String, String> attributes = new HashMap<String, String>(this.attributes);

    if (artifactKey != null) {
        attributes.put(ClasspathManager.GROUP_ID_ATTRIBUTE, artifactKey.getGroupId());
        attributes.put(ClasspathManager.ARTIFACT_ID_ATTRIBUTE, artifactKey.getArtifactId());
        attributes.put(ClasspathManager.VERSION_ATTRIBUTE, artifactKey.getVersion());
        attributes.put(ClasspathManager.PACKAGING_ATTRIBUTE, artifactKey.getPackaging());
        if (artifactKey.getClassifier() != null) {
            attributes.put(ClasspathManager.CLASSIFIER_ATTRIBUTE, artifactKey.getClassifier());
        }/* w ww  .  j  a va 2  s .  c o m*/
    }

    IClasspathAttribute[] attributesArray = new IClasspathAttribute[attributes.size()];
    int attributeIndex = 0;
    for (Map.Entry<String, String> attribute : attributes.entrySet()) {
        attributesArray[attributeIndex++] = JavaCore.newClasspathAttribute(attribute.getKey(),
                attribute.getValue());
    }

    IAccessRule[] accessRulesArray = accessRules.toArray(new IAccessRule[accessRules.size()]);
    IClasspathEntry entry;
    switch (kind) {
    case IClasspathEntry.CPE_CONTAINER:
        entry = JavaCore.newContainerEntry(path, //
                accessRulesArray, //
                attributesArray, //
                exported);
        break;
    case IClasspathEntry.CPE_LIBRARY:
        entry = JavaCore.newLibraryEntry(path, //
                sourcePath, //
                sourceRootPath, //
                accessRulesArray, //
                attributesArray, //
                exported);
        break;
    case IClasspathEntry.CPE_SOURCE:
        entry = JavaCore.newSourceEntry(path, //
                getInclusionPatterns(), //
                getExclusionPatterns(), //
                outputLocation, //
                attributesArray);
        break;
    case IClasspathEntry.CPE_PROJECT:
        entry = JavaCore.newProjectEntry(path, //
                accessRulesArray, //
                combineAccessRules, //
                attributesArray, //
                exported);
        break;
    case IClasspathEntry.CPE_VARIABLE:
        entry = JavaCore.newVariableEntry(path, //
                sourcePath, //
                sourceRootPath, //
                accessRulesArray, //
                attributesArray, //
                exported);
        break;
    default:
        throw new IllegalArgumentException("Unsupported IClasspathEntry kind=" + kind); //$NON-NLS-1$
    }
    return entry;
}

From source file:org.eclipse.che.plugin.maven.server.core.classpath.ClasspathHelper.java

License:Open Source License

public ClasspathEntryHelper addLibraryEntry(IPath libPath) {
    ClasspathEntryHelper helper = new ClasspathEntryHelper(libPath, IClasspathEntry.CPE_LIBRARY);
    addEntryHelper(helper);/*from w  w w  . j a v  a  2  s  . com*/
    return helper;
}

From source file:org.eclipse.che.plugin.testing.classpath.maven.server.MavenTestClasspathProviderTest.java

License:Open Source License

private ClasspathEntry externalLib(String fullPath) {
    ClasspathEntry cp = new ClasspathEntry();
    cp.external = true;//from   w  w  w .  j av a  2s  .  c o m
    cp.fullPath = fullPath;
    cp.fileSystemPath = fullPath;
    cp.kind = IClasspathEntry.CPE_LIBRARY;
    cp.resolvedEntries = Collections.emptyList();
    return cp;
}

From source file:org.eclipse.che.plugin.testing.classpath.maven.server.MavenTestClasspathProviderTest.java

License:Open Source License

private ClasspathEntry internalLib(String fullPath, String fileSystemPath) {
    ClasspathEntry cp = new ClasspathEntry();
    cp.external = false;/*from  w  w  w  .j  a  va 2 s  .  c om*/
    cp.fullPath = fullPath;
    cp.fileSystemPath = fileSystemPath;
    cp.kind = IClasspathEntry.CPE_LIBRARY;
    cp.resolvedEntries = Collections.emptyList();
    return cp;
}

From source file:org.eclipse.che.plugin.testing.classpath.maven.server.MavenTestClasspathProviderTest.java

License:Open Source License

private void buildMocks(List<ClasspathEntry> entries) throws JavaModelException {
    when(classpathService.getClasspath(anyString()))
            .thenReturn(entries.stream().map(ClasspathEntry::dto).collect(Collectors.toList()));

    for (ClasspathEntry entry : entries) {
        if (!entry.external && entry.kind == IClasspathEntry.CPE_LIBRARY) {
            IPath resourceLocation = new Path(entry.fileSystemPath);
            IResource result = mock(IResource.class);
            when(result.getLocation()).thenReturn(resourceLocation);

            when(workspaceRoot.findMember(new Path(entry.fullPath))).thenReturn(result);
        }/*from w ww  .j  a  v  a2  s. com*/
    }
}

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

License:Open Source License

/**
 * Add URL form the JavaProject.// w ww . j av  a  2s  . com
 * 
 * @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  a2 s . c  om*/
    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;
    }
}