我想构造 ParseTree 的子树,但唯一包含 insertChild(index,Object) 方法的类似乎是 CommonTree,我无法传递 RuleContext 类型的对象,因为它尝试将其转换为 Tree 对象,我得到“ java.lang.ClassCastException:Java8Parser$ClassDeclarationContext 无法转换为 org.antlr.runtime.tree.Tree”异常。
有任何想法吗?谢谢。(编辑:添加代码)
private void getSubtrees(ParseTree tree){
CommonTree commonTree = new CommonTree();
setRecursiveDescendants(commonTree, tree);
}
private CommonTree setRecursiveDescendants(CommonTree commonTree,ParseTree parseTree){
int n = parseTree.getChildCount();
for (int i = 0; i < n; i++) {
commonTree.insertChild(i, parseTree.getChild(i);
setRecursiveDescendants(commonTree, parseTree.getChild(i));
}
return commonTree;
}