I am playing around with the CheckerFramework and wanted to extract the effective @Nullable/@NonNull annotations of the return value and parameters of a method, e.g. some.package.Thing: Object compute(Object,Collection)
. The only way I found so far, is to generate source code that uses this method in different nullness contexts, so that I can infer the annotations from the checker results. But I am pretty sure that there is a way to extend the NullnessChecker, so that I can give it a method handle (obtained through reflection) of method on the classpath and export the effective nullness annotations. Can anybody give me some hints on where to start?
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 run extract-annotations mypackage.MyClass
to create a text file mypackage.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.