Java 如何从ASTParser打印所有方法声明和调用
我想打印一个类的所有方法中的所有方法调用。我正在使用ASTParser。下面是我的代码Java 如何从ASTParser打印所有方法声明和调用,java,abstract-syntax-tree,Java,Abstract Syntax Tree,我想打印一个类的所有方法中的所有方法调用。我正在使用ASTParser。下面是我的代码 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(Stri
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.*;
导入java.util.*;
公共类标识器扩展了ASTVisitor{
私有向量标识符=新向量();
公共向量getIdentifiers(){
返回标识符;
}
公共布尔访问(methodm){
System.out.println(“方法声明:+m”);
返回true;
}
公共布尔访问(MethodInvocation m){
System.out.println(“方法调用:+m”);
返回true;
}
}
输出仅显示一个方法声明。请让我知道如何打印所有已声明方法中的所有方法调用。谢谢您没有使用好的方法来检索源代码的字符串表示形式。您可以使用另一种方法从路径读取文件,并返回源的字符串表示形式:
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(文件路径)之前,请记住始终检查它是否为实际文件,例如:
或者,您可以打印从方法readFile返回的rawContent的内容,并检查要解析的代码是否与您的意思相同。谢谢-答案很有用。我的问题解决了。
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();
}
String filePath = file.getAbsolutePath();
if (file.isFile ()))
String source = readFileToString(filePath)