Adding Java Annotations at Runtime

It’s possible via bytecode instrumentation library such as Javassist.

In particular, take a look at AnnotationsAttribute class for an example on how to create / set annotations and tutorial section on bytecode API for general guidelines on how to manipulate class files.

This is anything but simple and straightforward, though – I would NOT recommend this approach and suggest you consider Tom’s answer instead unless you need to do this for a huge number of classes (or said classes aren’t available to you until runtime and thus writing an adapter is impossible).

Leave a Comment