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

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

Introduction

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

Prototype

IClasspathAttribute[] getExtraAttributes();

Source Link

Document

Returns the extra classpath attributes for this classpath entry.

Usage

From source file:org.eclipse.m2e.tests.BuildPathManagerTest.java

License:Open Source License

private static int getAttributeCount(IClasspathEntry entry, String name) {
    IClasspathAttribute[] attrs = entry.getExtraAttributes();
    int count = 0;
    for (int i = 0; i < attrs.length; i++) {
        if (name.equals(attrs[i].getName())) {
            count++;//from  w ww .  j  a  v a  2 s . c om
        }
    }
    return count;
}

From source file:org.eclipse.m2e.tests.common.ClasspathHelpers.java

License:Open Source License

public static IClasspathAttribute getClasspathAttribute(IClasspathEntry entry, String attributeName) {
    for (IClasspathAttribute a : entry.getExtraAttributes()) {
        if (attributeName.equals(a.getName())) {
            return a;
        }// w ww  .j a  v  a2  s.  c om
    }
    return null;
}

From source file:org.eclipse.m2e.tests.configurators.JavaProjectConfiguratorTest.java

License:Open Source License

private void assertHasAttribute(IClasspathAttribute expected, IClasspathEntry cpe) {
    IClasspathAttribute[] attrs = cpe.getExtraAttributes();
    assertNotNull(attrs);// w  w  w . j a  va 2  s .  c  o m

    for (IClasspathAttribute attr : attrs) {
        if (expected.equals(attr)) {
            return;
        }
    }

    fail("Expected classpath attribute " + expected.toString() + " for " + cpe.toString());
}

From source file:org.eclipse.m2e.tests.jdt.internal.MavenClasspathContainerSaveHelperTest.java

License:Open Source License

public void testClasspathContainerSace() throws Exception {
    IClasspathEntry[] entries = new IClasspathEntry[2];

    {//  w  ww.j  av a  2 s  . com
        IAccessRule[] accessRules = new IAccessRule[1];
        accessRules[0] = JavaCore.newAccessRule(new Path("aa/**"), IAccessRule.K_ACCESSIBLE);

        IClasspathAttribute[] attributes = new IClasspathAttribute[2];
        attributes[0] = JavaCore.newClasspathAttribute("foo", "11");
        attributes[1] = JavaCore.newClasspathAttribute("moo", "22");

        entries[0] = JavaCore.newProjectEntry(new Path("/foo"), accessRules, true, attributes, false);
    }

    {
        IAccessRule[] accessRules = new IAccessRule[1];
        accessRules[0] = JavaCore.newAccessRule(new Path("bb/**"), IAccessRule.K_DISCOURAGED);

        IClasspathAttribute[] attributes = new IClasspathAttribute[1];
        attributes[0] = JavaCore.newClasspathAttribute("foo", "aa");

        entries[1] = JavaCore.newLibraryEntry(new Path("/foo/moo.jar"), new Path("/foo/moo-sources.jar"),
                new Path("/foo/moo-javadoc.jar"), accessRules, attributes, false);
    }

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    helper.writeContainer(new MavenClasspathContainer(new Path(IClasspathManager.CONTAINER_ID), entries), os);
    os.close();

    IClasspathContainer container = helper.readContainer(new ByteArrayInputStream(os.toByteArray()));

    assertEquals(IClasspathManager.CONTAINER_ID, container.getPath().toString());

    IClasspathEntry[] classpathEntries = container.getClasspathEntries();
    assertEquals(2, classpathEntries.length);

    {
        IClasspathEntry entry = classpathEntries[0];
        assertEquals(IClasspathEntry.CPE_PROJECT, entry.getEntryKind());
        assertEquals("/foo", entry.getPath().toString());
        assertEquals(false, entry.isExported());
        assertEquals(true, entry.combineAccessRules());

        IAccessRule[] accessRules = entry.getAccessRules();
        assertEquals(1, accessRules.length);
        assertEquals(IAccessRule.K_ACCESSIBLE, accessRules[0].getKind());
        assertEquals("aa/**", accessRules[0].getPattern().toString());

        IClasspathAttribute[] attributes = entry.getExtraAttributes();
        assertEquals(2, attributes.length);
        assertEquals("foo", attributes[0].getName());
        assertEquals("11", attributes[0].getValue());
        assertEquals("moo", attributes[1].getName());
        assertEquals("22", attributes[1].getValue());
    }

    {
        IClasspathEntry entry = classpathEntries[1];
        assertEquals(IClasspathEntry.CPE_LIBRARY, entry.getEntryKind());
        assertEquals("/foo/moo.jar", entry.getPath().toString());
        assertEquals(false, entry.isExported());

        IAccessRule[] accessRules = entry.getAccessRules();
        assertEquals(1, accessRules.length);
        assertEquals(IAccessRule.K_DISCOURAGED, accessRules[0].getKind());
        assertEquals("bb/**", accessRules[0].getPattern().toString());

        IClasspathAttribute[] attributes = entry.getExtraAttributes();
        assertEquals(1, attributes.length);
        assertEquals("foo", attributes[0].getName());
        assertEquals("aa", attributes[0].getValue());
    }
}

From source file:org.eclipse.m2e.tests.jdt.JavaClasspathTest.java

License:Open Source License

public void test394042_ClasspathEntry4() throws Exception {
    IProject project = importProject("projects/394042_ClasspathEntry4/pom.xml");
    assertNoErrors(project);//w w  w  .  j  a  v a  2  s  .co m

    IJavaProject javaProject = JavaCore.create(project);

    IClasspathEntry[] cp = javaProject.getRawClasspath();

    assertEquals(cp.toString(), 5, cp.length);

    assertClasspath(new String[] { //
            "M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar", //
            "org.eclipse.jdt.launching.JRE_CONTAINER/.*", //
            "/394042_ClasspathEntry4/src/main/java", //
            "/394042_ClasspathEntry4/src/test/java", //
            "org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER",//
    }, cp);

    //Check Variable classpath entry attributes/accessrules are preserved
    IClasspathEntry junit = cp[0];

    assertEquals("/foo/bar/sources.jar", junit.getSourceAttachmentPath().toPortableString());

    assertEquals(2, junit.getExtraAttributes().length);
    assertEquals("file:/foo/bar/javadoc/", getClasspathAttribute(junit, "javadoc_location").getValue());
    assertEquals("UTF-8", getClasspathAttribute(junit, "source_encoding").getValue());

    assertEquals(1, junit.getAccessRules().length);
    assertEquals("foo/bar/**", junit.getAccessRules()[0].getPattern().toPortableString());
}

From source file:org.eclipse.m2e.wtp.tests.AbstractWTPTestCase.java

License:Open Source License

private static boolean hasExtraAttribute(IClasspathEntry entry, String expectedAttribute) {
    for (IClasspathAttribute cpa : entry.getExtraAttributes()) {
        if (expectedAttribute.equals(cpa.getName())) {
            return true;
        }// www .ja  va  2  s .c o  m
    }
    return false;
}

From source file:org.eclipse.pde.api.tools.internal.builder.ApiAnalysisBuilder.java

License:Open Source License

/**
 * Returns is the given classpath entry is optional or not
 * //from   w  ww.  jav  a2s . com
 * @param entry
 * @return true if the specified {@link IClasspathEntry} is optional, false
 *         otherwise
 */
boolean isOptional(IClasspathEntry entry) {
    IClasspathAttribute[] attribs = entry.getExtraAttributes();
    for (int i = 0, length = attribs.length; i < length; i++) {
        IClasspathAttribute attribute = attribs[i];
        if (IClasspathAttribute.OPTIONAL.equals(attribute.getName()) && "true".equals(attribute.getValue())) { //$NON-NLS-1$
            return true;
        }
    }
    return false;
}

From source file:org.eclipse.virgo.ide.jdt.internal.ui.decoration.JdtClasspathContainerElementDecorator.java

License:Open Source License

/**
 * Decorates the given <code>element</code>.
 *//*from w  w  w . j  a v a 2  s . com*/
public void decorate(Object element, IDecoration decoration) {
    // package fragments are the first elements below the JAR file
    if (element instanceof IPackageFragment) {
        IPackageFragment packageFragment = (IPackageFragment) element;
        if (shouldDecorateImportedPackageFragment(packageFragment)) {
            // make light gray and lock icon decoration
            decoration.setForegroundColor(ColorMap.GRAY_LIGHT);
            decoration.addOverlay(JdtUiImages.DESC_OVR_LOCKED, IDecoration.TOP_LEFT);
        } else if (shouldDecorateExportedPackageFragment(packageFragment)) {
            decoration.addOverlay(JdtUiImages.DESC_OVR_EXPORTED, IDecoration.TOP_RIGHT);
        }
    }
    // jar package fragments roots; decorate those that come from a test dependency
    else if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) element;
        try {
            if (ServerClasspathContainer.CLASSPATH_CONTAINER_PATH.equals(root.getRawClasspathEntry().getPath())
                    && root.getJavaProject().getProject().isAccessible() && root.getJavaProject().isOpen()) {
                ServerClasspathContainer cpContainer = (ServerClasspathContainer) JavaCore
                        .getClasspathContainer(ServerClasspathContainer.CLASSPATH_CONTAINER_PATH,
                                root.getJavaProject());
                if (cpContainer != null) {
                    for (IClasspathEntry entry : cpContainer.getClasspathEntries()) {
                        if (entry.getPath().equals(root.getPath()) && entry.getExtraAttributes() != null) {
                            for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
                                if (attribute.getName()
                                        .equals(ServerClasspathContainer.TEST_CLASSPATH_ATTRIBUTE_VALUE)) {
                                    decoration.setForegroundColor(ColorMap.GRAY_LIGHT);
                                    decoration.addOverlay(JdtUiImages.DESC_OVR_LOCKED, IDecoration.TOP_LEFT);
                                }
                            }
                            break;
                        }
                    }
                }

            }
        } catch (JavaModelException e) {
        }
    }
    // class files represent a single type file in a JAR
    else if (element instanceof IClassFile) {
        IClassFile classFile = (IClassFile) element;
        if (classFile.getParent() instanceof IPackageFragment) {
            if (shouldDecorateImportedPackageFragment((IPackageFragment) classFile.getParent())) {
                // make light gray
                decoration.setForegroundColor(ColorMap.GRAY_LIGHT);
            }
        }
    }
    // decorate the class path container and add the originating target runtime
    else if (element instanceof ClassPathContainer) {
        ClassPathContainer container = (ClassPathContainer) element;
        if (container.getClasspathEntry().getPath().equals(ServerClasspathContainer.CLASSPATH_CONTAINER_PATH)) {
            try {
                if (container.getJavaProject().getProject().isAccessible()
                        && container.getJavaProject().isOpen()) {
                    ServerClasspathContainer cpContainer = (ServerClasspathContainer) JavaCore
                            .getClasspathContainer(ServerClasspathContainer.CLASSPATH_CONTAINER_PATH,
                                    container.getJavaProject());
                    decoration.addSuffix(cpContainer.getDescriptionSuffix());
                }
            } catch (JavaModelException e) {
            }
        }
    }
}

From source file:org.eclipse.virgo.ide.jdt.internal.ui.properties.TestSourceFolderPreferencePage.java

License:Open Source License

public boolean performOk() {
    if (!modified) {
        return true;
    }/*from w ww.j  ava2 s.  c  o m*/

    try {
        List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
        for (IClasspathEntry entry : JavaCore.create(project).getRawClasspath()) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                Set<IClasspathAttribute> attrs = new HashSet<IClasspathAttribute>();
                for (IClasspathAttribute attr : entry.getExtraAttributes()) {
                    if (!attr.getName().equals(ServerModuleDelegate.TEST_CLASSPATH_ENTRY_ATTRIBUTE)) {
                        attrs.add(attr);
                    }
                }
                attrs.add(getClasspathAttribute(entry));

                entries.add(JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(),
                        entry.getExclusionPatterns(), entry.getOutputLocation(),
                        (IClasspathAttribute[]) attrs.toArray(new IClasspathAttribute[attrs.size()])));
            } else {
                entries.add(entry);
            }
        }

        JavaCore.create(project).setRawClasspath(
                (IClasspathEntry[]) entries.toArray(new IClasspathEntry[entries.size()]),
                new NullProgressMonitor());
    } catch (JavaModelException e) {
    }

    return true;
}

From source file:org.eclipse.virgo.ide.module.core.ServerModuleDelegate.java

License:Open Source License

public static Set<IClasspathEntry> getSourceClasspathEntries(IProject project, boolean onlyTestFolders) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        return Collections.emptySet();
    }//from w w  w . j  a v a2 s.  c o m
    Set<IClasspathEntry> entries = new LinkedHashSet<IClasspathEntry>();
    try {
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                if (onlyTestFolders && !isSourceFolder(entry.getExtraAttributes())
                        || !onlyTestFolders && isSourceFolder(entry.getExtraAttributes())) {
                    entries.add(entry);
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return entries;
}