ParenPad
Since Checkstyle 3.0
Description
Checks the policy on the padding of parentheses; that is whether a
space is required after a left parenthesis and before a right
parenthesis, or such spaces are forbidden. No check occurs at
the right parenthesis after an empty for iterator, at the left
parenthesis before an empty for initialization, or at the right
parenthesis of a try-with-resources resource specification where
the last resource variable has a trailing semicolon.
Use Check
EmptyForIteratorPad to validate empty for iterators and
EmptyForInitializerPad to validate empty for initializers.
Typecasts are also not checked, as there is
TypecastParenPad to validate them.
Properties
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 |
Examples
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
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.whitespace