1

I wanted to analyse a method body during compilation time and potentially introduce a minor modification to it. I have used com.sun.source.util.Trees.getTree(ExecutableElement) and TreeVisitor. Unfortunately, TreeVisitor.visit*Type(...) methods don't provide a full type path (for instance instead of java.util.List visitor returns just List).

The only reasonable option that I have found was Trees.getTypeMirror(TreePath) and Trees.getPath(CompilationUnitTree, Tree). As expected it doesn't work for types defined outside the current compilation unit (I don't have any idea how to access a compilation unit that contains java.util.List definition).

I also thought about imports analysis, but it doesn't solve the problem with wildcard imports.

I have also read responses to issue How do I find the type declaration of an identifier using the Java Tree Compiler API? which seems a bit similar to my problem, however, the code section lacks a lot of methods definitions.

AndyB
  • 490
  • 1
  • 4
  • 7

0 Answers0