0

I would like to use Byte-Buddy to generate annotations and I am able to accomplish this for simple annotations. What is the correct Byte-Buddy syntax to generate nested annotations?

For example I would like to generate the following annotation @MessageDriven which contains nested annotations:

@MessageDriven(
        activationConfig={
            @ActivationConfigProperty(propertyName="destination", propertyValue="remoteBidsWantedJMS/TOPIC.BIDSWANTED.QUOTEWANTEDSEVENT"),
            @ActivationConfigProperty(propertyName="providerAdapterJNDI", propertyValue="java:/RemoteBidsWantedJMSProvider"),
            @ActivationConfigProperty(propertyName="reconnectAttempts", propertyValue="60"),
            @ActivationConfigProperty(propertyName="reconnectInterval", propertyValue="10"),
            @ActivationConfigProperty(propertyName="acknowledgeMode", propertyValue="Auto-acknowledge"),
            @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Topic"),
            @ActivationConfigProperty(propertyName="subscriptionDurability", propertyValue="NonDurable"),
            @ActivationConfigProperty(propertyName="maxSession", propertyValue="1")
        })

What is wrong with my current syntax?

DynamicType.Unloaded dynamicTypeBuilder2 = new ByteBuddy()
                  .redefine(QuoteWantedsEventProcessorBean.class)
                  .name("com.tmcbonds.messaging.QuoteWantedsEventProcessorBean_BYTEBUDDY_REDEFINE_" + i)
                  .annotateType(AnnotationDescription.Builder.ofType(Pool.class)
                          .define("value", "TESTVALUE")
                          .build())
                  .annotateType(AnnotationDescription.Builder.ofType(MessageDriven.class)
                          .defineTypeArray("activationConfig", ActivationConfigProperty.class)
                          .define("propertyName", "destination")
                          .define("propertyValue", "remoteBidsWantedJMS/TOPIC.BIDSWANTED.QUOTEWANTEDSEVENT")
                          .build())
                  .make();

Exception is this:

Exception in thread "main" java.lang.IllegalArgumentException: [interface javax.ejb.ActivationConfigProperty] cannot be assigned to activationConfig at net.bytebuddy.description.annotation.AnnotationDescription$Builder.define(AnnotationDescription.java:852) at net.bytebuddy.description.annotation.AnnotationDescription$Builder.defineTypeArray(AnnotationDescription.java:1041) at net.bytebuddy.description.annotation.AnnotationDescription$Builder.defineTypeArray(AnnotationDescription.java:1029)

Thanks!

ckeenan
  • 21
  • 2
  • 1
    Hi, and welcome to StackOverflow. I've made an edit to your question (it may still be pending peer review and thus not visible) to better highlight the provided code. In future it really helps if you could format your question so that code is easily readable by the rest of us, see [this page](https://stackoverflow.com/editing-help) to see how you can format code in the future. – Jake Conkerton-Darby Nov 03 '17 at 15:26

1 Answers1

2

I was able to figure out the correct syntax to create the class file with the nested annotations:

        Unloaded<QuoteWantedsEventProcessorBean> dynamicTypeBuilder = new ByteBuddy()
                  .redefine(QuoteWantedsEventProcessorBean.class)
                  .name("com.tmcbonds.messaging.QuoteWantedsEventProcessorBean_BYTEBUDDY_REDEFINE_" + i)
                  .annotateType(AnnotationDescription.Builder.ofType(MessageDriven.class)
                          .defineAnnotationArray(
                                  "activationConfig",
                                  new TypeDescription.ForLoadedType(ActivationConfigProperty.class),
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "messageSelector")
                                        .define("propertyValue", "" + i)        
                                        .build(),
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "destination")
                                        .define("propertyValue", "remoteBidsWantedJMS/TOPIC.BIDSWANTED.QUOTEWANTEDSEVENT")
                                        .build(),
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "providerAdapterJNDI")
                                        .define("propertyValue", "java:/RemoteBidsWantedJMSProvider")
                                        .build(),                                           
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "reconnectAttempts")
                                        .define("propertyValue", "60")
                                        .build(),
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "reconnectInterval")
                                        .define("propertyValue", "10")
                                        .build(),
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "acknowledgeMode")
                                        .define("propertyValue", "Auto-acknowledge")
                                        .build(),
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "destinationType")
                                        .define("propertyValue", "javax.jms.Topic")
                                        .build(),
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "subscriptionDurability")
                                        .define("propertyValue", "NonDurable")
                                        .build(),
                                  AnnotationDescription.Builder.ofType(ActivationConfigProperty.class)
                                        .define("propertyName", "maxSession")
                                        .define("propertyValue", "1")
                                        .build()
                            )
                          .build())
                  .make();

Which generates the following in the class file:

@MessageDriven(description = "", activationConfig = {
        @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "0"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "remoteBidsWantedJMS/TOPIC.BIDSWANTED.QUOTEWANTEDSEVENT"),
        @ActivationConfigProperty(propertyName = "providerAdapterJNDI", propertyValue = "java:/RemoteBidsWantedJMSProvider"),
        @ActivationConfigProperty(propertyName = "reconnectAttempts", propertyValue = "60"),
        @ActivationConfigProperty(propertyName = "reconnectInterval", propertyValue = "10"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
        @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "NonDurable"),
        @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1") }, mappedName = "", messageListenerInterface = Object.class, name = "")
ckeenan
  • 21
  • 2