changed to regular expressions for @author and @version tags

- thanks to Lars for suggestion
This commit is contained in:
Rick Giles 2002-12-07 12:02:32 +00:00
parent a0054eede0
commit 730ade67d5
4 changed files with 121 additions and 50 deletions

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.0//EN"
"http://www.puppycrawl.com/dtds/configuration_1_0.dtd">
@ -27,7 +26,9 @@
<config name="IllegalImportCheck"/>
<config name="IllegalInstantiationCheck"/>
<config name="InnerAssignmentCheck"/>
<config name="JavadocTypeCheck"/>
<config name="JavadocTypeCheck">
<property name="authorFormat" value="\S"/>
</config>
<config name="JavadocMethodCheck">
<property name="checkUnusedThrows" value="true"/>
</config>

View File

@ -29,33 +29,63 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.Utils;
/**
* Checks that a type has Javadoc comment
* <p>
* Checks the Javadoc of a type.
* By default, does not check for author or version tags.
* The scope to verify is specified using the {@link Scope} class and
* defaults to {@link Scope#PRIVATE}. To verify another scope,
* set property scope to one of the {@link Scope} constants.
* To define the format for an author tag or a version tag
* set property authorFormat or versionFormat respectively to a
* regular expression
* (http://jakarta.apache.org/regexp/apidocs/org/apache/regexp/RE.html).
* </p>
* <p>
* An example of how to configure the check is:
* </p>
* <pre>
* &lt;check name="JavadocTypeCheck"/&gt;
* </pre>
* <p> An example of how to configure the check for the
* {@link Scope#PUBLIC} scope is:
*</p>
* <pre>
* &lt;check name="JavadocTypeCheck"&gt;
* &lt;property name="scope" value="public"/&gt;
* &lt;/check&gt;
* </pre>
* <p> An example of how to configure the check for an author tag
* and a version tag is:
*</p>
* <pre>
* &lt;check name="JavadocTypeCheck"&gt;
* &lt;property name="authorFormat" value="\S"/&gt;
* &lt;property name="versionFormat" value="\S"/&gt;
* &lt;/check&gt;
* </pre>
* <p> An example of how to configure the check for a
* CVS revision version tag is:
*</p>
* <pre>
* &lt;check name="JavadocTypeCheck"&gt;
* &lt;property name="versionFormat" value="\$Revision.*\$"/&gt;
* &lt;/check&gt;
* </pre>
*
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
public class JavadocTypeCheck
extends Check
{
/** the pattern to match author tag **/
private static final String MATCH_JAVADOC_AUTHOR_PAT = "@author\\s+\\S";
/** compiled regexp to match author tag **/
private static final RE MATCH_JAVADOC_AUTHOR =
Utils.createRE(MATCH_JAVADOC_AUTHOR_PAT);
/** the pattern to match version tag **/
private static final String MATCH_JAVADOC_VERSION_PAT = "@version\\s+\\S";
/** compiled regexp to match version tag **/
private static final RE MATCH_JAVADOC_VERSION =
Utils.createRE(MATCH_JAVADOC_VERSION_PAT);
/** the scope to check for */
private Scope mScope = Scope.PRIVATE;
/** whether to allow no author tag */
private boolean mAllowNoAuthor = false;
/** whether to require version tag */
private boolean mRequireVersion = false;
/** compiled regexp to match author tag **/
private RE mAuthorRE = null;
/** compiled regexp to match version tag **/
private RE mVersionRE = null;
/**
* Sets the scope to check.
* @param aFrom string to set scope from
@ -65,25 +95,18 @@ public class JavadocTypeCheck
mScope = Scope.getInstance(aFrom);
}
/**
* Sets whether to allow no author tag
* @param aAllowNoAuthor a <code>boolean</code> value
*/
public void setAllowNoAuthor(boolean aAllowNoAuthor)
/** @param aFormat author tag pattern */
public void setAuthorFormat(String aFormat)
{
mAllowNoAuthor = aAllowNoAuthor;
mAuthorRE = Utils.createRE("@author\\s+" + aFormat);
}
/**
* Sets whether to require a version tag.
* @param aRequireVersion a <code>boolean</code> value
*/
public void setRequireVersion(boolean aRequireVersion)
/** @param aFormat version tag pattern */
public void setVersionFormat(String aFormat)
{
mRequireVersion = aRequireVersion;
mVersionRE = Utils.createRE("@version\\s+" + aFormat);
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
@ -108,17 +131,20 @@ public class JavadocTypeCheck
}
else if (ScopeUtils.isOuterMostType(aAST)) {
// don't check author/version for inner classes
if (!mAllowNoAuthor
&& (MATCH_JAVADOC_AUTHOR.grep(cmt).length == 0))
if ((mAuthorRE != null)
&& (mAuthorRE.grep(cmt).length == 0))
{
// TODO: better error message
log(aAST.getLineNo(), "type.missingTag", "@author");
}
if (mRequireVersion
&& (MATCH_JAVADOC_VERSION.grep(cmt).length == 0))
if ((mVersionRE != null)
&& (mVersionRE.grep(cmt).length == 0))
{
// TODO: better error message
log(aAST.getLineNo(), "type.missingTag", "@version");
}
}
}
}

View File

@ -2,11 +2,13 @@
// Test case file for checkstyle.
// Created: 2001
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
/**
* Testing that the author and version tags are needed based on the
* boolean property.
* Testing author and version tag patterns
* @author Oliver Burn
* @version 1.0
*/
class InputJavadoc
{

View File

@ -136,10 +136,11 @@ public class JavadocTypeCheckTest extends BaseCheckTestCase
verify(c, fname, expected);
}
public void testAts() throws Exception
public void testAuthorRequired() throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(JavadocTypeCheck.class);
checkConfig.addAttribute("authorFormat", "\\S");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputWhitespace.java");
final String[] expected =
@ -148,35 +149,76 @@ public class JavadocTypeCheckTest extends BaseCheckTestCase
};
verify(c, fname, expected);
}
public void testNoAuthor()
public void testAuthorRegularEx()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(JavadocTypeCheck.class);
checkConfig.addAttribute("allowNoAuthor", "false");
checkConfig.addAttribute("requireVersion", "false");
checkConfig.addAttribute("authorFormat", "0*");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputJavadoc.java");
final String[] expected = {
"11: Type Javadoc comment is missing an @author tag."
};
verify(c, fname, expected);
}
public void testNoVersion()
public void testAuthorRegularExError()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(JavadocTypeCheck.class);
checkConfig.addAttribute("allowNoAuthor", "true");
checkConfig.addAttribute("requireVersion", "true");
checkConfig.addAttribute("authorFormat", "ABC");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputJavadoc.java");
final String[] expected = {
"11: Type Javadoc comment is missing an @version tag."
"13: Type Javadoc comment is missing an @author tag.",
};
verify(c, fname, expected);
}
public void testVersionRequired()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(JavadocTypeCheck.class);
checkConfig.addAttribute("versionFormat", "\\S");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputWhitespace.java");
final String[] expected = {
"13: Type Javadoc comment is missing an @version tag."
};
verify(c, fname, expected);
}
public void testVersionRegularEx()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(JavadocTypeCheck.class);
checkConfig.addAttribute("versionFormat", "[:digit:].*");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputJavadoc.java");
final String[] expected = {
};
verify(c, fname, expected);
}
public void testVersionRegularExError()
throws Exception
{
final DefaultConfiguration checkConfig =
createCheckConfig(JavadocTypeCheck.class);
checkConfig.addAttribute("versionFormat", "\\$Revision.*\\$");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputJavadoc.java");
final String[] expected = {
"13: Type Javadoc comment is missing an @version tag."
};
verify(c, fname, expected);
}