问题
I am new to Checkstyle though I use it mainly through the Maven Checkstyle plugin.
I have a situation where I think Checkstyle lacks flexibility. It could be me, not being familiar with Checkstyle and its configuration options.
The problem I have is with the ParenPad check with a token type of METHOD_CALL; I believe.
In the majority of the cases I don't allow any spaces after nor before the parenthesis in a method call.
e.g. object.method(arg1); Case 1
But when I am using StringBuffer or StringBuilder in building an SQL statement, text for an email or some other long string; I like to provide a LTR feel to the statements used in build up the long string so that it is easier to read.
e.g. buffer.append("String 1 "); Case 2
buffer.append( variable);
buffer.append( " String 2");
The style that I am trying to get is to allow none, one or many spaces after the left parenthesis of a mehod call but no spaces before the right parenthesis of the method call.
It seems that I cannot configure Checkstyle to allow this. I can get it to accept Case 1 and reject Case 2 or visa-versa but to get it to accept both I need to disable the ParenPad test all together which I find too drastic. I want both cases to be accepted and enforced using the checkstyle rules.
Please advise how I might accomplish this using Checkstyle. What configuration do I need? Are there 'tricks' in using Checkstyle to accomplish this? Are there Checkstyle extension to do what I need? Or do I need to write a custom check?
Seeking any help or advice.
Thanks in advance
回答1:
I have found a work around that I am using now but would like the situation to be improved. I have modified the Checkstyle rule for ParenPad to be as follows:
<module name="ParenPad">
<property name="option" value="nospace"/>
<property name="tokens" value="CTOR_CALL,LPAREN,RPAREN,SUPER_CTOR_CALL"/>
</module>
Basically it does no rule check for ParenPad for a Method call. While no spaces are enforced for all other occurrences of parenthesis, it is a free-for-all in regards to parenthesis used for a Method call.
What I need is to enforce no space before the right parenthesis of a Method call but allow none, one or more spaces after a left parenthesis of a Method call.
回答2:
You can use the tokens
property to achieve the desired effect. Just leave out the LPAREN
token in order to release the restriction from the left parenthesis:
<module name="ParenPad">
<property name="tokens" value="CTOR_CALL,METHOD_CALL,RPAREN,SUPER_CTOR_CALL"/>
</module>
In Eclipse, this would look like so:
来源:https://stackoverflow.com/questions/14048428/checkstyle-usage-of-parenpad-check