appendix A Static analysis annotations

 

To make static analysis more efficient, it’s useful to augment your Java program with additional hints. This is usually done via a special set of annotations distributed as a separate package. Throughout the book, we mentioned some of them. Notably, we discussed many nullity annotations, like @Nullable, in chapter 5. Here, I’d like to provide an overview of popular annotation packages and describe some commonly used annotations, so you may have a better idea about how they can help to reduce mistakes in your application.

Note that even some standard Java annotations may allow you to tailor static analysis to your API to some extent. The simplest is the @Deprecated annotation, available in JDK, which discourages users from using a particular API at all. In this case, the Java compiler itself plays the role of static analyzer and warns you. In Java 9, this annotation was extended; now, it’s possible to make the warning stronger by specifying the @Deprecated(forRemoval = true) parameter, which should encourage clients to migrate to something else.

A.1 Annotation packages

Most static analyzers either provide their own packages or understand annotations declared in other packages. Here’s a list of some popular packages used in Java projects:

A.2 Kinds of annotations