com.puppycrawl.tools.checkstyle.checks.annotation.MissingDeprecatedCheck Maven / Gradle / Ivy
Show all versions of checkstyle Show documentation
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2024 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.annotation;
import java.util.BitSet;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck;
import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTagInfo;
import com.puppycrawl.tools.checkstyle.utils.AnnotationUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
/**
*
* Verifies that the annotation {@code @Deprecated} and the Javadoc tag
* {@code @deprecated} are both present when either of them is present.
*
*
*
* Both ways of flagging deprecation serve their own purpose.
* The @Deprecated annotation is used for compilers and development tools.
* The @deprecated javadoc tag is used to document why something is deprecated
* and what, if any, alternatives exist.
*
*
*
* In order to properly mark something as deprecated both forms of
* deprecation should be present.
*
*
*
* Package deprecation is an exception to the rule of always using the
* javadoc tag and annotation to deprecate. It is not clear if the javadoc
* tool will support it or not as newer versions keep flip-flopping on if
* it is supported or will cause an error. See
* JDK-8160601.
* The deprecated javadoc tag is currently the only way to say why the package
* is deprecated and what to use instead. Until this is resolved, if you don't
* want to print violations on package-info, you can use a
* filter to ignore
* these files until the javadoc tool faithfully supports it. An example config
* using SuppressionSingleFilter is:
*
*
* <!-- required till https://bugs.openjdk.org/browse/JDK-8160601 -->
* <module name="SuppressionSingleFilter">
* <property name="checks" value="MissingDeprecatedCheck"/>
* <property name="files" value="package-info\.java"/>
* </module>
*
*
* -
* Property {@code violateExecutionOnNonTightHtml} - Control when to
* print violations if the Javadoc being examined by this check violates the
* tight html rules defined at
*
* Tight-HTML Rules.
* Type is {@code boolean}.
* Default value is {@code false}.
*
*
*
*
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
*
*
*
* Violation Message Keys:
*
*
* -
* {@code annotation.missing.deprecated}
*
* -
* {@code javadoc.duplicateTag}
*
* -
* {@code javadoc.missed.html.close}
*
* -
* {@code javadoc.parse.rule.error}
*
* -
* {@code javadoc.unclosedHtml}
*
* -
* {@code javadoc.wrong.singleton.html.tag}
*
*
*
* @since 5.0
*/
@StatelessCheck
public final class MissingDeprecatedCheck extends AbstractJavadocCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_MISSING_DEPRECATED =
"annotation.missing.deprecated";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_JAVADOC_DUPLICATE_TAG =
"javadoc.duplicateTag";
/** {@link Deprecated Deprecated} annotation name. */
private static final String DEPRECATED = "Deprecated";
/** Fully-qualified {@link Deprecated Deprecated} annotation name. */
private static final String FQ_DEPRECATED = "java.lang." + DEPRECATED;
/** Token types to find parent of. */
private static final BitSet TYPES_HASH_SET = TokenUtil.asBitSet(
TokenTypes.TYPE, TokenTypes.MODIFIERS, TokenTypes.ANNOTATION,
TokenTypes.ANNOTATIONS, TokenTypes.ARRAY_DECLARATOR,
TokenTypes.TYPE_PARAMETERS, TokenTypes.DOT);
@Override
public int[] getDefaultJavadocTokens() {
return getRequiredJavadocTokens();
}
@Override
public int[] getRequiredJavadocTokens() {
return new int[] {
JavadocTokenTypes.JAVADOC,
};
}
@Override
public void visitJavadocToken(DetailNode ast) {
final DetailAST parentAst = getParent(getBlockCommentAst());
final boolean containsAnnotation =
AnnotationUtil.containsAnnotation(parentAst, DEPRECATED)
|| AnnotationUtil.containsAnnotation(parentAst, FQ_DEPRECATED);
final boolean containsJavadocTag = containsDeprecatedTag(ast);
if (containsAnnotation ^ containsJavadocTag) {
log(parentAst.getLineNo(), MSG_KEY_ANNOTATION_MISSING_DEPRECATED);
}
}
/**
* Checks to see if the javadoc contains a deprecated tag.
*
* @param javadoc the javadoc of the AST
* @return true if contains the tag
*/
private boolean containsDeprecatedTag(DetailNode javadoc) {
boolean found = false;
for (DetailNode child : javadoc.getChildren()) {
if (child.getType() == JavadocTokenTypes.JAVADOC_TAG
&& child.getChildren()[0].getType() == JavadocTokenTypes.DEPRECATED_LITERAL) {
if (found) {
log(child.getLineNumber(), MSG_KEY_JAVADOC_DUPLICATE_TAG,
JavadocTagInfo.DEPRECATED.getText());
}
found = true;
}
}
return found;
}
/**
* Returns the parent node of the comment.
*
* @param commentBlock child node.
* @return parent node.
*/
private static DetailAST getParent(DetailAST commentBlock) {
DetailAST result = commentBlock.getParent();
if (TokenUtil.isRootNode(result)) {
result = commentBlock.getNextSibling();
}
while (true) {
final int type = result.getType();
if (TYPES_HASH_SET.get(type)) {
result = result.getParent();
}
else if (type == TokenTypes.SINGLE_LINE_COMMENT) {
result = result.getNextSibling();
}
else {
break;
}
}
return result;
}
}