简体   繁体   English

使用Eclipse JDT / AST内联Java代码

[英]Inlining java code using eclipse jdt/ast

I'm trying to inline java method using eclipse jdt/ast. 我正在尝试使用Eclipse jdt / ast内联Java方法。

For example, I'd like to make this code 例如,我想编写这段代码

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();
    }
}

into this. 到这个。

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");
    }
}

I could get the body block of hello() method stored in Block bl . 我可以获取存储在Block bl的hello()方法的主体块。

I also have the body block of main() method stored in Block block , and I could delete hello(); 我还将main()方法的主体块存储在Block block ,并且可以删除hello();。 ExpressionStatement s in the block. 块中的ExpressionStatement

Then, I need to insert the Block bl to where the hello(); 然后,我需要将Block bl插入hello(); is invoked. 被调用。 I tried 我试过了

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

and

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

where position is the location of hello() method in statements(), but both of them raises an error. 其中positionhello()方法在statement()中的位置,但是它们都引发错误。

What might be wrong? 可能是什么问题? As Block is Statement , I guess one can insert Block in Block#statements() . 由于BlockStatement ,我猜一个人可以在Block#statements()插入Block

在此处输入图片说明

ADDED 添加

Based on sevenforce answer, I could get the block inserted, but I have the { and } included. 基于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");
    }
    }
}

Is there any way to remove them? 有什么办法可以删除它们?

ADDED2 添加2

With this code: 使用此代码:

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

It shows only the first statement in hello() method. 它仅显示hello()方法中的第一条语句。 For example, with 例如,

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

I have only System.out.println("hello"); 我只有System.out.println("hello"); inlined. 内联。

There are several reasons for add throwing an exception. add引发异常有多种原因。 Normally the specific exception should give you a hint. 通常,特定的异常应该给您提示。 Eg the node to be added must be in the same AST . 例如,要添加的节点必须在同一AST

In your case I'd guess, that you attempt to add the same block object, which is already used in the MethodDeclaration node of hello() and each node is allowed to have just one parent. 在您的情况下,我想您尝试添加相同的block对象,该对象已在hello()MethodDeclaration节点中使用,并且每个节点只能有一个父对象。

A possible solution to that is copying the node: 一个可能的解决方案是复制节点:

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

For extracting single statements the following should work: 要提取单个语句,应执行以下操作:

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

To add all statements from the block you can just loop over: 要添加块中的所有语句,您可以循环遍历:

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));
}

Instead of adding to the block you might think of replacing the method invocation. 您可能会想到替换方法调用,而不是添加到块中。

final ASTRewrite rewriter = ASTRewrite.create(compilationUnit.getAST());

rewriter.replace(methodInvocationNode, blockNode, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM