DEV Community

Matt Sicker
Matt Sicker

Posted on

StackOverflow: Debug Java annotation processors using Intellij and Maven

I've been working on a new annotation processor in Log4j in order to support the new plugin dependency injection system. Inspired by the configuration performance gains in frameworks like Micronaut, I hoped to emulate part of the idea by generating metadata at compile time that can be queried at runtime to determine which plugins and beans to bother loading. This can save a decent amount of startup time on reflection and class path scanning, and it allows for caching more metadata in the future.

However, annotation processors in Java are notoriously finicky. I had initially tried to debug the code using logging, but the complexity of our Maven build makes it difficult to properly surface those debug logs without reducing build performance. Eventually, I looked into how I might be able to just set a breakpoint in my annotation processor and experiment with some live data to figure out why the API wasn't returning metadata I'd normally expect from the equivalent reflection APIs. This fantastic explanation from StackOverflow covers how to set up debugging.

In the end, most of my confusion in the annotation processing API resulted from a misunderstanding of how the @Inherited annotation works. In particular, annotations on an interface are not inherited regardless of what you try. In fact, this annotation only applies when used on annotations placed on class definitions. As it turns out, projects like Spring have a lot of boilerplate to support its fancy meta-annotations and inheritance system.

Here is the recipe.

Sidenote: I made it really detailed in some cases, skip the parts you already know how to do.

  1. First of all, download and install Maven, then download and install IntelliJ IDEA (referred to as IDEA from here on). (If you don't know how to use…

Discussion (0)