首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >解析Beanshell代码

解析Beanshell代码
EN

Stack Overflow用户
提问于 2017-11-26 14:44:28
回答 0查看 266关注 0票数 0

我正在尝试为用beanshell编写的代码编写一个基本的静态代码分析工具,它将执行一些基本的检查,比如未使用的变量、方法和可能永远不会计算为true的条件。

我尝试使用beanshell源代码分发版附带的解析器,如下所示:

代码语言:javascript
运行
复制
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代码:

代码语言:javascript
运行
复制
f1 () {
  return 1;
}

String f2(String x) {
    return x + f1() + " OK";
}

输出如下:

代码语言:javascript
运行
复制
f1 ( ) { 
( ) 
{ 

String f2 ( String x ) { 
String 
( String x ) 
{ 

基本上,我只得到解析后的方法声明。我找不到一种方法来访问内部的已解析语句。如何做到这一点?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47493699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档