哪些类型可用于Java注释成员? [英] Which types can be used for Java annotation members?
问题描述
今天,我想创造我的第一个注释界面如下本文档并我得到了编译器错误无效类型注释成员:
Today I wanted to create my first annotation interface following this documentation and I got the compiler error "Invalid type for annotation member":
public @interface MyAnnotation {
Object myParameter;
^^^^^^
}
显然对象
不能用作类型的注释成员。不幸的是我无法找到关于哪些类型一般可以使用的任何信息。
Obviously Object
cannot be used as type of an annotation member. Unfortunately I could not find any information on which types can be used in general.
这我发现使用试验和错误:
This I found out using trial-and-error:
字符串
- >
有效
INT
- >
有效
整数
- >
无效(奇怪)
的String []
- >
有效(令人惊讶的)
String[]
-->
Valid (Surprisingly)
对象
- >
无效
也许有人可以阐明哪些类型实际上是允许一些光,为什么。
Perhaps someone can shed some light on which types are actually allowed and why.
推荐答案
这是由的的JLS 第9.6.1。注释成员类型必须之一:
It's specified by section 9.6.1 of the JLS. The annotation member types must be one of:
- 原始
- 字符串
- 类
- 枚举
- 另一个注释
- 上述任何 的阵列
它似乎限制,但无疑也有原因吧。
It does seem restrictive, but no doubt there are reasons for it.
这篇关于哪些类型可用于Java注释成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!