Checkstyle and Generics
I'm trying to correct all Checkstyle warnings for my project, but I keep stumbling 开发者_JAVA技巧over something very annoying when I need to construct a class that takes a Generics argument. Take the following line, for example:
labels = new HashSet < String >();
Then Checkstyle complains that '">" is not followed by whitespace'. So I write the whitespace:
labels = new HashSet < String > ();
And now it complains that '"(" is preceded by whitespace'.
Is this a bug? Is there a way to bypass it without adding a @SupressWarnings annotation?
Make sure you are running at least the 5.0 version of Checkstyle as it fixed the handling of generics. See the release notes http://checkstyle.sf.net/releasenotes.html.
It probably thinks the >
is a greater than operator because there is a space before it.
The common way to space generics declarations is like this:
labels = new HashSet<String>();
Unless your checkstyle rules are tweaked, the above should most likely pass.
I have Checkstyle version 6.17 in use and I got this resolved by removing GENERIC_START
and GENERIC_END
from the tokens
field in WhitespaceAround rule.
精彩评论