简体   繁体   English

如何声明一个对象数组?

[英]how to declare an Array of Object in annoation?

Given that we can't create a hierarchy of annoations, why-is-not-possible-to-extend-annotations-in-java 鉴于我们无法创建注释层次结构, 因此为什么无法在Java中扩展注释

how to decalare an array of object to store an ordered list of different annotations 如何对对象数组进行贴花处理以存储不同批注的有序列表

public @interface View {
    Object[] elements(); 
//Invalid type Object[] for the annotation attribute View.elements; only 
primitive type, String, Class, annotation, enumeration are permitted
 or 1-dimensional arrays thereof
}
Elements could be @Field, @Tab, @Listing .... but i need to know the order

The needs is that i have a xml hierarchy tree like this 需要的是我有一个像这样的xml层次树

<view id="StkEntranceVoucher" name="StkEntranceVoucher" >
   <listingHeader />
   <listing >
      <ref mode="vp" target="titleText"/>
      <ref mode="vp" target="voucherNumber"/>      
   </listing>
   <panel>
      <tab>
         <tab>            
            <panel>
                 <field name="title" />
            </panel>
            <field name="titleText" />
            <field name="voucherNumber">
        </tab>
      </tab>
    </panel>
</view>

i want to be able to generate this XML config, with anotation configuration on the model : i am already able to do this 我希望能够生成该XML配置以及模型上的注释配置:我已经能够做到这一点

@View(queryFilter=@QueryFilter(orderBy="(?).id.num"), detailSize="580px",
    listingHeader=@ListingHeader(actions="printAll"),
    listing={ "id.num", "reference", "onDate", "fromSection", "supplier", 
                "totalField", "buyOrder.id.num", "buyOrder.onDate", "receiptVcr.id.num", "receiptVcr.onDate",
                "prdExitOrd.id.num", "prdExitOrd.onDate", "invExtract.id.num", "invExtract.onDate" }, 
    tabs={
        @Tab(name="GeneralTab",
        SubTabs={ 
            @SubTab( name="HeaderTab",
                style =@Style(type=StlType.print, colWidths={"50", "400"}, width="450", marginB=2, marginT=8 ),
                fields={                
                    @Field(name="titleText", mode="p", valueExpr="#{bean.mapID( wcx,'reportTitle')}", expectedType=String.class,
                        style=@Style(type=StlType.print, align="center", bold=true, fontSize=16, marginT=20, paddingB=4, paddingL=4, paddingR=4, paddingT=4, textAlign="center", width="200")),             
                    @Field(name="voucherNumberText", mode="p", valueExpr="#{bean.mapID( wcx,'VoucherNum')} : #{(?).id.num} #{bean.mapID( wcx,'VoucherOf')} #{util.format((?).onDate, 'dd/MM/yyyy')}", expectedType=String.class,
                        style=@Style(type=StlType.print, align="center", fontSize=10, textAlign="center" )),                        
                    @Field(name="id.sgSr", mode="", queryFilter=true),  
                    @Field(name="id.num", width="60px", mode="v"),
                    @Field(name="reference"),
                    @Field(name="onDate", mode="v"),                    
                    @Field(name="fromSection"),
                    @Field(name="supplier") }
                ),
            @SubTab( name="ItemsTab", subView=
                @SubView( name="StkEntranceVoucherItem", entityType=StkInputVoucherItem.class, editable=true, 
                    listingFields={ 
                        @Field(name="id.vcr", mode="", queryFilter=true),
                        @Field(name="id.item", width="60px", codeLabel=true),
                        @Field(name="id.item.label", width="150px", mode="rvp"),
                        @Field(name="quantity", width="60px"),
                        @Field(name="id.item.measureUnit", width="50px", mode="rpv"),
                        @Field(name="unitPrice", width="60px"),
                        @Field(name="amount", width="80px", mode="rvp", 
                                calcExpr="#{(?).quantity*(?).unitPrice}"),
                        @Field(name="batch", jsfCpn=JsfCpn.button, linkedViewName="StkInputVoucherBatch", 
                                styleClass="no_padding", width="24px"), 
                        @Field(name="comment", width="100px") }) ),
            @SubTab( name="StkEntranceVoucherTotalTab", style=@Style(css="float:right;"),  
                fields={ 
                    @Field( name="totalField", ofColFooter="amount", mode="rvp",
                        valueExpr="(?).amount", expectedType=Double.class,
                        calcExpr="#{(?)==bean.record?bean.sum(':amount'):(?).amount}") }) 
            })
    })

but its not fully the same syntax because here i use an array of @Tab and an array of @Field but i can't have an array of different annotations 但是它的语法不完全相同,因为在这里我使用@Tab数组和@Field数组,但是我不能使用不同注解数组

I hope, I got your idea. 希望我知道您的想法。 You want to achieve something like the following: 您想要实现以下目标:

@MyAnnotationContainer(
{@MyAnnotation1, @MyAnntation2, @MyAnntation3}
)
class MyClass {}

It is impossible in java. 在Java中是不可能的。 But you actually do not need this. 但是您实际上不需要这个。 You can put your annotations directly on the class: 您可以将注释直接放在类上:

@MyAnnotation1
@MyAnnotation2
@MyAnnotation3
class MyClass {}

Now you can get declared annotations from your class, so that you will actually get the ordered list that you want. 现在,您可以从类中获取已声明的批注,以便实际上将获取所需的有序列表。 Obviously you can filter the relevant annotations only and omit other (not yours) annotations. 显然,您只能过滤相关的注释,而忽略其他(不是您的)注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM