Added support for checking the file length does not exceed a specified max.

This commit is contained in:
Oliver Burn 2002-01-06 07:08:20 +00:00
parent 637bf620e5
commit d87ea06ebb
8 changed files with 51 additions and 1 deletions

View File

@ -77,6 +77,11 @@ This task is included in the checkstyle distribution.</p>
<td valign="top">Specifies the maximum constructor length. Default value is defined <a href="index.html#constructorLength">here</a>.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">maxFileLen</td>
<td valign="top">Specifies the maximum file length. Default value is defined <a href="index.html#fileLength">here</a>.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>

View File

@ -67,6 +67,10 @@ This command line tool is included in the checkstyle distribution.</p>
<td valign="top">checkstyle.maxconstructorlen</td>
<td valign="top">Specifies the maximum constructor length. Default value is defined <a href="index.html#constructorLength">here</a>.</td>
</tr>
<tr>
<td valign="top">checkstyle.maxfilelen</td>
<td valign="top">Specifies the maximum file length. Default value is defined <a href="index.html#fileLength">here</a>.</td>
</tr>
<tr>
<td valign="top">checkstyle.pattern.member</td>
<td valign="top">Specifies the regular expression to match against member variables. Default value is defined <a href="index.html#varformat">here</a>.</td>

View File

@ -39,6 +39,7 @@ div.tip {margin-left : 5%;margin-right : 5%;padding-left: 2%; padding-right: 2%;
<li>Ensure <span class="code">{}</span>'s are used for <span class="code">if/while/for/do</span> constructs. Note: this check can be turned off.</li>
<li>Lines are not longer than a specified length.</li>
<li>Methods and constructors that are longer than a specified number of lines.</li>
<li>Files that are longer than a specified number of lines.</li>
<li>Lines do not contain tabs. Note: this check can be turned off.</li>
</ul>
@ -150,6 +151,9 @@ div.tip {margin-left : 5%;margin-right : 5%;padding-left: 2%; padding-right: 2%;
<h3><a name="constructorLength">Constructor Body Length</a></h3>
<p>Checks for constructor bodies that are longer that a specified number of lines. The default is <span class="default">&quot;150&quot;</span>.
<h3><a name="fileLength">File Length</a></h3>
<p>Checks for files that are longer that a specified number of lines. The default is <span class="default">&quot;2000&quot;</span>.
<h3>Tab characters</h3>
<p>Checks for lines that contain tab (<code>'\t'</code>) characters. This can be turned off.</p>

View File

@ -126,6 +126,12 @@ public class CheckStyleTask
mConfig.setMaxConstructorLength(aLen);
}
/** @param aLen max allowed file length **/
public void setMaxFileLen(int aLen)
{
mConfig.setMaxFileLength(aLen);
}
/** @param aIgnore whether max line length should be ignored for
* import statements
*/

View File

@ -57,6 +57,8 @@ class Configuration
private static final int MAX_METHOD_LENGTH = 150;
/** the maximum constructor length **/
private static final int MAX_CONSTRUCTOR_LENGTH = 150;
/** the maximum file length **/
private static final int MAX_FILE_LENGTH = 2000;
////////////////////////////////////////////////////////////////////////////
// Member variables
@ -98,6 +100,8 @@ class Configuration
private int mMaxMethodLength = MAX_METHOD_LENGTH;
/** the maximum constructor length **/
private int mMaxConstructorLength = MAX_CONSTRUCTOR_LENGTH;
/** the maximum file length **/
private int mMaxFileLength = MAX_FILE_LENGTH;
/** whether to allow tabs **/
private boolean mAllowTabs = false;
/** whether to allow protected data **/
@ -158,6 +162,8 @@ class Configuration
aProps, aLog, MAX_METHOD_LENGTH_PROP, MAX_METHOD_LENGTH));
setMaxConstructorLength(getIntProperty(
aProps, aLog, MAX_CONSTRUCTOR_LENGTH_PROP, MAX_CONSTRUCTOR_LENGTH));
setMaxFileLength(getIntProperty(
aProps, aLog, MAX_FILE_LENGTH_PROP, MAX_FILE_LENGTH));
setAllowTabs(getBooleanProperty(aProps, ALLOW_TABS_PROP, mAllowTabs));
setAllowProtected(
@ -308,6 +314,12 @@ class Configuration
return mMaxConstructorLength;
}
/** @return the maximum file length **/
int getMaxFileLength()
{
return mMaxFileLength;
}
/** @return whether to allow tabs **/
boolean isAllowTabs()
{
@ -474,6 +486,14 @@ class Configuration
mMaxConstructorLength = aMaxConstructorLength;
}
/**
* @param aMaxFileLength the maximum file length
*/
void setMaxFileLength(int aMaxFileLength)
{
mMaxFileLength = aMaxFileLength;
}
/**
* @param aIgnoreImportLength whether to allow tabs
*/

View File

@ -42,6 +42,8 @@ interface Defn
String MAX_METHOD_LENGTH_PROP = "checkstyle.maxmethodlen";
/** property name for length of constructors **/
String MAX_CONSTRUCTOR_LENGTH_PROP = "checkstyle.maxconstructorlen";
/** property name for length of files **/
String MAX_FILE_LENGTH_PROP = "checkstyle.maxfilelen";
/** property name for allowing tabs **/
String ALLOW_TABS_PROP = "checkstyle.allow.tabs";
/** property name for allowing protected data **/

View File

@ -142,6 +142,8 @@ class VerifierImpl
{
mLines = aLines;
checkHeader();
// Iterate over the lines looking for long lines and tabs.
for (int i = 0; i < mLines.length; i++) {
// check for long line, but possibly allow imports
@ -159,7 +161,12 @@ class VerifierImpl
}
}
checkHeader();
// Check excessive number of lines
if (mLines.length > mConfig.getMaxFileLength()) {
log(1,
"file length is " + mLines.length + " lines (max allowed is " +
mConfig.getMaxFileLength() + ").");
}
}

View File

@ -238,12 +238,14 @@ public class CheckerTest
public void testSimple()
throws Exception
{
mConfig.setMaxFileLength(20);
mConfig.setMaxMethodLength(19);
mConfig.setMaxConstructorLength(9);
final Checker c = createChecker();
final String filepath = getPath("InputSimple.java");
assertNotNull(c);
final String[] expected = {
filepath + ":1: file length is 110 lines (max allowed is 20).",
filepath + ":3: Line does not match expected header line of '// Created: 2001'.",
filepath + ":18: line longer than 80 characters",
filepath + ":19: line contains a tab character",