diff --git a/docs/engine.html b/docs/engine.html index e94d8a097..1d85c7812 100644 --- a/docs/engine.html +++ b/docs/engine.html @@ -142,7 +142,7 @@ -

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

+

The check can be tailored for types, methods and others. What is included in others are the keywords switch, while, for, do, if, else, synchronized, try, catch, finally, static.

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/Scope.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Scope.java index 63bbaba88..dde79a536 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Scope.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Scope.java @@ -84,8 +84,7 @@ public final class Scope implements Comparable /** map from scope names to the respective Scope */ private static final Map NAME_TO_SCOPE = new HashMap(); - static - { + static { NAME_TO_SCOPE.put(SCOPENAME_NOTHING, NOTHING); NAME_TO_SCOPE.put(SCOPENAME_PUBLIC, PUBLIC); NAME_TO_SCOPE.put(SCOPENAME_PROTECTED, PROTECTED); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g index f069882b6..2e4d1b373 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/java.g @@ -307,7 +307,7 @@ field! { java.util.List exs = new java.util.ArrayList(); MethodSignature msig = new MethodSignature(); - final MyCommonAST[] lcurls = new MyCommonAST[2]; + final MyCommonAST[] stmtBraces = new MyCommonAST[2]; } : // method, constructor, or variable declaration mods:modifiers[msig.getModSet()] @@ -347,9 +347,9 @@ field! ver.reportStartMethodBlock(); } ( - s2:compoundStatement[lcurls] + s2:compoundStatement[stmtBraces] { - ver.verifyLCurlyMethod(msig.getLineNo(), lcurls[0]); + ver.verifyLCurlyMethod(msig.getLineNo(), stmtBraces[0]); ver.verifyMethodLength(#s2.getLineNo(), sCompoundLength); } @@ -370,8 +370,12 @@ field! ) // "static { ... }" class initializer - | "static" {ver.reportStartMethodBlock();} s3:compoundStatement[sIgnoreAST] {ver.reportEndMethodBlock();} - {#field = #(#[STATIC_INIT,"STATIC_INIT"], s3);} + | st:"static" {ver.reportStartMethodBlock();} s3:compoundStatement[stmtBraces] + { + #field = #(#[STATIC_INIT,"STATIC_INIT"], s3); + ver.reportEndMethodBlock(); + ver.verifyLCurlyOther(st.getLine(), stmtBraces[0]); + } // "{ ... }" instance initializer | {ver.reportStartMethodBlock();} s4:compoundStatement[sIgnoreAST] {ver.reportEndMethodBlock();} @@ -561,7 +565,7 @@ statement[int[] aType, MyCommonAST[] aCurlies] // Attach a label to the front of a statement | IDENT c:COLON^ {#c.setType(LABELED_STAT);} statement[sIgnoreType, sIgnoreAST] - // If-else statement + // If-else statement | ii:"if"^ LPAREN! expression RPAREN! statement[stmtType, stmtBraces] { aType[0] = STMT_IF; @@ -720,17 +724,32 @@ forIter // an exception handler try/catch block tryBlock - : t:"try"^ compoundStatement[sIgnoreAST] - { ver.verifyWSAroundBegin(t.getLine(), t.getColumn(), t.getText()); } +{ + final MyCommonAST[] stmtBraces = new MyCommonAST[2]; +} + : t:"try"^ compoundStatement[stmtBraces] + { + ver.verifyWSAroundBegin(t.getLine(), t.getColumn(), t.getText()); + ver.verifyLCurlyOther(t.getLine(), stmtBraces[0]); + } (handler)* - ( "finally"^ compoundStatement[sIgnoreAST] )? + ( + f:"finally"^ compoundStatement[stmtBraces] + { ver.verifyLCurlyOther(f.getLine(), stmtBraces[0]); } + )? ; // an exception handler handler - : c:"catch"^ LPAREN! parameterDeclaration[new MethodSignature()] RPAREN! compoundStatement[sIgnoreAST] - {ver.verifyWSAroundBegin(c.getLine(), c.getColumn(), c.getText());} +{ + final MyCommonAST[] stmtBraces = new MyCommonAST[2]; +} + : c:"catch"^ LPAREN! parameterDeclaration[new MethodSignature()] RPAREN! compoundStatement[stmtBraces] + { + ver.verifyWSAroundBegin(c.getLine(), c.getColumn(), c.getText()); + ver.verifyLCurlyOther(c.getLine(), stmtBraces[0]); + } ; diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index e32a62920..5c9b1e68e 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -297,6 +297,7 @@ public class CheckerTest filepath + ":109: Expected @param tag for 'aOne'.", filepath + ":109: Expected @param tag for 'aFour'.", filepath + ":109: Expected @param tag for 'aFive'.", + filepath + ":129: '{' should be on the previous line.", }; verify(c, filepath, expected); @@ -647,11 +648,14 @@ public class CheckerTest assertNotNull(c); final String[] expected = { filepath + ":19: '{' should be on the previous line.", - filepath + ":22: '{' should be on the previous line.", - filepath + ":29: '{' should be on the previous line.", - filepath + ":33: '{' should be on the previous line.", - filepath + ":51: '{' should be on the previous line.", - filepath + ":53: '{' should be on the previous line.", + filepath + ":21: '{' should be on the previous line.", + filepath + ":23: '{' should be on the previous line.", + filepath + ":30: '{' should be on the previous line.", + filepath + ":34: '{' should be on the previous line.", + filepath + ":42: '{' should be on the previous line.", + filepath + ":46: '{' should be on the previous line.", + filepath + ":52: '{' should be on the previous line.", + filepath + ":54: '{' should be on the previous line.", }; verify(c, filepath, expected); } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/InputLeftCurlyOther.java b/src/tests/com/puppycrawl/tools/checkstyle/InputLeftCurlyOther.java index 943aaf85c..6666bd3d4 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/InputLeftCurlyOther.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/InputLeftCurlyOther.java @@ -17,7 +17,8 @@ class InputLeftCurlyOther int a = 2; while (true) { - try { + try + { if (x > 0) { break;