Since Checkstyle 3.0
name | description | type | default value | since |
---|---|---|---|---|
option | Specify policy on how to pad parentheses. | PadOption | nospace |
3.0 |
tokens | tokens to check | subset of tokens ANNOTATION , ANNOTATION_FIELD_DEF , CTOR_CALL , CTOR_DEF , DOT , ENUM_CONSTANT_DEF , EXPR , LITERAL_CATCH , LITERAL_DO , LITERAL_FOR , LITERAL_IF , LITERAL_NEW , LITERAL_SWITCH , LITERAL_SYNCHRONIZED , LITERAL_WHILE , METHOD_CALL , METHOD_DEF , QUESTION , RESOURCE_SPECIFICATION , SUPER_CTOR_CALL , LAMBDA , RECORD_DEF , RECORD_PATTERN_DEF . | ANNOTATION , ANNOTATION_FIELD_DEF , CTOR_CALL , CTOR_DEF , DOT , ENUM_CONSTANT_DEF , EXPR , LITERAL_CATCH , LITERAL_DO , LITERAL_FOR , LITERAL_IF , LITERAL_NEW , LITERAL_SWITCH , LITERAL_SYNCHRONIZED , LITERAL_WHILE , METHOD_CALL , METHOD_DEF , QUESTION , RESOURCE_SPECIFICATION , SUPER_CTOR_CALL , LAMBDA , RECORD_DEF , RECORD_PATTERN_DEF . | 3.0 |
To configure the check:
<module name="Checker"> <module name="TreeWalker"> <module name="ParenPad"/> </module> </module>
Example:
class Example1 { int n; public void fun() { bar( 1); // violation 'is followed by whitespace' } public void bar(int k ) { // violation 'is preceded with whitespace' while (k > 0) { } StringBuilder obj = new StringBuilder(k); } public void fun2() { switch( n) { // violation 'is followed by whitespace' case 2: bar(n); default: break; } } }
To configure the check to require spaces for the parentheses of constructor, method, and super constructor calls:
<module name="Checker"> <module name="TreeWalker"> <module name="ParenPad"> <property name="tokens" value="LITERAL_FOR, LITERAL_CATCH, SUPER_CTOR_CALL"/> <property name="option" value="space"/> </module> </module> </module>
Example:
class Example2 { int x; public Example2(int n) { } public void fun() { try { throw new IOException(); } catch( IOException e) { // violation 'not preceded with whitespace' } catch( Exception e ) { } for ( int i = 0; i < x; i++ ) { } } class Bar extends Example2 { public Bar() { super(1 ); // violation 'not followed by whitespace' } public Bar(int k) { super( k ); for ( int i = 0; i < k; i++) { // violation 'not preceded with whitespace' } } } }
The following cases are not checked:
for ( ; i < j; i++, j--) // no check after left parenthesis for (Iterator it = xs.iterator(); it.hasNext(); ) // no check before right parenthesis try (Closeable resource = acquire(); ) // no check before right parenthesis
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.whitespace