Java 如何在注释中声明对象数组?
鉴于我们无法创建注释的层次结构, 如何使用对象数组存储不同注释的有序列表Java 如何在注释中声明对象数组?,java,Java,鉴于我们无法创建注释的层次结构, 如何使用对象数组存储不同注释的有序列表 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
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
需要的是我有一个这样的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>
但它的语法并不完全相同,因为这里我使用了@Tab数组和@Field数组,但我不能有不同注释的数组,我希望,我明白你的意思了。您希望实现以下目标:
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
@MyAnnotationContainer(
{@MyAnnotation1, @MyAnntation2, @MyAnntation3}
)
class MyClass {}
这在java中是不可能的。但实际上你并不需要这个。您可以将注释直接放在类上:
@MyAnnotation1
@MyAnnotation2
@MyAnnotation3
class MyClass {}
现在,您可以从类中获取声明的注释,这样您将实际获得所需的有序列表。显然,您可以只过滤相关注释,而忽略其他注释 你不能。这就是错误消息告诉您的。谢谢,但是在注释中存储有序列表的正确方法是什么?因为@JBNizet您不能这样做。但是,如果你解释一下你想要实现什么,我们可能会帮助你找到其他解决方案。我的英语不是很好,所以很难解释所有的问题。你可以使用数组,但它必须是一个包含基元类型、字符串、类、注释或枚举的数组。你确实理解这个需求,但解决方案并不合适,无论如何,谢谢。解决方案必须是层次结构。层次结构不能通过注释直接实现。然而,这听起来像是一个著名的xy问题。试着解释一下你的主要任务是什么。也许我们会建议你更好的解决方案。我会补充更多细节,希望你理解我在做什么