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.andmore.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Fix the project classpath entries. The method ensures that:
 * <ul>/*  w ww  .j a v  a2  s .  co m*/
 * <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.launch.internal.GradleClasspathProvider.java

License:Open Source License

private static boolean isOptional(IClasspathEntry entry) {
    for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
        if (IClasspathAttribute.OPTIONAL.equals(attribute.getName())
                && Boolean.parseBoolean(attribute.getValue())) {
            return true;
        }/*from w ww .ja va 2  s  .c o m*/
    }
    return false;
}

From source file:org.eclipse.buildship.core.launch.internal.LaunchConfigurationScope.java

License:Open Source License

private static Set<String> scopesFor(IClasspathEntry entry) {
    for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
        if (attribute.getName().equals("gradle_scope")) {
            return Sets.newHashSet(attribute.getValue().split(","));
        }/*from   www  . ja  v  a 2  s  .  c om*/
    }
    return null;
}

From source file:org.eclipse.buildship.core.launch.internal.LaunchConfigurationScope.java

License:Open Source License

private static Set<String> usedByScopesFor(IClasspathEntry entry) {
    for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
        if (attribute.getName().equals("gradle_used_by_scope")) {
            return Sets.newHashSet(attribute.getValue().split(","));
        }/*from  w w  w . java  2  s  . c om*/
    }
    return null;
}

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

License:Open Source License

private List<IClasspathEntry> calculateNewClasspath(List<IClasspathEntry> gradleSourceFolders)
        throws JavaModelException {
    // collect the paths of all source folders of the new Gradle model
    final Set<IPath> gradleModelSourcePaths = FluentIterable.from(gradleSourceFolders)
            .transform(new Function<IClasspathEntry, IPath>() {

                @Override//from ww w  .j  a  v  a2  s  .c om
                public IPath apply(IClasspathEntry entry) {
                    return entry.getPath();
                }
            }).toSet();

    // collect all source folders currently configured on the project
    List<IClasspathEntry> rawClasspath = ImmutableList.copyOf(this.project.getRawClasspath());

    // filter out the source folders that are part of the new or previous Gradle model (keeping
    // only the manually added source folders)
    final List<IClasspathEntry> manuallyAddedSourceFolders = FluentIterable.from(rawClasspath)
            .filter(new Predicate<IClasspathEntry>() {

                @Override
                public boolean apply(IClasspathEntry entry) {
                    // if a source folder is part of the new Gradle model, always treat it as a Gradle
                    // source folder
                    if (gradleModelSourcePaths.contains(entry.getPath())) {
                        return false;
                    }

                    // if a source folder is marked as coming from a previous Gradle model, treat it as
                    // a Gradle source folder
                    for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
                        if (attribute.getName().equals(CLASSPATH_ATTRIBUTE_FROM_GRADLE_MODEL)
                                && attribute.getValue().equals("true")) {
                            return false;
                        }
                    }

                    // treat it as a manually added source folder
                    return true;
                }
            }).toList();

    // new classpath = current source folders from the Gradle model + the previous ones defined
    // manually
    return ImmutableList.<IClasspathEntry>builder().addAll(gradleSourceFolders)
            .addAll(manuallyAddedSourceFolders).build();
}

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 ww  w.java  2 s. c  om
            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.
 *//*ww w  .j  a v  a  2s  . 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;
    }// w ww . j  a va 2 s.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);
    }/* w ww.  j  a  v a  2s.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.che.plugin.maven.server.core.classpath.ClasspathManager.java

License:Open Source License

private MavenArtifactKey getArtifactKey(IClasspathEntry classpathEntry) {
    IClasspathAttribute[] attributes = classpathEntry.getExtraAttributes();
    String groupId = null;//from  w  w  w  .j a  v  a 2 s . co m
    String artifactId = null;
    String version = null;
    String packaging = null;
    String classifier = null;
    for (IClasspathAttribute attribute : attributes) {
        if (ClasspathManager.GROUP_ID_ATTRIBUTE.equals(attribute.getName())) {
            groupId = attribute.getValue();
        } else if (ClasspathManager.ARTIFACT_ID_ATTRIBUTE.equals(attribute.getName())) {
            artifactId = attribute.getValue();
        } else if (ClasspathManager.VERSION_ATTRIBUTE.equals(attribute.getName())) {
            version = attribute.getValue();
        } else if (ClasspathManager.PACKAGING_ATTRIBUTE.equals(attribute.getName())) {
            packaging = attribute.getValue();
        } else if (ClasspathManager.CLASSIFIER_ATTRIBUTE.equals(attribute.getName())) {
            classifier = attribute.getValue();
        }
    }

    if (groupId != null && artifactId != null && version != null) {
        return new MavenArtifactKey(groupId, artifactId, version, packaging, classifier);
    }
    return null;
}