Enforcing null-checks in Java/Android applications
-
## 1. Rationale Null references are considered to be one of the most expensive mistakes in IT design. It's not surprising that there are numerous efforts to solve it. Here are a couple of examples from the Java world: * Kotlin fights it at the language level * many tools try to report it as early as possible, for example, IntelliJ IDEA can be configured to [warn](https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html) us about a possible NPE. Moreover, when the code below is compiled by the IDE, it automatically inserts null-checks, i.e. given the code snippet below ```java public void service(@NotNull String input) { // Process the input } ``` Resulting bytecode looks like if it's compiled from the following source: ```java public void service(@NotNull String input) { if (input == null) { throw new NullPointerException("Argument for @NotNull parameter 'input' must not be null"); } // Process the input } ``` *Kotlin* really solves the problem but *Java* is still a very popular language, so, we have to deal with nullable values. It's not always convenient to use *IntelliJ* build system for compiling sources to get that *null*-checks and the code which explicitly ensures preconditions via *checkNotNull()* or explicit *if (input == null) { throw new NullPointerException("") }* also looks not that appealing. More common setup is to configure a build through [*Gradle*](https://gradle.org/)/[*Maven*](http://maven.apache.org/)/[*Ant*](https://ant.apache.org/). It would not harm to get *IDE* tips on possible *null*-related problems and that auto-generated runtime checks without explicitly putting them into code. Current tool solves the second problem - it allows to add *null*-checks into *\*.class* files during compilation based on source code annotations. ## 2. License The project is licensed by MIT. ## 3. Design The whole project is based on a *Javac* extension. It plugs into the compilation process and automatically inserts *null*-checks into the resulting bytecode. That can be used either explicitly from the command line or from *Maven*/*Ant*. Also, there is a *Gradle* plugin which simplifie
-
## 1. Rationale Null references are considered to be one of the most expensive mistakes in IT design. It's not surprising that there are numerous efforts to solve it. Here are a couple of examples from the Java world: * Kotlin fights it at the language level * many tools try to report it as early as possible, for example, IntelliJ IDEA can be configured to [warn](https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html) us about a possible NPE. Moreover, when the code below is compiled by the IDE, it automatically inserts null-checks, i.e. given the code snippet below ```java public void service(@NotNull String input) { // Process the input } ``` Resulting bytecode looks like if it's compiled from the following source: ```java public void service(@NotNull String input) { if (input == null) { throw new NullPointerException("Argument for @NotNull parameter 'input' must not be null"); } // Process the input } ``` *Kotlin* really solves the problem but *Java* is still a very popular language, so, we have to deal with nullable values. It's not always convenient to use *IntelliJ* build system for compiling sources to get that *null*-checks and the code which explicitly ensures preconditions via *checkNotNull()* or explicit *if (input == null) { throw new NullPointerException("") }* also looks not that appealing. More common setup is to configure a build through [*Gradle*](https://gradle.org/)/[*Maven*](http://maven.apache.org/)/[*Ant*](https://ant.apache.org/). It would not harm to get *IDE* tips on possible *null*-related problems and that auto-generated runtime checks without explicitly putting them into code. Current tool solves the second problem - it allows to add *null*-checks into *\*.class* files during compilation based on source code annotations. ## 2. License The project is licensed by MIT. ## 3. Design The whole project is based on a *Javac* extension. It plugs into the compilation process and automatically inserts *null*-checks into the resulting bytecode. That can be used either explicitly from the command line or from *Maven*/*Ant*. Also, there is a *Gradle* plugin which simplifie
-
The above post should start with a link to the actual tool. Rest of the verbiage makes it difficult to find it and I think that the actual link doesn't even work.
Thank you for the feedback! I've corrected the links. About the post organization - not sure that it would be better to give a link without explaining what the tool is. Regards, Denis
-
Thank you for the feedback! I've corrected the links. About the post organization - not sure that it would be better to give a link without explaining what the tool is. Regards, Denis