Search in sources :

Example 1 with DocCommentTree

use of com.sun.source.doctree.DocCommentTree in project cxf by apache.

the class DumpJavaDoc method run.

@Override
public boolean run(DocletEnvironment docEnv) {
    final Elements utils = docEnv.getElementUtils();
    final DocTrees docTrees = docEnv.getDocTrees();
    try (OutputStream os = Files.newOutputStream(Paths.get(dumpFileName))) {
        final Properties javaDocMap = new Properties();
        for (Element element : docEnv.getIncludedElements()) {
            if (element.getKind() == ElementKind.CLASS) {
                final TypeElement classDoc = (TypeElement) element;
                final DocCommentTree classCommentTree = docTrees.getDocCommentTree(classDoc);
                if (classCommentTree != null) {
                    javaDocMap.put(classDoc.toString(), getAllComments(classCommentTree.getFullBody()));
                }
                for (Element member : classDoc.getEnclosedElements()) {
                    // Skip all non-public methods
                    if (!member.getModifiers().contains(Modifier.PUBLIC)) {
                        continue;
                    }
                    if (member.getKind() == ElementKind.METHOD) {
                        final ExecutableElement method = (ExecutableElement) member;
                        final DocCommentTree methodCommentTree = docTrees.getDocCommentTree(method);
                        final String qualifiedName = utils.getBinaryName(classDoc) + "." + method.getSimpleName();
                        if (methodCommentTree == null) {
                            javaDocMap.put(qualifiedName, "");
                        } else {
                            javaDocMap.put(qualifiedName, getAllComments(methodCommentTree.getFullBody()));
                            for (DocTree tree : methodCommentTree.getBlockTags()) {
                                if (tree.getKind() == DocTree.Kind.RETURN) {
                                    final ReturnTree returnTree = (ReturnTree) tree;
                                    javaDocMap.put(qualifiedName + ".returnCommentTag", getAllComments(returnTree.getDescription()));
                                } else if (tree.getKind() == DocTree.Kind.PARAM) {
                                    final ParamTree paramTree = (ParamTree) tree;
                                    final int index = getParamIndex(method, paramTree);
                                    if (index >= 0) {
                                        javaDocMap.put(qualifiedName + ".paramCommentTag." + index, getAllComments(paramTree.getDescription()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        javaDocMap.store(os, "");
        os.flush();
    } catch (final IOException ex) {
        reporter.print(Diagnostic.Kind.ERROR, ex.getMessage());
    }
    return true;
}
Also used : ParamTree(com.sun.source.doctree.ParamTree) TypeElement(javax.lang.model.element.TypeElement) OutputStream(java.io.OutputStream) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnTree(com.sun.source.doctree.ReturnTree) IOException(java.io.IOException) Elements(javax.lang.model.util.Elements) Properties(java.util.Properties) DocCommentTree(com.sun.source.doctree.DocCommentTree) DocTrees(com.sun.source.util.DocTrees) DocTree(com.sun.source.doctree.DocTree)

Example 2 with DocCommentTree

use of com.sun.source.doctree.DocCommentTree in project j2objc by google.

the class JavadocConverter method convertJavadoc.

/**
 * Returns an AST node for the javadoc comment of a specified class, method, or field element.
 */
static Javadoc convertJavadoc(TreePath path, String source, JavacEnvironment env, boolean reportWarnings) {
    DocTrees docTrees = DocTrees.instance(env.task());
    DocCommentTree docComment = docTrees.getDocCommentTree(path);
    if (docComment == null) {
        // Declaration does not have a javadoc comment.
        return null;
    }
    JavadocConverter converter = new JavadocConverter(env.treeUtilities().getElement(path), docComment, source, docTrees, path.getCompilationUnit(), reportWarnings);
    Javadoc result = new Javadoc();
    // First tag is the description.
    TagElement newTag = new TagElement().setTagKind(TagElement.TagKind.DESCRIPTION);
    converter.scan(docComment.getFirstSentence(), newTag);
    converter.scan(docComment.getBody(), newTag);
    if (!newTag.getFragments().isEmpty()) {
        List<TreeNode> fragments = newTag.getFragments();
        int start = fragments.get(0).getStartPosition();
        TreeNode lastFragment = fragments.get(fragments.size() - 1);
        int end = start + lastFragment.getLength();
        converter.setPos(newTag, start, end);
        result.addTag(newTag);
    }
    for (DocTree tag : docComment.getBlockTags()) {
        if (tag.getKind() != DocTree.Kind.ERRONEOUS) {
            newTag = new TagElement();
            converter.scan(tag, newTag);
            result.addTag(newTag);
        }
    }
    return result;
}
Also used : DocCommentTree(com.sun.source.doctree.DocCommentTree) DocTrees(com.sun.source.util.DocTrees) TreeNode(com.google.devtools.j2objc.ast.TreeNode) Javadoc(com.google.devtools.j2objc.ast.Javadoc) TagElement(com.google.devtools.j2objc.ast.TagElement) DocTree(com.sun.source.doctree.DocTree)

Aggregations

DocCommentTree (com.sun.source.doctree.DocCommentTree)2 DocTree (com.sun.source.doctree.DocTree)2 DocTrees (com.sun.source.util.DocTrees)2 Javadoc (com.google.devtools.j2objc.ast.Javadoc)1 TagElement (com.google.devtools.j2objc.ast.TagElement)1 TreeNode (com.google.devtools.j2objc.ast.TreeNode)1 ParamTree (com.sun.source.doctree.ParamTree)1 ReturnTree (com.sun.source.doctree.ReturnTree)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Properties (java.util.Properties)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 Elements (javax.lang.model.util.Elements)1