My checkstyle regex mismatch

Checkstyle has a LineLength module.  This enables us to automatically give info messages if the code has extra long lines in it.  The LineLength module has an ignorePattern property for those long lines that are ok.  We were setting this to ignore javadoc comments with long links in them.

What it was before:


The Problem

The problem was, this regex did not match cases where the @link was on one line and the link had been formatted to the next line (as would happen sometimes when we use Eclipse’s Ctrl+Shift+F code formatter), so those still showed up as infos.

What I Want to Match

Zero or more whitespace characters,
followed by a literal ‘*’,
followed by zero or more whitespace characters,
optionally followed by ‘@see ‘ or ‘{@link ‘,
followed by one or more non-whitespace characters.

My Good-enough Solution

After messing around for quite a while, this is the regex I eventually put in place:

^.*\*.*((@see)|(\{@link) )?[^ \t]+$

Notice the .* after the initial ^ though: we’re matching on any character, not just whitespace.  I wanted to do something like this:

^\s*\*\s*((@see)|(\{@link) )?[^ \t]+$

Or this:

^[ \t]*\*[ \t]*((@see)|(\{@link) )?[^ \t]+$

But when I did that, the comment lines weren’t matched, and they still showed up as infos.

Why is that?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s