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

Support @NonNull at package-info.java level #93

Open
cykl opened this issue Mar 5, 2022 Discussed in #89 · 3 comments
Open

Support @NonNull at package-info.java level #93

cykl opened this issue Mar 5, 2022 Discussed in #89 · 3 comments
Labels
needs discussion needs-research PR welcome A PR submitted for this issue would be welcome

Comments

@cykl
Copy link
Contributor

cykl commented Mar 5, 2022

Discussed in #89

Originally posted by cykl March 3, 2022
A common way to manage nullability annotations is to put a default @nonnull annotation at method / parameter / field level using package-info.java and then only add @nullable where relevant. Rational being that it is far more common to want something @nonnull than @nullable.

The idiomatic way to do it is to define an annotation like that

@Target( { ElementType.PACKAGE, ElementType.TYPE } )
@Retention( RetentionPolicy.RUNTIME )
@Documented
@Nonnull
@TypeQualifierDefault( ElementType.FIELD )
public @interface NonNullFields {
}

And then apply it to the package-info.java:

@NonNullApi
@NonNullFields
package my.package;

It tried to define a such annotation for record components and apply it. But record-builder seems to not care.

@Target( { ElementType.PACKAGE, ElementType.TYPE } )
@Retention( RetentionPolicy.RUNTIME )
@Documented
@Nonnull
@TypeQualifierDefault( { ElementType.RECORD_COMPONENT } )
public @interface NonNullRecordComponents {
} 
@cykl
Copy link
Contributor Author

cykl commented Mar 5, 2022

I spent a little time trying to understand how @TypeQualifierDefault works and how it impacts an annotation processor without much success. I set breakpoints in RecordBuilderProcessor#process and tried to figure an expression that would return the @nonnull annotation on the record member or constructor parameter.

If someone has some knowledge on this topic, any help would be appreciated.

I will skim through Findbugs, NullAway and others source code.

@Randgalt Randgalt added PR welcome A PR submitted for this issue would be welcome needs discussion needs-research labels Mar 21, 2022
@agentgt
Copy link

agentgt commented Apr 8, 2022

@cyki Using Findbug @TypeQualifierDefault is very legacy way of doing it.

The newer way is to use TYPE_USE annotations like JSpecify, Checker or Eclipse (possibly IntelliJ depending on which one you use). It is the correct way to do this.

Thus I recommend you check out the newer/better tools like Checker that support those annotations. It will also be much much easier to support if #106 is fixed. This is because it is part of the type. So wherever the type is it will be annotated correctly.

@cykl
Copy link
Contributor Author

cykl commented Apr 11, 2022

Thanks for the hint @agentgt .

State of nullness checking was so depressing that I put myself into hibernation a few years ago waiting for some sanity to appear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs discussion needs-research PR welcome A PR submitted for this issue would be welcome
Projects
None yet
Development

No branches or pull requests

3 participants