Issue #2889: Adds skipNoJavadoc option to MissingDeprecated check

This commit is contained in:
liscju 2016-02-10 18:24:39 +01:00 committed by Roman Ivanov
parent e091384b4f
commit d4791bca40
4 changed files with 113 additions and 2 deletions

View File

@ -70,6 +70,30 @@ import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
* <module name="JavadocDeprecated"/>
* </pre>
*
* <p>
* 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:
* </p>
*
* <pre> &lt;property name="skipNoJavadoc" value="true" /&gt;</pre>
*
* <p>Examples of validating source code with skipNoJavadoc:</p>
*
* <pre>
* <code>
* {@literal @}deprecated
* public static final int MY_CONST = 123456; // no violation
*
* &#47;** This javadoc is missing deprecated tag. *&#47;
* {@literal @}deprecated
* public static final int COUNTER = 10; // violation as javadoc exists
* </code>
* </pre>
*
* @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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -313,9 +313,45 @@ public String getNameIfPresent() { ... }
present and the @deprecated Javadoc tag is present when
either is present.</p>
</subsection>
<subsection name="Properties">
<table>
<tr>
<th>name</th>
<th>description</th>
<th>type</th>
<th>default value</th>
</tr>
<tr>
<td>skipNoJavadoc</td>
<td>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.
</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td>false</td>
</tr>
</table>
</subsection>
<subsection name="Examples">
<p> To configure the check:</p>
<source> &lt;module name=&quot;MissingDeprecated&quot;/&gt;
<source>
&lt;module name=&quot;MissingDeprecated&quot;/&gt;
</source>
<p> In addition you can configure this check with skipNoJavadoc option: </p>
<source>
&lt;module name=&quot;MissingDeprecated&quot;&gt;
&lt;property name="skipNoJavadoc" value="true" /&gt;
&lt;/module&gt;
</source>
<p>Examples of validating source code with skipNoJavadoc:</p>
<source>
@deprecated
public static final int MY_CONST = 123456; // no violation
/** This javadoc is missing deprecated tag. */
@deprecated
public static final int COUNTER = 10; // violation as javadoc exists
</source>
</subsection>