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

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

Introduction

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

Prototype

String getSource() throws JavaModelException;

Source Link

Document

Returns the source code associated with this element.

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./*from   w  ww .j a  v  a 2  s .  co 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:ca.uvic.chisel.javasketch.internal.ast.ASTResolver.java

License:Open Source License

/**
 * Connects an AST resolver to /*from   ww  w . j a  va2  s.c  om*/
 * @param modelElement
 * @return
 */
public ASTResolver connect(ITraceModel modelElement, IProgressMonitor monitor) {
    try {
        IJavaElement element = JavaSearchUtils.findElement(modelElement, monitor);
        if (element instanceof IMember) {
            IMember member = (IMember) element;
            ITypeRoot root = member.getTypeRoot();
            synchronized (cache) {
                ASTResolver resolver = cache.get(root);
                if (resolver == null) {
                    String source = root.getSource();
                    if (source != null) {
                        IDocument document = new Document(source);
                        ASTNode node = ASTUTils.getASTFor(root);
                        if (node != null) {
                            resolver = new ASTResolver(typeRoot, rootNode, document);
                            cache.put(root, resolver);
                        }
                    }
                }
                resolver.connectionCount++;
                return resolver;
            }
        }
    } catch (JavaModelException e) {
    } catch (InterruptedException e) {
    } catch (CoreException e) {
    }
    return null;
}

From source file:ca.uvic.chisel.javasketch.internal.ast.groups.ASTLoopGroupCalculator.java

License:Open Source License

private static IDocument getDocumentFor(IJavaElement element) {
    try {/* w w w . j  a  v a 2s .  com*/
        if (element instanceof IMethod) {

            IMethod method = (IMethod) element;
            String source = null;
            ITypeRoot root = method.getTypeRoot();

            source = root.getSource();
            if (source != null) {
                return new Document(source);
            }
        }
    } catch (CoreException e) {
        SketchPlugin.getDefault().log(e);
    }
    return null;
}

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./* www . j  av  a2  s  . co 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;
}