Assigned
Status Update
Comments
ku...@google.com <ku...@google.com>
ku...@google.com <ku...@google.com> #2
When we fix nullability in Jetpack libraries, it is very painful for clients. Also for us attempting to land in g3.
I think this is a good idea; however, I'm not sure if we'd run into any issues since these are currently synthetic annotations generated by Metalava. It's possible that creating real annotations might conflict with the SDK stubs.
Description
# Description:
########################################################################
Javax package contains "@ParametersAreNonnullByDefault" that can be used to force "@Nonnull" by default. To achieve cleaner implementation code and to do this in Android, can we add a "@ParametersAreNonNullByDefault"? This way developers do not have to specify both "@Nullable" and "@NonNull". We can assume "@NonNull" and only specify "@Nullable" when needed if they choose to.
########################################################################
# Examples:
########################################################################
For Example:
@NonNull
public String someMethod(@Nullable String input) {
return "" + input;
}
Can be written as:
public String someMethod(@Nullable String input) {
return "" + input;
}
Since there is an empty String that "input" is appending to, this will always be non-null regardless of the String input.
A common Android example would be the following from Fragments:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment, parent, false);
}
Can be written as:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup parent, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment, parent, false);
}
Since we are inflating a View, as long as "main_fragment" exists in the code base, this will return a non-null View. If this method fails to inflate the layout, it will throw an "InflateException". For "inflater" variable, this is annotated as "@NonNull" and so we can drop the inherited annotation for cleanliness. Since we know both "parent" and "savedInstanceState" variables are labeled as "@Nullable", we can simply keep these to let the developers know these can potentially be "null" before using them.
########################################################################
# Annotation should be added in:
########################################################################
androidx.annotation:annotation:x.x.x
########################################################################
# Documentation:
########################################################################
########################################################################
# References:
########################################################################