Java源码示例:com.github.javaparser.javadoc.Javadoc

示例1
private String buildOpMethodJavadoc(
    TypeElement opClass, ExecutableElement endpointMethod, boolean copyClassDescription) {
  Javadoc methodJavadoc = parseJavadoc(endpointMethod);
  if (!copyClassDescription) {
    return methodJavadoc.toText();
  }
  Javadoc classJavadoc = parseJavadoc(opClass);
  // Copy all endpoint method tags to the description, except for the `scope` parameter which
  // will be inferred by the Ops class
  methodJavadoc.getBlockTags().forEach(t -> {
    if (!t.getTagName().equals("param") || t.getName().map(s -> !s.equals("scope")).orElse(true)) {
      classJavadoc.addBlockTag(t);
    }
  });
  return classJavadoc.toText();
}
 
示例2
Optional<PackageAlias> read(PackageInfoSource packageInfoSource) {
    CompilationUnit cu = StaticJavaParser.parse(packageInfoSource.toInputStream());

    Optional<PackageIdentifier> optPackageIdentifier = cu.getPackageDeclaration()
            .map(NodeWithName::getNameAsString)
            .map(PackageIdentifier::new);

    Optional<Alias> optAlias = getJavadoc(cu)
            .map(Javadoc::getDescription)
            .map(JavadocDescription::toText)
            .map(JavadocAliasSource::new)
            .map(JavadocAliasSource::toAlias);

    return optPackageIdentifier.flatMap(packageIdentifier -> optAlias.map(alias ->
            new PackageAlias(packageIdentifier, alias)));
}
 
示例3
public String parseConfigDescription(String javadocComment) {
    if (javadocComment == null || javadocComment.trim().isEmpty()) {
        return Constants.EMPTY;
    }

    // the parser expects all the lines to start with "* "
    // we add it as it has been previously removed
    javadocComment = START_OF_LINE.matcher(javadocComment).replaceAll("* ");
    Javadoc javadoc = StaticJavaParser.parseJavadoc(javadocComment);

    if (isAsciidoc(javadoc)) {
        return handleEolInAsciidoc(javadoc);
    }

    return htmlJavadocToAsciidoc(javadoc.getDescription());
}
 
示例4
public SectionHolder parseConfigSection(String javadocComment, int sectionLevel) {
    if (javadocComment == null || javadocComment.trim().isEmpty()) {
        return new SectionHolder(Constants.EMPTY, Constants.EMPTY);
    }

    // the parser expects all the lines to start with "* "
    // we add it as it has been previously removed
    javadocComment = START_OF_LINE.matcher(javadocComment).replaceAll("* ");
    Javadoc javadoc = StaticJavaParser.parseJavadoc(javadocComment);

    if (isAsciidoc(javadoc)) {
        final String details = handleEolInAsciidoc(javadoc);
        final int endOfTitleIndex = details.indexOf(Constants.DOT);
        final String title = details.substring(0, endOfTitleIndex).replaceAll("^([^\\w])+", Constants.EMPTY).trim();
        return new SectionHolder(title, details);
    }

    return generateConfigSection(javadoc, sectionLevel);
}
 
示例5
private SectionHolder generateConfigSection(Javadoc javadoc, int sectionLevel) {
    final String generatedAsciiDoc = htmlJavadocToAsciidoc(javadoc.getDescription());
    if (generatedAsciiDoc.isEmpty()) {
        return new SectionHolder(Constants.EMPTY, Constants.EMPTY);
    }

    final String beginSectionDetails = IntStream
            .rangeClosed(0, Math.max(0, sectionLevel))
            .mapToObj(x -> "=").collect(Collectors.joining())
            + " ";

    final int endOfTitleIndex = generatedAsciiDoc.indexOf(Constants.DOT);
    if (endOfTitleIndex == -1) {
        return new SectionHolder(generatedAsciiDoc.trim(), beginSectionDetails + generatedAsciiDoc);
    } else {
        final String title = generatedAsciiDoc.substring(0, endOfTitleIndex).trim();
        final String introduction = generatedAsciiDoc.substring(endOfTitleIndex + 1).trim();
        final String details = beginSectionDetails + title + "\n\n" + introduction;

        return new SectionHolder(title, details.trim());
    }
}
 
示例6
private static Stream<String> parseJavadoc(Javadoc javadoc) {
  // parse main doc description
  Stream<String> stringsFromJavadocDescription =
      Stream.of(javadoc.getDescription()).flatMap(ImpSort::parseJavadocDescription);
  // grab tag names and parsed descriptions for block tags
  Stream<String> stringsFromBlockTags = javadoc.getBlockTags().stream().flatMap(tag -> {
    // only @throws and @exception have names who are importable; @param and others don't
    EnumSet<JavadocBlockTag.Type> blockTagTypesWithImportableNames =
        EnumSet.of(THROWS, EXCEPTION);
    Stream<String> importableTagNames = blockTagTypesWithImportableNames.contains(tag.getType())
        ? Stream.of(tag.getName()).filter(Optional::isPresent).map(Optional::get)
        : Stream.empty();
    Stream<String> tagDescriptions =
        Stream.of(tag.getContent()).flatMap(ImpSort::parseJavadocDescription);
    return Stream.concat(importableTagNames, tagDescriptions);
  });
  return Stream.concat(stringsFromJavadocDescription, stringsFromBlockTags);
}
 
示例7
private void parseClass(ClassOrInterfaceDeclaration classDeclaration,
        CompilationUnit compilationUnit) {
    Optional<AnnotationExpr> endpointAnnotation = classDeclaration
            .getAnnotationByClass(Endpoint.class);
    compilationUnit.getStorage().ifPresent(storage -> {
        String className = classDeclaration.getFullyQualifiedName()
                .orElse(classDeclaration.getNameAsString());
        qualifiedNameToPath.put(className, storage.getPath().toString());
    });
    if (!endpointAnnotation.isPresent()) {
        nonEndpointMap.put(classDeclaration.resolve().getQualifiedName(),
                classDeclaration);
    } else {
        Optional<Javadoc> javadoc = classDeclaration.getJavadoc();
        if (javadoc.isPresent()) {
            endpointsJavadoc.put(classDeclaration,
                    javadoc.get().getDescription().toText());
        } else {
            endpointsJavadoc.put(classDeclaration, "");
        }
        pathItems.putAll(createPathItems(
                getEndpointName(classDeclaration, endpointAnnotation.get()),
                classDeclaration));
    }
}
 
示例8
/**
 * Write the method parameters and it's doc to target api doc
 *
 * @param doc     target doc
 * @param comment parameters and method doc
 **/
private void fillParamDoc(APIDoc doc, JavadocComment comment, StringBuilder methodDesBuilder) {
    Javadoc javadoc = comment.parse();
    toString(javadoc.getDescription(), methodDesBuilder);
    doc.setDesc(methodDesBuilder.toString());
    methodDesBuilder.setLength(0);
    List<JavadocBlockTag> tags = javadoc.getBlockTags();
    if (comment.getCommentedNode().isPresent()) {
        Node node = comment.getCommentedNode().get();
        if (node instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) node;
            for (Parameter p : method.getParameters()) {
                String type = p.getType().asString();
                String name = p.getName().asString();
                List<Param> params = doc.getParams();
                Param param = new Param();
                param.setName(name);
                param.setType(type);
                for (JavadocBlockTag t : tags) {
                    if (t.getName().isPresent()) {
                        if (name.endsWith(t.getName().get())) {
                            toString(t.getContent(), methodDesBuilder);
                            param.setComment(methodDesBuilder.toString());
                            methodDesBuilder.setLength(0);
                        }
                    }
                }
                if (params == null) {
                    params = new ArrayList<>();
                    doc.setParams(params);
                }
                params.add(param);
            }
        }
    }
}
 
示例9
/**
 * Creates a javadoc comment object given a comment string.
 *
 * @param comment Comment.
 * @param indentation Indentation.
 * @return Javadoc comment object.
 */
public JavadocComment createJavadocComment(String comment, String indentation) {
  JavadocSnippet javadocSnippet = new JavadocSnippet("");
  if (StringUtils.isNotEmpty(comment)) {
    javadocSnippet = new JavadocSnippet(comment);
  }

  JavadocDescription description = new JavadocDescription();
  description.addElement(javadocSnippet);
  Javadoc javadoc = new Javadoc(description);
  return javadoc.toComment(indentation);
}
 
示例10
/**
 * 解析注释
 * @param comment
 */
public void accept(Comment comment) {
    if (!comment.isJavadocComment()) {
        this.setComment(comment.getContent());
        return;
    }
    Javadoc javadoc = comment.asJavadocComment().parse();
    this.setComment(CommentHelper.getDescription(javadoc.getDescription()));

    javadoc.getBlockTags().forEach(this::parse);
}
 
示例11
static Comment parseComment(Node node) {
    if (node.getComment().isPresent()) {
        Javadoc javadoc = JavaParser.parseJavadoc(node.getComment().get().getContent());
        Comment comment = new Comment();
        comment.setDescription(javadoc.getDescription().toText());

        List<JavadocBlockTag> javadocBlockTags = javadoc.getBlockTags();

        if (javadocBlockTags != null && javadocBlockTags.size() > 0) {
            List<Comment.Tag> tags = new ArrayList<Comment.Tag>();

            for (JavadocBlockTag javadocBlockTag : javadocBlockTags) {
                Comment.Tag tag = new Comment.Tag();
                tag.setTagName(javadocBlockTag.getTagName());
                tag.setName(javadocBlockTag.getName().orElse(null));
                tag.setContent(javadocBlockTag.getContent().toText());
                tags.add(tag);
            }

            comment.setTags(tags);
        }

        return comment;
    }

    return null;
}
 
示例12
private Optional<Javadoc> getJavadoc(CompilationUnit cu) {
    // NodeWithJavadoc#getJavadocでやってることと同じことをする
    return cu.getComment()
            .filter(comment -> comment instanceof JavadocComment)
            .map(comment -> (JavadocComment) comment)
            .map(JavadocComment::parse);
}
 
示例13
/**
 * Removes the parameter with the given name from the Javadoc of the given
 * method declaration
 * 
 * @param methodDeclaration
 * @param paramName
 */
private void removeParameterFromJavadoc(MethodDeclaration methodDeclaration, String paramName) {
	Optional<Javadoc> javadoc = methodDeclaration.getJavadoc();
	if (javadoc.isPresent()) {
		Javadoc methodJavadoc = javadoc.get();
		List<JavadocBlockTag> javadocTags = methodJavadoc.getBlockTags();

		for (Iterator<JavadocBlockTag> it = javadocTags.iterator(); it.hasNext();) {
			JavadocBlockTag javadocTag = it.next();
			Optional<String> javadocTagName = javadocTag.getName();
			boolean isEqualParamName = javadocTagName.isPresent() && javadocTagName.get().equals(paramName);
			boolean isParamType = javadocTag.getType().equals(JavadocBlockTag.Type.PARAM);
			if (isParamType && isEqualParamName) {
				it.remove();
			}
		}

		// create new Javadoc with remaining Javadoc tags because there is no way to
		// remove individual tags directly
		Javadoc newJavadoc = new Javadoc(methodJavadoc.getDescription());
		for (JavadocBlockTag blockTag : javadocTags) {
			newJavadoc.addBlockTag(blockTag);
		}

		methodDeclaration.setJavadocComment(newJavadoc);
	}
}
 
示例14
private String handleEolInAsciidoc(Javadoc javadoc) {
    // it's Asciidoc so we just pass through
    // it also uses platform specific EOL so we need to convert them back to \n
    String asciidoc = javadoc.getDescription().toText();
    asciidoc = REPLACE_WINDOWS_EOL.matcher(asciidoc).replaceAll("\n");
    asciidoc = REPLACE_MACOS_EOL.matcher(asciidoc).replaceAll("\n");
    return asciidoc;
}
 
示例15
private boolean isAsciidoc(Javadoc javadoc) {
    for (JavadocBlockTag blockTag : javadoc.getBlockTags()) {
        if ("asciidoclet".equals(blockTag.getTagName())) {
            return true;
        }
    }
    return false;
}
 
示例16
private void createFieldComment(Javadoc javadoc, FieldDeclaration field) {
    ClassComment classComment = classComments.get(className);
    if (classComment == null) {
        classComment = new ClassComment();
        classComments.put(className, classComment);
    }
    classComment.getFieldComments().add(createMemberParamTag(javadoc.getDescription(), field.getAnnotations().stream()));
}
 
示例17
private void recordMethodComment(Javadoc javadoc, MethodDeclaration method) {
    MethodIdentifier identifier = calculateMethodIdentifier(method);
    String comment = javadoc.getDescription().toText();
    List<MemberParameterTag> tags = createMethodParameterTags(javadoc, method);
    Map<Integer, String> responseComments = createResponseComments(javadoc);
    methodComments.put(identifier, new MethodComment(comment, tags, responseComments, classComments.get(className), isDeprecated(javadoc)));
}
 
示例18
private Map<Integer, String> createResponseComments(Javadoc javadoc) {
    return javadoc.getBlockTags().stream()
            .filter(t -> ResponseCommentExtractor.RESPONSE_TAG_NAME.equalsIgnoreCase(t.getTagName()))
            .map(t -> t.getContent().toText())
            .map(ResponseCommentExtractor::extract)
            .filter(Objects::nonNull)
            .collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
}
 
示例19
private Javadoc toJavaDoc(Comment comment) {
    return comment.asJavadocComment().parse();
}
 
示例20
private boolean isDeprecated(Javadoc javadoc) {
    return javadoc.getBlockTags().stream().anyMatch(t -> t.getType() == JavadocBlockTag.Type.DEPRECATED);
}
 
示例21
private void recordClassComment(Javadoc javadoc) {
    String comment = javadoc.getDescription().toText();
    Map<Integer, String> responseComments = createResponseComments(javadoc);
    classComments.put(className, new ClassComment(comment, responseComments, isDeprecated(javadoc)));
}
 
示例22
private List<MemberParameterTag> createMethodParameterTags(Javadoc javadoc, MethodDeclaration method) {
    return javadoc.getBlockTags().stream()
            .filter(t -> t.getType() == JavadocBlockTag.Type.PARAM)
            .map(t -> createMethodParameterTag(t, method))
            .collect(Collectors.toList());
}