Search code examples
javastatic-analysischeckstyle

How to enforce static imports for some methods using checkstyle?


How to enforce static imports for some methods using checkstyle?

For example

I want the following method to be used only from static import:

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

So code like this shouldn't be allowed:

if (Objects.nonNull(varName)) {

Any ideas how to achieve that with (preferably) standard or non standard tools?


Solution

  • You could forbid the String that imports Objects:

    <module name="RegexpSinglelineJava">
        <!-- Please statically import methods -->
        <property name="format" value="import java.util.Objects;"/>
    </module>