java language specification 翻译 9.6 Annotation Types 注解类型

来源:互联网 发布:医药公司进销存软件 编辑:程序博客网 时间:2024/05/21 08:04

9.6 注解类型

一个注解类型的声明指定一个新的注解类型,一个特殊的接口类型。为了区别一个注解声明和一个一般的接口声明,在关键词interface 前加一个at-符号(@)。

AnnotationTypeDeclaration:
{InterfaceModifier} @ interface Identifier AnnotationTypeBody

注解类型声明:
{接口修饰符} @ interface 标识符 声明类型主体

注意at-符号(@)和关键词 interface 是不同的标识符。可以在它们中间加一个空格,但是并不建议这么做。

在注解类型声明中,注解修饰符的规则在9.7.4和9.7.5节中定义。

在注解类型声明中,Identifier 是注解类型的名字。

如果注解类型和任何它的内部(enclosing)的类和接口有相同的名字,就会产生一个编译时错误。

每个注解类型的直接父接口是 java.lang.annotation.Annotation

根据注解类型声明语法,一个注解类型声明不能是泛类,而且不能有 extends

因为一个注解类型不能显示声明一个父类或父接口,因此一个声明类型的子类或子接口不能是声明类型。同理,java.lang.annotation.Annotation 不是声明类型。

一个注解类型从 java.lang.annotation.Annotation 中继承了几个成员,包括隐式的从Object 中继承的方法,然而这些方法没有定义声明类型的成员(9.6.1节)。

因为这些方法没有定义注解类型的成员,在用注解类型是用它们是不合理的(9.7节)。如果没有这条规则,我们就不能保证(elements were of the types representable in annotations, or that accessor methods for them woule be available.)。

除非显示的在此修改,所有使用与普通接口定义的规则也适用于注解的定义。

例如,注解类型和普通的类和接口类型共享相同的命名空间;在定义接口合法的地方,都可以定义注解类型,并且有相同的范围和可访问性。

9.6.1 注解类型要素(Annotation Type Elements)

注解类型的主体可能包含方法们的声明,每一个都是注解类型的要素(element)。一个注解类型没有要素(elements),除非这些显示声明的方法。

AnnoatitonTypeBody:
{{AnnotationTypeMemberDeclaration}}

AnnotationTypeMemberDeclaration:
AnnotationTypeElementDeclaration
ConstantDeclaration
ClassDeclaration
InterfaceDeclaration
;

AnnotationTypeElementDeclaration:
{AnnotationTypeElementModifier} UnannTypeIdentifier () [Dims] [DefaultValue];

AnnotationTypeElementModifier:
(one of)
Annotation public
abstract

原创粉丝点击