Byte Buddy嵌套注释

时间:2017-11-03 15:04:18

标签: byte-buddy

我想使用Byte-Buddy生成注释,我可以通过简单的注释完成此操作。什么是正确的Byte-Buddy语法来生成嵌套注释?

例如,我想生成以下注释@MessageDriven,其中包含嵌套注释:

@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")
        })

我目前的语法有什么问题?

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();

例外是:

线程中的异常" main" java.lang.IllegalArgumentException:[interface javax.ejb.ActivationConfigProperty]无法分配给activationConfig         at net.bytebuddy.description.annotation.AnnotationDescription $ Builder.define(AnnotationDescription.java:852)         at net.bytebuddy.description.annotation.AnnotationDescription $ Builder.defineTypeArray(AnnotationDescription.java:1041)         在net.bytebuddy.description.annotation.AnnotationDescription $ Builder.defineTypeArray(AnnotationDescription.java:1029)

谢谢!

1 个答案:

答案 0 :(得分:2)

我能够找出使用嵌套注释创建类文件的正确语法:

        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();

在类文件中生成以下内容:

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