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 x;
public Example1(int n) {
}
public void fun() {
try {
throw new IOException();
}
catch( IOException e) { // violation 'is followed by whitespace'
}
catch(Exception e ) {} // violation 'is preceded with whitespace'
for ( int i = 0; i < x; i++ ) { // 2 violations
// ''(' is followed by whitespace'
// '')' is preceded with whitespace'
}
}
public void fun2() {
switch( x) { // violation 'is followed by whitespace'
case 2:
break;
default:
break;
}
}
class Bar extends Example1 {
public Bar() {
super(1 ); // violation '')' is preceded with whitespace'
}
public Bar(int k) {
super( k ); // 2 violations
// ''(' is followed by whitespace'
// '')' is preceded with whitespace'
for ( int i = 0; i < k; i++) { // violation ''(' is followed by whitespace'
}
}
}
}
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++ ) {
// violation 2 lines above 'not followed by whitespace'
}
}
public void fun2() {
switch( x) {
case 2:
break;
default:
break;
}
}
class Bar extends Example2 {
public Bar() {
super(1 ); // violation 'not followed by whitespace'
}
public Bar(int k) {
super( k );
// violation below 'not preceded with whitespace'
for ( int i = 0; i < k; i++) {
}
}
}
}
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.
Fully Qualified Name
com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheck
Use this fully qualified class name in configuration when an exact class reference is required.






