0

I'm working on java checkstyle configuration in Eclipse. I need to add warning for all public methods that don't consist specific code in the first line. Example:

public void doA(){
    blabla();
    //some code
}

public String doB(int i){
    blabla();
    //some code
}

public Boolean doC(String str){
    //some code
}

What I want from checkstyle is to mark the line or report error when there is method that has public modifier and doesn't start with calling blabla() method. I tried to make some multiline regexp strings with /n as a newline but so far no success.

edit: so far I tried this but without success:

<module name="RegexpMultiline">
    <property name="format" value="(public)(\\s+)((?:[a-z][a-z0-9_]*))(\\s+)((?:[a-z][a-z0-9_]*))(\\(.*\\))(\\{)(/n)(blablabla)"/>
    <property name="message" value="Public method should have blablabla."/>
</module>

1 Answers1

2

Use Pattern.MULTILINE directive in the search pattern: "(?m)...\\s*...". This applies the pattern across lines.

Use Pattern.DOTALL directive in the search pattern: "(?s)...\\s*...". This captures '\nwith a.`.

Use it combined: "(?ms)...".

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thanks for an answer but how I should use it in the checkstyle xml? So far I tried one solution but doesn't work. I added it to original post above. – user1942920 Jan 02 '13 at 14:40
  • 1
    This is not going to work, because you need a regex which matches when `blabla();` is *not* present. This called a "negative lookahead assertion". I fiddled with my Checkstyle for a while and could not get it to accept the `(?!...)` construct, even though a hand-coded java.util.regex pattern matcher worked fine. You may end up having to write a custom check. – barfuin Jan 02 '13 at 19:59
  • @Thomas: Personally I would rename blabla, and make a `@Deprecated` blabla. But would have thought that something like `"(?ms)\npublic[^\n{]+\\s*{(?!\\bblabla\\().)*}\n\n"` *(errorneous, to-do)* would work. – Joop Eggen Jan 03 '13 at 05:38