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:com.google.cloud.tools.eclipse.appengine.libraries.LibraryClasspathContainerInitializer.java

License:Apache License

private static boolean jarPathsAreValid(LibraryClasspathContainer container) {
    IClasspathEntry[] classpathEntries = container.getClasspathEntries();
    for (int i = 0; i < classpathEntries.length; i++) {
        IClasspathEntry classpathEntry = classpathEntries[i];
        if (!classpathEntry.getPath().toFile().exists() || (classpathEntry.getSourceAttachmentPath() != null
                && !classpathEntry.getSourceAttachmentPath().toFile().exists())) {
            return false;
        }//from ww w .  j  av a2  s. c om
    }
    return true;
}

From source file:com.google.cloud.tools.eclipse.appengine.libraries.LibraryClasspathContainerInitializerTest.java

License:Apache License

@Test
public void testInitialize_ifSourceArtifactJarPathInvalidContainerResolvedFromScratch()
        throws CoreException, IOException {
    File artifactFile = temporaryFolder.newFile();
    assertFalse(new File(NON_EXISTENT_FILE).exists());

    IClasspathEntry entry = mock(IClasspathEntry.class);
    when(entry.getPath()).thenReturn(new Path(artifactFile.getAbsolutePath()));
    when(entry.getSourceAttachmentPath()).thenReturn(new Path(NON_EXISTENT_FILE));
    IClasspathEntry[] entries = new IClasspathEntry[] { entry };
    LibraryClasspathContainer container = mock(LibraryClasspathContainer.class);
    when(container.getClasspathEntries()).thenReturn(entries);
    when(serializer.loadContainer(any(IJavaProject.class), any(IPath.class))).thenReturn(container);

    LibraryClasspathContainerInitializer containerInitializer = new LibraryClasspathContainerInitializer(
            TEST_CONTAINER_PATH, serializer, resolverService);
    containerInitializer.initialize(new Path(TEST_LIBRARY_PATH), testProject.getJavaProject());

    verifyContainerResolvedFromScratch();
}

From source file:com.google.cloud.tools.eclipse.appengine.libraries.persistence.LibraryClasspathContainerSerializerTest.java

License:Apache License

private void compare(LibraryClasspathContainer container, LibraryClasspathContainer otherContainer) {
    assertEquals(container.getPath(), otherContainer.getPath());
    assertEquals(container.getKind(), otherContainer.getKind());
    assertEquals(container.getDescription(), otherContainer.getDescription());
    for (int i = 0; i < container.getClasspathEntries().length; i++) {
        IClasspathEntry classpathEntry = container.getClasspathEntries()[i];
        IClasspathEntry otherClasspathEntry = otherContainer.getClasspathEntries()[i];
        assertEquals(classpathEntry.getPath(), otherClasspathEntry.getPath());
        assertEquals(classpathEntry.getEntryKind(), otherClasspathEntry.getEntryKind());
        assertEquals(classpathEntry.getSourceAttachmentPath(), otherClasspathEntry.getSourceAttachmentPath());
        assertEquals(classpathEntry.isExported(), otherClasspathEntry.isExported());
        for (int j = 0; j < classpathEntry.getAccessRules().length; j++) {
            IAccessRule accessRule = classpathEntry.getAccessRules()[j];
            IAccessRule otherAccessRule = otherClasspathEntry.getAccessRules()[j];
            assertEquals(accessRule.getKind(), otherAccessRule.getKind());
            assertEquals(accessRule.getPattern(), otherAccessRule.getPattern());
        }/* ww  w . jav a  2  s  .  co m*/
        for (int k = 0; k < classpathEntry.getExtraAttributes().length; k++) {
            IClasspathAttribute classpathAttribute = classpathEntry.getExtraAttributes()[k];
            IClasspathAttribute otherClasspathAttribute = otherClasspathEntry.getExtraAttributes()[k];
            assertEquals(classpathAttribute.getName(), otherClasspathAttribute.getName());
            assertEquals(classpathAttribute.getValue(), otherClasspathAttribute.getValue());
        }
    }
}

From source file:com.google.cloud.tools.eclipse.appengine.libraries.persistence.SerializableClasspathEntry.java

License:Apache License

public SerializableClasspathEntry(IClasspathEntry entry, IPath baseDirectory) {
    setAttributes(entry.getExtraAttributes());
    setAccessRules(entry.getAccessRules());
    setSourcePath(entry.getSourceAttachmentPath());
    setPath(PathUtil.relativizePath(entry.getPath(), baseDirectory).toString());
}

From source file:com.google.cloud.tools.eclipse.appengine.libraries.repository.M2RepositoryService.java

License:Apache License

@Override
public IClasspathEntry rebuildClasspathEntry(IClasspathEntry classpathEntry)
        throws LibraryRepositoryServiceException {
    MavenCoordinates mavenCoordinates = transformer.createMavenCoordinates(classpathEntry.getExtraAttributes());
    Artifact artifact = resolveArtifact(mavenCoordinates);
    return JavaCore.newLibraryEntry(new Path(artifact.getFile().getAbsolutePath()),
            classpathEntry.getSourceAttachmentPath(), null /*  sourceAttachmentRootPath */,
            classpathEntry.getAccessRules(), classpathEntry.getExtraAttributes(), true /* isExported */);
}

From source file:com.google.gwt.eclipse.core.runtime.GWTJarsRuntimeTest.java

License:Open Source License

public void testGetValidationClasspathEntries() throws Exception {
    IClasspathEntry[] cpEntries = runtime.getClasspathEntries();

    // Look for the validation-specific classpath entries
    List<IClasspathEntry> validationCpEntries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry cpEntry : cpEntries) {
        if (cpEntry.getPath().lastSegment().startsWith(GWTRuntime.VALIDATION_API_JAR_PREFIX)) {
            validationCpEntries.add(cpEntry);
        }//w w  w  .  j  ava2s  .  com
    }

    if (validationCpEntries.size() == 0) {
        String sdkVersion = runtime.getVersion();

        // Can't be an internal version, because it would have the
        // validation jars
        assertFalse(SdkUtils.isInternal(sdkVersion));

        // Sdk version must be pre-GWT 2.3.0
        assertTrue(SdkUtils.compareVersionStrings(sdkVersion, "2.3.0") < 0);

        return;
    }

    // Make sure that there are at least two of them
    assertEquals(2, validationCpEntries.size());

    IClasspathEntry sourcesEntry = null;
    IClasspathEntry binaryEntry = null;

    for (IClasspathEntry validationClasspathEntry : validationCpEntries) {
        // Verify the entry types
        assertEquals(IClasspathEntry.CPE_LIBRARY, validationClasspathEntry.getEntryKind());
        assertEquals(IPackageFragmentRoot.K_BINARY, validationClasspathEntry.getContentKind());

        if (validationClasspathEntry.getPath().lastSegment().contains("sources")) {
            sourcesEntry = validationClasspathEntry;
        } else {
            binaryEntry = validationClasspathEntry;
        }
    }

    // Verify that the sources and binary entries correspond to each other
    assertTrue(Util.findSourcesJarForClassesJar(binaryEntry.getPath()).equals(sourcesEntry.getPath()));

    // Verify that the source attachment path has been set for the binary
    // entry
    assertTrue(binaryEntry.getSourceAttachmentPath().equals(sourcesEntry.getPath()));
}

From source file:com.google.gwt.eclipse.core.runtime.GWTRuntimeContainerInitializerTest.java

License:Open Source License

/**
 * TODO: We need to revisit this test. Since we don't allow container updates
 * right now, it is not clear that the test is sufficiently strong.
 *//*from w w w . j  av  a 2  s . c om*/
public void testRequestClasspathContainerUpdate() throws CoreException {
    IJavaProject testProject = getTestProject();

    IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(defaultRuntimePath, testProject);

    final List<IClasspathEntry> newClasspathEntries = new ArrayList<IClasspathEntry>();
    IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries();
    for (IClasspathEntry classpathEntry : classpathEntries) {

        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IClasspathAttribute[] extraAttributes = classpathEntry.getExtraAttributes();
            List<IClasspathAttribute> newAttributes = new ArrayList<IClasspathAttribute>();
            for (IClasspathAttribute extraAttribute : extraAttributes) {
                String attributeName = extraAttribute.getName();
                if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attributeName)) {
                    String attributeValue = extraAttribute.getValue() + "modified";
                    extraAttribute = JavaCore.newClasspathAttribute(attributeName, attributeValue);
                }

                newAttributes.add(extraAttribute);
            }

            IPath sourceAttachmentPath = new Path("/sourceAttachmentPath");
            IPath sourceAttachmentRootPath = new Path("sourceAttachmentRootPath");

            classpathEntry = JavaCore.newLibraryEntry(classpathEntry.getPath(), sourceAttachmentPath,
                    sourceAttachmentRootPath, classpathEntry.getAccessRules(),
                    newAttributes.toArray(new IClasspathAttribute[0]), classpathEntry.isExported());
        }

        newClasspathEntries.add(classpathEntry);
    }

    // Update the classpath container
    initializer.requestClasspathContainerUpdate(defaultRuntimePath, testProject,
            new ClasspathContainerAdapter(classpathContainer) {
                @Override
                public IClasspathEntry[] getClasspathEntries() {
                    return newClasspathEntries.toArray(new IClasspathEntry[0]);
                }
            });

    // Check that the modifications took effect
    IClasspathContainer updatedContainer = JavaCore.getClasspathContainer(defaultRuntimePath, testProject);
    for (IClasspathEntry classpathEntry : updatedContainer.getClasspathEntries()) {
        if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) {
            // Ignore all non-library entries
            continue;
        }

        for (IClasspathAttribute attribute : classpathEntry.getExtraAttributes()) {
            if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attribute.getName())) {
                String value = attribute.getValue();
                assertTrue(value.endsWith("modified"));
            }
        }

        IPath sourceAttachmentPath = classpathEntry.getSourceAttachmentPath();
        assertEquals(new Path("/sourceAttachmentPath"), sourceAttachmentPath);

        IPath sourceAttachmentRootPath = classpathEntry.getSourceAttachmentRootPath();
        assertEquals(new Path("sourceAttachmentRootPath"), sourceAttachmentRootPath);
    }
}

From source file:com.javapathfinder.vjp.DefaultProperties.java

License:Open Source License

private static String getSourcepathEntry(IJavaProject project) {

    StringBuilder sourcepath = new StringBuilder();
    IClasspathEntry[] paths;/*from   ww w  .  j  a  va2  s.  c  o  m*/

    try {
        paths = project.getResolvedClasspath(true);
    } catch (JavaModelException e) {
        VJP.logError("Could not retrieve project classpaths.", e);
        return "";
    }

    for (IClasspathEntry entry : paths) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            sourcepath.append(getAbsolutePath(project, entry.getPath()));
            sourcepath.append(Config.LIST_SEPARATOR);
        } else if (entry.getSourceAttachmentPath() != null) {
            IPath path = entry.getSourceAttachmentPath();
            if (path.getFileExtension() == null) { //null for a directory
                sourcepath.append(path);
                sourcepath.append(Config.LIST_SEPARATOR);
            }
        }
    }
    if (sourcepath.length() > 0)
        sourcepath.setLength(sourcepath.length() - 1); //remove that trailing separator
    // VJP.logInfo(sourcepath.toString());
    return sourcepath.toString();
}

From source file:com.liferay.ide.project.core.PluginClasspathContainerInitializer.java

License:Open Source License

@Override
public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project,
        IClasspathContainer containerSuggestion) throws CoreException {

    final String key = PluginClasspathContainer.getDecorationManagerKey(project.getProject(),
            containerPath.toString());//  w ww .  j a  va2s. c o  m

    final IClasspathEntry[] entries = containerSuggestion.getClasspathEntries();

    cpDecorations.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();

        if (srcpath != null || attrs.length > 0) {
            final String eid = entry.getPath().toString();
            final ClasspathDecorations dec = new ClasspathDecorations();

            dec.setSourceAttachmentPath(srcpath);
            dec.setSourceAttachmentRootPath(srcrootpath);
            dec.setExtraAttributes(attrs);

            cpDecorations.setDecorations(key, eid, dec);
        }
    }

    cpDecorations.save();

    IPath portalDir = null;
    String javadocURL = null;
    IPath sourceLocation = null;

    if (containerSuggestion instanceof PluginClasspathContainer) {
        portalDir = ((PluginClasspathContainer) containerSuggestion).getPortalDir();
        javadocURL = ((PluginClasspathContainer) containerSuggestion).getJavadocURL();
        sourceLocation = ((PluginClasspathContainer) containerSuggestion).getSourceLocation();
    } else {
        portalDir = ServerUtil.getPortalDir(project);

        try {
            ILiferayRuntime liferayRuntime = ServerUtil.getLiferayRuntime(project.getProject());

            if (liferayRuntime != null) {
                javadocURL = liferayRuntime.getJavadocURL();

                sourceLocation = liferayRuntime.getSourceLocation();
            }
        } catch (Exception e) {
            ProjectCore.logError(e);
        }
    }

    if (portalDir != null) {
        IClasspathContainer newContainer = getCorrectContainer(containerPath, containerPath.segment(1), project,
                portalDir, javadocURL, sourceLocation);

        JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project },
                new IClasspathContainer[] { newContainer }, null);
    }
}

From source file:com.liferay.ide.project.core.SDKClasspathContainerInitializer.java

License:Open Source License

@Override
public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project,
        IClasspathContainer containerSuggestion) throws CoreException {
    final String key = SDKClasspathContainer.getDecorationManagerKey(project.getProject(),
            containerPath.toString());/*w ww.  j ava 2s.com*/

    final IClasspathEntry[] entries = containerSuggestion.getClasspathEntries();

    cpDecorations.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();

        if (srcpath != null || attrs.length > 0) {
            final String eid = entry.getPath().toString();
            final ClasspathDecorations dec = new ClasspathDecorations();

            dec.setSourceAttachmentPath(srcpath);
            dec.setSourceAttachmentRootPath(srcrootpath);
            dec.setExtraAttributes(attrs);

            cpDecorations.setDecorations(key, eid, dec);
        }
    }

    cpDecorations.save();

    IPath portalDir = null;
    IPath portalGlobalDir = null;
    String javadocURL = null;
    IPath sourceLocation = null;
    IPath bundleDir = null;
    IPath[] bundleDependencyJarPaths = null;

    PortalBundle bundle = ServerUtil.getPortalBundle(project.getProject());

    boolean containerChanged = true;

    if (containerSuggestion instanceof SDKClasspathContainer) {
        portalDir = ((SDKClasspathContainer) containerSuggestion).getPortalDir();
        bundleDir = ((SDKClasspathContainer) containerSuggestion).getBundleDir();
        portalGlobalDir = ((SDKClasspathContainer) containerSuggestion).getPortalGlobalDir();
        javadocURL = ((SDKClasspathContainer) containerSuggestion).getJavadocURL();
        sourceLocation = ((SDKClasspathContainer) containerSuggestion).getSourceLocation();
        bundleDependencyJarPaths = ((SDKClasspathContainer) containerSuggestion).getBundleLibDependencyPath();

        if (bundle != null && bundle.getAppServerPortalDir().equals(portalDir)) {
            containerChanged = false;
        }
    }

    if (containerChanged == true) {
        if (bundle == null) {
            return;
        }

        portalDir = bundle.getAppServerPortalDir();
        portalGlobalDir = bundle.getAppServerLibGlobalDir();
        bundleDependencyJarPaths = bundle.getBundleDependencyJars();
    }

    IPath[] sdkDependencyPaths = getSDKDependencies(project);

    if (portalDir != null && portalGlobalDir != null) {
        IClasspathContainer newContainer = new SDKClasspathContainer(containerPath, project, portalDir,
                javadocURL, sourceLocation, portalGlobalDir, bundleDir, bundleDependencyJarPaths,
                sdkDependencyPaths);

        JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { project },
                new IClasspathContainer[] { newContainer }, null);
    }
}