我试图通过获取ExpressionStatements并返回它们的子节点及其子节点来获取AST节点的所有子节点,但算法陷入了第一次ExpStat和我找不到原因。如何查找ASTNode的所有子节点(儿童和儿童孩子)
首先我创建了一个访客功能,找到我的类的所有ExpressionStatements,然后调用该函数找到你的孩子
private void analyseClass(ICompilationUnit classe) throws JavaModelException {
// ICompilationUnit unit == class
// now create the AST for the ICompilationUnits
CompilationUnit parse = parse(classe);
// Calls the method for visit node in AST e return your information
ExpressionStatementVisitor visitor = new ExpressionStatementVisitor();
parse.accept(visitor);
// Write in the screen: ExpressionStatement and your type next
for (ExpressionStatement method : visitor.getExpression()) {
//String t = null;
// 32 -> METHOD_INVOCATION type
if (method.getExpression().getNodeType() == 32) {
getChildren(method);
results.append("\n\n");
}
// 48 -> SUPER_METHOD_INVOCATION type
else if (method.getExpression().getNodeType() == 48) {
// results.append("\n SuperMethodInvocation: " + t);
//getChildren(method);
//results.append("\n\n");
} else {
//getChildren(method);
//results.append("\n\n");
}
}
}
找到孩子递归函数:
public static void getChildren(ASTNode node) {
if (node != null) {
List<ASTNode> children = new ArrayList<ASTNode>();
List list = node.structuralPropertiesForType();
for (int i = 0; i < list.size(); i++) {
Object child = node.getStructuralProperty((StructuralPropertyDescriptor) list.get(i));
if (child instanceof ASTNode) {
children.add((ASTNode) child);
}
if (children.get(0) != null) {
String c = children.toString();
results.append("Children Node: " + c + "\n");
getChildren(children.get(0));
}
}
} else {
return;
}
}
我们假设在课堂上有:
a.getTheDataA().getTheDataB().getTheDataC().getTheData();
b.getTheDataA().getTheDataB().getTheDataC().getTheData();
c.getTheE(a,b).getTheF(getTheDataB).getTheH();
getChildren function rea ds只有a.getTheDataA()。getTheDataB()。getTheDataC()。getTheData();并返回他的孩子和儿童的孩子是这样的:
我卡在这一天,我需要从我所看到的递归
返回相同的结果,但感谢您的帮助。 ((: –