如何从 ASTParser 打印所有方法声明和调用 [英] How to print all method declaration and invocation from ASTParser

查看:28
本文介绍了如何从 ASTParser 打印所有方法声明和调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个类的所有方法中的所有方法调用.我正在使用 ASTParser.以下是我的代码

I want to print all the method invocations within all methods of a Class. I am using ASTParser. Following is my code

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import java .io.*;

public class ASTParserDemo {
public static void main(String[] args) {


    ASTParserDemo demo = new ASTParserDemo();
    String rawContent = demo.readFile();

    //String rawContent = "public class HelloWorld { public String s = \"hello\"; public static void main(String[] args) { HelloWorld hw = new HelloWorld(); String s1 = hw.s; } }";
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(rawContent.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    AST ast = cu.getAST();
    IdentifierVisitor iv = new IdentifierVisitor();
    cu.accept(iv);

}

public String readFile() {
    StringBuffer fileContent = new StringBuffer();
    BufferedReader br = null;
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("C:\\research\\android-projects\\AsyncSearch.java"));

        while ((sCurrentLine = br.readLine()) != null) {
            //System.out.println(sCurrentLine);
            fileContent.append(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return fileContent.toString();
}

}

import org.eclipse.jdt.core.dom.*;

import java.util.*;

public class IdentifierVisitor extends ASTVisitor {

private Vector<String> identifiers = new Vector<String>();

public Vector<String> getIdentifiers(){
    return identifiers; 
}


public boolean visit(MethodDeclaration m){
    System.out.println("METHOD DECLARATION : " + m);
    return true;
}

public boolean visit(MethodInvocation m){
    System.out.println("METHOD INVOCATION : " + m);
    return true;
}

}

输出仅显示一个方法声明.请让我知道如何在所有声明的方法中打印所有方法调用.谢谢

the output is showing only one method declaration. Please let me know how do I print all method invocations within all declared methods. Thanks

推荐答案

您没有使用好的方法来检索源代码的字符串表示形式.您可以使用另一种方法从路径中读取文件并返回源的字符串表示形式:

You're not using a good method to retrieve the string representation of your source code. You can use an alternative method for read a file from your path and return a string representation of source:

public static String readFileToString(String filePath) throws IOException {
    StringBuilder fileData = new StringBuilder(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));

    char[] buf = new char[10];
    int numRead = 0;
    while ((numRead = reader.read(buf)) != -1) {
        //          System.out.println(numRead);
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
        buf = new char[1024];
    }
    reader.close();
    return  fileData.toString();    
}

记得在调用 readFileToString(filePath) 之前总是检查它是否是一个实际的文件,例如:

Remember to always check whether it is an actual file before calling readFileToString(filePath) eg:

String filePath = file.getAbsolutePath();
if (file.isFile ())) 
     String source = readFileToString(filePath) 

或者,您可以打印从方法 readFile 返回的 rawContent 的内容,并检查您要解析的代码是否与您的意思相同.

Alternatively you can print the contents of rawContent returned from your method readFile and check that the code you want to parse is actually the same as what you mean.

这篇关于如何从 ASTParser 打印所有方法声明和调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆