spring-io / spring-javaformat

Apache License 2.0
797 stars 110 forks source link

Add checkstyle rule to prevent Objects.requireNonNull() #339

Closed mhalbritter closed 1 month ago

mhalbritter commented 2 years ago

We should add a rule preventing calls to Objects.requireNonNull(), instead we should use Assert.notNull() from Spring Framework. Objects.requireNonNull() throws a NullPointerException, while Assert.notNull() throws a IllegalArgumentException.

wilkinsona commented 1 month ago

Unfortunately, this can't be done robustly using Checkstyle. Its AST is created from parsing the source. As such, it doesn't contain enough information to be certain that it's java.lang.Objects.requireNonNull() that's being called and not requireNonNull() on some other class named Objects. We'll check it instead in Boot's own build using ArchUnit. ArchUnit works with bytecode so it knows exactly what's being called.