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.annotation; 021 022import java.util.BitSet; 023 024import com.puppycrawl.tools.checkstyle.StatelessCheck; 025import com.puppycrawl.tools.checkstyle.api.DetailAST; 026import com.puppycrawl.tools.checkstyle.api.DetailNode; 027import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; 028import com.puppycrawl.tools.checkstyle.api.TokenTypes; 029import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck; 030import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTagInfo; 031import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil; 032import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 033 034/** 035 * <div> 036 * Verifies that the annotation {@code @Deprecated} and the Javadoc tag 037 * {@code @deprecated} are both present when either of them is present. 038 * </div> 039 * 040 * <p> 041 * Both ways of flagging deprecation serve their own purpose. 042 * The @Deprecated annotation is used for compilers and development tools. 043 * The @deprecated javadoc tag is used to document why something is deprecated 044 * and what, if any, alternatives exist. 045 * </p> 046 * 047 * <p> 048 * In order to properly mark something as deprecated both forms of 049 * deprecation should be present. 050 * </p> 051 * 052 * <p> 053 * Package deprecation is an exception to the rule of always using the 054 * javadoc tag and annotation to deprecate. It is not clear if the javadoc 055 * tool will support it or not as newer versions keep flip-flopping on if 056 * it is supported or will cause an error. See 057 * <a href="https://bugs.openjdk.org/browse/JDK-8160601">JDK-8160601</a>. 058 * The deprecated javadoc tag is currently the only way to say why the package 059 * is deprecated and what to use instead. Until this is resolved, if you don't 060 * want to print violations on package-info, you can use a 061 * <a href="https://checkstyle.org/filters/index.html">filter</a> to ignore 062 * these files until the javadoc tool faithfully supports it. An example config 063 * using SuppressionSingleFilter is: 064 * </p> 065 * <pre> 066 * <!-- required till https://bugs.openjdk.org/browse/JDK-8160601 --> 067 * <module name="SuppressionSingleFilter"> 068 * <property name="checks" value="MissingDeprecatedCheck"/> 069 * <property name="files" value="package-info\.java"/> 070 * </module> 071 * </pre> 072 * <ul> 073 * <li> 074 * Property {@code violateExecutionOnNonTightHtml} - Control when to 075 * print violations if the Javadoc being examined by this check violates the 076 * tight html rules defined at 077 * <a href="https://checkstyle.org/writingjavadocchecks.html#Tight-HTML_rules"> 078 * Tight-HTML Rules</a>. 079 * Type is {@code boolean}. 080 * Default value is {@code false}. 081 * </li> 082 * </ul> 083 * 084 * <p> 085 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 086 * </p> 087 * 088 * <p> 089 * Violation Message Keys: 090 * </p> 091 * <ul> 092 * <li> 093 * {@code annotation.missing.deprecated} 094 * </li> 095 * <li> 096 * {@code javadoc.duplicateTag} 097 * </li> 098 * <li> 099 * {@code javadoc.missed.html.close} 100 * </li> 101 * <li> 102 * {@code javadoc.parse.rule.error} 103 * </li> 104 * <li> 105 * {@code javadoc.unclosedHtml} 106 * </li> 107 * <li> 108 * {@code javadoc.wrong.singleton.html.tag} 109 * </li> 110 * </ul> 111 * 112 * @since 5.0 113 */ 114@StatelessCheck 115public final class MissingDeprecatedCheck extends AbstractJavadocCheck { 116 117 /** 118 * A key is pointing to the warning message text in "messages.properties" 119 * file. 120 */ 121 public static final String MSG_KEY_ANNOTATION_MISSING_DEPRECATED = 122 "annotation.missing.deprecated"; 123 124 /** 125 * A key is pointing to the warning message text in "messages.properties" 126 * file. 127 */ 128 public static final String MSG_KEY_JAVADOC_DUPLICATE_TAG = 129 "javadoc.duplicateTag"; 130 131 /** {@link Deprecated Deprecated} annotation name. */ 132 private static final String DEPRECATED = "Deprecated"; 133 134 /** Fully-qualified {@link Deprecated Deprecated} annotation name. */ 135 private static final String FQ_DEPRECATED = "java.lang." + DEPRECATED; 136 137 /** Token types to find parent of. */ 138 private static final BitSet TYPES_HASH_SET = TokenUtil.asBitSet( 139 TokenTypes.TYPE, TokenTypes.MODIFIERS, TokenTypes.ANNOTATION, 140 TokenTypes.ANNOTATIONS, TokenTypes.ARRAY_DECLARATOR, 141 TokenTypes.TYPE_PARAMETERS, TokenTypes.DOT); 142 143 @Override 144 public int[] getDefaultJavadocTokens() { 145 return getRequiredJavadocTokens(); 146 } 147 148 @Override 149 public int[] getRequiredJavadocTokens() { 150 return new int[] { 151 JavadocTokenTypes.JAVADOC, 152 }; 153 } 154 155 @Override 156 public void visitJavadocToken(DetailNode ast) { 157 final DetailAST parentAst = getParent(getBlockCommentAst()); 158 159 final boolean containsAnnotation = 160 AnnotationUtil.containsAnnotation(parentAst, DEPRECATED) 161 || AnnotationUtil.containsAnnotation(parentAst, FQ_DEPRECATED); 162 163 final boolean containsJavadocTag = containsDeprecatedTag(ast); 164 165 if (containsAnnotation ^ containsJavadocTag) { 166 log(parentAst.getLineNo(), MSG_KEY_ANNOTATION_MISSING_DEPRECATED); 167 } 168 } 169 170 /** 171 * Checks to see if the javadoc contains a deprecated tag. 172 * 173 * @param javadoc the javadoc of the AST 174 * @return true if contains the tag 175 */ 176 private boolean containsDeprecatedTag(DetailNode javadoc) { 177 boolean found = false; 178 for (DetailNode child : javadoc.getChildren()) { 179 if (child.getType() == JavadocTokenTypes.JAVADOC_TAG 180 && child.getChildren()[0].getType() == JavadocTokenTypes.DEPRECATED_LITERAL) { 181 if (found) { 182 log(child.getLineNumber(), MSG_KEY_JAVADOC_DUPLICATE_TAG, 183 JavadocTagInfo.DEPRECATED.getText()); 184 } 185 found = true; 186 } 187 } 188 return found; 189 } 190 191 /** 192 * Returns the parent node of the comment. 193 * 194 * @param commentBlock child node. 195 * @return parent node. 196 */ 197 private static DetailAST getParent(DetailAST commentBlock) { 198 DetailAST result = commentBlock.getParent(); 199 200 if (TokenUtil.isRootNode(result)) { 201 result = commentBlock.getNextSibling(); 202 } 203 204 while (true) { 205 final int type = result.getType(); 206 if (TYPES_HASH_SET.get(type)) { 207 result = result.getParent(); 208 } 209 else if (type == TokenTypes.SINGLE_LINE_COMMENT) { 210 result = result.getNextSibling(); 211 } 212 else { 213 break; 214 } 215 } 216 217 return result; 218 } 219 220}