Search in sources :

Example 11 with JavadocTag

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

the class JavadocUtilTest 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 = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.ALL).getValidTags();
    assertWithMessage("Invalid first arg").that(tags.get(0).getFirstArg()).isEqualTo("List link text");
}
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 12 with JavadocTag

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

Example 13 with JavadocTag

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

the class JavaDocUtil method getJavadocTags.

/**
 * Gets validTags from a given piece of Javadoc.
 *
 * @param aCmt the Javadoc comment to process.
 * @param aTagType the type of validTags we're interested in
 * @return all standalone validTags from the given javadoc.
 */
public static JavadocTags getJavadocTags(TextBlock aCmt, JavadocTagType aTagType) {
    final String[] text = aCmt.getText();
    final List<JavadocTag> tags = Lists.newArrayList();
    final List<InvalidJavadocTag> invalidTags = Lists.newArrayList();
    Pattern blockTagPattern = Utils.createPattern("/\\*{2,}\\s*@(\\p{Alpha}+)\\s");
    for (int i = 0; i < text.length; i++) {
        final String s = text[i];
        final Matcher blockTagMatcher = blockTagPattern.matcher(s);
        if ((aTagType.equals(JavadocTagType.ALL) || aTagType.equals(JavadocTagType.BLOCK)) && blockTagMatcher.find()) {
            final String tagName = blockTagMatcher.group(1);
            String content = s.substring(blockTagMatcher.end(1));
            if (content.endsWith("*/")) {
                content = content.substring(0, content.length() - 2);
            }
            final int line = aCmt.getStartLineNo() + i;
            int col = blockTagMatcher.start(1) - 1;
            if (i == 0) {
                col += aCmt.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 (aTagType.equals(JavadocTagType.ALL) || aTagType.equals(JavadocTagType.INLINE)) {
            // Match JavaDoc text after comment characters
            final Pattern commentPattern = Utils.createPattern("^\\s*(?:/\\*{2,}|\\*+)\\s*(.*)");
            final Matcher commentMatcher = commentPattern.matcher(s);
            final String commentContents;
            // offset including comment characters
            final int commentOffset;
            if (!commentMatcher.find()) {
                // No leading asterisks, still valid
                commentContents = s;
                commentOffset = 0;
            } else {
                commentContents = commentMatcher.group(1);
                commentOffset = commentMatcher.start(1) - 1;
            }
            // The last '}' may
            final Pattern tagPattern = Utils.createPattern(".*?\\{@(\\p{Alpha}+)\\s+([^\\}]*)");
            // appear on the next
            // line ...
            final Matcher tagMatcher = tagPattern.matcher(commentContents);
            while (tagMatcher.find()) {
                if (tagMatcher.groupCount() == 2) {
                    final String tagName = tagMatcher.group(1);
                    final String tagValue = tagMatcher.group(2).trim();
                    final int line = aCmt.getStartLineNo() + i;
                    int col = commentOffset + (tagMatcher.start(1) - 1);
                    if (i == 0) {
                        col += aCmt.getStartColNo();
                    }
                    if (JavadocTagInfo.isValidName(tagName)) {
                        tags.add(new JavadocTag(line, col, tagName, tagValue));
                    } else {
                        invalidTags.add(new InvalidJavadocTag(line, col, tagName));
                    }
                }
            // else Error: Unexpected match count for inline JavaDoc tag
            }
        }
        blockTagPattern = Utils.createPattern("^\\s*\\**\\s*@(\\p{Alpha}+)\\s");
    }
    return new JavadocTags(tags, invalidTags);
}
Also used : Pattern(java.util.regex.Pattern) 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) JavadocTags(com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTags)

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