2016-11-11 7 views
0

私はASTノードのすべてのサブノードを取得しようとしていますが、ExpressionStatementsを取得してその子とその子を返しますが、最初のExpStatと私はなぜ見つけることができません。ASTNodeのすべてのサブノード(子供と子供の子供)を見つける方法

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

まず私はその後、私は再帰的に子供たちを見つけるために、機能あなたの子供

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

を見つけるために、関数を呼び出し、私のクラスのすべてのExpressionStatementsを見つけるために訪問者機能を作成しましたクラス内にあるとしましょう:

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();そして、このように彼の子供と子供の子供を返します。

print screen

私は私が見たものから再帰

答えて

0

に助けが必要な、この1日にこだわっている、あなたは今までの最初の要素を取得children、私はchildren要素がヌルでないかどうかを確認するステートメントチェックを取り出して、別の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

+0

同じ結果が返されましたが、助けてくれてありがとうございます。 ((: –

0

の最初の要素に解決を得ることだと思います!

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