A Checkstyle Rule for OpenJDK's Local Variable Type Inference Style Guidelines

476 views Asked by At

Background:

OpenJDK have released a style guide for local variable type inference.

I was looking for a checkstyle built in rule that addresses it but didn't found one.

What I'm trying to get is pretty simple but I'm not that familiar with writing custom Modules.

Example:

Allow:

  • instance creation, it's clear what's being created
var something = new Anything(); // OK
  • it's a literal, it's obvious what type it has
var something = "literal"; // OK

Violation:

  • even with good naming, no clue what's the type
var something = anything.doAnything(); // Violation
  • it improves readability to use var here
Anything<Foo<Bar>> something = new Anything<>(); // Violation
  • not a big deal but just for consistency
String something = "literal"; // Violation

Question:

Is there any existing rule I can extend / tweak to get the above? Should I try writing my own rule? I assume RegExp can handle some cases but not all.

1

There are 1 answers

0
Andrei Amarfii On

You can tweak this one : enter image description here

find it here : https://checkstyle.sourceforge.io/checks/coding/matchxpath.html#MatchXpath

Note : MatchXpath is Since Checkstyle 8.39