gpt4 book ai didi

java - @Deprecated 注释是否由 AbstractProcessor 处理?

转载 作者:行者123 更新时间:2023-11-30 08:39:03 25 4
gpt4 key购买 nike

我试图找到处理 @Deprecated 注释的逻辑。

在 Eclipse 中,打开“类型层次结构”(F4) 只会显示我自己的 AbstractProcessor 实现。

有谁知道处理@Deprecated注解的类叫什么?

附言。 @Deprecated 注释就是一个例子。我也很好奇其他“核心”Java 语言注释。

编辑:看过com.sun.tools.javac.processing.JavacProcessingEnvironment (一个好的切入点是 GrepCode 的方法 discoverAndRunProcs(...)) 我相信 @dot_Sp0T 是正确的。因此,我会接受他的回答是正确的。

最佳答案

看看 Javadoc for @deprecated

@Documented
@Retention(value=RUNTIME)
@Target(value={CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE})
public @interface Deprecated

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.

Since:

1.5

See The Java™ Language Specification:

9.6.3.6 @Deprecated

从这个 Javadoc 条目中我们了解到注释在运行时仍然存在,因此我们可以假设它没有在声明代码中处理。

javadoc 的最后一行将我们引向 this section of the Java Language Specification

9.6.4.6. @Deprecated

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.

A Java compiler must produce a deprecation warning when a type, method, field, or constructor whose declaration is annotated with @Deprecated is used (overridden, invoked, or referenced by name) in a construct which is explicitly or implicitly declared, unless:

- The use is within an entity that is itself annotated with the annotation @Deprecated; or
- The use is within an entity that is annotated to suppress the warning with the annotation @SuppressWarnings("deprecation"); or
- The use and declaration are both within the same outermost class.

Use of the @Deprecated annotation on a local variable declaration or on a parameter declaration has no effect.

The only implicitly declared construct that can cause a deprecation warning is a container annotation (§9.7.5). Namely, if T is a repeatable annotation type and TC is its containing annotation type, and TC is deprecated, then repeating the @T annotation will cause a deprecation warning. The warning is due to the implicit @TC container annotation. It is strongly discouraged to deprecate a containing annotation type without deprecating the corresponding repeatable annotation type.

因此,据我所知,@deprecated 注释由编译器本身处理(resp 在使用 eclipse 的情况下由 eclipse 的代码分析实用程序......)。
也就是说,如果 注释被处理了。如评论中所述,给定的编译器可能无法处理此注释或任何其他默认注释(唉,这可能与语言规范冲突)。

要找到正在处理这个或另一个注解的特定类,最好的办法是搜索相应的 JVM 源(如果你接触到它们)以查找对注解的引用,然后自己分析代码以确定它是否真的确实处理它。
一个可能对您有帮助的网站是 GrepCode .该站点具有资源集合、用于筛选它们的(或多或少用户友好的)搜索实用程序以及(最重要的)正确的语法突出显示。


扩展这个问题:查找有关 java 内置信息的最佳位置是 Java Language Specification (v8) (在这里引用太大了)。

要查找有关 java 内置注释的具体信息,您可以查看 the predefined annotation types :

9.6.4. Predefined Annotation Types 

Several annotation types are predefined in the libraries of the Java SE platform. Some of these predefined annotation types have special semantics. These semantics are specified in this section. This section does not provide a complete specification for the predefined annotations contained here in; that is the role of the appropriate API specifications. Only those semantics that require special behavior on the part of a Java compiler or Java Virtual Machine implementation are specified here.

关于java - @Deprecated 注释是否由 AbstractProcessor 处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36303384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com