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

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

Introduction

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

Prototype

IPath getSourceAttachmentRootPath();

Source Link

Document

Returns the path within the source archive or folder where package fragments are located.

Usage

From source file:org.eclipse.jst.server.core.RuntimeClasspathProviderDelegate.java

License:Open Source License

/**
 * Request that the classpath container for the given runtime and id be updated
 * with the given classpath container entries.
 * // ww  w .j  a va  2 s  .c om
 * @param runtime a runtime
 * @param entries an array of classpath entries
 */
public void requestClasspathContainerUpdate(IRuntime runtime, IClasspathEntry[] entries) {
    // default behaviour is to save the source path entries
    if (runtime == null || entries == null)
        return;

    // find the source attachments
    List<SourceAttachmentUpdate> srcAttachments = new ArrayList<SourceAttachmentUpdate>();

    for (IClasspathEntry entry : entries) {
        if (entry.getSourceAttachmentPath() != null
                || (entry.getExtraAttributes() != null && entry.getExtraAttributes().length > 0)) {
            SourceAttachmentUpdate sau = new SourceAttachmentUpdate();
            sau.runtimeId = runtime.getId();
            sau.entry = entry.getPath();
            sau.sourceAttachmentPath = entry.getSourceAttachmentPath();
            sau.sourceAttachmentRootPath = entry.getSourceAttachmentRootPath();
            sau.attributes = entry.getExtraAttributes();
            srcAttachments.add(sau);
        }
    }
    sourceAttachments = srcAttachments;
    save();
}

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

License:Open Source License

/**
 * Extracts and persists custom source/javadoc attachment info
 *//*from w w w .j av a2  s  . c  o  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 . ja va2 s.  com*/

    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  .jav  a 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.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;// w ww. j a  va 2s . 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.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 ww .  j a v a  2s  .  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 {/*from   ww w.  ja  v  a2 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);
    }
}

From source file:org.maven.ide.eclipse.scala.ScalaProjectConfigurator.java

License:Open Source License

private static void addDeployableAttribute(IJavaProject javaProject, IClasspathAttribute deployableAttribute,
        IProgressMonitor monitor) throws JavaModelException, CoreException {
    if (javaProject == null)
        return;/* w  w  w .java 2 s .  c om*/
    ClasspathContainerInitializer scalaInitializer = JavaCore
            .getClasspathContainerInitializer(SCALA_CONTAINER_PATH);
    if (scalaInitializer == null)
        return;
    IPath scalaContainerPath = Path.fromPortableString(SCALA_CONTAINER_PATH);
    Boolean updateAble = scalaInitializer.canUpdateClasspathContainer(scalaContainerPath, javaProject);
    final IClasspathContainer scalaLibrary = JavaCore.getClasspathContainer(scalaContainerPath, javaProject);
    final IClasspathEntry[] cpEntries = scalaLibrary.getClasspathEntries();

    for (int i = 0; i < cpEntries.length; i++) {
        IClasspathEntry cpe = cpEntries[i];
        LinkedHashMap<String, IClasspathAttribute> attrs = new LinkedHashMap<String, IClasspathAttribute>();
        for (IClasspathAttribute attr : cpe.getExtraAttributes()) {
            //Keep all existing attributes except the non_deployable key
            if (!attr.getName().equals(NON_DEPLOYABLE_KEY)) {
                attrs.put(attr.getName(), attr);
            }
        }
        attrs.put(deployableAttribute.getName(), deployableAttribute);
        IClasspathAttribute[] newAttrs = attrs.values().toArray(new IClasspathAttribute[attrs.size()]);
        cpEntries[i] = JavaCore.newLibraryEntry(cpe.getPath(), cpe.getSourceAttachmentPath(),
                cpe.getSourceAttachmentRootPath(), cpe.getAccessRules(), newAttrs, cpe.isExported());
    }

    IClasspathContainer candidateScalaContainer = new IClasspathContainer() {
        public IPath getPath() {
            return scalaLibrary.getPath();
        }

        public IClasspathEntry[] getClasspathEntries() {
            return cpEntries;
        }

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

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

    if (updateAble) {
        scalaInitializer.requestClasspathContainerUpdate(scalaContainerPath, javaProject,
                candidateScalaContainer);
    } else {
        IJavaProject[] jPArray = { javaProject };
        IClasspathContainer[] cpArray = { candidateScalaContainer };
        JavaCore.setClasspathContainer(scalaContainerPath, jPArray, cpArray, null);
    }
}

From source file:runjettyrun.tabs.action.AddClassFolderAction.java

License:Open Source License

/**
 * Adds all exported entries defined by <code>proj</code> to the list
 * <code>runtimeEntries</code>.
 *
 * @param proj//from   ww  w  . j a  va2 s  . c  o m
 * @param runtimeEntries
 * @throws JavaModelException
 */
protected void collectExportedEntries(IJavaProject proj, List<IRuntimeClasspathEntry> runtimeEntries)
        throws CoreException {
    IClasspathEntry[] entries = proj.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.isExported()) {
            IRuntimeClasspathEntry rte = null;
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_CONTAINER:
                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), proj);
                int kind = 0;
                switch (container.getKind()) {
                case IClasspathContainer.K_APPLICATION:
                    kind = IRuntimeClasspathEntry.USER_CLASSES;
                    break;
                case IClasspathContainer.K_SYSTEM:
                    kind = IRuntimeClasspathEntry.BOOTSTRAP_CLASSES;
                    break;
                case IClasspathContainer.K_DEFAULT_SYSTEM:
                    kind = IRuntimeClasspathEntry.STANDARD_CLASSES;
                    break;
                }
                rte = JavaRuntime.newRuntimeContainerClasspathEntry(entry.getPath(), kind, proj);
                break;
            case IClasspathEntry.CPE_LIBRARY:
                rte = JavaRuntime.newArchiveRuntimeClasspathEntry(entry.getPath());
                rte.setSourceAttachmentPath(entry.getSourceAttachmentPath());
                rte.setSourceAttachmentRootPath(entry.getSourceAttachmentRootPath());
                break;
            case IClasspathEntry.CPE_PROJECT:
                String name = entry.getPath().segment(0);
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
                if (p.exists()) {
                    IJavaProject jp = JavaCore.create(p);
                    if (jp.exists()) {
                        rte = JavaRuntime.newProjectRuntimeClasspathEntry(jp);
                    }
                }
                break;
            case IClasspathEntry.CPE_VARIABLE:
                rte = JavaRuntime.newVariableRuntimeClasspathEntry(entry.getPath());
                break;
            default:
                break;
            }
            if (rte != null) {
                if (!runtimeEntries.contains(rte)) {
                    runtimeEntries.add(rte);
                }
            }
        }
    }
}