I use IntelliJ's null-checking mechanism to prevent nullpointer crashes. I've successfully set up all Java method parameters to be @NonNull by default using this answer.
After creating package-info.java which is used to define package annotations in Java. All java files which are direct descendants of that package have methods with default @NonNull parameters in my Android Studio project.
The obvious problem is that I need to define @ParametersAreNonnullByDefault for all Java classes in that package i.e. including all subpackages.
How can I define @ParametersAreNonnullByDefault so that it propagates down to all subpackage java files? I want to have all of my internal code's method annotated with @NonNull by default.
/**
* File: package-info.java
* Make all method parameters @NonNull by default
*/
@ParametersAreNonnullByDefault
package com.intive.nearbyplaces.main;
import javax.annotation.ParametersAreNonnullByDefault;
The problem was that I couldn't force Android Studio to respect @ParametersAreNonnullByDefault in all subpackages (i.e. recursively for all java files in com.company.name)
I wrote a gradle script, however, to check whether package-info.java files are generated inside each of the subpackage folders and create it if necessary. The script runs just before assembleDebug task.
So it turns out it is possible to force @ParametersAreNonnullByDefault annotation on all java classes in your project.
Download the source here. Remember to replace line 19 with your package name.
Usage:
Remember to include package-info.java files in .gitignore
Checkstyle rule:
checks whether package-info.java is included in each subpackage.
Source:
Extended version which generates package info files for src main, test & androidTest folders can be found here.
Findbugs & Lint:
It does work with findbugs & lint.