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

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

Introduction

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

Prototype

IAccessRule[] getAccessRules();

Source Link

Document

Returns the possibly empty list of access rules for this entry.

Usage

From source file:org.eclipse.jst.common.project.facet.core.internal.ClasspathUtil.java

License:Open Source License

private static IClasspathEntry setOwners(final IClasspathEntry cpe, final String owners) {
    final List<IClasspathAttribute> attrs = new ArrayList<IClasspathAttribute>();

    for (IClasspathAttribute attr : cpe.getExtraAttributes()) {
        if (!attr.getName().equals(OWNER_PROJECT_FACETS_ATTR)) {
            attrs.add(attr);//from ww w. ja  va 2s  .  c  o  m
        }
    }

    if (owners != null) {
        attrs.add(JavaCore.newClasspathAttribute(OWNER_PROJECT_FACETS_ATTR, owners));
    }

    return new ClasspathEntry(cpe.getContentKind(), cpe.getEntryKind(), cpe.getPath(),
            cpe.getInclusionPatterns(), cpe.getExclusionPatterns(), cpe.getSourceAttachmentPath(),
            cpe.getSourceAttachmentRootPath(), cpe.getOutputLocation(), cpe.isExported(), cpe.getAccessRules(),
            cpe.combineAccessRules(), attrs.toArray(new IClasspathAttribute[attrs.size()]));
}

From source file:org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.java

License:Open Source License

public static IClasspathEntry modifyDependencyPath(IClasspathEntry entry, IPath dependencyPath) {
    IClasspathEntry newEntry = null;//from   w  w w .java  2  s .  co  m
    IClasspathAttribute[] newAttributes = modifyDependencyPath(entry.getExtraAttributes(), dependencyPath);

    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_CONTAINER:
        newEntry = JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), newAttributes,
                entry.isExported());
        break;
    case IClasspathEntry.CPE_LIBRARY:
        newEntry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(),
                entry.getSourceAttachmentRootPath(), entry.getAccessRules(), newAttributes, entry.isExported());
        break;
    case IClasspathEntry.CPE_VARIABLE:
        newEntry = JavaCore.newVariableEntry(entry.getPath(), entry.getSourceAttachmentPath(),
                entry.getSourceAttachmentRootPath(), entry.getAccessRules(), newAttributes, entry.isExported());
        break;
    case IClasspathEntry.CPE_PROJECT:
        newEntry = JavaCore.newProjectEntry(entry.getPath(), entry.getAccessRules(), entry.combineAccessRules(),
                newAttributes, entry.isExported());
        break;
    case IClasspathEntry.CPE_SOURCE:
        newEntry = JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(),
                entry.getExclusionPatterns(), entry.getOutputLocation(), newAttributes);
        break;
    }
    return newEntry;
}

From source file:org.eclipse.jst.j2ee.internal.classpathdep.UpdateClasspathAttributesOperation.java

License:Open Source License

/**
 * Updates the specified Java project so that only the specified classpath entries have
 * the WTP component dependency attribute.
 * @param javaProject Target Java project.
 * @param entries Classpath entries that should have the component dependency attribute. Map from IClasspathEntry
 * to the IClasspathAttribute for the WTP classpath component dependency.
 * @param modifyComponentDep True if modifying the dependency attribute, false if modifying the non-dependency attribute.
 * @throws CoreException Thrown if an error is encountered.
 *//*  w  ww  . ja va 2s.c  om*/
private void updateDependencyAttributes(final IJavaProject javaProject, final Map entriesWithAttrib,
        final boolean modifyComponentDep, final boolean isLegacyJ2EE) throws CoreException {
    if (javaProject == null || !javaProject.getProject().isAccessible()) {
        return;
    }

    final List updatedClasspath = new ArrayList();
    final IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    boolean needToUpdateClasspath = false;
    IClasspathAttribute attrib = UpdateClasspathAttributeUtil.createDependencyAttribute();
    if (!modifyComponentDep) {
        attrib = UpdateClasspathAttributeUtil.createNonDependencyAttribute();
    }
    for (int i = 0; i < rawClasspath.length; i++) {
        IClasspathEntry entry = rawClasspath[i];
        final int kind = entry.getEntryKind();
        boolean hasAttribute = ClasspathDependencyUtil
                .checkForComponentDependencyAttribute(entry,
                        modifyComponentDep ? DependencyAttributeType.CLASSPATH_COMPONENT_DEPENDENCY
                                : DependencyAttributeType.CLASSPATH_COMPONENT_NONDEPENDENCY,
                        isLegacyJ2EE) != null;
        boolean shouldHaveAttribute = entriesWithAttrib.containsKey(entry);
        boolean updateAttributes = false;
        IClasspathAttribute[] updatedAttributes = null;
        if (shouldHaveAttribute) {
            if (!hasAttribute) {
                // should have the attribute and currently missing it
                attrib = (IClasspathAttribute) entriesWithAttrib.get(entry);
                updatedAttributes = updateAttributes(entry.getExtraAttributes(), attrib, true);
                needToUpdateClasspath = true;
                updateAttributes = true;
            }
        } else if (hasAttribute) {
            // should not have the attribute and currently has it
            updatedAttributes = updateAttributes(entry.getExtraAttributes(), attrib, false);
            needToUpdateClasspath = true;
            updateAttributes = true;
        }

        if (updateAttributes) {
            switch (kind) {
            case IClasspathEntry.CPE_CONTAINER:
                entry = JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), updatedAttributes,
                        entry.isExported());
                break;
            case IClasspathEntry.CPE_LIBRARY:
                entry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(),
                        entry.getSourceAttachmentRootPath(), entry.getAccessRules(), updatedAttributes,
                        entry.isExported());
                break;
            case IClasspathEntry.CPE_VARIABLE:
                entry = JavaCore.newVariableEntry(entry.getPath(), entry.getSourceAttachmentPath(),
                        entry.getSourceAttachmentRootPath(), entry.getAccessRules(), updatedAttributes,
                        entry.isExported());
                break;
            case IClasspathEntry.CPE_PROJECT: // although project entries are not yet supported, allow the attribute here and let the validator flag as an error
                entry = JavaCore.newProjectEntry(entry.getPath(), entry.getAccessRules(),
                        entry.combineAccessRules(), updatedAttributes, entry.isExported());
                break;
            case IClasspathEntry.CPE_SOURCE: // although source entries are not supported, allow the attribute here and let the validator flag as an error
                entry = JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(),
                        entry.getExclusionPatterns(), entry.getOutputLocation(), updatedAttributes);
                break;
            }
        }

        updatedClasspath.add(entry);
    }
    if (needToUpdateClasspath) {
        final IClasspathEntry[] updatedCPArray = (IClasspathEntry[]) updatedClasspath
                .toArray(new IClasspathEntry[updatedClasspath.size()]);
        javaProject.setRawClasspath(updatedCPArray, null);
    }
}

From source file:org.eclipse.m2e.jdt.internal.BuildPathManager.java

License:Open Source License

void attachSourcesAndJavadoc(IPackageFragmentRoot fragment, File sources, File javadoc,
        IProgressMonitor monitor) {/*from ww  w. ja  va  2 s.  c o m*/
    IJavaProject javaProject = fragment.getJavaProject();

    IPath srcPath = sources != null ? Path.fromOSString(sources.getAbsolutePath()) : null;
    String javaDocUrl = getJavaDocUrl(javadoc);

    try {
        IClasspathEntry[] cp = javaProject.getRawClasspath();
        for (int i = 0; i < cp.length; i++) {
            IClasspathEntry entry = cp[i];
            if (IClasspathEntry.CPE_LIBRARY == entry.getEntryKind()
                    && entry.equals(fragment.getRawClasspathEntry())) {
                List<IClasspathAttribute> attributes = new ArrayList<IClasspathAttribute>(
                        Arrays.asList(entry.getExtraAttributes()));

                if (srcPath == null) {
                    // configure javadocs if available
                    if (javaDocUrl != null) {
                        attributes.add(JavaCore.newClasspathAttribute(
                                IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javaDocUrl));
                    }
                }

                cp[i] = JavaCore.newLibraryEntry(entry.getPath(), srcPath, null, entry.getAccessRules(), //
                        attributes.toArray(new IClasspathAttribute[attributes.size()]), // 
                        entry.isExported());

                break;
            }
        }

        javaProject.setRawClasspath(cp, monitor);
    } catch (CoreException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.eclipse.m2e.jdt.internal.ClasspathEntryDescriptor.java

License:Open Source License

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

    this.accessRules = new ArrayList<IAccessRule>();
    for (IAccessRule rule : entry.getAccessRules()) {
        this.accessRules.add(rule);
    }/*from   ww w  .ja  v a  2s . c om*/

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

    this.sourceAttachmentPath = entry.getSourceAttachmentPath();
    this.sourceAttachmentRootPath = entry.getSourceAttachmentRootPath();
    setInclusionPatterns(entry.getInclusionPatterns());
    setExclusionPatterns(entry.getExclusionPatterns());
    this.combineAccessRules = entry.combineAccessRules();
}

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];

    {//from  ww w.  j  a  v  a2  s. c  o m
        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. c  om

    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.objectteams.otdt.tests.AbstractJavaModelTests.java

License:Open Source License

protected void setUpProjectCompliance(IJavaProject javaProject, String compliance)
        throws JavaModelException, IOException {
    // Look for version to set and return if that's already done
    String version = compliance; // assume that the values of CompilerOptions.VERSION_* are used
    if (version.equals(javaProject.getOption(CompilerOptions.OPTION_Compliance, false))) {
        return;/*from  ww w .j  a v  a 2  s  .  c  o  m*/
    }
    String jclLibString;
    String newJclLibString;
    String newJclSrcString;
    if (compliance.charAt(2) > '4') {
        jclLibString = "JCL_LIB";
        newJclLibString = "JCL15_LIB";
        newJclSrcString = "JCL15_SRC";
    } else {
        jclLibString = "JCL15_LIB";
        newJclLibString = "JCL_LIB";
        newJclSrcString = "JCL_SRC";
    }

    // ensure variables are set
    setUpJCLClasspathVariables(compliance);

    // set options
    Map options = new HashMap();
    options.put(CompilerOptions.OPTION_Compliance, version);
    options.put(CompilerOptions.OPTION_Source, version);
    options.put(CompilerOptions.OPTION_TargetPlatform, version);
    javaProject.setOptions(options);

    // replace JCL_LIB with JCL15_LIB, and JCL_SRC with JCL15_SRC
    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    IPath jclLib = new Path(jclLibString);
    for (int i = 0, length = classpath.length; i < length; i++) {
        IClasspathEntry entry = classpath[i];
        if (entry.getPath().equals(jclLib)) {
            classpath[i] = JavaCore.newVariableEntry(new Path(newJclLibString), new Path(newJclSrcString),
                    entry.getSourceAttachmentRootPath(), entry.getAccessRules(), new IClasspathAttribute[0],
                    entry.isExported());
            break;
        }
    }
    javaProject.setRawClasspath(classpath, null);
}

From source file:org.eclipse.virgo.ide.jdt.internal.core.classpath.ServerClasspathContainer.java

License:Open Source License

private IAccessRule[] mergeAccessRules(List<IClasspathEntry> entries, IPath path, IAccessRule... allowedRules) {
    IClasspathEntry entry = null;//from  w ww  .  j  a  v  a  2s.co  m
    // Check if the path is already in and merge if so
    for (IClasspathEntry existingEntry : entries) {
        if (existingEntry.getPath().equals(path)) {
            Set<IAccessRule> existingRules = new TreeSet<IAccessRule>(new Comparator<IAccessRule>() {

                public int compare(IAccessRule o1, IAccessRule o2) {
                    if (o1.getKind() == o2.getKind()) {
                        return o1.getPattern().toString().compareTo(o2.getPattern().toString());
                    } else if (o1.getKind() == IAccessRule.K_NON_ACCESSIBLE) {
                        return 1;
                    } else if (o2.getKind() == IAccessRule.K_NON_ACCESSIBLE) {
                        return -1;
                    } else if (o1.getKind() == IAccessRule.K_ACCESSIBLE) {
                        return 1;
                    } else if (o2.getKind() == IAccessRule.K_ACCESSIBLE) {
                        return -1;
                    }
                    return 0;
                }
            });

            existingRules.addAll(Arrays.asList(existingEntry.getAccessRules()));
            existingRules.addAll(Arrays.asList(allowedRules));
            allowedRules = existingRules.toArray(new IAccessRule[existingRules.size()]);
            entry = existingEntry;
            break;
        }
    }

    if (entry != null) {
        entries.remove(entry);
    }
    return allowedRules;
}

From source file:org.eclipse.xtext.xbase.ui.validation.XbaseUIValidator.java

License:Open Source License

protected RestrictionKind computeRestriction(IJavaProject project, IType type) {
    try {//w  ww  .  j ava 2  s  . c  o  m
        IPackageFragmentRoot root = (IPackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        if (root == null) {
            return RestrictionKind.VALID;
        }
        IClasspathEntry entry = getResolvedClasspathEntry(project, root);
        if (entry == null) {
            return RestrictionKind.VALID;
        }
        IAccessRule[] rules = entry.getAccessRules();
        String typePath = type.getFullyQualifiedName().replace('.', '/');
        char[] typePathAsArray = typePath.toCharArray();
        for (IAccessRule rule : rules) {
            char[] patternArray = ((ClasspathAccessRule) rule).pattern;
            if (CharOperation.pathMatch(patternArray, typePathAsArray, true, '/')) {
                if (rule.getKind() == IAccessRule.K_DISCOURAGED) {
                    return RestrictionKind.DISCOURAGED;
                } else if (rule.getKind() == IAccessRule.K_NON_ACCESSIBLE) {
                    return RestrictionKind.FORBIDDEN;
                }
                return RestrictionKind.VALID;
            }
        }
    } catch (JavaModelException jme) {
        // ignore
    }
    return RestrictionKind.VALID;
}