From d87ea06ebb50622ac5327ec6c9765486aa032ff1 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Sun, 6 Jan 2002 07:08:20 +0000 Subject: [PATCH] Added support for checking the file length does not exceed a specified max. --- docs/anttask.html | 5 +++++ docs/cmdline.html | 4 ++++ docs/index.html | 4 ++++ .../tools/checkstyle/CheckStyleTask.java | 6 ++++++ .../tools/checkstyle/Configuration.java | 20 +++++++++++++++++++ .../com/puppycrawl/tools/checkstyle/Defn.java | 2 ++ .../tools/checkstyle/VerifierImpl.java | 9 ++++++++- .../tools/checkstyle/CheckerTest.java | 2 ++ 8 files changed, 51 insertions(+), 1 deletion(-) diff --git a/docs/anttask.html b/docs/anttask.html index ac057a0dd..b96156442 100644 --- a/docs/anttask.html +++ b/docs/anttask.html @@ -77,6 +77,11 @@ This task is included in the checkstyle distribution.

Specifies the maximum constructor length. Default value is defined here. No + + maxFileLen + Specifies the maximum file length. Default value is defined here. + No + diff --git a/docs/cmdline.html b/docs/cmdline.html index 799ee5f8e..57f65210c 100644 --- a/docs/cmdline.html +++ b/docs/cmdline.html @@ -67,6 +67,10 @@ This command line tool is included in the checkstyle distribution.

checkstyle.maxconstructorlen Specifies the maximum constructor length. Default value is defined here. + + checkstyle.maxfilelen + Specifies the maximum file length. Default value is defined here. + checkstyle.pattern.member Specifies the regular expression to match against member variables. Default value is defined here. diff --git a/docs/index.html b/docs/index.html index 1b380a404..a3e6d796e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -39,6 +39,7 @@ div.tip {margin-left : 5%;margin-right : 5%;padding-left: 2%; padding-right: 2%;
  • Ensure {}'s are used for if/while/for/do constructs. Note: this check can be turned off.
  • Lines are not longer than a specified length.
  • Methods and constructors that are longer than a specified number of lines.
  • +
  • Files that are longer than a specified number of lines.
  • Lines do not contain tabs. Note: this check can be turned off.
  • @@ -150,6 +151,9 @@ div.tip {margin-left : 5%;margin-right : 5%;padding-left: 2%; padding-right: 2%;

    Constructor Body Length

    Checks for constructor bodies that are longer that a specified number of lines. The default is "150". +

    File Length

    +

    Checks for files that are longer that a specified number of lines. The default is "2000". +

    Tab characters

    Checks for lines that contain tab ('\t') characters. This can be turned off.

    diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java index eed7c5ea3..a715ab9c4 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/CheckStyleTask.java @@ -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 */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java index fe2d95e2a..a2446df5b 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java @@ -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 */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java index 81cca84c5..8f74a682b 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Defn.java @@ -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 **/ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/VerifierImpl.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/VerifierImpl.java index 473f4bb3c..ec2f0eefb 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/VerifierImpl.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/VerifierImpl.java @@ -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() + ")."); + } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index a6e288447..0bb8f7b6a 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -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",