2012-04-23 70 views
2

我想用另一個treepathscanner訪問方法體,但我不知道,要傳遞哪個參數,因爲我總是在TreePath構造函數中得到一個NPE。Java編譯器樹api:NPE

@Override 
public Object visitMethod(MethodTree node, Trees p) { 
    ... 
    node.getBody().accept(new MethodVisitor(m, repository), node.getBody()); 
    ... 
} 


public class MethodVisitor extends TreePathScanner<Object, BlockTree> {...} 

例外:

An annotation processor threw an uncaught exception. 
Consult the following stack trace for details. 
java.lang.NullPointerException 
    at com.sun.source.util.TreePath.<init>(TreePath.java:94) 
    at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:65) 
    at com.sun.source.util.TreeScanner.scan(TreeScanner.java:90) 
    at com.sun.source.util.TreeScanner.visitBlock(TreeScanner.java:158) 
    at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:764) 
    at cz.cvut.kbss.odpa.parser.visitor.OdpaVisitor.visitMethod(OdpaVisitor.java:93) 
    at cz.cvut.kbss.odpa.parser.visitor.OdpaVisitor.visitMethod(OdpaVisitor.java:29) 
    at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:676) 
    at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:67) 
    ... 

感謝您的幫助。

回答

2

萬一有人需要它,叫它正確的做法是:

new MethodVisitor(m, repository).scan(p.getPath(compilationUnitTree, node), node.getBody());