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