How to find all sub nodes (children and childrens children) of an ASTNode

I'm trying to get all the sub nodes of an AST node by taking ExpressionStatements and returning their child and its sub children, but the algorithm gets stuck in the first ExpStat and I can not find why.

First I created a visitor function to find all the ExpressionStatements of my class, then I call the function to find your 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");
        }
    }
}

The function to find the children recursively:

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; 
    }       
}

Let's say in class have:

a.getTheDataA().getTheDataB().getTheDataC().getTheData();
b.getTheDataA().getTheDataB().getTheDataC().getTheData();
c.getTheE(a,b).getTheF(getTheDataB).getTheH();

The getChildren function reads only a.getTheDataA().getTheDataB().getTheDataC().getTheData(); and returns his children and childrens children like this:

print screen

I'm stuck on this one day, I need help on recursion

Upvotes: 0

Views: 1652

Answers (2)

RESOLVED!

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);} 

Upvotes: 0

Tim
Tim

Reputation: 609

from what I see, you only ever get the first element of children, I think you need to take out the statement checking to see if childrenelement is not null into a separate for loop, and check each element within it.

Something like:

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; 
    }       
}

I haven't run the code, but I think the issue is that you only get first element of children

Upvotes: 1

Related Questions