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.ajdt.core.AspectJCorePreferences.java

License:Open Source License

public static List<IClasspathEntry> resolveDependentProjectClasspath(IClasspathEntry projEntry,
        IProject requiredProj) {//w w w  .  j a va 2 s. com
    // add all output locations and exported classpath entities
    // AspectJ compiler doesn't understand the concept of a java project
    List<IClasspathEntry> actualEntries = new ArrayList<IClasspathEntry>();

    try {
        JavaProject requiredJavaProj = (JavaProject) JavaCore.create(requiredProj);
        // bug 288395 Do not use the default mechanism for resolving classpath here
        // this will look into jar files at the Classpath header in the jar's manifest
        // and include jar files that are potentially missing, but have no effect on
        // the build.
        Object resolvedClasspath = requiredJavaProj.resolveClasspath(requiredJavaProj.getRawClasspath(), true,
                false);
        IClasspathEntry[] requiredEntries = extractRequiredEntries(resolvedClasspath);
        for (int i = 0; i < requiredEntries.length; i++) {
            IClasspathEntry requiredEntry = requiredEntries[i];
            if (requiredEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

                // always add source entries even if not explicitly exported
                // don't add the source folder itself, but instead add the outfolder
                IPath outputLocation = requiredEntry.getOutputLocation();
                if (outputLocation != null) {
                    IAccessRule[] rules = projEntry.getAccessRules();
                    IClasspathAttribute[] attributes = projEntry.getExtraAttributes();

                    // only add the out folder if it already exists
                    if (requiredProj.getFolder(outputLocation.removeFirstSegments(1)).exists()) {
                        IClasspathEntry outFolder = JavaCore.newLibraryEntry(outputLocation,
                                requiredEntry.getPath(), requiredProj.getFullPath(), rules, attributes,
                                projEntry.isExported());
                        actualEntries.add(outFolder);
                    }
                }
            } else if (requiredEntry.isExported()) {
                // must recur through this entry and add entries that it contains
                actualEntries.addAll(resolveClasspath(requiredEntry, requiredProj));

            }
        } // for (int i = 0; i < requiredEntries.length; i++)

        IPath outputLocation = requiredJavaProj.getOutputLocation();
        // Output location may not exist.  Do not put output location of required project
        // on path unless it exists
        boolean exists = false;
        // bug 244330 check to see if the project folder is also the output folder
        if (outputLocation.segmentCount() == 1) {
            exists = true;
        } else {
            if (requiredProj.getWorkspace().getRoot().getFolder(outputLocation).exists()) {
                exists = true;
            }
        }

        if (exists) {
            IClasspathEntry outFolder = JavaCore.newLibraryEntry(outputLocation, null,
                    requiredProj.getFullPath());
            actualEntries.add(outFolder);
        }
    } catch (JavaModelException e) {
    }
    return actualEntries;
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
 * Firstly, add library to the Java build path if it's not there already,
 * then mark the entry as being on the aspect path
 * @param project/*from   ww  w . j  a  v  a 2  s  . c om*/
 * @param path
 */
private static void addAttribute(IProject project, String jarPath, int eKind, IClasspathAttribute attribute) {
    IJavaProject jp = JavaCore.create(project);

    try {
        IClasspathEntry[] cp = jp.getRawClasspath();
        int cpIndex = getIndexInBuildPathEntry(cp, jarPath);
        if (cpIndex >= 0) { // already on classpath
            // add attribute to classpath entry
            // if it doesn't already exist
            IClasspathEntry pathAdd = cp[cpIndex];
            // only add attribute if this element is not already on the path
            if (isAspectPathAttribute(attribute) ? !isOnAspectpath(pathAdd) : !isOnInpath(pathAdd)) {
                IClasspathAttribute[] attributes = pathAdd.getExtraAttributes();
                IClasspathAttribute[] newattrib = new IClasspathAttribute[attributes.length + 1];
                System.arraycopy(attributes, 0, newattrib, 0, attributes.length);
                newattrib[attributes.length] = attribute;
                switch (pathAdd.getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                    pathAdd = JavaCore.newLibraryEntry(pathAdd.getPath(), pathAdd.getSourceAttachmentPath(),
                            pathAdd.getSourceAttachmentRootPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    pathAdd = JavaCore.newVariableEntry(pathAdd.getPath(), pathAdd.getSourceAttachmentPath(),
                            pathAdd.getSourceAttachmentRootPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_CONTAINER:
                    pathAdd = JavaCore.newContainerEntry(pathAdd.getPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_PROJECT:
                    pathAdd = JavaCore.newProjectEntry(pathAdd.getPath(), pathAdd.getAccessRules(), true,
                            newattrib, pathAdd.isExported());
                    break;
                }

                cp[cpIndex] = pathAdd;
                jp.setRawClasspath(cp, null);
            }
        } else {
            addEntryToJavaBuildPath(jp, attribute, jarPath, eKind);
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

public static IClasspathEntry copyContainerEntry(IClasspathEntry containerEntry,
        IClasspathAttribute[] extraAttrs) {
    return JavaCore.newContainerEntry(containerEntry.getPath(), containerEntry.getAccessRules(), extraAttrs,
            containerEntry.isExported());
}

From source file:org.eclipse.andmore.internal.project.LibraryClasspathContainerInitializer.java

License:Open Source License

private static List<IClasspathEntry> convertJarsToClasspathEntries(final IProject iProject,
        Set<File> jarFiles) {
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(jarFiles.size());

    // and process the jar files list, but first sanitize it to remove dups.
    JarListSanitizer sanitizer = new JarListSanitizer(
            iProject.getFolder(SdkConstants.FD_OUTPUT).getLocation().toFile(),
            new AndroidPrintStream(iProject, null /*prefix*/, AndmoreAndroidPlugin.getOutStream()));

    String errorMessage = null;//from   w  w  w.ja  va 2 s  .c o  m

    try {
        List<File> sanitizedList = sanitizer.sanitize(jarFiles);

        for (File jarFile : sanitizedList) {
            if (jarFile instanceof CPEFile) {
                CPEFile cpeFile = (CPEFile) jarFile;
                IClasspathEntry e = cpeFile.getClasspathEntry();

                entries.add(JavaCore.newLibraryEntry(e.getPath(), e.getSourceAttachmentPath(),
                        e.getSourceAttachmentRootPath(), e.getAccessRules(), e.getExtraAttributes(),
                        true /*isExported*/));
            } else {
                String jarPath = jarFile.getAbsolutePath();

                IPath sourceAttachmentPath = null;
                IClasspathAttribute javaDocAttribute = null;

                File jarProperties = new File(jarPath + DOT_PROPERTIES);
                if (jarProperties.isFile()) {
                    Properties p = new Properties();
                    InputStream is = null;
                    try {
                        p.load(is = new FileInputStream(jarProperties));

                        String value = p.getProperty(ATTR_SRC);
                        if (value != null) {
                            File srcPath = getFile(jarFile, value);

                            if (srcPath.exists()) {
                                sourceAttachmentPath = new Path(srcPath.getAbsolutePath());
                            }
                        }

                        value = p.getProperty(ATTR_DOC);
                        if (value != null) {
                            File docPath = getFile(jarFile, value);
                            if (docPath.exists()) {
                                try {
                                    javaDocAttribute = JavaCore.newClasspathAttribute(
                                            IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
                                            docPath.toURI().toURL().toString());
                                } catch (MalformedURLException e) {
                                    AndmoreAndroidPlugin.log(e, "Failed to process 'doc' attribute for %s",
                                            jarProperties.getAbsolutePath());
                                }
                            }
                        }

                    } catch (FileNotFoundException e) {
                        // shouldn't happen since we check upfront
                    } catch (IOException e) {
                        AndmoreAndroidPlugin.log(e, "Failed to read %s", jarProperties.getAbsolutePath());
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException e) {
                                // ignore
                            }
                        }
                    }
                }

                if (javaDocAttribute != null) {
                    entries.add(JavaCore.newLibraryEntry(new Path(jarPath), sourceAttachmentPath,
                            null /*sourceAttachmentRootPath*/, new IAccessRule[0],
                            new IClasspathAttribute[] { javaDocAttribute }, true /*isExported*/));
                } else {
                    entries.add(JavaCore.newLibraryEntry(new Path(jarPath), sourceAttachmentPath,
                            null /*sourceAttachmentRootPath*/, true /*isExported*/));
                }
            }
        }
    } catch (DifferentLibException e) {
        errorMessage = e.getMessage();
        AndmoreAndroidPlugin.printErrorToConsole(iProject, (Object[]) e.getDetails());
    } catch (Sha1Exception e) {
        errorMessage = e.getMessage();
    }

    processError(iProject, errorMessage, AndmoreAndroidConstants.MARKER_DEPENDENCY, true /*outputToConsole*/);

    return entries;
}

From source file:org.eclipse.andmore.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Fix the project classpath entries. The method ensures that:
 * <ul>/*w  ww .  ja  v a2  s.c  om*/
 * <li>The project does not reference any old android.zip/android.jar archive.</li>
 * <li>The project does not use its output folder as a sourc folder.</li>
 * <li>The project does not reference a desktop JRE</li>
 * <li>The project references the AndroidClasspathContainer.
 * </ul>
 * @param javaProject The project to fix.
 * @throws JavaModelException
 */
public static void fixProjectClasspathEntries(IJavaProject javaProject) throws JavaModelException {

    // get the project classpath
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IClasspathEntry[] oldEntries = entries;
    boolean forceRewriteOfCPE = false;

    // check if the JRE is set as library
    int jreIndex = ProjectHelper.findClasspathEntryByPath(entries, JavaRuntime.JRE_CONTAINER,
            IClasspathEntry.CPE_CONTAINER);
    if (jreIndex != -1) {
        // the project has a JRE included, we remove it
        entries = ProjectHelper.removeEntryFromClasspath(entries, jreIndex);
    }

    // get the output folder
    IPath outputFolder = javaProject.getOutputLocation();

    boolean foundFrameworkContainer = false;
    IClasspathEntry foundLibrariesContainer = null;
    IClasspathEntry foundDependenciesContainer = null;

    for (int i = 0; i < entries.length;) {
        // get the entry and kind
        IClasspathEntry entry = entries[i];
        int kind = entry.getEntryKind();

        if (kind == IClasspathEntry.CPE_SOURCE) {
            IPath path = entry.getPath();

            if (path.equals(outputFolder)) {
                entries = ProjectHelper.removeEntryFromClasspath(entries, i);

                // continue, to skip the i++;
                continue;
            }
        } else if (kind == IClasspathEntry.CPE_CONTAINER) {
            String path = entry.getPath().toString();
            if (AndmoreAndroidConstants.CONTAINER_FRAMEWORK.equals(path)) {
                foundFrameworkContainer = true;
            } else if (AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES.equals(path)) {
                foundLibrariesContainer = entry;
            } else if (AndmoreAndroidConstants.CONTAINER_DEPENDENCIES.equals(path)) {
                foundDependenciesContainer = entry;
            }
        }

        i++;
    }

    // look to see if we have the m2eclipse nature
    boolean m2eNature = false;
    try {
        m2eNature = javaProject.getProject().hasNature("org.eclipse.m2e.core.maven2Nature");
    } catch (CoreException e) {
        AndmoreAndroidPlugin.log(e, "Failed to query project %s for m2e nature",
                javaProject.getProject().getName());
    }

    // if the framework container is not there, we add it
    if (!foundFrameworkContainer) {
        // add the android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_FRAMEWORK)));
    }

    // same thing for the library container
    if (foundLibrariesContainer == null) {
        // add the exported libraries android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries, JavaCore
                .newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES), true));
    } else if (!m2eNature && !foundLibrariesContainer.isExported()) {
        // the container is present but it's not exported and since there's no m2e nature
        // we do want it to be exported.
        // keep all the other parameters the same.
        entries = ProjectHelper.replaceEntryInClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES),
                        foundLibrariesContainer.getAccessRules(), foundLibrariesContainer.getExtraAttributes(),
                        true));
        forceRewriteOfCPE = true;
    }

    // same thing for the dependencies container
    if (foundDependenciesContainer == null) {
        // add the android dependencies container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES), true));
    } else if (!m2eNature && !foundDependenciesContainer.isExported()) {
        // the container is present but it's not exported and since there's no m2e nature
        // we do want it to be exported.
        // keep all the other parameters the same.
        entries = ProjectHelper.replaceEntryInClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES),
                        foundDependenciesContainer.getAccessRules(),
                        foundDependenciesContainer.getExtraAttributes(), true));
        forceRewriteOfCPE = true;
    }

    // set the new list of entries to the project
    if (entries != oldEntries || forceRewriteOfCPE) {
        javaProject.setRawClasspath(entries, new NullProgressMonitor());
    }

    // If needed, check and fix compiler compliance and source compatibility
    ProjectHelper.checkAndFixCompilerCompliance(javaProject);
}

From source file:org.eclipse.buildship.core.workspace.internal.WtpClasspathUpdater.java

License:Open Source License

private static void replaceGradleClasspathContainerAttribute(IJavaProject project, String plusKey,
        String plusValue, String minusKey, SubMonitor progress) throws JavaModelException {
    IClasspathEntry[] oldClasspath = project.getRawClasspath();
    IClasspathEntry[] newClasspath = new IClasspathEntry[oldClasspath.length];
    for (int i = 0; i < oldClasspath.length; i++) {
        IClasspathEntry entry = oldClasspath[i];
        if (isGradleClasspathContainer(entry)) {
            IClasspathAttribute[] attributes = replaceClasspathAttribute(entry.getExtraAttributes(), plusKey,
                    plusValue, minusKey);
            newClasspath[i] = JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), attributes,
                    entry.isExported());
        } else {//from  w  ww .j  av a 2 s  . c  o  m
            newClasspath[i] = entry;
        }
    }
    project.setRawClasspath(newClasspath, progress);
}

From source file:org.eclipse.buildship.wtp.core.configurator.WebApplicationConfigurator.java

License:Open Source License

/**
 * TODO: Refactor this and markAsNonDeployable.
 * TODO: Test to ensure duplicate attributes aren't allowed.
 *///from   ww w  .  j a v a  2 s  .  co m
private IClasspathEntry markAsDeployable(IClasspathEntry entry) {
    IClasspathAttribute newAttribute = JavaCore.newClasspathAttribute(
            IClasspathDependencyConstants.CLASSPATH_COMPONENT_DEPENDENCY, "/WEB-INF/lib");

    if (Arrays.asList(entry.getExtraAttributes()).contains(newAttribute)) {
        return entry;
    }

    List<IClasspathAttribute> gradleContainerAttributes = new ArrayList<IClasspathAttribute>(
            Arrays.asList(entry.getExtraAttributes()));
    gradleContainerAttributes.add(newAttribute);
    return JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(),
            gradleContainerAttributes.toArray(new IClasspathAttribute[gradleContainerAttributes.size()]),
            entry.isExported());
}

From source file:org.eclipse.buildship.wtp.core.configurator.WebApplicationConfigurator.java

License:Open Source License

private IClasspathEntry markAsNonDeployable(IClasspathEntry entry) {
    IClasspathAttribute newAttribute = JavaCore.newClasspathAttribute(
            IClasspathDependencyConstants.CLASSPATH_COMPONENT_NON_DEPENDENCY, "/WEB-INF/lib");

    if (Arrays.asList(entry.getExtraAttributes()).contains(newAttribute)) {
        return entry;
    }//from   w  w w .  j av a  2s . c  om

    List<IClasspathAttribute> gradleContainerAttributes = new ArrayList<IClasspathAttribute>(
            Arrays.asList(entry.getExtraAttributes()));
    gradleContainerAttributes.add(newAttribute);
    return JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(),
            gradleContainerAttributes.toArray(new IClasspathAttribute[gradleContainerAttributes.size()]),
            entry.isExported());
}

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);
    }/*  www.jav a2 s  .c o m*/

    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.jdt.internal.core.JavaModelManager.java

License:Open Source License

public IClasspathEntry resolveVariableEntry(IClasspathEntry entry, boolean usePreviousSession) {

    if (entry.getEntryKind() != IClasspathEntry.CPE_VARIABLE)
        return entry;

    IPath resolvedPath = getResolvedVariablePath(entry.getPath(), usePreviousSession);
    if (resolvedPath == null)
        return null;
    // By passing a null reference path, we keep it relative to workspace root.
    resolvedPath = ClasspathEntry.resolveDotDot(null, resolvedPath);

    Object target = JavaModel.getTarget(resolvedPath, false);
    if (target == null)
        return null;

    // inside the workspace
    if (target instanceof IResource) {
        IResource resolvedResource = (IResource) target;
        switch (resolvedResource.getType()) {

        case IResource.PROJECT:
            // internal project
            return JavaCore.newProjectEntry(resolvedPath, entry.getAccessRules(), entry.combineAccessRules(),
                    entry.getExtraAttributes(), entry.isExported());
        case IResource.FILE:
            // internal binary archive
            return JavaCore.newLibraryEntry(resolvedPath,
                    getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession),
                    getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession),
                    entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
        case IResource.FOLDER:
            // internal binary folder
            return JavaCore.newLibraryEntry(resolvedPath,
                    getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession),
                    getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession),
                    entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
        }//from w  ww .j ava  2s.co  m
    }
    if (target instanceof File) {
        File externalFile = JavaModel.getFile(target);
        if (externalFile != null) {
            // external binary archive
            return JavaCore.newLibraryEntry(resolvedPath,
                    getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession),
                    getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession),
                    entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
        } else {
            // non-existing file
            if (resolvedPath.isAbsolute()) {
                return JavaCore.newLibraryEntry(resolvedPath,
                        getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession),
                        getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession),
                        entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
            }
        }
    }
    return null;
}