Example usage for org.eclipse.jdt.core IPackageFragmentRoot K_BINARY

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot K_BINARY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot K_BINARY.

Prototype

int K_BINARY

To view the source code for org.eclipse.jdt.core IPackageFragmentRoot K_BINARY.

Click Source Link

Document

Kind constant for a binary path root.

Usage

From source file:at.bestsolution.fxide.jdt.corext.javadoc.JavaDocLocations.java

License:Open Source License

public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
    if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
        return getProjectJavadocLocation((IJavaProject) element);
    }/* w ww . j a  v  a2s. co  m*/

    IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);
    if (root == null) {
        return null;
    }

    if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
        IClasspathEntry entry = root.getResolvedClasspathEntry();
        URL javadocLocation = getLibraryJavadocLocation(entry);
        if (javadocLocation != null) {
            return getLibraryJavadocLocation(entry);
        }
        entry = root.getRawClasspathEntry();
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_VARIABLE:
            return getLibraryJavadocLocation(entry);
        default:
            return null;
        }
    } else {
        return getProjectJavadocLocation(root.getJavaProject());
    }
}

From source file:at.bestsolution.fxide.jdt.corext.util.JavaModelUtil.java

License:Open Source License

/**
 * Returns the classpath entry of the given package fragment root. This is the raw entry, except
 * if the root is a referenced library, in which case it's the resolved entry.
 *
 * @param root a package fragment root/* w  ww.ja  v  a  2s.  com*/
 * @return the corresponding classpath entry
 * @throws JavaModelException if accessing the entry failed
 * @since 3.6
 */
public static IClasspathEntry getClasspathEntry(IPackageFragmentRoot root) throws JavaModelException {
    IClasspathEntry rawEntry = root.getRawClasspathEntry();
    int rawEntryKind = rawEntry.getEntryKind();
    switch (rawEntryKind) {
    case IClasspathEntry.CPE_LIBRARY:
    case IClasspathEntry.CPE_VARIABLE:
    case IClasspathEntry.CPE_CONTAINER: // should not happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=305037
        if (root.isArchive() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
            IClasspathEntry resolvedEntry = root.getResolvedClasspathEntry();
            if (resolvedEntry.getReferencingEntry() != null)
                return resolvedEntry;
            else
                return rawEntry;
        }
    }
    return rawEntry;
}

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

/**
 * Returns the Javadoc for a package which could be present in package.html, package-info.java
 * or from an attached Javadoc./*from   w ww  .j a v a 2 s .  c  o  m*/
 *
 * @param packageFragment the package which is requesting for the document
 * @return the document content in HTML format or <code>null</code> if there is no associated
 *         Javadoc
 * @throws CoreException if the Java element does not exists or an exception occurs while
 *             accessing the file containing the package Javadoc
 * @since 3.9
 */
public static String getHTMLContent(IPackageFragment packageFragment) throws CoreException {
    IPackageFragmentRoot root = (IPackageFragmentRoot) packageFragment
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

    //1==> Handle the case when the documentation is present in package-info.java or package-info.class file
    ITypeRoot packageInfo;
    boolean isBinary = root.getKind() == IPackageFragmentRoot.K_BINARY;
    if (isBinary) {
        packageInfo = packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
    } else {
        packageInfo = packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
    }
    if (packageInfo != null && packageInfo.exists()) {
        String cuSource = packageInfo.getSource();
        //the source can be null for some of the class files
        if (cuSource != null) {
            Javadoc packageJavadocNode = getPackageJavadocNode(packageFragment, cuSource);
            if (packageJavadocNode != null) {
                IJavaElement element;
                if (isBinary) {
                    element = ((IClassFile) packageInfo).getType();
                } else {
                    element = packageInfo.getParent(); // parent is the IPackageFragment
                }
                return new JavadocContentAccess2(element, packageJavadocNode, cuSource).toHTML();
            }
        }
    }

    // 2==> Handle the case when the documentation is done in package.html file. The file can be either in normal source folder or coming from a jar file
    else {
        Object[] nonJavaResources = packageFragment.getNonJavaResources();
        // 2.1 ==>If the package.html file is present in the source or directly in the binary jar
        for (Object nonJavaResource : nonJavaResources) {
            if (nonJavaResource instanceof IFile) {
                IFile iFile = (IFile) nonJavaResource;
                if (iFile.exists() && JavaModelUtil.PACKAGE_HTML.equals(iFile.getName())) {
                    return getIFileContent(iFile);
                }
            }
        }

        // 2.2==>The file is present in a binary container
        if (isBinary) {
            for (Object nonJavaResource : nonJavaResources) {
                // The content is from an external binary class folder
                if (nonJavaResource instanceof IJarEntryResource) {
                    IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
                    String encoding = getSourceAttachmentEncoding(root);
                    if (JavaModelUtil.PACKAGE_HTML.equals(jarEntryResource.getName())
                            && jarEntryResource.isFile()) {
                        return getHTMLContent(jarEntryResource, encoding);
                    }
                }
            }
            //2.3 ==>The file is present in the source attachment path.
            String contents = getHTMLContentFromAttachedSource(root, packageFragment);
            if (contents != null)
                return contents;
        }
    }

    //3==> Handle the case when the documentation is coming from the attached Javadoc
    if ((root.isArchive() || root.isExternal())) {
        return packageFragment.getAttachedJavadoc(null);

    }

    return null;
}

From source file:byke.DependencyAnalysis.java

License:Open Source License

private void assertValid(IPackageFragment fragment) throws InvalidElement {
    if (fragment == null)
        throw new InvalidElement("Null Package Fragment");
    try {//  w  w  w  . ja v a2  s  .com
        if (fragment.getKind() == IPackageFragmentRoot.K_BINARY)
            throw new InvalidElement("Binary Package");
    } catch (JavaModelException e) {
        throw new InvalidElement(e);
    }
}

From source file:com.blackducksoftware.integration.eclipseplugin.common.services.ProjectInformationService.java

License:Apache License

public int getNumBinaryDependencies(final List<IPackageFragmentRoot> packageFragmentRoots) {
    int numBinary = 0;
    for (final IPackageFragmentRoot root : packageFragmentRoots) {
        try {/*  www . j a  v  a  2s.co  m*/
            if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
                numBinary++;
            }
        } catch (final JavaModelException e) {
            /*
             * Occurs if root does not exist or an exception occurs while accessing
             * resource. If this happens, assume root is not binary and therefore do
             * not increment count
             */
        }
    }
    return numBinary;
}

From source file:com.blackducksoftware.integration.eclipseplugin.common.services.ProjectInformationService.java

License:Apache License

public URL getBinaryDependencyFilepath(final IPackageFragmentRoot packageFragmentRoot) {
    try {//  w w  w .  ja va  2 s.  c  o m
        if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
            return packageFragmentRoot.getPath().toFile().toURI().toURL();
        }
    } catch (final JavaModelException e) {
        /*
         * If root does not exist or exception occurs while accessing
         * resource, do not add its filepath to the list of binary
         * dependency filepaths
         */
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:com.blackducksoftware.integration.eclipseplugin.common.services.ProjectInformationServiceTest.java

License:Apache License

@Test
public void testGettingNumBinaryDependencies() {
    final ProjectInformationService service = new ProjectInformationService(depService, extractor);
    try {//  ww  w  .j a va2s  . c  o m
        Mockito.when(nonBinaryRoot.getKind()).thenReturn(0);
        Mockito.when(binaryRoot1.getKind()).thenReturn(IPackageFragmentRoot.K_BINARY);
        final List<IPackageFragmentRoot> roots = Arrays.asList(nonBinaryRoot, binaryRoot1);
        assertEquals("Not counting binary dependencies properly", 1, service.getNumBinaryDependencies(roots));
    } catch (final CoreException e) {
    }
}

From source file:com.blackducksoftware.integration.eclipseplugin.common.services.ProjectInformationServiceTest.java

License:Apache License

@Test
public void testGettingBinaryFilepathsWithoutDeviceIDs() throws CoreException {
    final ProjectInformationService service = new ProjectInformationService(depService, extractor);
    Mockito.when(nonBinaryRoot.getKind()).thenReturn(0);
    Mockito.when(nonBinaryRoot.getPath()).thenReturn(nonBinaryPath);
    Mockito.when(nonBinaryPath.getDevice()).thenReturn(null);
    Mockito.when(nonBinaryPath.toOSString()).thenReturn("/non/binary");
    Mockito.when(binaryRoot1.getKind()).thenReturn(IPackageFragmentRoot.K_BINARY);
    Mockito.when(binaryRoot1.getPath()).thenReturn(binaryPath1);
    Mockito.when(binaryPath1.getDevice()).thenReturn(null);
    Mockito.when(binaryPath1.toOSString()).thenReturn("/binary/path/1");

    final List<IPackageFragmentRoot> roots = Arrays.asList(nonBinaryRoot, binaryRoot1);
    final List<URL> binaryDependencies = service.getBinaryDependencyFilepaths(roots);
    assertEquals("Not gettting binary dependencies correctly", 1, binaryDependencies.size());
    assertEquals("Not getting correct binary dependencies", Arrays.asList("/binary/path/1"),
            binaryDependencies);//w  w  w.ja va  2s .  c o  m
}

From source file:com.blackducksoftware.integration.eclipseplugin.common.services.ProjectInformationServiceTest.java

License:Apache License

@Test
public void testGettingBinaryFilepathsWithDeviceIDs() throws CoreException {
    final ProjectInformationService service = new ProjectInformationService(depService, extractor);
    Mockito.when(binaryRoot1.getKind()).thenReturn(IPackageFragmentRoot.K_BINARY);
    Mockito.when(binaryRoot1.getPath()).thenReturn(binaryPath1);
    Mockito.when(binaryPath1.getDevice()).thenReturn("fake/device/id/1");
    Mockito.when(binaryPath1.toOSString()).thenReturn("fake/device/id/1/binary/path/1");
    Mockito.when(binaryRoot2.getKind()).thenReturn(IPackageFragmentRoot.K_BINARY);
    Mockito.when(binaryRoot2.getPath()).thenReturn(binaryPath2);
    Mockito.when(binaryPath2.getDevice()).thenReturn("fake/device/id/2");
    Mockito.when(binaryPath2.toOSString()).thenReturn("fake/device/id/2/binary/path/2");
    final List<IPackageFragmentRoot> roots = Arrays.asList(binaryRoot1, binaryRoot2);
    final List<URL> binaryDependencies = service.getBinaryDependencyFilepaths(roots);
    assertEquals("Not gettting binary dependencies correctly", 2, binaryDependencies.size());
    assertEquals("Not getting correct binary dependencies", Arrays.asList("/binary/path/1", "/binary/path/2"),
            binaryDependencies);//from   w  w  w  .  j a v a2s.  co m
}

From source file:com.blackducksoftware.integration.eclipseplugin.common.services.ProjectInformationServiceTest.java

License:Apache License

private void prepareRootsAndPaths() throws CoreException, MalformedURLException {
    Mockito.when(mavenRoot1.getPath()).thenReturn(mavenPath1);
    Mockito.when(mavenRoot1.getKind()).thenReturn(IPackageFragmentRoot.K_BINARY);
    Mockito.when(mavenPath1.getDevice()).thenReturn(null);
    Mockito.when(mavenPath1.toFile().toURI().toURL()).thenReturn(MAVEN_1);
    Mockito.when(mavenRoot2.getPath()).thenReturn(mavenPath2);
    Mockito.when(mavenRoot2.getKind()).thenReturn(IPackageFragmentRoot.K_BINARY);
    Mockito.when(mavenPath2.getDevice()).thenReturn(null);
    Mockito.when(mavenPath2.toFile().toURI().toURL()).thenReturn(MAVEN_2);
    Mockito.when(gradleRoot1.getPath()).thenReturn(gradlePath1);
    Mockito.when(gradleRoot1.getKind()).thenReturn(IPackageFragmentRoot.K_BINARY);
    Mockito.when(gradlePath1.getDevice()).thenReturn(null);
    Mockito.when(gradlePath1.toFile().toURI().toURL()).thenReturn(GRADLE_1);
    Mockito.when(gradleRoot2.getPath()).thenReturn(gradlePath2);
    Mockito.when(gradleRoot2.getKind()).thenReturn(IPackageFragmentRoot.K_BINARY);
    Mockito.when(gradlePath2.getDevice()).thenReturn(null);
    Mockito.when(gradlePath2.toFile().toURI().toURL()).thenReturn(GRADLE_2);
}