2016-11-11 1 views
0

ExpressionStatements를 가져 와서 하위 노드와 하위 노드를 반환하여 AST 노드의 모든 하위 노드를 가져 오려고하는데 알고리즘이 작동하지 않습니다. 첫 번째 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 함수는 ds 만 a.getTheDataA(). getTheDataB(). getTheDataC(). getTheData(); 이 같은 그의 아이들과 아이의 아이를 반환

print screen

나는 내가 무엇을보고에서 재귀

답변

0

에 도움이 필요,이 날에 갇혔어요, 당신은 오직의 첫 번째 요소를 얻을 수 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

+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);} 
관련 문제