Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify list of nullness annotations #888

Open
vdaniloff opened this issue Apr 12, 2022 · 0 comments
Open

Allow to specify list of nullness annotations #888

vdaniloff opened this issue Apr 12, 2022 · 0 comments

Comments

@vdaniloff
Copy link

vdaniloff commented Apr 12, 2022

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant