From 585f7c20a1e9e3ca4a04ffedf4e1cedc449acd97 Mon Sep 17 00:00:00 2001 From: Michael Studman Date: Thu, 27 Mar 2008 11:18:29 +0000 Subject: [PATCH] Adding omitIgnoredModules attribute to the checkstyle task (for WriteTag) and adding enum/annotation support to WriteTag --- .../tools/checkstyle/CheckStyleTask.java | 20 ++++++++++++-- .../checks/javadoc/WriteTagCheck.java | 22 +++++++++++----- .../tools/checkstyle/InputWriteTag.java | 2 +- .../tools/checkstyle/InputWriteTag2.java | 26 +++++++++++++++++++ .../checks/javadoc/WriteTagCheckTest.java | 19 ++++++++++++++ src/xdocs/anttask.xml | 11 ++++++++ 6 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/InputWriteTag2.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java index 36a6821a4..382575636 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java @@ -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(), diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/WriteTagCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/WriteTagCheck.java index fa9e17f62..53a5eff7c 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/WriteTagCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/WriteTagCheck.java @@ -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); } } diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputWriteTag.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputWriteTag.java index 01be2277c..9c88dea1f 100644 --- a/src/testinputs/com/puppycrawl/tools/checkstyle/InputWriteTag.java +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputWriteTag.java @@ -24,4 +24,4 @@ class InputWriteTag public void anotherMethod() { } -} +} \ No newline at end of file diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputWriteTag2.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputWriteTag2.java new file mode 100644 index 000000000..14628b55e --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputWriteTag2.java @@ -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; +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/javadoc/WriteTagCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/javadoc/WriteTagCheckTest.java index f84ed3b88..08c68d6b8 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/javadoc/WriteTagCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/javadoc/WriteTagCheckTest.java @@ -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, diff --git a/src/xdocs/anttask.xml b/src/xdocs/anttask.xml index 027a70a00..9d772daaf 100755 --- a/src/xdocs/anttask.xml +++ b/src/xdocs/anttask.xml @@ -215,6 +215,17 @@ No + + + omitIgnoredModules + + 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 "true". + + No +