1

ASTVisitor 的默认实现以深度优先的方式遍历。因此可以按出现的顺序获取源文件。我的要求是从 ASTNode 构建一个 java 树

我的动力来自以下

  • API 来比较 AST?

  • http://www.programcreek.com/2011/11/use-jdt-astparser-to-parse-java-file/

  • Eclipse 抽象语法树差异

    public class AST_Json_Bulid_tree 
    

    {

    static private List  sourcecodeStatObj = new LinkedList();  
    
    public static void parse(String str, final String path) 
    {
        ASTParser parser = ASTParser.newParser(AST.JLS8 );
        parser.setSource(str.toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT); 
        parser.setResolveBindings(true); // we need bindings later on
        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    
        try{
            cu.accept(new ASTVisitor() 
            {           
    
            public boolean visit(ForStatement node) 
            {               
                List<String> initializer = node.initializers();
                Expression exp = node.getExpression();
                String expression = "";
                List updater = node.updaters();             
                int length = node.getBody().getLength();                        
                int startposition = node.getStartPosition();
    
    
                int startLineNumber = cu.getLineNumber(node.getStartPosition());
                int nodeLength = node.getLength();                      
                int endLineNumber = cu.getLineNumber(node.getStartPosition() + nodeLength);
    
                boolean isexpNull = false ;
    
                forcount += 1;
    
                if(exp == null)
                {
                    isexpNull = true;
                }
    
    
    
    
                if(!isexpNull)
                {
                    this.names.add(exp.toString());
                    expression = exp.toString();                                
                }               
    
                Map forobj=new LinkedHashMap();
                forobj.put("Node_Type", "FOR_STATEMENT");
                forobj.put("keyword", "for");
    
                forobj.put("Initializers", initializer.toString());
                forobj.put("Expression", expression);
                forobj.put("Updater", updater.toString());  
    
                forobj.put("CF.BeginLine, CF.EndLine", "(" + startLineNumber + ", "  + endLineNumber + ")");
    
                String astjsonText = JSONValue.toJSONString(forobj);
    
                try 
                    {
                        sourcecodeStatObj.add(forobj);
                    } 
    
                catch (Exception e) 
                    {                       
                        e.printStackTrace();
                    }
    
                //Accept the visit of the other parts
                //super.visit(node);
                if(!isexpNull)
                {                           
                    exp.accept(this);
                }
    
                for (Iterator it = node.initializers().iterator(); it.hasNext(); ) 
                {
                    Expression e = (Expression) it.next();
                    e.accept(this);
    
                }
    
    
                for (Iterator it = node.updaters().iterator(); it.hasNext(); ) 
                {
                    Expression e = (Expression) it.next();
                    e.accept(this);                         
                }
    
                node.getBody().accept(this);
    
    
                return false; // continue 
            }
    
            public boolean visit(WhileStatement node) 
            {
                // Similar code goes here
                return false; // don't continue to visit   
            }
    
            public boolean visit(DoStatement node) 
            {           
                // Similar code goes here                   
                return false; // don't continue to visit  
            }
    
            public boolean visit (IfStatement node) 
            {
                // Similar code goes here                                           
                return false; // don't continue to visit  
            }
    
    
            public boolean visit(MethodDeclaration node) 
            {                               
                // Similar code goes here                   
                return false;                       
    
            }               
            public boolean visit(TypeDeclaration node)
            {   
                // Similar code goes here                                       
                return true;
            }
    
             public boolean visit(AnnotationTypeDeclaration node) 
             {
                // Similar code goes here                                            
                return true;
             }
    
    
             public boolean visit(PackageDeclaration node) 
             {  
                // Similar code goes here                                        
                return false;
             }
    
             public boolean visit(EnumDeclaration node)
             {
                // Similar code goes here                                            
                 return true;
            }
    
            @SuppressWarnings("static-access")
            public boolean visit(Block node)
            {
                // Similar code goes here                   
                return true;
            }
    
            public boolean visit(ImportDeclaration node)
            {
                // Similar code goes here                   
                return false;
    
            }
    
            //Other STATEMENTs goes here 
    
            });
    
                }
    
        finally
        {       
            writeJsonFile(path);                                
        }   
    }
    

    }

所以这里的 sourcecodeStatObj 包含所有的项目,以访问的顺序,即深度优先搜索。

此处省略了其他类和方法的构造。

如果您给我方向和转折,我将不胜感激!谢谢!

4

0 回答 0