com.sun.source.doctree.ParamTree Java Examples

The following examples show how to use com.sun.source.doctree.ParamTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MethodDocumentation.java    From spring-auto-restdocs with Apache License 2.0 6 votes vote down vote up
public static MethodDocumentation fromMethodDoc(DocletEnvironment docEnv,
        Element methodElement) {
    MethodDocumentation md = new MethodDocumentation();
    md.comment = cleanupDocComment(docEnv.getElementUtils().getDocComment(methodElement));

    Optional.ofNullable(docEnv.getDocTrees().getDocCommentTree(methodElement))
            .ifPresent(docCommentTree -> docCommentTree.getBlockTags().forEach(tag -> {
                if (tag.getKind().equals(DocTree.Kind.PARAM)) {
                    ParamTree paramTag = (ParamTree) tag;
                    md.parameters.put(paramTag.getName().toString(),
                            unescapeJava(paramTag.getDescription().toString()));
                } else if (tag instanceof BlockTagTree) {
                    md.tags.put(
                            cleanupTagName(((BlockTagTree) tag).getTagName()),
                            unescapeJava(cleanupTagValue(tag.toString())));
                }
            }));

    return md;
}
 
Example #2
Source File: VeryPretty.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitParam(ParamTree node, Void p) {
    printTagName(node);
    needSpace();
    if(node.isTypeParameter()) {
       print('<');
    }
    doAccept((DCTree)node.getName());
    if(node.isTypeParameter()) {
       print('>');
    }
    if(!node.getDescription().isEmpty()) {
        needSpace();
    }
    for (DocTree docTree : node.getDescription()) {
        doAccept((DCTree)docTree);
    }
    return null;
}
 
Example #3
Source File: JavadocImports.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static Element paramElementFor(Element methodOrClass, ParamTree ptag) {
    ElementKind kind = methodOrClass.getKind();
    List<? extends Element> params = Collections.emptyList();
    if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
        ExecutableElement ee = (ExecutableElement) methodOrClass;
        params = ptag.isTypeParameter()
                ? ee.getTypeParameters()
                : ee.getParameters();
    } else if (kind.isClass() || kind.isInterface()) {
        TypeElement te = (TypeElement) methodOrClass;
        params = te.getTypeParameters();
    }

    for (Element param : params) {
        if (param.getSimpleName().contentEquals(ptag.getName().getName())) {
            return param;
        }
    }
    return null;
}
 
Example #4
Source File: Analyzer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NbBundle.Messages({"# {0} - @param name", "UNKNOWN_TYPEPARAM_DESC=Unknown @param: {0}",
                    "# {0} - @param name", "DUPLICATE_PARAM_DESC=Duplicate @param name: {0}"})
private void checkParamDeclared(ParamTree tree, List<? extends Element> list,
        DocTreePathHandle dtph, int start, int end, List<ErrorDescription> errors) {
    Name name = tree.getName().getName();
    boolean found = false;
    for (Element e: list) {
        if(ctx.isCanceled()) { return; }
        if (name.equals(e.getSimpleName())) {
            if(!foundParams.add(e)) {
                errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, DUPLICATE_PARAM_DESC(name), new RemoveTagFix(dtph, "@param").toEditorFix())); // NOI18N
            }
            found = true;
        }
    }
    if (!found) {
        errors.add(ErrorDescriptionFactory.forSpan(ctx, start, end, UNKNOWN_TYPEPARAM_DESC(name), new RemoveTagFix(dtph, "@param").toEditorFix())); //NOI18N
    }
}
 
Example #5
Source File: DumpJavaDoc.java    From cxf with Apache License 2.0 5 votes vote down vote up
private int getParamIndex(final ExecutableElement method, final ParamTree paramTree) {
    final List<? extends VariableElement> parameters = method.getParameters();
    
    for (int i = 0; i < parameters.size(); ++i) {
        if (paramTree.getName().getName().contentEquals(parameters.get(i).getSimpleName())) {
            return i;
        }
    } 
    
    return -1;
}
 
Example #6
Source File: Checker.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
    boolean typaram = tree.isTypeParameter();
    IdentifierTree nameTree = tree.getName();
    Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;

    if (paramElement == null) {
        switch (env.currElement.getKind()) {
            case CLASS: case INTERFACE: {
                if (!typaram) {
                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                    break;
                }
            }
            case METHOD: case CONSTRUCTOR: {
                env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
                break;
            }

            default:
                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
    } else {
        foundParams.add(paramElement);
    }

    warnIfEmpty(tree, tree.getDescription());
    return super.visitParam(tree, ignore);
}
 
Example #7
Source File: JavadocConverter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitParam(ParamTree node, TagElement tag) {
  IdentifierTree identifier = node.getName();
  if (identifier == null || node.isTypeParameter()) {
    return null;
  }
  List<? extends VariableElement> params = element instanceof ExecutableElement
      ? ((ExecutableElement) element).getParameters() : Collections.emptyList();
      tag.setTagKind(TagElement.TagKind.PARAM);
  String name = identifier.toString();
  VariableElement param = null;
  for (VariableElement p : params) {
    if (name.equals(p.getSimpleName().toString())) {
      param = p;
      break;
    }
  }
  // param will be null if the @param tag refers to a nonexistent parameter.
  TreeNode nameNode = param != null ? new SimpleName(param) : new SimpleName(name);
  setPos(identifier, nameNode);
  tag.addFragment(nameNode);
  scan(node.getDescription(), tag);
  int lastEnd = nameNode.getStartPosition();
  for (TreeNode fragment : tag.getFragments()) {
    // Fix up positions to match JDT's.
    // TODO(tball): remove and fix JavadocGenerator after javac switch.
    if (fragment.getKind() == TreeNode.Kind.TEXT_ELEMENT) {
      TextElement text = (TextElement) fragment;
      text.setText(" " + text.getText());
      text.setSourceRange(text.getStartPosition(), text.getLength() + 1);
    }
    int thisEnd = lastEnd + fragment.getLength();
    setPos(fragment, lastEnd, thisEnd);
    lastEnd = thisEnd;
  }
  setPos(tag, pos(node), endPos(node));
  tag.setLineNumber(nameNode.getLineNumber());
  return null;
}
 
Example #8
Source File: Checker.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
    boolean typaram = tree.isTypeParameter();
    IdentifierTree nameTree = tree.getName();
    Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;

    if (paramElement == null) {
        switch (env.currElement.getKind()) {
            case CLASS: case INTERFACE: {
                if (!typaram) {
                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                    break;
                }
            }
            case METHOD: case CONSTRUCTOR: {
                env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
                break;
            }

            default:
                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
    } else {
        foundParams.add(paramElement);
    }

    warnIfEmpty(tree, tree.getDescription());
    return super.visitParam(tree, ignore);
}
 
Example #9
Source File: Checker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
    boolean typaram = tree.isTypeParameter();
    IdentifierTree nameTree = tree.getName();
    Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;

    if (paramElement == null) {
        switch (env.currElement.getKind()) {
            case CLASS: case INTERFACE: {
                if (!typaram) {
                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                    break;
                }
            }
            case METHOD: case CONSTRUCTOR: {
                env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
                break;
            }

            default:
                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
    } else {
        foundParams.add(paramElement);
    }

    warnIfEmpty(tree, tree.getDescription());
    return super.visitParam(tree, ignore);
}
 
Example #10
Source File: Checker.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
    boolean typaram = tree.isTypeParameter();
    IdentifierTree nameTree = tree.getName();
    Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;

    if (paramElement == null) {
        switch (env.currElement.getKind()) {
            case CLASS: case INTERFACE: {
                if (!typaram) {
                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                    break;
                }
            }
            case METHOD: case CONSTRUCTOR: {
                env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
                break;
            }

            default:
                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
    } else {
        foundParams.add(paramElement);
    }

    warnIfEmpty(tree, tree.getDescription());
    return super.visitParam(tree, ignore);
}
 
Example #11
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private  List<? extends DocTree> getParamTrees(Element element, boolean isTypeParameters) {
    List<DocTree> out = new ArrayList<>();
    for (DocTree dt : getBlockTags(element, PARAM)) {
        ParamTree pt = (ParamTree) dt;
        if (pt.isTypeParameter() == isTypeParameters) {
            out.add(dt);
        }
    }
    return out;
}
 
Example #12
Source File: CommentHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public IdentifierTree getName(DocTree dtree) {
    switch (dtree.getKind()) {
        case PARAM:
            return ((ParamTree)dtree).getName();
        case SERIAL_FIELD:
            return ((SerialFieldTree)dtree).getName();
        default:
            return null;
        }
}
 
Example #13
Source File: CommentHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public String getParameterName(DocTree dtree) {
    if (dtree.getKind() == PARAM) {
        return ((ParamTree) dtree).getName().toString();
    } else {
        return null;
    }
}
 
Example #14
Source File: Checker.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
    boolean typaram = tree.isTypeParameter();
    IdentifierTree nameTree = tree.getName();
    Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;

    if (paramElement == null) {
        switch (env.currElement.getKind()) {
            case CLASS: case INTERFACE: {
                if (!typaram) {
                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                    break;
                }
            }
            case METHOD: case CONSTRUCTOR: {
                env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
                break;
            }

            default:
                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
    } else {
        boolean unique = foundParams.add(paramElement);

        if (!unique) {
            env.messages.warning(REFERENCE, tree, "dc.exists.param", nameTree);
        }
    }

    warnIfEmpty(tree, tree.getDescription());
    return super.visitParam(tree, ignore);
}
 
Example #15
Source File: Checker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
    boolean typaram = tree.isTypeParameter();
    IdentifierTree nameTree = tree.getName();
    Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;

    if (paramElement == null) {
        switch (env.currElement.getKind()) {
            case CLASS: case INTERFACE: {
                if (!typaram) {
                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                    break;
                }
            }
            case METHOD: case CONSTRUCTOR: {
                env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
                break;
            }

            default:
                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
    } else {
        foundParams.add(paramElement);
    }

    warnIfEmpty(tree, tree.getDescription());
    return super.visitParam(tree, ignore);
}
 
Example #16
Source File: Checker.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
    boolean typaram = tree.isTypeParameter();
    IdentifierTree nameTree = tree.getName();
    Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;

    if (paramElement == null) {
        switch (env.currElement.getKind()) {
            case CLASS: case INTERFACE: {
                if (!typaram) {
                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                    break;
                }
            }
            case METHOD: case CONSTRUCTOR: {
                env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
                break;
            }

            default:
                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
    } else {
        foundParams.add(paramElement);
    }

    warnIfEmpty(tree, tree.getDescription());
    return super.visitParam(tree, ignore);
}
 
Example #17
Source File: Checker.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("fallthrough")
public Void visitParam(ParamTree tree, Void ignore) {
    boolean typaram = tree.isTypeParameter();
    IdentifierTree nameTree = tree.getName();
    Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;

    if (paramElement == null) {
        switch (env.currElement.getKind()) {
            case CLASS: case INTERFACE: {
                if (!typaram) {
                    env.messages.error(REFERENCE, tree, "dc.invalid.param");
                    break;
                }
            }
            case METHOD: case CONSTRUCTOR: {
                env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
                break;
            }

            default:
                env.messages.error(REFERENCE, tree, "dc.invalid.param");
                break;
        }
    } else {
        foundParams.add(paramElement);
    }

    warnIfEmpty(tree, tree.getDescription());
    return super.visitParam(tree, ignore);
}
 
Example #18
Source File: ImmutableDocTreeTranslator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected final ParamTree rewriteChildren(ParamTree tree) {
    ParamTree value = tree;
    IdentifierTree name = (IdentifierTree) translate(tree.getName());
    List<? extends DocTree> description = translateDoc(tree.getDescription());
    if (name != tree.getName() || description != tree.getDescription()) {
        value = make.Param(tree.isTypeParameter(), name, description);
    }
    return value;
}
 
Example #19
Source File: JavadocCompletionQuery.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean containsParam(List<? extends DocTree> blockTags, String name) {
    for (DocTree blockTag : blockTags) {
        if(blockTag.getKind() == Kind.PARAM) {
            ParamTree param = (ParamTree) blockTag;
            if(name.contentEquals(param.getName().getName())) {
                return true;
            }
        }
    }
    return false;
}
 
Example #20
Source File: CommentHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public boolean isTypeParameter(DocTree dtree) {
    if (dtree.getKind() == PARAM) {
        return ((ParamTree)dtree).isTypeParameter();
    }
    return false;
}
 
Example #21
Source File: DumpJavaDoc.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example #22
Source File: ImmutableDocTreeTranslator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public DocTree visitParam(ParamTree tree, Object p) {
    return rewriteChildren(tree);
}
 
Example #23
Source File: TreeFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public ParamTree Param(boolean isTypeParameter, com.sun.source.doctree.IdentifierTree name, List<? extends DocTree> description) {
    return docMake.at(NOPOS).newParamTree(isTypeParameter, name, description);
}
 
Example #24
Source File: DoctreeTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testAddDocComment() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
        "package hierbas.del.litoral;\n" +
        "\n" +
        "public class Test {\n" +
        "\n" +
        "    private void test() {\n" +
        "    }\n" +
        "}\n");
    String golden =
        "package hierbas.del.litoral;\n" +
        "\n" +
        "public class Test {\n" +
        "\n" +
        "    /**\n" +
        "     * Test method\n" +
        "     * @param test\n" +
        "     */\n" +
        "    private void test() {\n" +
        "    }\n" +
        "}\n";

    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {
        @Override
        public void run(final WorkingCopy wc) throws IOException {
            wc.toPhase(JavaSource.Phase.RESOLVED);
            final TreeMaker make = wc.getTreeMaker();
            new ErrorAwareTreePathScanner<Void, Void>() {
                @Override
                public Void visitMethod(final MethodTree mt, Void p) {
                    ParamTree param = make.Param(false, make.DocIdentifier("test"), new LinkedList<DocTree>());
                    DocCommentTree newDoc = make.DocComment(
                            Collections.singletonList(make.Text("Test method")),
                            Collections.EMPTY_LIST,
                            Collections.singletonList(param));
                    wc.rewrite(mt, null, newDoc);
                    return super.visitMethod(mt, p);
                }
            }.scan(wc.getCompilationUnit(), null);
        }
    };
    src.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    //System.err.println(res);
    assertEquals(golden, res);
}
 
Example #25
Source File: RefactoringVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * @since 1.47
 */
@Override
public DocTree visitParam(ParamTree node, Element p) {
    return docScanner.visitParam(node, p, null);
}
 
Example #26
Source File: RefactoringVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public DocTree visitParam(ParamTree node, Element p) {
    return instance.visitParam(node, p);
}
 
Example #27
Source File: JavadocFormatter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override public boolean matches(DocTree t) {
    return t.getKind() == DocTree.Kind.PARAM && !((ParamTree) t).isTypeParameter();
}
 
Example #28
Source File: JavadocFormatter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override public boolean matches(DocTree t) {
    return t.getKind() == DocTree.Kind.PARAM && ((ParamTree) t).isTypeParameter();
}
 
Example #29
Source File: JavadocFormatter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public Object visitParam(ParamTree node, Object p) {
    return formatDef(node.getName().getName(), node.getDescription());
}
 
Example #30
Source File: RefactoringVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public DocTree visitParam(ParamTree node, Element p, Void ignore) {
    return super.visitParam(node, p);
}