From d4791bca408a40bf3d91bd2c46fb53287d6ff700 Mon Sep 17 00:00:00 2001
From: liscju
+ * In addition you can configure this check with skipNoJavadoc
+ * option to allow it to ignore cases when JavaDoc is missing,
+ * but still warns when JavaDoc is present but either
+ * {@link Deprecated Deprecated} is missing from JavaDoc or
+ * {@link Deprecated Deprecated} is missing from the element.
+ * To configure this check to allow it use:
+ * Examples of validating source code with skipNoJavadoc: <property name="skipNoJavadoc" value="true" />
+ *
+ *
+ *
+ *
* @author Travis Schneeberger
*/
public final class MissingDeprecatedCheck extends AbstractCheck {
@@ -116,6 +140,17 @@ public final class MissingDeprecatedCheck extends AbstractCheck {
/** Multiline finished at next Javadoc. */
private static final String NEXT_TAG = "@";
+ /** Is deprecated element valid without javadoc. */
+ private boolean skipNoJavadoc;
+
+ /**
+ * Set skipJavadoc value.
+ * @param skipNoJavadoc user's value of skipJavadoc
+ */
+ public void setSkipNoJavadoc(boolean skipNoJavadoc) {
+ this.skipNoJavadoc = skipNoJavadoc;
+ }
+
@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
@@ -152,7 +187,7 @@ public final class MissingDeprecatedCheck extends AbstractCheck {
final boolean containsJavadocTag = containsJavadocTag(javadoc);
- if (containsAnnotation ^ containsJavadocTag) {
+ if (containsAnnotation ^ containsJavadocTag && !(skipNoJavadoc && javadoc == null)) {
log(ast.getLineNo(), MSG_KEY_ANNOTATION_MISSING_DEPRECATED);
}
}
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/annotation/MissingDeprecatedCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/annotation/MissingDeprecatedCheckTest.java
index fda0cdeab..956b17ebb 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/annotation/MissingDeprecatedCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/annotation/MissingDeprecatedCheckTest.java
@@ -169,4 +169,17 @@ public class MissingDeprecatedCheckTest extends BaseCheckTestSupport {
verify(checkConfig, getPath("InputMissingDeprecated2.java"), expected);
}
+
+ @Test
+ public void testSkipNoJavadocOption() throws Exception {
+ final DefaultConfiguration checkConfig = createCheckConfig(MissingDeprecatedCheck.class);
+ checkConfig.addAttribute("skipNoJavadoc", "true");
+
+ final String[] expected = {
+ "10: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
+ "26: " + getCheckMessage(MSG_KEY_ANNOTATION_MISSING_DEPRECATED),
+ };
+
+ verify(checkConfig, getPath("InputMissingDeprecatedSkipNoJavadoc.java"), expected);
+ }
}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/annotation/InputMissingDeprecatedSkipNoJavadoc.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/annotation/InputMissingDeprecatedSkipNoJavadoc.java
new file mode 100644
index 000000000..971576a59
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/annotation/InputMissingDeprecatedSkipNoJavadoc.java
@@ -0,0 +1,27 @@
+package com.puppycrawl.tools.checkstyle.checks.annotation;
+
+public class InputMissingDeprecatedSkipNoJavadoc {
+ @Deprecated
+ public static final int MY_CONST = 123456;
+
+ /**
+ * Useless comment.
+ */
+ @Deprecated
+ public static final int MY_CONST2 = 234567;
+
+ /**
+ * Useless comment.
+ *
+ * @deprecated Not for public use.
+ */
+ @Deprecated
+ public static final int MY_CONST3 = 345678;
+
+ /**
+ * Useless comment.
+ *
+ * @deprecated Not for public use.
+ */
+ public static final int MY_CONST4 = 345678;
+}
diff --git a/src/xdocs/config_annotation.xml b/src/xdocs/config_annotation.xml
index e97e40545..0176a311c 100644
--- a/src/xdocs/config_annotation.xml
+++ b/src/xdocs/config_annotation.xml
@@ -313,9 +313,45 @@ public String getNameIfPresent() { ... }
present and the @deprecated Javadoc tag is present when
either is present.
+ * {@literal @}deprecated
+ * public static final int MY_CONST = 123456; // no violation
+ *
+ * /** This javadoc is missing deprecated tag. */
+ * {@literal @}deprecated
+ * public static final int COUNTER = 10; // violation as javadoc exists
+ *
+ *
| name | +description | +type | +default value | +
|---|---|---|---|
| skipNoJavadoc | +When this property is set to true check ignore cases + when JavaDoc is missing, but still warns when JavaDoc is + present but either @deprecated is missing from JavaDoc or + @deprecated is missing from the element. + | +boolean | +false | +
To configure the check:
-In addition you can configure this check with skipNoJavadoc option:
+Examples of validating source code with skipNoJavadoc:
+