我正在尝试为用beanshell编写的代码编写一个基本的静态代码分析工具,它将执行一些基本的检查,比如未使用的变量、方法和可能永远不会计算为true的条件。
我尝试使用beanshell源代码分发版附带的解析器,如下所示:
import java.io.FileInputStream;
import java.io.IOException;
import bsh.ParseException;
import bsh.Parser;
import bsh.SimpleNode;
public class FindUnusedVariablesTask {
String sourseFilePath;
public FindUnusedVariablesTask(String sourseFilePath) {
this.sourseFilePath = sourseFilePath;
}
public String perform() throws ParseException, IOException {
FileInputStream sourceStream = new FileInputStream(sourseFilePath);
Parser p = new Parser(sourceStream);
while (!p.Line()) {
SimpleNode node = p.popNode();
System.out.println(node.getText());
for (int i=0; i<node.jjtGetNumChildren(); i++)
System.out.println(node.getChild(i).getText());
}
sourceStream.close();
return "";
}
}
对于下面的beanshell代码:
f1 () {
return 1;
}
String f2(String x) {
return x + f1() + " OK";
}
输出如下:
f1 ( ) {
( )
{
String f2 ( String x ) {
String
( String x )
{
基本上,我只得到解析后的方法声明。我找不到一种方法来访问内部的已解析语句。如何做到这一点?
https://stackoverflow.com/questions/47493699
复制相似问题