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

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

Introduction

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

Prototype

IPath getSourceAttachmentPath();

Source Link

Document

Returns the path to the source archive or folder associated with this classpath entry, or null if this classpath entry has no source attachment.

Usage

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

License:Open Source License

/**
 * Extracts and persists custom source/javadoc attachment info
 *//*  w w  w  .j  av a  2 s. co m*/
public void persistAttachedSourcesAndJavadoc(IJavaProject project, IClasspathContainer containerSuggestion,
        IProgressMonitor monitor) throws CoreException {
    IFile pom = project.getProject().getFile(IMavenConstants.POM_FILE_NAME);
    IMavenProjectFacade facade = projectManager.create(pom, false, null);
    if (facade == null) {
        return;
    }

    // collect all source/javadoc attachement
    Properties props = new Properties();
    IClasspathEntry[] entries = containerSuggestion.getClasspathEntries();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (IClasspathEntry.CPE_LIBRARY == entry.getEntryKind()) {
            String path = entry.getPath().toPortableString();
            if (entry.getSourceAttachmentPath() != null) {
                props.put(path + PROPERTY_SRC_PATH, entry.getSourceAttachmentPath().toPortableString());
            }
            if (entry.getSourceAttachmentRootPath() != null) {
                props.put(path + PROPERTY_SRC_ROOT, entry.getSourceAttachmentRootPath().toPortableString());
            }
            String javadocUrl = getJavadocLocation(entry);
            if (javadocUrl != null) {
                props.put(path + PROPERTY_JAVADOC_URL, javadocUrl);
            }
        }
    }

    // eliminate all "standard" source/javadoc attachement we get from local repo
    entries = getClasspath(facade, CLASSPATH_DEFAULT, null, true, monitor);
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (IClasspathEntry.CPE_LIBRARY == entry.getEntryKind()) {
            String path = entry.getPath().toPortableString();
            String value = (String) props.get(path + PROPERTY_SRC_PATH);
            if (value != null && entry.getSourceAttachmentPath() != null
                    && value.equals(entry.getSourceAttachmentPath().toPortableString())) {
                props.remove(path + PROPERTY_SRC_PATH);
            }
            value = (String) props.get(path + PROPERTY_SRC_ROOT);
            if (value != null && entry.getSourceAttachmentRootPath() != null
                    && value.equals(entry.getSourceAttachmentRootPath().toPortableString())) {
                props.remove(path + PROPERTY_SRC_ROOT);
            }
        }
    }

    // persist custom source/javadoc attachement info
    File file = getSourceAttachmentPropertiesFile(project.getProject());
    try {
        OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
        try {
            props.store(os, null);
        } finally {
            os.close();
        }
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, MavenJdtPlugin.PLUGIN_ID, -1,
                "Can't save classpath container changes", e));
    }

    // update classpath container. suboptimal as this will re-calculate classpath
    updateClasspath(project.getProject(), monitor);
}

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 w  w  w . j  a v  a 2  s.  c  o m*/

    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.BuildPathManagerTest.java

License:Open Source License

public void testDownloadSources_001_sourceAttachment() throws Exception {
    new File(repo, "downloadsources/downloadsources-t001/0.0.1/downloadsources-t001-0.0.1-sources.jar")
            .delete();//from w  w  w  . j  a  va  2 s .c o m
    new File(repo, "downloadsources/downloadsources-t002/0.0.1/downloadsources-t002-0.0.1-sources.jar")
            .delete();

    IProject project = createExisting("downloadsources-p001", "projects/downloadsources/p001");
    waitForJobsToComplete();

    IJavaProject javaProject = JavaCore.create(project);
    final IClasspathContainer container = BuildPathManager.getMaven2ClasspathContainer(javaProject);

    IPath entryPath = container.getClasspathEntries()[0].getPath();

    IPath srcPath = new Path("/a");
    IPath srcRoot = new Path("/b");
    String javaDocUrl = "c";

    IClasspathAttribute attribute = JavaCore
            .newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javaDocUrl);

    final IClasspathEntry entry = JavaCore.newLibraryEntry(entryPath, //
            srcPath, srcRoot, new IAccessRule[0], //
            new IClasspathAttribute[] { attribute }, // 
            false /*not exported*/);

    BuildPathManager buildpathManager = getBuildPathManager();

    IClasspathContainer containerSuggestion = new IClasspathContainer() {
        public IClasspathEntry[] getClasspathEntries() {
            return new IClasspathEntry[] { entry };
        }

        public String getDescription() {
            return container.getDescription();
        }

        public int getKind() {
            return container.getKind();
        }

        public IPath getPath() {
            return container.getPath();
        }
    };
    buildpathManager.persistAttachedSourcesAndJavadoc(javaProject, containerSuggestion, monitor);
    waitForJobsToComplete();

    // check custom source/javadoc
    IClasspathContainer container2 = BuildPathManager.getMaven2ClasspathContainer(javaProject);
    IClasspathEntry entry2 = container2.getClasspathEntries()[0];
    assertEquals(entryPath, entry2.getPath());
    assertEquals(srcPath, entry2.getSourceAttachmentPath());
    assertEquals(srcRoot, entry2.getSourceAttachmentRootPath());
    assertEquals(javaDocUrl, buildpathManager.getJavadocLocation(entry2));

    File file = buildpathManager.getSourceAttachmentPropertiesFile(project);
    assertEquals(true, file.canRead());

    // check project delete
    deleteProject(project);
    waitForJobsToComplete();
    assertEquals(false, file.canRead());
}

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);//from  w  ww.jav  a2s  .c  o 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.pde.internal.core.ClasspathComputer.java

License:Open Source License

private static void addLibraryEntry(IProject project, IPluginLibrary library, IPath sourceAttachment,
        IClasspathAttribute[] attrs, ArrayList<IClasspathEntry> result) throws JavaModelException {
    String name = ClasspathUtilCore.expandLibraryName(library.getName());
    IResource jarFile = project.findMember(name);
    if (jarFile == null)
        return;/*from w  ww . ja va2 s  . c o  m*/

    IPackageFragmentRoot root = JavaCore.create(project).getPackageFragmentRoot(jarFile);
    if (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
        IClasspathEntry oldEntry = root.getRawClasspathEntry();
        // If we have the same binary root but new or different source, we should recreate the entry 
        if ((sourceAttachment == null && oldEntry.getSourceAttachmentPath() != null)
                || (sourceAttachment != null && sourceAttachment.equals(oldEntry.getSourceAttachmentPath()))) {
            if (!result.contains(oldEntry)) {
                result.add(oldEntry);
                return;
            }
        }
    }

    IClasspathEntry entry = createClasspathEntry(project, jarFile, name, sourceAttachment, attrs,
            library.isExported());
    if (!result.contains(entry))
        result.add(entry);
}

From source file:org.eclipse.virgo.ide.jdt.internal.core.util.ClasspathUtils.java

License:Open Source License

/**
 * Stores the configured source attachments paths in the projects settings area.
 *
 * @param project the java project to store the preferences for
 * @param containerSuggestion the configured classpath container entries
 *///from w  w  w. jav a  2s  . c o  m
public static void storeSourceAttachments(IJavaProject project, IClasspathContainer containerSuggestion) {
    IEclipsePreferences preferences = JdtCorePlugin.getDefault().getProjectPreferences(project.getProject());
    for (IClasspathEntry entry : containerSuggestion.getClasspathEntries()) {
        IPath path = entry.getPath();
        IPath sourcePath = entry.getSourceAttachmentPath();
        if (sourcePath != null) {
            preferences.put("source.attachment-" + path.lastSegment().toString(), sourcePath.toString());
        }
    }
}

From source file:org.eclipse.vjet.eclipse.javalaunch.utils.EclipseResourceUtils.java

License:Open Source License

/**
 * Searches through the tree of dependencies and adds to a list of projects.
 * @param javaProject - The Java project to search
 * @param transitiveClosureProjectList - The list to store the projects
 * @throws JavaModelException//from   w  w  w.  ja  v  a 2s .  c  o m
 */
public static void getTransitiveClosureDependencies(IJavaProject javaProject,
        Map<String, IJavaProject> transitiveClosureProjectList, Map<String, IPath> transitiveLibrarySet)
        throws JavaModelException {

    IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(true);

    if (classPathEntries != null) {

        for (IClasspathEntry classPathEntry : classPathEntries) {

            if (classPathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IResource classPathProject = ResourcesPlugin.getWorkspace().getRoot()
                        .findMember(classPathEntry.getPath());
                if (classPathProject != null) {
                    if (transitiveClosureProjectList.containsKey(classPathProject.getName()) == false) {

                        IJavaProject subJavaProject = getJavaProject(classPathProject);
                        transitiveClosureProjectList.put(classPathProject.getName(), subJavaProject);

                        getTransitiveClosureDependencies(subJavaProject, transitiveClosureProjectList,
                                transitiveLibrarySet);
                    }
                }
            } else if (classPathEntry != null && classPathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                if (classPathEntry.getSourceAttachmentPath() != null) {
                    String key = classPathEntry.getSourceAttachmentPath().toString();
                    transitiveLibrarySet.put(key, classPathEntry.getSourceAttachmentPath());
                }
            }
        }
    }

}

From source file:org.grails.ide.eclipse.core.internal.classpath.GrailsClasspathContainer.java

License:Open Source License

/**
 * Stores the configured source attachments paths in the projects settings area.
 * @param project the java project to store the preferences for
 * @param containerSuggestion the configured classpath container entries
 *//*from   w  w  w.ja  v a  2  s .co m*/
public static void storeSourceAttachments(IJavaProject project, IClasspathContainer containerSuggestion) {
    SpringCorePreferences prefs = SpringCorePreferences.getProjectPreferences(project.getProject(),
            GrailsCoreActivator.PLUGIN_ID);
    for (IClasspathEntry entry : containerSuggestion.getClasspathEntries()) {
        IPath path = entry.getPath();
        IPath sourcePath = entry.getSourceAttachmentPath();
        if (sourcePath != null) {
            prefs.putString("source.attachment-" + path.lastSegment().toString(), sourcePath.toString());
        }
        for (IClasspathAttribute attribute : entry.getExtraAttributes()) {
            if (attribute.getName().equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) {
                String value = attribute.getValue();
                prefs.putString("javadoc.location-" + path.lastSegment().toString(), value);
            }
        }
    }
}

From source file:org.jboss.ide.eclipse.as.classpath.core.jee.AbstractClasspathContainerInitializer.java

License:Open Source License

public void requestClasspathContainerUpdate(final IPath containerPath, final IJavaProject project,
        final IClasspathContainer sg)

        throws CoreException

{
    String key = AbstractClasspathContainer.getDecorationManagerKey(containerPath.toString());

    IClasspathEntry[] entries = sg.getClasspathEntries();
    ClasspathDecorationsManager decorations = AbstractClasspathContainer.getDecorationsManager();
    decorations.clearAllDecorations(key);

    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];

        final IPath srcpath = entry.getSourceAttachmentPath();
        final IPath srcrootpath = entry.getSourceAttachmentRootPath();
        final IClasspathAttribute[] attrs = entry.getExtraAttributes();
        final String eid = entry.getPath().toString();
        final ClasspathDecorations dec = new ClasspathDecorations();

        dec.setSourceAttachmentPath(srcpath);
        dec.setSourceAttachmentRootPath(srcrootpath);
        dec.setExtraAttributes(attrs);//from   w  w w .  java 2  s  .c  o  m

        decorations.setDecorations(key, eid, dec);
    }
    decorations.save();
    final IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, project);
    ((AbstractClasspathContainer) container).refresh();
}

From source file:org.jboss.tools.common.jdt.core.buildpath.MaterializeLibraryJob.java

License:Open Source License

private IClasspathEntry getNewClasspathEntry(IClasspathEntry entry, IPath destinationFilePath)
        throws CoreException {
    try {/*w  w w.  j av  a  2  s  .c o m*/
        return JavaCore.newLibraryEntry(destinationFilePath,
                (keepSourceAttachments) ? entry.getSourceAttachmentPath() : null,
                (keepSourceAttachments) ? entry.getSourceAttachmentRootPath() : null, entry.getAccessRules(),
                entry.getExtraAttributes(), entry.isExported());
    } catch (Exception e) {
        IStatus status = new Status(IStatus.ERROR, JDTExtActivator.PLUGIN_ID,
                NLS.bind(Messages.MaterializeLibraryJob_Error_creating_classpath_entry, e.getMessage()), e);
        throw new CoreException(status);
    }
}