ckeenan
ckeenan

Reputation: 21

Byte Buddy Nested Annotations

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!

Upvotes: 0

Views: 212

Answers (1)

ckeenan
ckeenan

Reputation: 21

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 = "")

Upvotes: 2

Related Questions