001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2022 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.Optional; 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.FileContents; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; 030 031/** 032 * <p> 033 * Checks for missing Javadoc comments in package-info.java files. 034 * </p> 035 * <p> 036 * Rationale: description and other related documentation for a package can be written up 037 * in the package-info.java file and it gets used in the production of the Javadocs. 038 * See <a 039 * href="https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javadoc.html#packagecomment" 040 * >link</a> for more info. 041 * </p> 042 * <p> 043 * To configure the check: 044 * </p> 045 * <pre> 046 * <module name="MissingJavadocPackage"/> 047 * </pre> 048 * <p>Example:</p> 049 * <pre> 050 * /** 051 * * Provides API classes 052 * */ 053 * package com.checkstyle.api; // no violation 054 * /* 055 * * Block comment is not a javadoc 056 * */ 057 * package com.checkstyle.api; // violation 058 * </pre> 059 * <p> 060 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 061 * </p> 062 * <p> 063 * Violation Message Keys: 064 * </p> 065 * <ul> 066 * <li> 067 * {@code package.javadoc.missing} 068 * </li> 069 * </ul> 070 * 071 * @since 8.22 072 */ 073@StatelessCheck 074public class MissingJavadocPackageCheck extends AbstractCheck { 075 076 /** 077 * A key is pointing to the warning message text in "messages.properties" 078 * file. 079 */ 080 public static final String MSG_PKG_JAVADOC_MISSING = "package.javadoc.missing"; 081 082 @Override 083 public int[] getDefaultTokens() { 084 return getRequiredTokens(); 085 } 086 087 @Override 088 public int[] getAcceptableTokens() { 089 return getRequiredTokens(); 090 } 091 092 @Override 093 public int[] getRequiredTokens() { 094 return new int[] {TokenTypes.PACKAGE_DEF}; 095 } 096 097 @Override 098 public boolean isCommentNodesRequired() { 099 return true; 100 } 101 102 // suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166 103 @SuppressWarnings("deprecation") 104 @Override 105 public void visitToken(DetailAST ast) { 106 final FileContents contents = getFileContents(); 107 if (contents.inPackageInfo() && !hasJavadoc(ast)) { 108 log(ast, MSG_PKG_JAVADOC_MISSING); 109 } 110 } 111 112 /** 113 * Checks that there is javadoc before ast. 114 * Because of <a href="https://github.com/checkstyle/checkstyle/issues/4392">parser bug</a> 115 * parser can place javadoc comment either as previous sibling of package definition 116 * or (if there is annotation between package def and javadoc) inside package definition tree. 117 * So we should look for javadoc in both places. 118 * 119 * @param ast {@link TokenTypes#PACKAGE_DEF} token to check 120 * @return true if there is javadoc, false otherwise 121 */ 122 private static boolean hasJavadoc(DetailAST ast) { 123 final boolean hasJavadocBefore = ast.getPreviousSibling() != null 124 && isJavadoc(ast.getPreviousSibling()); 125 return hasJavadocBefore || hasJavadocAboveAnnotation(ast); 126 } 127 128 /** 129 * Checks javadoc existence in annotations list. 130 * 131 * @param ast package def 132 * @return true if there is a javadoc, false otherwise 133 */ 134 private static boolean hasJavadocAboveAnnotation(DetailAST ast) { 135 final Optional<DetailAST> firstAnnotationChild = Optional.of(ast.getFirstChild()) 136 .map(DetailAST::getFirstChild) 137 .map(DetailAST::getFirstChild); 138 boolean result = false; 139 if (firstAnnotationChild.isPresent()) { 140 for (DetailAST child = firstAnnotationChild.get(); child != null; 141 child = child.getNextSibling()) { 142 if (isJavadoc(child)) { 143 result = true; 144 break; 145 } 146 } 147 } 148 return result; 149 } 150 151 /** 152 * Checks that ast is a javadoc comment. 153 * 154 * @param ast token to check 155 * @return true if ast is a javadoc comment, false otherwise 156 */ 157 private static boolean isJavadoc(DetailAST ast) { 158 return ast.getType() == TokenTypes.BLOCK_COMMENT_BEGIN && JavadocUtil.isJavadocComment(ast); 159 } 160}