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

标签 java annotations

我试图找到处理 @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/

相关文章:

java - 内存不足错误,同时在 java 中读取大型 CSV 文件(数百万行)

java - 我正在为我的应用程序构建 googlemap 跟踪,现在我被困了五天多了,我认为,,简单''问题

java - Jackson JsonView 和 JsonTypeInfo

java - "MediaType.APPLICATION_JSON"的注释类型 Produces 的属性值未定义

java - @ElementCollection Java持久化(Hibernate)导致加载重复实例

java - java中的缓存

java Float.MAX_VALUE 转 Double

java - 注释m :n relationship with helper class (2 foreign keys + additional attributes) with JPA 2. 0

java - 由于缺少私有(private)字段,将 json 解析为 myObj 失败

java - 如何将字符串转换为日期对象?