Adding omitIgnoredModules attribute to the checkstyle task (for WriteTag) and adding enum/annotation support to WriteTag

This commit is contained in:
Michael Studman 2008-03-27 11:18:29 +00:00
parent 31b643724b
commit 585f7c20a1
6 changed files with 90 additions and 10 deletions

View File

@ -93,6 +93,13 @@ public class CheckStyleTask extends Task
/** the maximum number of warnings that are tolerated. */
private int mMaxWarnings = Integer.MAX_VALUE;
/**
* whether to omit ignored modules - some modules may log above
* their severity depending on their configuration (e.g. WriteTag) so
* need to be included
*/
private boolean mOmitIgnoredModules = true;
////////////////////////////////////////////////////////////////////////////
// Setters for ANT specific attributes
////////////////////////////////////////////////////////////////////////////
@ -229,6 +236,12 @@ public class CheckStyleTask extends Task
mPackageNamesFile = aFile;
}
/** @param aOmit whether to omit ignored modules */
public void setOmitIgnoredModules(boolean aOmit)
{
mOmitIgnoredModules = aOmit;
}
////////////////////////////////////////////////////////////////////////////
// Setters for Checker configuration attributes
////////////////////////////////////////////////////////////////////////////
@ -351,8 +364,11 @@ public class CheckStyleTask extends Task
Checker c = null;
try {
final Properties props = createOverridingProperties();
final Configuration config = ConfigurationLoader.loadConfiguration(
mConfigLocation, new PropertiesExpander(props), true);
final Configuration config =
ConfigurationLoader.loadConfiguration(
mConfigLocation,
new PropertiesExpander(props),
mOmitIgnoredModules);
final DefaultContext context = new DefaultContext();
final ClassLoader loader = new AntClassLoader(getProject(),

View File

@ -128,7 +128,11 @@ public class WriteTagCheck
@Override
public int[] getDefaultTokens()
{
return new int[] {TokenTypes.INTERFACE_DEF, TokenTypes.CLASS_DEF, };
return new int[] {TokenTypes.INTERFACE_DEF,
TokenTypes.CLASS_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.ANNOTATION_DEF,
};
}
@Override
@ -136,7 +140,11 @@ public class WriteTagCheck
{
return new int[] {TokenTypes.INTERFACE_DEF,
TokenTypes.CLASS_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.ANNOTATION_DEF,
TokenTypes.METHOD_DEF,
TokenTypes.ENUM_CONSTANT_DEF,
TokenTypes.ANNOTATION_FIELD_DEF,
};
}
@ -159,7 +167,7 @@ public class WriteTagCheck
/**
* Verifies that a type definition has a required tag.
* @param aLineNo the line number for the type definition.
* @param aCmt the Javadoc comment for the type definition.
* @param aComment the Javadoc comment for the type definition.
* @param aTag the required tag name.
* @param aTagRE regexp for the full tag.
* @param aFormatRE regexp for the tag value.
@ -167,7 +175,7 @@ public class WriteTagCheck
*/
private void checkTag(
int aLineNo,
String[] aCmt,
String[] aComment,
String aTag,
Pattern aTagRE,
Pattern aFormatRE,
@ -178,19 +186,19 @@ public class WriteTagCheck
}
int tagCount = 0;
for (int i = 0; i < aCmt.length; i++) {
final String s = aCmt[i];
for (int i = 0; i < aComment.length; i++) {
final String s = aComment[i];
final Matcher matcher = aTagRE.matcher(s);
if (matcher.find()) {
tagCount += 1;
final int contentStart = matcher.start(1);
final String content = s.substring(contentStart);
if ((aFormatRE != null) && !aFormatRE.matcher(content).find()) {
log(aLineNo + i - aCmt.length, "type.tagFormat", aTag,
log(aLineNo + i - aComment.length, "type.tagFormat", aTag,
aFormat);
}
else {
logTag(aLineNo + i - aCmt.length, aTag, content);
logTag(aLineNo + i - aComment.length, aTag, content);
}
}

View File

@ -24,4 +24,4 @@ class InputWriteTag
public void anotherMethod()
{
}
}
}

View File

@ -0,0 +1,26 @@
////////////////////////////////////////////////////////////////////////////////
// Test case file for checkstyle.
// Created: 2004
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
/**
* @incomplete This enum needs more code...
*/
enum InputWriteTag2 {
/**
* @incomplete This enum constant needs more code...
*/
FOO;
}
/**
* @incomplete This annotation needs more code...
*/
@interface InputWriteTag3 {
/**
* @incomplete This annotation field needs more code...
*/
int foo() default 0;
}

View File

@ -160,6 +160,25 @@ public class WriteTagCheckTest extends BaseCheckTestSupport
verify(mCheckConfig, getPath("InputWriteTag.java"), expected);
}
@Test
public void testEnumsAndAnnotations() throws Exception
{
mCheckConfig.addAttribute("tag", "@incomplete");
mCheckConfig.addAttribute("tagFormat", ".*");
mCheckConfig.addAttribute("severity", "ignore");
mCheckConfig.addAttribute("tagSeverity", "error");
mCheckConfig.addAttribute("tokens",
"ANNOTATION_DEF, ENUM_DEF, ANNOTATION_FIELD_DEF, ENUM_CONSTANT_DEF");
final String[] expected =
{
"9: @incomplete=This enum needs more code...",
"13: @incomplete=This enum constant needs more code...",
"19: @incomplete=This annotation needs more code...",
"23: @incomplete=This annotation field needs more code...",
};
verify(mCheckConfig, getPath("InputWriteTag2.java"), expected);
}
@Override
protected void verify(Checker aC,
File[] aProcessedFiles,

View File

@ -215,6 +215,17 @@
</td>
<td>No</td>
</tr>
<tr>
<td>omitIgnoredModules</td>
<td>
For efficiency, Checkstyle does not invoke modules with a configured severity of "ignore"
(since their output would be ignored anyway). A small number of modules may choose to log above their
configured severity level and so always need to be invoked. This settings specifies that behaviour.
Defaults to <span class="default">&quot;true&quot;</span>.
</td>
<td>No</td>
</tr>
</table>
</section>