添加一个stackoverflow上的回答吧。以下
Java's standard annotation processors provide a callback only at declarations, such as method and field declarations.
It's possible to use an annotation processor to analyze expressions, but it's a bit more work. You will have to write an annotation processor that, at each method, obtains the AST (abstract syntax tree or parse tree) for the method and then visits each expression in the method. The AST is compiler-specific.
The two best-known projects that do this are the Checker Framework and Project Lombok. Maybe you could take inspiration from their implementations, or even write your annotation processor on top of one of them.
大概意思是,java annotation processer 只能拿到声明级别的信息,如类,方法的声明相关信息,拿不到其定义的内容。要想拿到类和方法定义的具体内容,需要使用到AST。
鉴于我的需要只是拿到源文件的内容,我还是使用读取文件的方式好了。。。