Implemented #98 Add ignore option to the JavadocVariable check

This commit is contained in:
ychulovskyy 2014-09-13 20:24:52 +02:00
parent a4e79cf45c
commit 7ed4071252
4 changed files with 104 additions and 2 deletions

View File

@ -26,6 +26,7 @@ import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import java.util.regex.Pattern;
/**
* Checks that a variable has Javadoc comment.
@ -42,6 +43,12 @@ public class JavadocVariableCheck
/** the visibility scope where Javadoc comments shouldn't be checked **/
private Scope mExcludeScope;
/** the regular expression to ignore variable name */
private String mIgnoreNameRegexp;
/** the pattern to ignore variable name */
private Pattern mIgnoreNamePattern;
/**
* Sets the scope to check.
* @param aFrom string to get the scope from
@ -60,6 +67,30 @@ public class JavadocVariableCheck
mExcludeScope = Scope.getInstance(aScope);
}
/**
* Sets the variable names to ignore in the check.
* @param aRegexp regexp to define variable names to ignore.
*/
public void setIgnoreNamePattern(String aRegexp)
{
mIgnoreNameRegexp = aRegexp;
if (!(aRegexp == null || aRegexp.length() == 0)) {
mIgnoreNamePattern = Pattern.compile(aRegexp);
}
else {
mIgnoreNamePattern = null;
}
}
/**
* Gets the variable names to ignore in the check.
* @return true regexp string to define variable names to ignore.
*/
public String getIgnoreNamePattern()
{
return mIgnoreNameRegexp;
}
@Override
public int[] getDefaultTokens()
{
@ -83,6 +114,18 @@ public class JavadocVariableCheck
}
}
/**
* Decides whether the variable name of an AST is in the ignore list.
* @param aAST the AST to check
* @return true if the variable name of aAST is in the ignore list.
*/
private boolean isIgnored(DetailAST aAST)
{
final String name = aAST.findFirstToken(TokenTypes.IDENT).getText();
return mIgnoreNamePattern != null
&& mIgnoreNamePattern.matcher(name).matches();
}
/**
* Whether we should check this node.
* @param aAST a given node.
@ -90,7 +133,7 @@ public class JavadocVariableCheck
*/
private boolean shouldCheck(final DetailAST aAST)
{
if (ScopeUtils.inCodeBlock(aAST)) {
if (ScopeUtils.inCodeBlock(aAST) || isIgnored(aAST)) {
return false;
}
@ -113,5 +156,4 @@ public class JavadocVariableCheck
|| !scope.isIn(mExcludeScope)
|| !surroundingScope.isIn(mExcludeScope));
}
}

View File

@ -268,6 +268,7 @@ public class JavadocTypeCheckTest extends BaseCheckTestSupport
"75: Missing a Javadoc comment.",
"87: Missing a Javadoc comment.",
"99: Missing a Javadoc comment.",
"111: Missing a Javadoc comment.",
};
verify(checkConfig,
getPath("javadoc" + File.separator + "InputNoJavadoc.java"),
@ -304,6 +305,7 @@ public class JavadocTypeCheckTest extends BaseCheckTestSupport
"75: Missing a Javadoc comment.",
"87: Missing a Javadoc comment.",
"99: Missing a Javadoc comment.",
"111: Missing a Javadoc comment.",
};
verify(checkConfig,
getPath("javadoc" + File.separator + "InputNoJavadoc.java"),

View File

@ -140,6 +140,7 @@ public class JavadocVariableCheckTest
"101:9: Missing a Javadoc comment.",
"102:9: Missing a Javadoc comment.",
"103:9: Missing a Javadoc comment.",
"113:9: Missing a Javadoc comment.",
};
verify(checkConfig,
getPath("javadoc" + File.separator + "InputNoJavadoc.java"),
@ -203,9 +204,61 @@ public class JavadocVariableCheckTest
"101:9: Missing a Javadoc comment.",
"102:9: Missing a Javadoc comment.",
"103:9: Missing a Javadoc comment.",
"113:9: Missing a Javadoc comment.",
};
verify(checkConfig,
getPath("javadoc" + File.separator + "InputNoJavadoc.java"),
expected);
}
@Test
public void testIgnoredVariableNames()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(JavadocVariableCheck.class);
checkConfig.addAttribute("ignoreNamePattern", "log|logger");
final String[] expected = {
"5:5: Missing a Javadoc comment.",
"6:5: Missing a Javadoc comment.",
"7:5: Missing a Javadoc comment.",
"8:5: Missing a Javadoc comment.",
"16:9: Missing a Javadoc comment.",
"17:9: Missing a Javadoc comment.",
"18:9: Missing a Javadoc comment.",
"19:9: Missing a Javadoc comment.",
"28:9: Missing a Javadoc comment.",
"29:9: Missing a Javadoc comment.",
"30:9: Missing a Javadoc comment.",
"31:9: Missing a Javadoc comment.",
"40:9: Missing a Javadoc comment.",
"41:9: Missing a Javadoc comment.",
"42:9: Missing a Javadoc comment.",
"43:9: Missing a Javadoc comment.",
"53:5: Missing a Javadoc comment.",
"54:5: Missing a Javadoc comment.",
"55:5: Missing a Javadoc comment.",
"56:5: Missing a Javadoc comment.",
"64:9: Missing a Javadoc comment.",
"65:9: Missing a Javadoc comment.",
"66:9: Missing a Javadoc comment.",
"67:9: Missing a Javadoc comment.",
"76:9: Missing a Javadoc comment.",
"77:9: Missing a Javadoc comment.",
"78:9: Missing a Javadoc comment.",
"79:9: Missing a Javadoc comment.",
"88:9: Missing a Javadoc comment.",
"89:9: Missing a Javadoc comment.",
"90:9: Missing a Javadoc comment.",
"91:9: Missing a Javadoc comment.",
"100:9: Missing a Javadoc comment.",
"101:9: Missing a Javadoc comment.",
"102:9: Missing a Javadoc comment.",
"103:9: Missing a Javadoc comment.",
};
verify(checkConfig,
getPath("javadoc" + File.separator + "InputNoJavadoc.java"),
expected);
}
}

View File

@ -107,4 +107,9 @@ class PackageClass {
void foo3() {}
private void foo4() {}
}
class IgnoredName {
// ignore by name
private int logger;
}
}