【问题标题】:Inlining java code using eclipse jdt/ast使用 eclipse jdt/ast 内联 java 代码
【发布时间】:2012-10-08 20:41:21
【问题描述】:

我正在尝试使用 eclipse jdt/ast 内联 java 方法。

例如,我想做这段代码

class Hello {
    static void hello() {
        System.out.println("hello");
    }

    public static void main(String[] args) {
        hello();
        System.out.println("z");
        hello();
        System.out.println("h");
        hello();
    }
}

进入这个。

class Hello {    
    public static void main(String[] args) {
        System.out.println("hello");
        System.out.println("z");
        System.out.println("hello");
        System.out.println("h");
        System.out.println("hello");
    }
}

我可以得到 hello() 方法的主体块存储在Block bl 中。

我在Block block中也有main()方法的body块,我可以删除hello(); ExpressionStatements 在区块中。

然后,我需要将Block bl 插入到调用hello(); 的位置。 我试过了

block.statements().add(position, bl.getAST());

block.statements().add(position, bl);

其中position 是 statements() 中 hello() 方法的位置,但两者都会引发错误。

可能出了什么问题?由于BlockStatement,我想可以在Block#statements() 中插入Block

添加

根据 Sevenforce 的回答,我可以插入块,但我包含 {}

class Hello {
    public static void main(String[] args) {
    {
        System.out.println("hello");
    }
    System.out.println("z");
    {
        System.out.println("hello");
    }
    System.out.println("h");
    {
        System.out.println("hello");
    }
    }
}

有什么办法可以去掉?

添加2

使用此代码:

ASTNode singleStmt = (ASTNode) bl.statements().get(0);
block.statements().add(position, ASTNode.copySubtree(bl.getAST(), singleStmt));

它只显示hello() 方法中的第一条语句。例如,与

static void hello() {
    System.out.println("hello");
    System.out.println("hello2");
}

我只有 System.out.println("hello"); 内联。

【问题讨论】:

    标签: java eclipse abstract-syntax-tree eclipse-jdt


    【解决方案1】:

    add 抛出异常有几个原因。 通常,特定的异常应该给你一个提示。例如。要添加的节点必须在同一个AST

    在您的情况下,我猜您尝试添加相同的 block 对象,该对象已在 hello()MethodDeclaration 节点中使用,并且每个节点只允许有一个父节点。

    一个可能的解决方案是复制节点:

    block.statements().add(position, ASTNode.copySubtree(bl.getAST(), bl));
    

    对于提取单个语句,以下应该可以工作:

    ASTNode singleStmt = (ASTNode) bl.statements().get(0);
    block.statements().add(position, ASTNode.copySubtree(bl.getAST(), singleStmt));
    

    要添加块中的所有语句,您可以循环:

    for (int i = 0; i < bl.statements().size(); i++) {
         ASTNode singleStmt = (ASTNode) bl.statements().get(i);
         block.statements().add(position + i,
             ASTNode.copySubtree(bl.getAST(), singleStmt));
    }
    

    【讨论】:

    • 感谢您的回答,它有效。但是,我在代码中包含了 { 和 }。我更新了帖子。
    • 您添加的评论有效,除了一件事:不是 statements.get(0),而是 statements().get(0);
    • 还有一个问题。我更新了问题。你愿意检查一下吗?谢谢。
    • 对于你的循环方法,当语句有块时它不应该工作。
    【解决方案2】:

    您可能会考虑替换方法调用,而不是添加到块中。

    final ASTRewrite rewriter = ASTRewrite.create(compilationUnit.getAST());
    
    rewriter.replace(methodInvocationNode, blockNode, null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多