没有@Target 的注释类型可以在任何地方使用吗?
Can an annotation type without a @Target be used anywhere?
Java 教程内容如下:
If an @Target meta-annotation is not present on an annotation type T , then an annotation of type T may be written as a modifier for any declaration except a type parameter declaration.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/annotation/Target.html
其他手册说,如果@Target 不存在,注释可以在除 TYPE_USE 或 TYPE_PARAMETER 场景之外的任何地方使用。
我不太确定“参数声明”在那种情况下是什么意思。这个 class 编译,注解“@EveryWhere”确实无处不在,没有任何 @Target 注解。包括转换操作、lambda 参数和泛型声明。
import java.util.ArrayList;
import java.util.function.Predicate;
@interface EveryWhere{}
public @EveryWhere class AnnotedEveryWhere<@EveryWhere T> extends @EveryWhere Object{
@EveryWhere int i = 0;
@EveryWhere <@EveryWhere T> String method(@EveryWhere ArrayList<@EveryWhere String> array) {
@EveryWhere Predicate<@EveryWhere ArrayList<@EveryWhere String>> pred =
(@EveryWhere ArrayList<@EveryWhere String> lambdaParameter)->{
@EveryWhere ArrayList<@EveryWhere String> insideLambda = new @EveryWhere ArrayList<@EveryWhere String>();
return (@EveryWhere boolean) true;};
return (@EveryWhere String) "String";
}
}
这取决于 Java 版本。 Java SE 18 说
If an @Target meta-annotation is not present on an annotation
interface T, then an annotation of type T may be written as a modifier
for any declaration.
不幸的是,https://javaalmanac.io 没有在必要的详细级别上进行比较,以找出它在哪个版本中发生了变化。
Java 教程内容如下:
If an @Target meta-annotation is not present on an annotation type T , then an annotation of type T may be written as a modifier for any declaration except a type parameter declaration.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/annotation/Target.html
其他手册说,如果@Target 不存在,注释可以在除 TYPE_USE 或 TYPE_PARAMETER 场景之外的任何地方使用。
我不太确定“参数声明”在那种情况下是什么意思。这个 class 编译,注解“@EveryWhere”确实无处不在,没有任何 @Target 注解。包括转换操作、lambda 参数和泛型声明。
import java.util.ArrayList;
import java.util.function.Predicate;
@interface EveryWhere{}
public @EveryWhere class AnnotedEveryWhere<@EveryWhere T> extends @EveryWhere Object{
@EveryWhere int i = 0;
@EveryWhere <@EveryWhere T> String method(@EveryWhere ArrayList<@EveryWhere String> array) {
@EveryWhere Predicate<@EveryWhere ArrayList<@EveryWhere String>> pred =
(@EveryWhere ArrayList<@EveryWhere String> lambdaParameter)->{
@EveryWhere ArrayList<@EveryWhere String> insideLambda = new @EveryWhere ArrayList<@EveryWhere String>();
return (@EveryWhere boolean) true;};
return (@EveryWhere String) "String";
}
}
这取决于 Java 版本。 Java SE 18 说
If an @Target meta-annotation is not present on an annotation interface T, then an annotation of type T may be written as a modifier for any declaration.
不幸的是,https://javaalmanac.io 没有在必要的详细级别上进行比较,以找出它在哪个版本中发生了变化。