我正在尝试找到处理@Deprecated
注释的逻辑。
在Eclipse中,打开“type hierachy”(F4)只显示我自己的AbstractProcessor实现。
有谁知道处理@Deprecated注释的类是什么?
PS。 @Deprecated
注释就是一个例子。我对其他“核心”java语言注释也感到好奇。
编辑:在GrepCode上查看了com.sun.tools.javac.processing.JavacProcessingEnvironment(一个很好的入口点是方法discoverAndRunProcs(...))我相信@ dot_Sp0T是正确的。我会因此接受他的回答。
答案 0 :(得分:2)
@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
注释由编译器本身处理(如果通过eclipse使用eclipse进行代码分析实用程序,则为resp。)。
那是如果完全处理注释。正如评论中所述,给定的编译器可能无法处理此或任何其他默认注释(可能与语言规范冲突)。
找到处理这个或另一个注释的特定类的最佳选择是搜索相应的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.
答案 1 :(得分:0)
您不必扩展AbstractProcessor
来处理注释。它只是一个方便的实现,在Java 6之前它不存在。注释的问题在于任何人都可以处理任何注释,并且正如您正确识别的那样,很难确定谁在处理注释。此外,很难区分注释的使用注释事物和注释处理。
即
@Deprecated
void deprecatedMethod()
难以区分
void deprecatedProcessing() {
if (xxx.getAnnotation(Deprecated.class)) {
/// Do something
}
}
因为如果你从二进制角度看这个类,它们都有引用java.lang.Deprecated
。如果你看一下源代码,就可以更容易确定一个是在处理(注意在反射中引用类时缺少@符号),但是如果你想对所有库进行大量搜索,你需要源代码所有这些图书馆。
我的回答是没有简单的方法来搜索代码进行注释处理。