Search in sources :

Example 6 with JavadocTag

use of com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag 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 7 with JavadocTag

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

the class JavadocUtilsTest method testInlineTagLinkText.

@Test
public void testInlineTagLinkText() {
    final String[] text = { "/** {@link List link text }" };
    final Comment comment = new Comment(text, 1, 1, text[0].length());
    final List<JavadocTag> tags = JavadocUtils.getJavadocTags(comment, JavadocUtils.JavadocTagType.ALL).getValidTags();
    assertEquals("List link text", tags.get(0).getFirstArg());
}
Also used : Comment(com.puppycrawl.tools.checkstyle.api.Comment) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) Test(org.junit.Test)

Example 8 with JavadocTag

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

the class JavadocUtilsTest method testTagPositions.

@Test
public void testTagPositions() {
    final String[] text = { "/** @see elsewhere", "    also {@link Name value} */" };
    final Comment comment = new Comment(text, 1, 2, text[1].length());
    final List<JavadocTag> tags = JavadocUtils.getJavadocTags(comment, JavadocUtils.JavadocTagType.ALL).getValidTags();
    assertEquals(2, tags.size());
    for (final JavadocTag tag : tags) {
        if (JavadocTagInfo.SEE.getName().equals(tag.getTagName())) {
            assertEquals(1, tag.getLineNo());
            assertEquals(5, tag.getColumnNo());
        } else if (JavadocTagInfo.LINK.getName().equals(tag.getTagName())) {
            assertEquals(2, tag.getLineNo());
            assertEquals(10, tag.getColumnNo());
        } else {
            fail("Unexpected tag: " + tag);
        }
    }
}
Also used : Comment(com.puppycrawl.tools.checkstyle.api.Comment) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) Test(org.junit.Test)

Example 9 with JavadocTag

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

the class JavadocUtilTest method testInlineTagMethodRef.

@Test
public void testInlineTagMethodRef() {
    final String[] text = { "/** {@link List#add(Object)}" };
    final Comment comment = new Comment(text, 1, 1, text[0].length());
    final List<JavadocTag> tags = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.ALL).getValidTags();
    assertWithMessage("Invalid first arg").that(tags.get(0).getFirstArg()).isEqualTo("List#add(Object)");
}
Also used : Comment(com.puppycrawl.tools.checkstyle.api.Comment) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) Test(org.junit.jupiter.api.Test)

Example 10 with JavadocTag

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

the class JavadocUtilTest method testTagPositions.

@Test
public void testTagPositions() {
    final String[] text = { "/** @see elsewhere", "    also {@link Name value} */" };
    final Comment comment = new Comment(text, 1, 2, text[1].length());
    final List<JavadocTag> tags = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.ALL).getValidTags();
    assertWithMessage("Invalid tags size").that(tags).hasSize(2);
    final JavadocTag seeTag = tags.get(0);
    assertWithMessage("Invalid tag name").that(seeTag.getTagName()).isEqualTo(JavadocTagInfo.SEE.getName());
    assertWithMessage("Invalid line number").that(seeTag.getLineNo()).isEqualTo(1);
    assertWithMessage("Invalid column number").that(seeTag.getColumnNo()).isEqualTo(4);
    final JavadocTag linkTag = tags.get(1);
    assertWithMessage("Invalid tag name").that(linkTag.getTagName()).isEqualTo(JavadocTagInfo.LINK.getName());
    assertWithMessage("Invalid line number").that(linkTag.getLineNo()).isEqualTo(2);
    assertWithMessage("Invalid column number").that(linkTag.getColumnNo()).isEqualTo(10);
}
Also used : Comment(com.puppycrawl.tools.checkstyle.api.Comment) InvalidJavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag) JavadocTag(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag) Test(org.junit.jupiter.api.Test)

Aggregations

JavadocTag (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag)13 InvalidJavadocTag (com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag)10 Comment (com.puppycrawl.tools.checkstyle.api.Comment)8 JavadocTags (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTags)5 Test (org.junit.jupiter.api.Test)5 Matcher (java.util.regex.Matcher)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 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