0

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

Brett Walker
  • 3,566
  • 1
  • 18
  • 36

2 Answers2

0

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:

Eclipse Checkstyle Paren Pad config

barfuin
  • 16,865
  • 10
  • 85
  • 132
  • two things. The first, I should have said that I used checkstyle exclusively through maven so this interface is new to me. Maybe I should learn how to use checkstyle in eclipse also. The second, from my understanding Left Parenthesis and Method call are separate tokens. They cannot be combined to given the desired effect I need. I have updated the question to be more explicit with my needs. Thanks for your response. – Brett Walker Jan 01 '13 at 06:12
  • Thanks for the clarification. Updated my answer accordingly. – barfuin Jan 01 '13 at 09:14
0

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.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36