首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Byte Buddy嵌套注释

Byte Buddy嵌套注释
EN

Stack Overflow用户
提问于 2017-11-03 23:04:18
回答 1查看 129关注 0票数 0

我想使用Byte-Buddy来生成注释,我能够为简单的注释实现这一点。生成嵌套注释的正确Byte-Buddy语法是什么?

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

代码语言:javascript
复制
@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")
        })

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

代码语言:javascript
复制
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中出现异常:无法将接口javax.ejb.ActivationConfigProperty分配给位于net.bytebuddy.description.annotation.AnnotationDescription$Builder.defineTypeArray(AnnotationDescription.java:1029)的net.bytebuddy.description.annotation.AnnotationDescription$Builder.define(AnnotationDescription.java:852)处的activationConfig

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-11-04 00:27:25

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

代码语言:javascript
复制
        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();

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

代码语言:javascript
复制
@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 = "")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47098810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档