Search in sources :

Example 1 with InvalidJavadocTag

use of com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag in project checkstyle by checkstyle.

the class JavadocUtils method getJavadocTags.

/**
     * Gets validTags from a given piece of Javadoc.
     * @param textBlock
     *        the Javadoc comment to process.
     * @param tagType
     *        the type of validTags we're interested in
     * @return all standalone validTags from the given javadoc.
     */
public static JavadocTags getJavadocTags(TextBlock textBlock, JavadocTagType tagType) {
    final String[] text = textBlock.getText();
    final List<JavadocTag> tags = new ArrayList<>();
    final List<InvalidJavadocTag> invalidTags = new ArrayList<>();
    for (int i = 0; i < text.length; i++) {
        final String textValue = text[i];
        final Matcher blockTagMatcher = getBlockTagPattern(i).matcher(textValue);
        if ((tagType == JavadocTagType.ALL || tagType == JavadocTagType.BLOCK) && blockTagMatcher.find()) {
            final String tagName = blockTagMatcher.group(1);
            String content = textValue.substring(blockTagMatcher.end(1));
            if (content.endsWith("*/")) {
                content = content.substring(0, content.length() - 2);
            }
            final int line = textBlock.getStartLineNo() + i;
            int col = blockTagMatcher.start(1) - 1;
            if (i == 0) {
                col += textBlock.getStartColNo();
            }
            if (JavadocTagInfo.isValidName(tagName)) {
                tags.add(new JavadocTag(line, col, tagName, content.trim()));
            } else {
                invalidTags.add(new InvalidJavadocTag(line, col, tagName));
            }
        } else // No block tag, so look for inline validTags
        if (tagType == JavadocTagType.ALL || tagType == JavadocTagType.INLINE) {
            lookForInlineTags(textBlock, i, tags, invalidTags);
        }
    }
    return new JavadocTags(tags, invalidTags);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) JavadocTags(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTags)

Example 2 with InvalidJavadocTag

use of com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag in project debezium by debezium.

the class UnusedImports method processJavaDocLinkParameters.

protected void processJavaDocLinkParameters(DetailAST aAST) {
    final FileContents contents = getFileContents();
    final int lineNo = aAST.getLineNo();
    final TextBlock cmt = contents.getJavadocBefore(lineNo);
    if (cmt != null) {
        final JavadocTags tags = JavaDocUtil.getJavadocTags(cmt, JavadocUtils.JavadocTagType.ALL);
        for (final JavadocTag tag : tags.getValidTags()) {
            processJavaDocTag(tag);
        }
        for (final InvalidJavadocTag tag : tags.getInvalidTags()) {
            log(tag.getLine(), tag.getCol(), "import.invalidJavaDocTag", tag.getName());
        }
    }
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) TextBlock(com.puppycrawl.tools.checkstyle.api.TextBlock) JavadocTags(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTags)

Example 3 with InvalidJavadocTag

use of com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag in project checkstyle by checkstyle.

the class JavadocUtilTest method testInvalidTags.

@Test
public void testInvalidTags() {
    final String[] text = { "/** @fake block", " * {@bogus inline}", " * {@link List valid}" };
    final Comment comment = new Comment(text, 1, 3, text[2].length());
    final JavadocTags allTags = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.ALL);
    assertWithMessage("Unexpected invalid tags size").that(allTags.getInvalidTags()).hasSize(2);
    assertTag("Unexpected invalid tag", new InvalidJavadocTag(1, 4, "fake"), allTags.getInvalidTags().get(0));
    assertTag("Unexpected invalid tag", new InvalidJavadocTag(2, 4, "bogus"), allTags.getInvalidTags().get(1));
    assertWithMessage("Unexpected valid tags size").that(allTags.getValidTags()).hasSize(1);
    assertTag("Unexpected valid tag", new JavadocTag(3, 4, "link", "List valid"), allTags.getValidTags().get(0));
}
Also used : Comment(com.puppycrawl.tools.checkstyle.api.Comment) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) JavadocTags(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTags) Test(org.junit.jupiter.api.Test)

Example 4 with InvalidJavadocTag

use of com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag in project checkstyle by checkstyle.

the class JavadocUtils method lookForInlineTags.

/**
     * Looks for inline tags in comment and adds them to the proper tags collection.
     * @param comment comment text block
     * @param lineNumber line number in the comment
     * @param validTags collection of valid tags
     * @param invalidTags collection of invalid tags
     */
private static void lookForInlineTags(TextBlock comment, int lineNumber, final List<JavadocTag> validTags, final List<InvalidJavadocTag> invalidTags) {
    final String text = comment.getText()[lineNumber];
    // Match Javadoc text after comment characters
    final Matcher commentMatcher = COMMENT_PATTERN.matcher(text);
    final String commentContents;
    // offset including comment characters
    final int commentOffset;
    if (commentMatcher.find()) {
        commentContents = commentMatcher.group(1);
        commentOffset = commentMatcher.start(1) - 1;
    } else {
        // No leading asterisks, still valid
        commentContents = text;
        commentOffset = 0;
    }
    final Matcher tagMatcher = INLINE_TAG_PATTERN.matcher(commentContents);
    while (tagMatcher.find()) {
        final String tagName = tagMatcher.group(1);
        final String tagValue = tagMatcher.group(2).trim();
        final int line = comment.getStartLineNo() + lineNumber;
        int col = commentOffset + tagMatcher.start(1) - 1;
        if (lineNumber == 0) {
            col += comment.getStartColNo();
        }
        if (JavadocTagInfo.isValidName(tagName)) {
            validTags.add(new JavadocTag(line, col, tagName, tagValue));
        } else {
            invalidTags.add(new InvalidJavadocTag(line, col, tagName));
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag)

Example 5 with InvalidJavadocTag

use of com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag in project checkstyle by checkstyle.

the class JavadocUtil method getJavadocTags.

/**
 * Gets validTags from a given piece of Javadoc.
 *
 * @param textBlock
 *        the Javadoc comment to process.
 * @param tagType
 *        the type of validTags we're interested in
 * @return all standalone validTags from the given javadoc.
 */
public static JavadocTags getJavadocTags(TextBlock textBlock, JavadocTagType tagType) {
    final boolean getBlockTags = tagType == JavadocTagType.ALL || tagType == JavadocTagType.BLOCK;
    final boolean getInlineTags = tagType == JavadocTagType.ALL || tagType == JavadocTagType.INLINE;
    final List<TagInfo> tags = new ArrayList<>();
    if (getBlockTags) {
        tags.addAll(BlockTagUtil.extractBlockTags(textBlock.getText()));
    }
    if (getInlineTags) {
        tags.addAll(InlineTagUtil.extractInlineTags(textBlock.getText()));
    }
    final List<JavadocTag> validTags = new ArrayList<>();
    final List<InvalidJavadocTag> invalidTags = new ArrayList<>();
    for (TagInfo tag : tags) {
        final int col = tag.getPosition().getColumn();
        // Add the starting line of the comment to the line number to get the actual line number
        // in the source.
        // Lines are one-indexed, so need a off-by-one correction.
        final int line = textBlock.getStartLineNo() + tag.getPosition().getLine() - 1;
        if (JavadocTagInfo.isValidName(tag.getName())) {
            validTags.add(new JavadocTag(line, col, tag.getName(), tag.getValue()));
        } else {
            invalidTags.add(new InvalidJavadocTag(line, col, tag.getName()));
        }
    }
    return new JavadocTags(validTags, invalidTags);
}
Also used : JavadocTagInfo(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTagInfo) TagInfo(com.puppycrawl.tools.checkstyle.checks.javadoc.utils.TagInfo) ArrayList(java.util.ArrayList) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) JavadocTags(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTags)

Aggregations

InvalidJavadocTag (com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag)6 JavadocTag (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag)6 JavadocTags (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTags)5 Matcher (java.util.regex.Matcher)3 ArrayList (java.util.ArrayList)2 Comment (com.puppycrawl.tools.checkstyle.api.Comment)1 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)1 TextBlock (com.puppycrawl.tools.checkstyle.api.TextBlock)1 JavadocTagInfo (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTagInfo)1 TagInfo (com.puppycrawl.tools.checkstyle.checks.javadoc.utils.TagInfo)1 Pattern (java.util.regex.Pattern)1 Test (org.junit.jupiter.api.Test)1