Example usage for org.eclipse.jdt.core ITypeRoot exists

List of usage examples for org.eclipse.jdt.core ITypeRoot exists

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeRoot exists.

Prototype

boolean exists();

Source Link

Document

Returns whether this Java element exists in the model.

Usage

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.// www  . j  a v  a  2 s  .  c  om
 *
 * @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:org.eclipse.ajdt.core.builder.AJCompilationParticipantResult.java

License:Open Source License

@Override
public boolean hasAnnotations() {
    if (hasAnnotationsCache == null) {
        try {/*from  ww  w.j  a v  a 2 s.  c  om*/
            if (file != null) {
                ITypeRoot root = (ITypeRoot) JavaCore.create(file);
                if (root.exists()) {
                    hasAnnotationsCache = hasAnnotations(root);
                } else {
                    hasAnnotationsCache = false;
                }
            }
        } catch (JavaModelException e) {
            hasAnnotationsCache = false;
        }

    }

    return hasAnnotationsCache;
}

From source file:org.eclipse.che.jdt.javadoc.ASTProvider.java

License:Open Source License

/**
 * Checks whether the given Java element has accessible source.
 *
 * @param je the Java element to test/* w  w w  .j  a v a  2  s. c  o  m*/
 * @return <code>true</code> if the element has source
 * @since 3.2
 */
private static boolean hasSource(ITypeRoot je) {
    if (je == null || !je.exists())
        return false;

    try {
        return je.getBuffer() != null;
    } catch (JavaModelException ex) {
        LOG.error(ex.getMessage(), ex);
    }
    return false;
}

From source file:org.eclipse.che.jdt.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./* ww w. ja  v a2s .  c  o  m*/
 *
 * @param packageFragment the package which is requesting for the document
 * @param urlPrefix
 * @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, String urlPrefix) 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, urlPrefix).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, urlPrefix);
            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:org.eclipse.recommenders.internal.rcp.JavaElementSelections.java

License:Open Source License

/**
 * Returns the {@link IJavaElement} at the given offset in the editor.
 *
 *//*w w w  . j a  v a  2s .  c  om*/
public static Optional<IJavaElement> resolveJavaElementFromEditor(final JavaEditor editor, final int offset) {
    ensureIsNotNull(editor);
    ITypeRoot root = findTypeRoot(editor).orNull();
    if (root != null && root.exists()) {
        return resolveJavaElementFromTypeRootInEditor(root, offset);
    }
    return absent();
}

From source file:org.eclipse.recommenders.internal.rcp.JavaElementSelections.java

License:Open Source License

private static boolean isInvalidSelection(ITypeRoot root, final int offset) {
    try {/*w ww . j a  v  a2 s. co  m*/
        if (!root.exists()) {
            return true;
        }
        // check whether the type root is part of an package fragment root. If not, it's an invalid selection and
        // all resolutions are likely to fail. Thus, return true (=invalid):
        IJavaElement ancestor = root.getAncestor(IJavaProject.PACKAGE_FRAGMENT_ROOT);
        if (!ancestor.exists()) {
            return true;
        }
        ISourceRange range = root.getSourceRange();
        return range == null || offset < 0 || offset > range.getLength();
    } catch (Exception e) {
        log(ERROR_EXCEPTION_WHILE_CHECKING_OFFSETS, e);
        return false;
    }
}