sevntu-checkstyle / sevntu.checkstyle

Additional Checkstyle checks, that could be added as extension to EclipseCS plugin and maven-checkstyle-plugin, Sonar checkstyle plugin, extension for CheckStyle IDEA plugin.
http://sevntu-checkstyle.github.io/sevntu.checkstyle/
190 stars 147 forks source link

Allow to specify list of nullness annotations #888

Open vdaniloff opened 2 years ago

vdaniloff commented 2 years ago

http://sevntu-checkstyle.github.io/sevntu.checkstyle/apidocs/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheck.html

/var/tmp $ cat SpringTest.java

package org.example;

import java.time.Clock;
import org.springframework.beans.factory.annotation.Autowired;

public class SpringTest {
    private final Clock clock;

    public SpringTest(@Autowired Clock clock) {
        this.clock = clock;
    }
}

/var/tmp $ cat config.xml

<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="Jsr305Annotations">
            <property name="packages" value="org.example"/>
        </module>
    </module>
</module>

For Linux users:

java -classpath checkstyle-10.1-all.jar:sevntu-checks-1.41.0.jar com.puppycrawl.tools.checkstyle.Main -c config.xml SpringTest.java

For Windows users:

C:\tmp> java -classpath checkstyle-10.1-all.jar;sevntu-checks-1.41.0.jar com.puppycrawl.tools.checkstyle.Main -c config.xml SpringTest.java

Starting audit...
[ERROR] C:\work\checkstyle\SpringTest.java:9:27:
 No nullness Annotation for parameter 
definition found. [Jsr305Annotations]

Audit done.
Checkstyle ends with 1 errors.    

While actually there is definitely no defect in validation logic, it could be valueable to have configuration option to provide list of annotations, which mark parameters/return values as no needing validation. For example: Spring @Autowired injection checks for nullness by provided required argument (true by default).

This can also be used for some other nullness annotations like NotNull, used commonly in REST services, other Dependency Injection framework-related annotations, IDE specific nullness annotations such as org.jetbrains.annotations.NotNull and so on.

Could be similar to Checkstyle JavadocType configuration

<module name="Jsr305Annotations">
   <property name="allowedAnnotations" value="Autowired,NonNull"/>
 </module>