001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2025 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.javadoc;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Optional;
025import java.util.function.Function;
026import java.util.regex.Pattern;
027import java.util.stream.Stream;
028
029import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
030import com.puppycrawl.tools.checkstyle.api.DetailNode;
031import com.puppycrawl.tools.checkstyle.api.JavadocCommentsTokenTypes;
032import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
033import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
034
035/**
036 * <div>
037 * Checks that
038 * <a href="https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html#firstsentence">
039 * Javadoc summary sentence</a> does not contain phrases that are not recommended to use.
040 * Summaries that contain only the {@code {@inheritDoc}} tag are skipped.
041 * Summaries that contain a non-empty {@code {@return}} are allowed.
042 * Check also violate Javadoc that does not contain first sentence, though with {@code {@return}} a
043 * period is not required as the Javadoc tool adds it.
044 * </div>
045 *
046 * <p>
047 * Note: For defining a summary, both the first sentence and the @summary tag approaches
048 * are supported.
049 * </p>
050 *
051 * @since 6.0
052 */
053@FileStatefulCheck
054public class SummaryJavadocCheck extends AbstractJavadocCheck {
055
056    /**
057     * A key is pointing to the warning message text in "messages.properties"
058     * file.
059     */
060    public static final String MSG_SUMMARY_FIRST_SENTENCE = "summary.first.sentence";
061
062    /**
063     * A key is pointing to the warning message text in "messages.properties"
064     * file.
065     */
066    public static final String MSG_SUMMARY_JAVADOC = "summary.javaDoc";
067
068    /**
069     * A key is pointing to the warning message text in "messages.properties"
070     * file.
071     */
072    public static final String MSG_SUMMARY_JAVADOC_MISSING = "summary.javaDoc.missing";
073
074    /**
075     * A key is pointing to the warning message text in "messages.properties" file.
076     */
077    public static final String MSG_SUMMARY_MISSING_PERIOD = "summary.javaDoc.missing.period";
078
079    /**
080     * This regexp is used to convert multiline javadoc to single-line without stars.
081     */
082    private static final Pattern JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN =
083            Pattern.compile("\n +(\\*)|^ +(\\*)");
084
085    /**
086     * This regexp is used to remove html tags, whitespace, and asterisks from a string.
087     */
088    private static final Pattern HTML_ELEMENTS =
089            Pattern.compile("<[^>]*>");
090
091    /** Default period literal. */
092    private static final String DEFAULT_PERIOD = ".";
093
094    /**
095     * Specify the regexp for forbidden summary fragments.
096     */
097    private Pattern forbiddenSummaryFragments = CommonUtil.createPattern("^$");
098
099    /**
100     * Specify the period symbol. Used to check the first sentence ends with a period. Periods that
101     * are not followed by a whitespace character are ignored (eg. the period in v1.0). Because some
102     * periods include whitespace built into the character, if this is set to a non-default value
103     * any period will end the sentence, whether it is followed by whitespace or not.
104     */
105    private String period = DEFAULT_PERIOD;
106
107    /**
108     * Whether to validate untagged summary text in Javadoc.
109     */
110    private boolean shouldValidateUntaggedSummary = true;
111
112    /**
113     * Setter to specify the regexp for forbidden summary fragments.
114     *
115     * @param pattern a pattern.
116     * @since 6.0
117     */
118    public void setForbiddenSummaryFragments(Pattern pattern) {
119        forbiddenSummaryFragments = pattern;
120    }
121
122    /**
123     * Setter to specify the period symbol. Used to check the first sentence ends with a period.
124     * Periods that are not followed by a whitespace character are ignored (eg. the period in v1.0).
125     * Because some periods include whitespace built into the character, if this is set to a
126     * non-default value any period will end the sentence, whether it is followed by whitespace or
127     * not.
128     *
129     * @param period period's value.
130     * @since 6.2
131     */
132    public void setPeriod(String period) {
133        this.period = period;
134    }
135
136    @Override
137    public int[] getDefaultJavadocTokens() {
138        return new int[] {
139            JavadocCommentsTokenTypes.JAVADOC_CONTENT,
140            JavadocCommentsTokenTypes.SUMMARY_INLINE_TAG,
141            JavadocCommentsTokenTypes.RETURN_INLINE_TAG,
142        };
143    }
144
145    @Override
146    public int[] getRequiredJavadocTokens() {
147        return getAcceptableJavadocTokens();
148    }
149
150    @Override
151    public void visitJavadocToken(DetailNode ast) {
152        if (isSummaryTag(ast) && isDefinedFirst(ast.getParent())) {
153            shouldValidateUntaggedSummary = false;
154            validateSummaryTag(ast);
155        }
156        else if (isInlineReturnTag(ast)) {
157            shouldValidateUntaggedSummary = false;
158            validateInlineReturnTag(ast);
159        }
160    }
161
162    @Override
163    public void leaveJavadocToken(DetailNode ast) {
164        if (ast.getType() == JavadocCommentsTokenTypes.JAVADOC_CONTENT) {
165            if (shouldValidateUntaggedSummary && !startsWithInheritDoc(ast)) {
166                validateUntaggedSummary(ast);
167            }
168            shouldValidateUntaggedSummary = true;
169        }
170    }
171
172    /**
173     * Checks the javadoc text for {@code period} at end and forbidden fragments.
174     *
175     * @param ast the javadoc text node
176     */
177    private void validateUntaggedSummary(DetailNode ast) {
178        final String summaryDoc = getSummarySentence(ast);
179        if (summaryDoc.isEmpty()) {
180            log(ast.getLineNumber(), MSG_SUMMARY_JAVADOC_MISSING);
181        }
182        else if (!period.isEmpty()) {
183            if (summaryDoc.contains(period)) {
184                final Optional<String> firstSentence = getFirstSentence(ast, period);
185
186                if (firstSentence.isPresent()) {
187                    if (containsForbiddenFragment(firstSentence.get())) {
188                        log(ast.getLineNumber(), MSG_SUMMARY_JAVADOC);
189                    }
190                }
191                else {
192                    log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
193                }
194            }
195            else {
196                log(ast.getLineNumber(), MSG_SUMMARY_FIRST_SENTENCE);
197            }
198        }
199    }
200
201    /**
202     * Whether the {@code {@summary}} tag is defined first in the javadoc.
203     *
204     * @param inlineTagNode node of type {@link JavadocCommentsTokenTypes#JAVADOC_INLINE_TAG}
205     * @return {@code true} if the {@code {@summary}} tag is defined first in the javadoc
206     */
207    private static boolean isDefinedFirst(DetailNode inlineTagNode) {
208        boolean isDefinedFirst = true;
209        DetailNode currentAst = inlineTagNode.getPreviousSibling();
210        while (currentAst != null && isDefinedFirst) {
211            switch (currentAst.getType()) {
212                case JavadocCommentsTokenTypes.TEXT:
213                    isDefinedFirst = currentAst.getText().isBlank();
214                    break;
215                case JavadocCommentsTokenTypes.HTML_ELEMENT:
216                    isDefinedFirst = isHtmlTagWithoutText(currentAst);
217                    break;
218                case JavadocCommentsTokenTypes.LEADING_ASTERISK:
219                case JavadocCommentsTokenTypes.NEWLINE:
220                    // Ignore formatting tokens
221                    break;
222                default:
223                    isDefinedFirst = false;
224                    break;
225            }
226            currentAst = currentAst.getPreviousSibling();
227        }
228        return isDefinedFirst;
229    }
230
231    /**
232     * Whether some text is present inside the HTML element or tag.
233     *
234     * @param node DetailNode of type {@link JavadocCommentsTokenTypes#HTML_ELEMENT}
235     * @return {@code true} if some text is present inside the HTML element
236     */
237    public static boolean isHtmlTagWithoutText(DetailNode node) {
238        boolean isEmpty = true;
239        final DetailNode htmlContentToken =
240             JavadocUtil.findFirstToken(node, JavadocCommentsTokenTypes.HTML_CONTENT);
241
242        if (htmlContentToken != null) {
243            final DetailNode child = htmlContentToken.getFirstChild();
244            isEmpty = child.getType() == JavadocCommentsTokenTypes.HTML_ELEMENT
245                        && isHtmlTagWithoutText(child);
246        }
247        return isEmpty;
248    }
249
250    /**
251     * Checks if the given node is an inline summary tag.
252     *
253     * @param javadocInlineTag node
254     * @return {@code true} if inline tag is of
255     *       type {@link JavadocCommentsTokenTypes#SUMMARY_INLINE_TAG}
256     */
257    private static boolean isSummaryTag(DetailNode javadocInlineTag) {
258        return javadocInlineTag.getType() == JavadocCommentsTokenTypes.SUMMARY_INLINE_TAG;
259    }
260
261    /**
262     * Checks if the given node is an inline return node.
263     *
264     * @param javadocInlineTag node
265     * @return {@code true} if inline tag is of
266     *       type {@link JavadocCommentsTokenTypes#RETURN_INLINE_TAG}
267     */
268    private static boolean isInlineReturnTag(DetailNode javadocInlineTag) {
269        return javadocInlineTag.getType() == JavadocCommentsTokenTypes.RETURN_INLINE_TAG;
270    }
271
272    /**
273     * Checks the inline summary (if present) for {@code period} at end and forbidden fragments.
274     *
275     * @param inlineSummaryTag node of type {@link JavadocCommentsTokenTypes#SUMMARY_INLINE_TAG}
276     */
277    private void validateSummaryTag(DetailNode inlineSummaryTag) {
278        final DetailNode descriptionNode = JavadocUtil.findFirstToken(
279                inlineSummaryTag, JavadocCommentsTokenTypes.DESCRIPTION);
280        final String inlineSummary = getContentOfInlineCustomTag(descriptionNode);
281        final String summaryVisible = getVisibleContent(inlineSummary);
282        if (summaryVisible.isEmpty()) {
283            log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_JAVADOC_MISSING);
284        }
285        else if (!period.isEmpty()) {
286            final boolean isPeriodNotAtEnd =
287                    summaryVisible.lastIndexOf(period) != summaryVisible.length() - 1;
288            if (isPeriodNotAtEnd) {
289                log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_MISSING_PERIOD);
290            }
291            else if (containsForbiddenFragment(inlineSummary)) {
292                log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_JAVADOC);
293            }
294        }
295    }
296
297    /**
298     * Checks the inline return for forbidden fragments.
299     *
300     * @param inlineReturnTag node of type {@link JavadocCommentsTokenTypes#RETURN_INLINE_TAG}
301     */
302    private void validateInlineReturnTag(DetailNode inlineReturnTag) {
303        final DetailNode descriptionNode = JavadocUtil.findFirstToken(
304                inlineReturnTag, JavadocCommentsTokenTypes.DESCRIPTION);
305        final String inlineReturn = getContentOfInlineCustomTag(descriptionNode);
306        final String returnVisible = getVisibleContent(inlineReturn);
307        if (returnVisible.isEmpty()) {
308            log(inlineReturnTag.getLineNumber(), MSG_SUMMARY_JAVADOC_MISSING);
309        }
310        else if (containsForbiddenFragment(inlineReturn)) {
311            log(inlineReturnTag.getLineNumber(), MSG_SUMMARY_JAVADOC);
312        }
313    }
314
315    /**
316     * Gets the content of inline custom tag.
317     *
318     * @param descriptionNode node of type {@link JavadocCommentsTokenTypes#DESCRIPTION}
319     * @return String consisting of the content of inline custom tag.
320     */
321    public static String getContentOfInlineCustomTag(DetailNode descriptionNode) {
322        final StringBuilder customTagContent = new StringBuilder(256);
323        DetailNode curNode = descriptionNode;
324        while (curNode != null) {
325            if (curNode.getFirstChild() == null
326                && curNode.getType() != JavadocCommentsTokenTypes.LEADING_ASTERISK) {
327                customTagContent.append(curNode.getText());
328            }
329
330            DetailNode toVisit = curNode.getFirstChild();
331            while (curNode != descriptionNode && toVisit == null) {
332                toVisit = curNode.getNextSibling();
333                curNode = curNode.getParent();
334            }
335
336            curNode = toVisit;
337        }
338        return customTagContent.toString();
339    }
340
341    /**
342     * Gets the string that is visible to user in javadoc.
343     *
344     * @param summary entire content of summary javadoc.
345     * @return string that is visible to user in javadoc.
346     */
347    private static String getVisibleContent(String summary) {
348        final String visibleSummary = HTML_ELEMENTS.matcher(summary).replaceAll("");
349        return visibleSummary.trim();
350    }
351
352    /**
353     * Tests if first sentence contains forbidden summary fragment.
354     *
355     * @param firstSentence string with first sentence.
356     * @return {@code true} if first sentence contains forbidden summary fragment.
357     */
358    private boolean containsForbiddenFragment(String firstSentence) {
359        final String javadocText = JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN
360                .matcher(firstSentence).replaceAll(" ");
361        return forbiddenSummaryFragments.matcher(trimExcessWhitespaces(javadocText)).find();
362    }
363
364    /**
365     * Trims the given {@code text} of duplicate whitespaces.
366     *
367     * @param text the text to transform.
368     * @return the finalized form of the text.
369     */
370    private static String trimExcessWhitespaces(String text) {
371        final StringBuilder result = new StringBuilder(256);
372        boolean previousWhitespace = true;
373
374        for (char letter : text.toCharArray()) {
375            final char print;
376            if (Character.isWhitespace(letter)) {
377                if (previousWhitespace) {
378                    continue;
379                }
380
381                previousWhitespace = true;
382                print = ' ';
383            }
384            else {
385                previousWhitespace = false;
386                print = letter;
387            }
388
389            result.append(print);
390        }
391
392        return result.toString();
393    }
394
395    /**
396     * Checks if the node starts with an {&#64;inheritDoc}.
397     *
398     * @param root the root node to examine.
399     * @return {@code true} if the javadoc starts with an {&#64;inheritDoc}.
400     */
401    private static boolean startsWithInheritDoc(DetailNode root) {
402        boolean found = false;
403        DetailNode node = root.getFirstChild();
404
405        while (node != null) {
406            if (node.getType() == JavadocCommentsTokenTypes.JAVADOC_INLINE_TAG
407                    && node.getFirstChild().getType()
408                            == JavadocCommentsTokenTypes.INHERIT_DOC_INLINE_TAG) {
409                found = true;
410            }
411            if ((node.getType() == JavadocCommentsTokenTypes.TEXT
412                    || node.getType() == JavadocCommentsTokenTypes.HTML_ELEMENT)
413                    && !CommonUtil.isBlank(node.getText())) {
414                break;
415            }
416            node = node.getNextSibling();
417        }
418
419        return found;
420    }
421
422    /**
423     * Finds and returns summary sentence.
424     *
425     * @param ast javadoc root node.
426     * @return violation string.
427     */
428    private static String getSummarySentence(DetailNode ast) {
429        final StringBuilder result = new StringBuilder(256);
430        DetailNode node = ast.getFirstChild();
431        while (node != null) {
432            if (node.getType() == JavadocCommentsTokenTypes.TEXT) {
433                result.append(node.getText());
434            }
435            else {
436                final String summary = result.toString();
437                if (CommonUtil.isBlank(summary)
438                        && node.getType() == JavadocCommentsTokenTypes.HTML_ELEMENT) {
439                    final DetailNode htmlContentToken = JavadocUtil.findFirstToken(
440                            node, JavadocCommentsTokenTypes.HTML_CONTENT);
441                    result.append(getStringInsideHtmlTag(summary, htmlContentToken));
442                }
443            }
444            node = node.getNextSibling();
445        }
446        return result.toString().trim();
447    }
448
449    /**
450     * Get concatenated string within text of html tags.
451     *
452     * @param result javadoc string
453     * @param detailNode htmlContent node
454     * @return java doc tag content appended in result
455     */
456    private static String getStringInsideHtmlTag(String result, DetailNode detailNode) {
457        final StringBuilder contents = new StringBuilder(result);
458        if (detailNode != null) {
459            DetailNode tempNode = detailNode.getFirstChild();
460            while (tempNode != null) {
461                if (tempNode.getType() == JavadocCommentsTokenTypes.TEXT) {
462                    contents.append(tempNode.getText());
463                }
464                tempNode = tempNode.getNextSibling();
465            }
466        }
467        return contents.toString();
468    }
469
470    /**
471     * Finds the first sentence.
472     *
473     * @param ast The Javadoc root node.
474     * @param period The configured period symbol.
475     * @return An Optional containing the first sentence
476     *     up to and excluding the period, or an empty
477     *     Optional if no ending was found.
478     */
479    private static Optional<String> getFirstSentence(DetailNode ast, String period) {
480        final List<String> sentenceParts = new ArrayList<>();
481        Optional<String> result = Optional.empty();
482        for (String text : (Iterable<String>) streamTextParts(ast)::iterator) {
483            final Optional<String> sentenceEnding = findSentenceEnding(text, period);
484
485            if (sentenceEnding.isPresent()) {
486                sentenceParts.add(sentenceEnding.get());
487                result = Optional.of(String.join("", sentenceParts));
488                break;
489            }
490            sentenceParts.add(text);
491        }
492        return result;
493    }
494
495    /**
496     * Streams through all the text under the given node.
497     *
498     * @param node The Javadoc node to examine.
499     * @return All the text in all nodes that have no child nodes.
500     */
501    private static Stream<String> streamTextParts(DetailNode node) {
502        final Stream<String> result;
503        if (node.getFirstChild() == null) {
504            result = Stream.of(node.getText());
505        }
506        else {
507            final List<Stream<String>> childStreams = new ArrayList<>();
508            DetailNode child = node.getFirstChild();
509            while (child != null) {
510                childStreams.add(streamTextParts(child));
511                child = child.getNextSibling();
512            }
513            result = childStreams.stream().flatMap(Function.identity());
514        }
515        return result;
516    }
517
518    /**
519     * Finds the end of a sentence. The end of sentence detection here could be replaced in the
520     * future by Java's built-in BreakIterator class.
521     *
522     * @param text The string to search.
523     * @param period The period character to find.
524     * @return An Optional containing the string up to and excluding the period,
525     *     or empty Optional if no ending was found.
526     */
527    private static Optional<String> findSentenceEnding(String text, String period) {
528        int periodIndex = text.indexOf(period);
529        Optional<String> result = Optional.empty();
530        while (periodIndex >= 0) {
531            final int afterPeriodIndex = periodIndex + period.length();
532
533            // Handle western period separately as it is only the end of a sentence if followed
534            // by whitespace. Other period characters often include whitespace in the character.
535            if (!DEFAULT_PERIOD.equals(period)
536                || afterPeriodIndex >= text.length()
537                || Character.isWhitespace(text.charAt(afterPeriodIndex))) {
538                final String resultStr = text.substring(0, periodIndex);
539                result = Optional.of(resultStr);
540                break;
541            }
542            periodIndex = text.indexOf(period, afterPeriodIndex);
543        }
544        return result;
545    }
546}