可以在任何地方使用无@target的注释类型吗?

发布于 2025-01-22 19:28:25 字数 1224 浏览 2 评论 0 原文

Java教程说了以下内容:

如果注释类型t上不存在@target元通道,则可以将类型T的注释写为任何声明的修饰符,除非类型参数声明。

其他手册说,如果@target不存在@target,则不存在注释可以在任何位置使用type_use或type_parameter方案。

在这种情况下,我不确定“参数声明”的含义。该类编译,注释“ @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";
    }

}

The Java Tutorials says what follows:

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

Other manuals says that if @Target is not present an annotation can be used in any place except TYPE_USE or TYPE_PARAMETER scenarios.

I'm not really sure about what "parameter declaration" means in that case. This class compiles, and the annotation "@EveryWhere" is present really everywhere, without any @Target annotation. Including cast operations, lambda parameters and generics declarations.

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";
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2025-01-29 19:28:25

这取决于Java版本。 Java SE 18说

如果注释上不存在@target元通道
接口t,然后可以写入T型的注释作为修饰符
对于任何声明。

不幸的是 https://javaalmanac.io 不需要在哪个版本上找到它更改的版本所必需的详细级别。

It depends on Java version. Java SE 18 says

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.

Unfortunately https://javaalmanac.io does not do diffs at the level of detail necessary to find out in which version it changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文