Я пытаюсь получить все вспомогательные узлы узла AST, используя ExpressionStatements и возвращая их дочерний элемент и его дочерние дочерние элементы, но алгоритм застревает в первый 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 rea ds только a.getTheDataA(). getTheDataB(). getTheDataC(). getTheData(); и возвращает его детей и детские детей, как это:
Я застрял на этот один день, мне нужна помощь по рекурсии
Вернул тот же результат, но спасибо за помощь. ((: –