From f4860ef3b4536407751eaca2c7f1da60da628ebc Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Fri, 22 Feb 2002 03:11:36 +0000 Subject: [PATCH] Implemented brace checking for "if" and "while" --- build.xml | 1 + docs/engine.html | 1 + .../com/puppycrawl/tools/checkstyle/Checker.java | 9 +++------ .../puppycrawl/tools/checkstyle/Configuration.java | 9 +++------ .../puppycrawl/tools/checkstyle/MyModifierSet.java | 9 +++------ .../com/puppycrawl/tools/checkstyle/Verifier.java | 9 +++------ src/checkstyle/com/puppycrawl/tools/checkstyle/java.g | 11 +++++++++-- .../com/puppycrawl/tools/checkstyle/CheckerTest.java | 8 ++++++++ 8 files changed, 31 insertions(+), 26 deletions(-) diff --git a/build.xml b/build.xml index fceecb553..895468b47 100644 --- a/build.xml +++ b/build.xml @@ -160,6 +160,7 @@ staticPattern="^s[A-Z][a-zA-Z0-9]*$" paramPattern="^a[A-Z][a-zA-Z0-9]*$" lcurlyMethod="nl" + lcurlyOther="nlow" lcurlyType="nl"> +

The check can be tailored for types, methods and others. What is included in others are: switch, while, if, . It does not include array initializers, .

Long Lines

Checks for lines that are longer than a specified length. The default is "80". This can be turned off for import statements.

diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java index adaff4d44..c03eb31d4 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java @@ -188,8 +188,7 @@ public class Checker */ private int checkPackageHtml(String[] aFiles) { - if (!mConfig.isRequirePackageHtml()) - { + if (!mConfig.isRequirePackageHtml()) { return 0; } @@ -199,14 +198,12 @@ public class Checker { final File file = new File(aFiles[i]); final File packageDir = file.getParentFile(); - if (!checkedPackages.contains(packageDir)) - { + if (!checkedPackages.contains(packageDir)) { final File packageDoc = new File(packageDir, "package.html"); final String docFile = packageDoc.toString(); fireFileStarted(docFile); - if (!packageDoc.exists()) - { + if (!packageDoc.exists()) { final LineText error = new LineText(0, "missing package documentation file."); fireErrors(docFile, new LineText[]{error} ); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java index 4396ac286..852f54cdb 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Configuration.java @@ -481,8 +481,7 @@ public class Configuration **/ public int getHeaderIgnoreLineNo() { - if (mHeaderIgnoreLineNo.isEmpty()) - { + if (mHeaderIgnoreLineNo.isEmpty()) { return -1; } else @@ -764,14 +763,12 @@ public class Configuration { mHeaderIgnoreLineNo.clear(); - if (aList == null) - { + if (aList == null) { return; } final StringTokenizer tokens = new StringTokenizer(aList, ","); - while (tokens.hasMoreTokens()) - { + while (tokens.hasMoreTokens()) { final String ignoreLine = tokens.nextToken(); mHeaderIgnoreLineNo.add(new Integer(ignoreLine)); } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/MyModifierSet.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/MyModifierSet.java index b81c41e58..e0442aecf 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/MyModifierSet.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/MyModifierSet.java @@ -101,16 +101,13 @@ class MyModifierSet /** @return the visibility scope of the modifiers. */ Scope getVisibilityScope() { - if (containsPublic()) - { + if (containsPublic()) { return Scope.PUBLIC; } - if (containsProtected()) - { + else if (containsProtected()) { return Scope.PROTECTED; } - if (containsPrivate()) - { + else if (containsPrivate()) { return Scope.PRIVATE; } return Scope.PACKAGE; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java index 69a50f857..41c9be0c8 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Verifier.java @@ -315,8 +315,7 @@ class Verifier **/ void verifyVariable(MyVariable aVar) { - if (inMethodBlock()) - { + if (inMethodBlock()) { checkVariable(aVar, mConfig.getLocalVarRegexp(), mConfig.getLocalVarPat()); @@ -1075,8 +1074,7 @@ class Verifier /** checks that a file contains a valid header **/ private void checkHeader() { - if (mConfig.getHeaderLines().length > mLines.length) - { + if (mConfig.getHeaderLines().length > mLines.length) { log(1, "Missing a header - not enough lines in file."); } else @@ -1098,8 +1096,7 @@ class Verifier createRE(headerLine).match(mLines[i]) : headerLine.equals(mLines[i]); - if (!match) - { + if (!match) { log(i + 1, "Line does not match expected header line of '" + mConfig.getHeaderLines()[i] + "'."); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g index 89874983a..7567e0c69 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g @@ -538,6 +538,7 @@ statement[int[] aType, MyCommonAST[] aCurlies] { final MyModifierSet modSet = new MyModifierSet(); final int[] stmtType = new int[1]; + final MyCommonAST[] stmtBraces = new MyCommonAST[2]; stmtType[0] = STMT_OTHER; } // A list of statements in curly braces -- start a new scope! @@ -561,13 +562,16 @@ statement[int[] aType, MyCommonAST[] aCurlies] | IDENT c:COLON^ {#c.setType(LABELED_STAT);} statement[sIgnoreType, sIgnoreAST] // If-else statement - | ii:"if"^ LPAREN! expression RPAREN! statement[stmtType, sIgnoreAST] + | ii:"if"^ LPAREN! expression RPAREN! statement[stmtType, stmtBraces] { aType[0] = STMT_IF; ver.verifyWSAroundBegin(ii.getLine(), ii.getColumn(), ii.getText()); if (stmtType[0] != STMT_COMPOUND) { ver.reportNeedBraces(ii); } + else { + ver.verifyLCurlyOther(ii.getLine(), stmtBraces[0]); + } } ( // CONFLICT: the old "dangling-else" problem... @@ -604,12 +608,15 @@ statement[int[] aType, MyCommonAST[] aCurlies] } // While statement - | ww:"while"^ LPAREN! expression RPAREN! statement[stmtType, sIgnoreAST] + | ww:"while"^ LPAREN! expression RPAREN! statement[stmtType, stmtBraces] { ver.verifyWSAroundBegin(ww.getLine(), ww.getColumn(), ww.getText()); if (stmtType[0] != STMT_COMPOUND) { ver.reportNeedBraces(ww); } + else { + ver.verifyLCurlyOther(ww.getLine(), stmtBraces[0]); + } } // do-while statement diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index 46c73129b..7e33515f8 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -112,6 +112,8 @@ public class CheckerTest filepath + ":39: 'try' is not followed by whitespace.", filepath + ":41: 'catch' is not followed by whitespace.", filepath + ":58: 'if' is not followed by whitespace.", + filepath + ":59: '{' should be on the previous line.", + filepath + ":75: '{' should be on the previous line.", filepath + ":76: 'return' is not followed by whitespace.", filepath + ":88: cast needs to be followed by whitespace.", filepath + ":97: '?' is not preceeded with whitespace.", @@ -170,6 +172,8 @@ public class CheckerTest filepath + ":39: 'try' is not followed by whitespace.", filepath + ":41: 'catch' is not followed by whitespace.", filepath + ":58: 'if' is not followed by whitespace.", + filepath + ":59: '{' should be on the previous line.", + filepath + ":75: '{' should be on the previous line.", filepath + ":76: 'return' is not followed by whitespace.", filepath + ":97: '?' is not preceeded with whitespace.", filepath + ":97: '?' is not followed by whitespace.", @@ -206,6 +210,8 @@ public class CheckerTest assertNotNull(c); final String[] expected = { filepath + ":13: type Javadoc comment is missing an @author tag.", + filepath + ":59: '{' should be on the previous line.", + filepath + ":75: '{' should be on the previous line.", }; verify(c, filepath, expected); } @@ -632,6 +638,8 @@ public class CheckerTest final String filepath = getPath("InputLeftCurlyOther.java"); assertNotNull(c); final String[] expected = { + filepath + ":19: '{' should be on the previous line.", + filepath + ":22: '{' should be on the previous line.", filepath + ":33: '{' should be on the previous line.", }; verify(c, filepath, expected);