Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding annotation with a given value using Byte Buddy

How can I use Byte Buddy to add an annotation with a given value?

I'm playing around with generating test classes for JUnit and I'd like to annotate a generated class by @RunWith(SomeRunner.class).

like image 972
Kimble Avatar asked Jan 08 '16 13:01

Kimble


1 Answers

You can annotate a class within the fluent API:

new ByteBuddy()
  .subclass(Object.class)
  .annotateType(AnnotationDescription.Builder.ofType(RunWith.class)
                                             .define("value", SomeRunner.class)
                                             .build())
  .make();

Alternatively to the AnnotationDescription.Builder you can also hand over a loaded annotation, the builder automatically converts it to the internal description format.

like image 192
Rafael Winterhalter Avatar answered Oct 12 '22 23:10

Rafael Winterhalter