简体   繁体   English

声明注释和类型注释之间的区别

[英]Difference between declaration annotations and type annotations

Java 8 introduced type annotations with JSR308. Java 8使用JSR308引入了类型注释。 According to the Java language specifications 根据Java语言规范

type annotations can be used anywhere a type is used, such as declarations, generic arguments, casts etc. 类型注释可以在任何使用类型的地方使用,例如声明,泛型参数,强制转换等。

I'm relatively new to Java, Java 8 was the first version of java I used, so I'm not familiar with non-'type annotations', ie declaration annotations. 我是Java的新手,Java 8是我使用的第一个java版本,所以我不熟悉非'type注释',即声明注释。

How are declaration annotations different from type annotations? 声明注释与类型注释有何不同? I want to know because I keep hearing about them in manuals and it looks like 'type annotations' are a superset of 'declaration annotations'. 我想知道,因为我一直在手册中听到它们,看起来“类型注释”是“声明注释”的超集。

Most annotations right now are declaration annotations, for example @Override : 现在大多数注释都是声明注释,例如@Override

class Foo implements Runnable {
    @Override // applies to the declaration of Foo.run()
    public void run() {
    }
}

Type annotations expand the possible annotation targets to uses of types which are not declarations (casts, type arguments and so on). 类型注释将可能的注释目标扩展为使用声明类型(强制转换,类型参数等)。 Type annotations can appear in declarations, but type annotations are not a superset of declaration annotations. 类型注释可以出现声明中,但类型注释不是声明注释的超集。 For example, @Override is never a type annotation. 例如, @Override永远不是类型注释。

In cases which are ambiguous, for example @Foo int x; 在含糊不清的情况下,例如@Foo int x; , JLS §9.7.4 outlines specific rules for whether it's the declaration or type that's considered to be annotated . JLS§9.7.4概述了是否是被认为是注释的声明或类型的具体规则 In some cases, it's even considered as both. 在某些情况下,它甚至被视为两者。

The following are a few unambiguous examples of type annotations: 以下是类型注释的一些明确示例:

// cast
String str = (@Foo String) take();
// type argument
List<@Foo String> list = new ArrayList<>();

I suppose I could also add that the issue with type annotations at the moment is that there's no official API that lets an annotation processor (basically a compiler plug-in) do anything meaningful with them. 我想我还可以补充说,目前类型注释的问题在于没有官方的API允许注释处理器(基本上是编译器插件)对它们做任何有意义的事情。 As a result of this, type annotations are generally only used by third-party tools, like Lombok and the Eclipse compiler. 因此,类型注释通常仅由第三方工具使用,如Lombok和Eclipse编译器。

Both type annotations and declaration annotations still exist in Java, and they are distinct and non-overlapping. 类型注释和声明注释在Java中仍然存在,并且它们是不同的且不重叠的。

A type annotation can be written on any use of a type . 类型注释可以在任何类型的使用上编写。 It conceptually creates a new, more specific type. 它在概念上创建了一种新的,更具体的类型。 That is, it describes what values the type represents. 也就是说,它描述了类型所代表的值。

As an example, the int type contains values ..., -2, -1, 0, 1, 2, ... 例如, int类型包含值..., - 2,-1,0,1,2,...
The @Positive int type contains values 1, 2, ... @Positive int类型包含值1,2,...
Therefore, @Positive int is a subtype of int . 因此, @Positive int是的子类型int

A declaration annotation can be written on any declaration (a class, method, or variable). 声明注释可以写在任何声明 (类,方法或变量)上。 It describes the thing being declared, but does not describe run-time values. 它描述了被声明的内容,但没有描述运行时值。 Here are examples of declaration annotations: 以下是声明注释的示例:

@Deprecated
class MyClass { ... }

says that programmers should not use MyClass . 说程序员不应该使用MyClass

@Override
void myMethod() { ... }

says that myMethod overrides a declaration in a superclass or interface. myMethod会覆盖超类或接口中的声明。

@SuppressWarnings(...)
int myField = INITIALIZATION-EXPRESSION;

says that the compiler should not issue warnings about code in the initialization expression. 说编译器不应该在初始化表达式中发出有关代码的警告。

Here are examples that use both a declaration annotation and a type annotation: 以下是使用声明注释和类型注释的示例:

@Override
@NonNull String myMethod() { ... }

@GuardedBy("myLock")
@Regex String myField;

Note that the type annotation describes the value, and the declaration annotation says something about the method or use of the field. 请注意,类型注释描述了值,声明注释说明了该字段的方法或用法。

As a matter of style, declaration annotations are written on their own line, and type annotations are written directly before the type, on the same line. 作为一种风格,声明注释是在它们自己的行上写的,类型注释直接写在类型之前,在同一行上。

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

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