1) In my code bases, I established the convention of explicitly adding `static` to all members of @UtilityClass classes, but for a different reason: without that, the methods would not be usable with `import static`.
To help enforce conventions around @UtilityClass members being static or not, I wrote the a set of rules for my light-weight static code analysis tool
mandor:
- UtilityClassMemberInconsistentlyStatic
members should be declared consistently static / not static (i.e. within every @UtilityClass, either all members have the `static` keyword, or none)
- UtilityClassMemberMarkedStatic
members should not be declared static as it is redundant
- UtilityClassMemberWithoutStaticModifier
members should be explicitly declared static to make them usable in static imports
Obviously, the rules are mutually exclusive. Projects should pick one and add it to their unit tests. Here's an example how to run the third rule:
public class ExampleMandorTest
{
@Test
public void testRule()
{
new SourceBundle().importSources("src/main/java")
.importSources("src/test/java")
.verifyStrictly(new UtilityClassMemberWithoutStaticModifier());
}
}
For a project with 48K lines of code, this takes only about 3 seconds on my machine.