001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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.Locale; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 026import com.puppycrawl.tools.checkstyle.api.DetailAST; 027import com.puppycrawl.tools.checkstyle.api.TokenTypes; 028import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 029import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 030 031/** 032 * <p> 033 * Checks that the Javadoc content begins from the same position 034 * for all Javadoc comments in the project. Any leading asterisks and spaces 035 * are not counted as the beginning of the content and are therefore ignored. 036 * </p> 037 * <p> 038 * It is possible to enforce two different styles: 039 * </p> 040 * <ul> 041 * <li> 042 * {@code first_line} - Javadoc content starts from the first line: 043 * <pre> 044 * /** Summary text. 045 * * More details. 046 * */ 047 * public void method(); 048 * </pre> 049 * </li> 050 * <li> 051 * {@code second_line} - Javadoc content starts from the second line: 052 * <pre> 053 * /** 054 * * Summary text. 055 * * More details. 056 * */ 057 * public void method(); 058 * </pre> 059 * </li> 060 * </ul> 061 * <p> 062 * This check does not validate the Javadoc summary itself nor its presence. 063 * The check will not report any violations for missing or malformed javadoc summary. 064 * To validate the Javadoc summary use 065 * <a href="https://checkstyle.org/checks/javadoc/summaryjavadoc.html#SummaryJavadoc"> 066 * SummaryJavadoc</a> check. 067 * </p> 068 * <p> 069 * The <a href="https://docs.oracle.com/en/java/javase/11/docs/specs/doc-comment-spec.html"> 070 * Documentation Comment Specification</a> permits leading asterisks on the first line. 071 * For these Javadoc comments: 072 * </p> 073 * <pre> 074 * /*** 075 * * Some text. 076 * */ 077 * /************ 078 * * Some text. 079 * */ 080 * /** ** 081 * * Some text. 082 * */ 083 * </pre> 084 * <p> 085 * The documentation generated will be just "Some text." without any asterisks. 086 * Since these asterisks will not appear in the generated documentation, 087 * they should not be considered as the beginning of the Javadoc content. 088 * In such cases, the check assumes that the Javadoc content begins on the second line. 089 * </p> 090 * <ul> 091 * <li> 092 * Property {@code location} - Specify the policy on placement of the Javadoc content. 093 * Type is {@code com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocContentLocationOption}. 094 * Default value is {@code second_line}. 095 * </li> 096 * </ul> 097 * <p> 098 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 099 * </p> 100 * <p> 101 * Violation Message Keys: 102 * </p> 103 * <ul> 104 * <li> 105 * {@code javadoc.content.first.line} 106 * </li> 107 * <li> 108 * {@code javadoc.content.second.line} 109 * </li> 110 * </ul> 111 * 112 * @since 8.27 113 */ 114@StatelessCheck 115public class JavadocContentLocationCheck extends AbstractCheck { 116 117 /** 118 * A key is pointing to the warning message text in "messages.properties" file. 119 */ 120 public static final String MSG_JAVADOC_CONTENT_FIRST_LINE = "javadoc.content.first.line"; 121 122 /** 123 * A key is pointing to the warning message text in "messages.properties" file. 124 */ 125 public static final String MSG_JAVADOC_CONTENT_SECOND_LINE = "javadoc.content.second.line"; 126 127 /** 128 * Specify the policy on placement of the Javadoc content. 129 */ 130 private JavadocContentLocationOption location = JavadocContentLocationOption.SECOND_LINE; 131 132 @Override 133 public int[] getRequiredTokens() { 134 return new int[] { 135 TokenTypes.BLOCK_COMMENT_BEGIN, 136 }; 137 } 138 139 @Override 140 public int[] getAcceptableTokens() { 141 return getRequiredTokens(); 142 } 143 144 @Override 145 public int[] getDefaultTokens() { 146 return getRequiredTokens(); 147 } 148 149 @Override 150 public boolean isCommentNodesRequired() { 151 return true; 152 } 153 154 /** 155 * Setter to specify the policy on placement of the Javadoc content. 156 * 157 * @param value string to decode location from 158 * @throws IllegalArgumentException if unable to decode 159 * @since 8.27 160 */ 161 public void setLocation(String value) { 162 location = JavadocContentLocationOption.valueOf(value.trim().toUpperCase(Locale.ENGLISH)); 163 } 164 165 @Override 166 public void visitToken(DetailAST ast) { 167 if (isMultilineComment(ast) && JavadocUtil.isJavadocComment(ast)) { 168 final String commentContent = JavadocUtil.getJavadocCommentContent(ast); 169 final int indexOfFirstNonBlankLine = findIndexOfFirstNonBlankLine(commentContent); 170 if (indexOfFirstNonBlankLine >= 0) { 171 if (location == JavadocContentLocationOption.FIRST_LINE 172 && indexOfFirstNonBlankLine != 0) { 173 log(ast, MSG_JAVADOC_CONTENT_FIRST_LINE); 174 } 175 else if (location == JavadocContentLocationOption.SECOND_LINE 176 && indexOfFirstNonBlankLine != 1) { 177 log(ast, MSG_JAVADOC_CONTENT_SECOND_LINE); 178 } 179 } 180 } 181 } 182 183 /** 184 * Checks if a DetailAST of type {@code TokenTypes.BLOCK_COMMENT_BEGIN} span 185 * more than one line. The node always has at least one child of the type 186 * {@code TokenTypes.BLOCK_COMMENT_END}. 187 * 188 * @param node node to check 189 * @return {@code true} for multi-line comment nodes 190 */ 191 private static boolean isMultilineComment(DetailAST node) { 192 return !TokenUtil.areOnSameLine(node, node.getLastChild()); 193 } 194 195 /** 196 * Returns the index of the first non-blank line. 197 * All lines consists only of asterisks and whitespaces are treated as blank. 198 * 199 * @param commentContent Javadoc content to process 200 * @return the index of the first non-blank line or {@code -1} if all lines are blank 201 */ 202 private static int findIndexOfFirstNonBlankLine(String commentContent) { 203 int lineNo = 0; 204 boolean noContent = true; 205 for (int i = 0; i < commentContent.length(); ++i) { 206 final char character = commentContent.charAt(i); 207 if (character == '\n') { 208 ++lineNo; 209 } 210 else if (character != '*' && !Character.isWhitespace(character)) { 211 noContent = false; 212 break; 213 } 214 } 215 if (noContent) { 216 lineNo = -1; 217 } 218 return lineNo; 219 } 220 221}