How to get parent tree node from JCTree (com.sun.tools.javac.tree.JCTree) in Java?
For example:
public class MyTreeTranslator extends TreeTranslator {
@Override
public void visitMethodDef(JCTree.JCMethodDecl jcMethodDecl) {
// get jcMethodDecl's parent tree
}
}
You can't. The tree is one-way. Only strategy is to find the associated 'top'
JCCompilationUnitand walk it recursively (this takes a very long time), keeping track of parentage. Then when you find the JCTree, you have the parent.-- EDIT --
Paying a little closer attention to your snippet, you don't have to double-walk it here: Get rid of
visitMethodDefentirely; instead, write avisitXmethod for every place a method can appear. Which, currently, is only within type definitions (visitTypeDef(JCTree.JCTypeDecl), I think). for-each through all the method defs defined in each (this will take someinstanceofcreativity, or alternatively invokingapplyon it with a parameterized visitor).