001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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                case JavadocCommentsTokenTypes.HTML_ELEMENT ->
215                    isDefinedFirst = isHtmlTagWithoutText(currentAst);
216                case JavadocCommentsTokenTypes.LEADING_ASTERISK,
217                     JavadocCommentsTokenTypes.NEWLINE -> {
218                    // Ignore formatting tokens
219                }
220                default -> isDefinedFirst = false;
221            }
222            currentAst = currentAst.getPreviousSibling();
223        }
224        return isDefinedFirst;
225    }
226
227    /**
228     * Whether some text is present inside the HTML element or tag.
229     *
230     * @param node DetailNode of type {@link JavadocCommentsTokenTypes#HTML_ELEMENT}
231     * @return {@code true} if some text is present inside the HTML element
232     */
233    public static boolean isHtmlTagWithoutText(DetailNode node) {
234        boolean isEmpty = true;
235        final DetailNode htmlContentToken =
236             JavadocUtil.findFirstToken(node, JavadocCommentsTokenTypes.HTML_CONTENT);
237
238        if (htmlContentToken != null) {
239            final DetailNode child = htmlContentToken.getFirstChild();
240            isEmpty = child.getType() == JavadocCommentsTokenTypes.HTML_ELEMENT
241                        && isHtmlTagWithoutText(child);
242        }
243        return isEmpty;
244    }
245
246    /**
247     * Checks if the given node is an inline summary tag.
248     *
249     * @param javadocInlineTag node
250     * @return {@code true} if inline tag is of
251     *       type {@link JavadocCommentsTokenTypes#SUMMARY_INLINE_TAG}
252     */
253    private static boolean isSummaryTag(DetailNode javadocInlineTag) {
254        return javadocInlineTag.getType() == JavadocCommentsTokenTypes.SUMMARY_INLINE_TAG;
255    }
256
257    /**
258     * Checks if the given node is an inline return node.
259     *
260     * @param javadocInlineTag node
261     * @return {@code true} if inline tag is of
262     *       type {@link JavadocCommentsTokenTypes#RETURN_INLINE_TAG}
263     */
264    private static boolean isInlineReturnTag(DetailNode javadocInlineTag) {
265        return javadocInlineTag.getType() == JavadocCommentsTokenTypes.RETURN_INLINE_TAG;
266    }
267
268    /**
269     * Checks the inline summary (if present) for {@code period} at end and forbidden fragments.
270     *
271     * @param inlineSummaryTag node of type {@link JavadocCommentsTokenTypes#SUMMARY_INLINE_TAG}
272     */
273    private void validateSummaryTag(DetailNode inlineSummaryTag) {
274        final DetailNode descriptionNode = JavadocUtil.findFirstToken(
275                inlineSummaryTag, JavadocCommentsTokenTypes.DESCRIPTION);
276        final String inlineSummary = getContentOfInlineCustomTag(descriptionNode);
277        final String summaryVisible = getVisibleContent(inlineSummary);
278        if (summaryVisible.isEmpty()) {
279            log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_JAVADOC_MISSING);
280        }
281        else if (!period.isEmpty()) {
282            final boolean isPeriodNotAtEnd =
283                    summaryVisible.lastIndexOf(period) != summaryVisible.length() - 1;
284            if (isPeriodNotAtEnd) {
285                log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_MISSING_PERIOD);
286            }
287            else if (containsForbiddenFragment(inlineSummary)) {
288                log(inlineSummaryTag.getLineNumber(), MSG_SUMMARY_JAVADOC);
289            }
290        }
291    }
292
293    /**
294     * Checks the inline return for forbidden fragments.
295     *
296     * @param inlineReturnTag node of type {@link JavadocCommentsTokenTypes#RETURN_INLINE_TAG}
297     */
298    private void validateInlineReturnTag(DetailNode inlineReturnTag) {
299        final DetailNode descriptionNode = JavadocUtil.findFirstToken(
300                inlineReturnTag, JavadocCommentsTokenTypes.DESCRIPTION);
301        final String inlineReturn = getContentOfInlineCustomTag(descriptionNode);
302        final String returnVisible = getVisibleContent(inlineReturn);
303        if (returnVisible.isEmpty()) {
304            log(inlineReturnTag.getLineNumber(), MSG_SUMMARY_JAVADOC_MISSING);
305        }
306        else if (containsForbiddenFragment(inlineReturn)) {
307            log(inlineReturnTag.getLineNumber(), MSG_SUMMARY_JAVADOC);
308        }
309    }
310
311    /**
312     * Gets the content of inline custom tag.
313     *
314     * @param descriptionNode node of type {@link JavadocCommentsTokenTypes#DESCRIPTION}
315     * @return String consisting of the content of inline custom tag.
316     */
317    public static String getContentOfInlineCustomTag(DetailNode descriptionNode) {
318        final StringBuilder customTagContent = new StringBuilder(256);
319        DetailNode curNode = descriptionNode;
320        while (curNode != null) {
321            if (curNode.getFirstChild() == null
322                && curNode.getType() != JavadocCommentsTokenTypes.LEADING_ASTERISK) {
323                customTagContent.append(curNode.getText());
324            }
325
326            DetailNode toVisit = curNode.getFirstChild();
327            while (curNode != descriptionNode && toVisit == null) {
328                toVisit = curNode.getNextSibling();
329                curNode = curNode.getParent();
330            }
331
332            curNode = toVisit;
333        }
334        return customTagContent.toString();
335    }
336
337    /**
338     * Gets the string that is visible to user in javadoc.
339     *
340     * @param summary entire content of summary javadoc.
341     * @return string that is visible to user in javadoc.
342     */
343    private static String getVisibleContent(String summary) {
344        final String visibleSummary = HTML_ELEMENTS.matcher(summary).replaceAll("");
345        return visibleSummary.trim();
346    }
347
348    /**
349     * Tests if first sentence contains forbidden summary fragment.
350     *
351     * @param firstSentence string with first sentence.
352     * @return {@code true} if first sentence contains forbidden summary fragment.
353     */
354    private boolean containsForbiddenFragment(String firstSentence) {
355        final String javadocText = JAVADOC_MULTILINE_TO_SINGLELINE_PATTERN
356                .matcher(firstSentence).replaceAll(" ");
357        return forbiddenSummaryFragments.matcher(trimExcessWhitespaces(javadocText)).find();
358    }
359
360    /**
361     * Trims the given {@code text} of duplicate whitespaces.
362     *
363     * @param text the text to transform.
364     * @return the finalized form of the text.
365     */
366    private static String trimExcessWhitespaces(String text) {
367        final StringBuilder result = new StringBuilder(256);
368        boolean previousWhitespace = true;
369
370        for (int index = 0; index < text.length(); index++) {
371            final char letter = text.charAt(index);
372            final char print;
373            if (Character.isWhitespace(letter)) {
374                if (previousWhitespace) {
375                    continue;
376                }
377
378                previousWhitespace = true;
379                print = ' ';
380            }
381            else {
382                previousWhitespace = false;
383                print = letter;
384            }
385
386            result.append(print);
387        }
388
389        return result.toString();
390    }
391
392    /**
393     * Checks if the node starts with an {&#64;inheritDoc}.
394     *
395     * @param root the root node to examine.
396     * @return {@code true} if the javadoc starts with an {&#64;inheritDoc}.
397     */
398    private static boolean startsWithInheritDoc(DetailNode root) {
399        boolean found = false;
400        DetailNode node = root.getFirstChild();
401
402        while (node != null) {
403            if (node.getType() == JavadocCommentsTokenTypes.JAVADOC_INLINE_TAG
404                    && node.getFirstChild().getType()
405                            == JavadocCommentsTokenTypes.INHERIT_DOC_INLINE_TAG) {
406                found = true;
407            }
408            if ((node.getType() == JavadocCommentsTokenTypes.TEXT
409                    || node.getType() == JavadocCommentsTokenTypes.HTML_ELEMENT)
410                    && !CommonUtil.isBlank(node.getText())) {
411                break;
412            }
413            node = node.getNextSibling();
414        }
415
416        return found;
417    }
418
419    /**
420     * Finds and returns summary sentence.
421     *
422     * @param ast javadoc root node.
423     * @return violation string.
424     */
425    private static String getSummarySentence(DetailNode ast) {
426        final StringBuilder result = new StringBuilder(256);
427        DetailNode node = ast.getFirstChild();
428        while (node != null) {
429            if (node.getType() == JavadocCommentsTokenTypes.TEXT) {
430                result.append(node.getText());
431            }
432            else {
433                final String summary = result.toString();
434                if (CommonUtil.isBlank(summary)
435                        && node.getType() == JavadocCommentsTokenTypes.HTML_ELEMENT) {
436                    final DetailNode htmlContentToken = JavadocUtil.findFirstToken(
437                            node, JavadocCommentsTokenTypes.HTML_CONTENT);
438                    result.append(getStringInsideHtmlTag(summary, htmlContentToken));
439                }
440            }
441            node = node.getNextSibling();
442        }
443        return result.toString().trim();
444    }
445
446    /**
447     * Get concatenated string within text of html tags.
448     *
449     * @param result javadoc string
450     * @param detailNode htmlContent node
451     * @return java doc tag content appended in result
452     */
453    private static String getStringInsideHtmlTag(String result, DetailNode detailNode) {
454        final StringBuilder contents = new StringBuilder(result);
455        if (detailNode != null) {
456            DetailNode tempNode = detailNode.getFirstChild();
457            while (tempNode != null) {
458                if (tempNode.getType() == JavadocCommentsTokenTypes.TEXT) {
459                    contents.append(tempNode.getText());
460                }
461                tempNode = tempNode.getNextSibling();
462            }
463        }
464        return contents.toString();
465    }
466
467    /**
468     * Finds the first sentence.
469     *
470     * @param ast The Javadoc root node.
471     * @param period The configured period symbol.
472     * @return An Optional containing the first sentence
473     *     up to and excluding the period, or an empty
474     *     Optional if no ending was found.
475     */
476    private static Optional<String> getFirstSentence(DetailNode ast, String period) {
477        final List<String> sentenceParts = new ArrayList<>();
478        Optional<String> result = Optional.empty();
479        for (String text : (Iterable<String>) streamTextParts(ast)::iterator) {
480            final Optional<String> sentenceEnding = findSentenceEnding(text, period);
481
482            if (sentenceEnding.isPresent()) {
483                sentenceParts.add(sentenceEnding.get());
484                result = Optional.of(String.join("", sentenceParts));
485                break;
486            }
487            sentenceParts.add(text);
488        }
489        return result;
490    }
491
492    /**
493     * Streams through all the text under the given node.
494     *
495     * @param node The Javadoc node to examine.
496     * @return All the text in all nodes that have no child nodes.
497     */
498    private static Stream<String> streamTextParts(DetailNode node) {
499        final Stream<String> result;
500        if (node.getFirstChild() == null) {
501            result = Stream.of(node.getText());
502        }
503        else {
504            final List<Stream<String>> childStreams = new ArrayList<>();
505            DetailNode child = node.getFirstChild();
506            while (child != null) {
507                childStreams.add(streamTextParts(child));
508                child = child.getNextSibling();
509            }
510            result = childStreams.stream().flatMap(Function.identity());
511        }
512        return result;
513    }
514
515    /**
516     * Finds the end of a sentence. The end of sentence detection here could be replaced in the
517     * future by Java's built-in BreakIterator class.
518     *
519     * @param text The string to search.
520     * @param period The period character to find.
521     * @return An Optional containing the string up to and excluding the period,
522     *     or empty Optional if no ending was found.
523     */
524    private static Optional<String> findSentenceEnding(String text, String period) {
525        int periodIndex = text.indexOf(period);
526        Optional<String> result = Optional.empty();
527        while (periodIndex >= 0) {
528            final int afterPeriodIndex = periodIndex + period.length();
529
530            // Handle western period separately as it is only the end of a sentence if followed
531            // by whitespace. Other period characters often include whitespace in the character.
532            if (!DEFAULT_PERIOD.equals(period)
533                || afterPeriodIndex >= text.length()
534                || Character.isWhitespace(text.charAt(afterPeriodIndex))) {
535                final String resultStr = text.substring(0, periodIndex);
536                result = Optional.of(resultStr);
537                break;
538            }
539            periodIndex = text.indexOf(period, afterPeriodIndex);
540        }
541        return result;
542    }
543}