Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an Annotation Processor to create a list of classes with a certain annotation

I have a custom annotation that I've implemented and I'd like to use an annotation processor to generate a list of all the classes in my app that use that particular annotation.

I've found this tutorial which describes how to generate a class file using an annotation processor, so it should be quite easy to generate a class for each class with my annotation.

What I can't figure out is how I can collect all of that information into a single class. There doesn't seem to be a way to modify a class, so I can't append new items to the list once the class has been generated the first time.

Is there a way to use an annotation processor to generate a method that will return the list of all classes in an app that are annotated with a particular annotation?

like image 756
emmby Avatar asked Jan 30 '14 16:01

emmby


People also ask

What is an annotation processor?

Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.

How do I write an annotation processor?

To create our custom Annotation Processor we need to make a class that extends AbstractProcessor which defines the base methods for the processing. We have to override four methods to provide our implementations for the processing.

How does annotation processing work?

An annotation processor processes these annotations at compile time or runtime to provide functionality such as code generation, error checking, etc. The java. lang package provides some core annotations and also gives us the capability to create our custom annotations that can be processed with annotation processors.

Which of the following is annotation processing tool?

The apt tool is a command-line utility for annotation processing.


1 Answers

Generated classes do not necessarily have to correspond one-to-one to the input classes being processed. Plus, you can search for classes (Elements) that are annotated with a given annotation via the RoundEnvironment:

roundEnvironment.getElementsAnnotatedWith(MyAnnotation.class)

From this you can generate a single class with a method that returns a collection of the classes found.

A couple issues around this to highlight:

  • Annotation processors can run along with other annotation processors and thus have to deal with classes generated at compile time. To aid this, Java annotation processing is performed in rounds to allow processors to catch the outputs of others. To be compatible with other processors you need to gracefully handle the ErrorType.
  • Only classes in the current compilation pass are returned from the RoundEnvironmnet methods so classes in external libraries will not be included.
  • IDEs (cough cough Eclipse) implement the annotation processing facilities of Java differently which can be a problem for processors that require a full non-partial compilation like I've described.

Coincidentally, I created a similar project recently that does what you are looking for:

https://github.com/johncarl81/silver

Silver is very much a WIP and uses a lot of library code to accomplish the task, but it may give you an idea for what's possible.

like image 93
John Ericksen Avatar answered Sep 28 '22 05:09

John Ericksen