我正在玩 CheckerFramework 并想提取方法的返回值和参数的有效 @Nullable/@NonNull 注释,例如some.package.Thing: Object compute(Object,Collection)
。到目前为止,我发现的唯一方法是生成在不同空值上下文中使用此方法的源代码,以便我可以从检查器结果中推断出注释。但是我很确定有一种方法可以扩展 NullnessChecker,这样我就可以在类路径上给它一个方法句柄(通过反射获得)并导出有效的空值注释。谁能给我一些关于从哪里开始的提示?
1 回答
As background, the Checker Framework lets you write annotations on types, such as List<@NonNull String>
, but it also applies defaulting and inference.
The final type annotation is written to the class file. Therefore, you can use tools that read the class file.
javap -v MyFile.class
will show you a lot of information including the type annotations.The Annotation File Utilities read annotations from, and write annotations to,
.java
files,.class
files, and text files. This is what I would use, but I am not sure of your use case. I would compile the.java
file, then runextract-annotations mypackage.MyClass
to create a text filemypackage.MyClass.jaif
. A human or a tool can read that file.If the annotations have run-time retention (most annotations, such as
@Nullable
, do), you can also obtain them via reflection. This requires you to load the class under analysis, however. You can see a tutorial or another Stack Overflow question.