如何找到一个ASTNode的所有子节点(children和childrens children)
How to find all sub nodes (children and childrens children) of an ASTNode
我试图通过获取 ExpressionStatements 并返回它们的 child 及其子 children 来获取 AST 节点的所有子节点,但是算法卡在第一个 ExpStat 和我找不到原因。
首先我创建了一个访问者函数来查找我的 class 的所有 ExpressionStatements,然后我调用该函数来查找您的 children
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");
}
}
}
递归查找children的函数:
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;
}
}
假设 class 有:
a.getTheDataA().getTheDataB().getTheDataC().getTheData();
b.getTheDataA().getTheDataB().getTheDataC().getTheData();
c.getTheE(a,b).getTheF(getTheDataB).getTheH();
getChildren 函数只读取 a.getTheDataA().getTheDataB().getTheDataC().getTheData(); returns 他的 children 和 children children 像这样:
print screen
有一天我卡在这上面了,我需要递归方面的帮助
据我所知,你只能得到 children
的第一个元素,我认为你需要将检查 children
元素是否不为 null 的语句取出到一个单独的 for
循环,并检查其中的每个元素。
类似于:
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);
}
}
for(ASTNode node : children){
if (node != null) {
String c = children.toString();
results.append("Children Node: " + c + "\n");
getChildren(node);
}
}
}else {
return;
}
}
我没有 运行 代码,但我认为问题在于您只能获得 children
的第一个元素
已解决!
public static int getChildren(ASTNode node,int n) {
int cont = n;
String compara = "[]";
List<ASTNode> children = new ArrayList<ASTNode>();
@SuppressWarnings("rawtypes")
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);
}
}
String teste = children.toString();
// Se a string do filho for igual a [] -> CHEGOU AO FIM
//e retorna resultado do contador para analyseClass
if (teste.equals(compara)) {
results.append("NMCS = "+cont+"\n");
return cont;
}
// Aumenta o contador se o nó filho for MethodInvocation ou
//SuperMethodInvocation
if (node.getNodeType() == 32) {
cont++;
} else if (node.getNodeType() == 48) {
cont++;
}
// Recursão para encontrar próximo nó (filho do filho)
return getChildren(children.get(0),cont);}
我试图通过获取 ExpressionStatements 并返回它们的 child 及其子 children 来获取 AST 节点的所有子节点,但是算法卡在第一个 ExpStat 和我找不到原因。
首先我创建了一个访问者函数来查找我的 class 的所有 ExpressionStatements,然后我调用该函数来查找您的 children
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");
}
}
}
递归查找children的函数:
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;
}
}
假设 class 有:
a.getTheDataA().getTheDataB().getTheDataC().getTheData();
b.getTheDataA().getTheDataB().getTheDataC().getTheData();
c.getTheE(a,b).getTheF(getTheDataB).getTheH();
getChildren 函数只读取 a.getTheDataA().getTheDataB().getTheDataC().getTheData(); returns 他的 children 和 children children 像这样:
print screen
有一天我卡在这上面了,我需要递归方面的帮助
据我所知,你只能得到 children
的第一个元素,我认为你需要将检查 children
元素是否不为 null 的语句取出到一个单独的 for
循环,并检查其中的每个元素。
类似于:
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);
}
}
for(ASTNode node : children){
if (node != null) {
String c = children.toString();
results.append("Children Node: " + c + "\n");
getChildren(node);
}
}
}else {
return;
}
}
我没有 运行 代码,但我认为问题在于您只能获得 children
已解决!
public static int getChildren(ASTNode node,int n) {
int cont = n;
String compara = "[]";
List<ASTNode> children = new ArrayList<ASTNode>();
@SuppressWarnings("rawtypes")
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);
}
}
String teste = children.toString();
// Se a string do filho for igual a [] -> CHEGOU AO FIM
//e retorna resultado do contador para analyseClass
if (teste.equals(compara)) {
results.append("NMCS = "+cont+"\n");
return cont;
}
// Aumenta o contador se o nó filho for MethodInvocation ou
//SuperMethodInvocation
if (node.getNodeType() == 32) {
cont++;
} else if (node.getNodeType() == 48) {
cont++;
}
// Recursão para encontrar próximo nó (filho do filho)
return getChildren(children.get(0),cont);}