Esquire Theme by Matthew Buchanan
Social icons by Tim van Damme

09

Sep

Using AspectJ for JSR 305 Annotations (@Nonnull)

For fun a made a rather useful aspect for checking not null using JSR305 annotations. The weaving will only happen if you use a @CheckForNull on the top of the class or on a particular method. Then you can decide if you want to default that values can always be @Nonnull with @ParametersAreNonnullByDefault

If your concerned with performance later on you can remove the @CheckForNull and manually add Guava’s checkNotNulls.

I should note that I have sort taking my own interpretation of @CheckForNull. FindBugs may vomit and not like it. There is probably a better annotation to use so please comment for a better recommendation.

Here is the aspect in all its glory make sure you name it ending in “.aj”: Above is an example bean that will be validated.

Above is the maven depenency you will need for the JSR305 annotations its also a transitive dependency of Guava.