哪些类型可用于 Java 注释成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1458535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:29:07  来源:igfitidea点击:

Which types can be used for Java annotation members?

javaannotations

提问by Daniel Rikowski

Today I wanted to create my first annotation interface following this documentationand I got this compiler error

今天我想按照这个文档创建我的第一个注释界面,但我收到了这个编译器错误

Invalid type for annotation member":
public @interface MyAnnotation {
    Object myParameter;
    ^^^^^^
}
Invalid type for annotation member":
public @interface MyAnnotation {
    Object myParameter;
    ^^^^^^
}

Obviously Objectcannot be used as type of an annotation member. Unfortunately I could not find any information on which types can be used in general.

显然Object不能用作注释成员的类型。不幸的是,我找不到任何关于一般可以使用哪些类型的信息。

This I found out using trial-and-error:

这是我通过反复试验发现的:

  • String→ Valid
  • int→ Valid
  • Integer→ Invalid (Surprisingly)
  • String[]→ Valid (Surprisingly)
  • Object→ Invalid
  • String→ 有效
  • int→ 有效
  • Integer→ 无效(令人惊讶)
  • String[]→ 有效(令人惊讶)
  • Object→ 无效

Perhaps someone can shed some light on which types are actually allowed and why.

也许有人可以阐明哪些类型实际上是允许的以及为什么。

采纳答案by skaffman

It's specified by section 9.6.1 of the JLS. The annotation member types must be one of:

它由JLS 的第 9.6.1 节指定。注释成员类型必须是以下之一:

  • primitive
  • String
  • an Enum
  • another Annotation
  • Class
  • an array of any of the above
  • 原始
  • 细绳
  • 枚举
  • 另一个注解
  • 班级
  • 以上任何一个的数组

It does seem restrictive, but no doubt there are reasons for it.

它看起来确实有限制,但毫无疑问是有原因的。

Also note that multidimensional arrays (e.g. String[][]) are implicitly forbidden by the above rule.

还要注意,多维数组(例如String[][])被上述规则隐式禁止。

Arrays of Class are not allowed as described in this answer.

本答案所述,不允许使用 Class 数组。

回答by KLE

I agree with Skaffman for the Types available.

我同意 Skaffman 的可用类型。

Additional restriction : it has to be a compile-time constant.

附加限制:它必须是编译时常量

For example, the following are forbidden:

例如,禁止以下行为:

@MyAnnot("a" + myConstantStringMethod())
@MyAnnot(1 + myConstantIntMethod())

回答by Josh

The concept of annotations fits really well with the design of my project, until I realized you can't have complex datatypes in the annotation. I got around it by using the class of what I wanted to instantiate rather than an instantiated object of that class. It's not perfect, but java rarely is.

注释的概念非常适合我的项目设计,直到我意识到注释中不能有复杂的数据类型。我通过使用我想要实例化的类而不是该类的实例化对象来解决它。它并不完美,但 Java 很少如此。

@interface Decorated { Class<? extends PropertyDecorator> decorator() }

interface PropertyDecorator { String decorate(String value) }

class TitleCaseDecorator implements PropertyDecorator {
    String decorate(String value)
}

class Person {
    @Decorated(decorator = TitleCaseDecorator.class)
    String name
}

回答by fikovnik

Also don't forget that annotations themselves can be part of an annotation definition. This allows some simple annotation nesting - handy in cases where you would like to have one annotation present many times.

也不要忘记注解本身可以是注解定义的一部分。这允许一些简单的注释嵌套 - 在您希望多次出现一个注释的情况下很方便。

For example:

例如:

@ComplexAnnotation({
    @SimpleAnnotation(a="...", b=3),
    @SimpleAnnotation(a="...", b=3),
    @SimpleAnnotation(a="...", b=3)
})
public Object foo() {...}

where SimpleAnnotationis

这里SimpleAnnotation

@Target(ElementType.METHOD)
public @interface SimpleAnnotation {
    public String a();
    public int b();
)

and ComplexAnnotationis

并且ComplexAnnotation

@Target(ElementType.METHOD)
public @interface ComplexAnnotation {
    public SimpleAnnotation[] value() default {};
)

Examples taken from: http://web.archive.org/web/20131216093805/https://blogs.oracle.com/toddfast/entry/creating_nested_complex_java_annotations

示例取自:http: //web.archive.org/web/20131216093805/https: //blogs.oracle.com/toddfast/entry/creating_nested_complex_java_annotations

(original URL: https://blogs.oracle.com/toddfast/entry/creating_nested_complex_java_annotations)

(原文网址:https: //blogs.oracle.com/toddfast/entry/creating_nested_complex_java_annotations