[코드 재 구성 & JDT] AST 를 옮 겨 다 니 며 각 노드 의 모든 직접 하위 노드 를 가 져 옵 니 다.

public class DataNode {
	public ASTNode node; //     AST  
	public int label; //  
	public List childrenLables = new ArrayList<>(); //         
	public List childrenNodes = new ArrayList<>(); //      
	public boolean isLeaf = false; //       
	public String nodeType = "unknown";
}




public static int ID = 0; //    

//     CompilationUnit   , label 0
public static void getDirectChildren(ASTNode node, int label, Map Nodes){
	
	//           
	DataNode myNode = new DataNode();
	Nodes.put(label, myNode);
	myNode.label = label;
	myNode.node = node;
	myNode.nodeType = node.getClass().toString();
	List listProperty = node.structuralPropertiesForType();
	boolean hasChildren = false;
	for(int i = 0; i < listProperty.size(); i++){
		StructuralPropertyDescriptor propertyDescriptor = (StructuralPropertyDescriptor) listProperty.get(i);
		if(propertyDescriptor instanceof ChildListPropertyDescriptor){//ASTNode  
			ChildListPropertyDescriptor childListPropertyDescriptor = (ChildListPropertyDescriptor)propertyDescriptor;
			Object children = node.getStructuralProperty(childListPropertyDescriptor);
			List childrenNodes = (List)children;
			for(ASTNode childNode: childrenNodes){
				//      
				if(childNode == null)
					continue;
				hasChildren = true;
				myNode.childrenNodes.add(childNode);
				myNode.childrenLables.add((++ID));
				getDirectChildren(childNode, ID, Nodes);//    
				//System.out.println("childrenList:   "+childNode+"   "+childNode.getClass());
			}
			
		}else if(propertyDescriptor instanceof ChildPropertyDescriptor){//  ASTNode
			ChildPropertyDescriptor childPropertyDescriptor = (ChildPropertyDescriptor)propertyDescriptor;
			Object child = node.getStructuralProperty(childPropertyDescriptor);
			ASTNode childNode = (ASTNode)child;
			if(childNode == null)
				continue;
			hasChildren = true;
			//       
			myNode.childrenNodes.add(childNode);
			myNode.childrenLables.add((++ID));
			getDirectChildren(childNode, ID, Nodes);//    
			
			//System.out.println("child:   "+childNode +"  "+childNode.getClass());
		}
	}
	if(hasChildren){
		//       
		myNode.isLeaf = false;
	}
	else{
		//  ,     
		myNode.isLeaf = true;
	}
}

좋은 웹페이지 즐겨찾기