diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/LineLengthCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/LineLengthCheck.java new file mode 100644 index 000000000..5d035dc52 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/LineLengthCheck.java @@ -0,0 +1,103 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2002 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.Utils; +import org.apache.regexp.RE; +import org.apache.regexp.RESyntaxException; +import org.apache.commons.beanutils.ConversionException; + +/** + * Checks for long lines. + * + *

+ * Rationale: Long lines are hard to read in printouts or if developers + * have limited screen space for the source code, e.g. if the IDE displays + * additional information like project tree, class hierarchy, etc. + *

+ * + *

+ * Note: Support for the special handling of imports in CheckStyle Version 2 + * has been dropped as it is a special case of regexp: The user can set + * the ignorePattern to "^import" and achieve the same effect. + *

+ * + * @author Lars Kühne + */ +public class LineLengthCheck extends Check +{ + /** the maximum number of columns in a line */ + private int mMax = 80; + + /** the regexp when long lines are ignored */ + private RE mIgnorePattern; + + public LineLengthCheck() + { + setIgnorePattern("^$"); + } + + public int[] getDefaultTokens() + { + return new int[0]; + } + + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public void beginTree() + { + final String[] lines = getLines(); + for (int i = 0; i < lines.length; i++) { + + final String line = lines[i]; + final int realLength = Utils.lengthExpandedTabs( + line, line.length(), getTabWidth()); + + + if (realLength > mMax && !mIgnorePattern.match(line)) { + log(i + 1, "maxLineLen", new Integer(mMax)); + } + } + } + + /** + * @param aLength the maximum length of a line + */ + public void setMax(int aLength) + { + mMax = aLength; + } + + /** + * Set the ignore pattern. + * @param aFormat a String value + * @throws org.apache.commons.beanutils.ConversionException unable to parse aFormat + */ + public void setIgnorePattern(String aFormat) + { + try { + mIgnorePattern = Utils.getRE(aFormat); + } + catch (RESyntaxException e) { + throw new ConversionException("unable to parse " + aFormat, e); + } + } + +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/LineLengthCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/LineLengthCheckTest.java new file mode 100644 index 000000000..49138cebc --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/LineLengthCheckTest.java @@ -0,0 +1,29 @@ +package com.puppycrawl.tools.checkstyle; + +import com.puppycrawl.tools.checkstyle.checks.LineLengthCheck; + +public class LineLengthCheckTest extends BaseCheckTestCase +{ + public LineLengthCheckTest(String aName) + { + super(aName); + } + + + public void testSimple() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(LineLengthCheck.class.getName()); + checkConfig.addProperty("max", "80"); + checkConfig.addProperty("ignorePattern", "^.*is OK.*regexp.*$"); + final Checker c = createChecker(checkConfig); + final String filepath = getPath("InputSimple.java"); + final String[] expected = { + "18: Line is longer than 80 characters.", + "145: Line is longer than 80 characters.", + }; + verify(c, filepath, expected); + } + +}