ParenPad
Since Checkstyle 3.0
Description
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++) {
}
}
}
}
When the check is configured with default settings:
<module name="Checker">
<module name="TreeWalker">
<module name="ParenPad"/>
</module>
</module>
The following cases are intentionally not checked. 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:
class Example3 {
void m(int i, int j, Iterator xs, Closeable resource) throws Exception {
for ( ; i < j; i++, j--) {} // ok, no check after left '('
for (Iterator it = xs; it.hasNext(); ) {} // ok, no check before right ')'
try (Closeable r = resource; ) {} // ok, no check before right ')
}
}
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.






