在MethodDeclaration中插入ASTNode

时间:2017-07-20 20:51:45

标签: java eclipse-jdt

我有一个像这样的JDT AST MethodDeclaration

MethodDeclaration md = ast.newMethodDeclaration();
SimpleName sn = ast.newSimpleName("myMethod");
md.setName(sn);

然后我基于包含一些Java代码的ASTNode创建了一个String,如下所示:

String body = "int a = 1;\n int b = 2;\n return (a + b);";

ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setSource(body.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);

ASTNode result = parser.createAST(null);

现在我尝试将ASTNode插入MethodDeclaration对象作为方法的主体,所以最后应该看起来像这样:

void myMethod(){
    int a = 1;
    int b = 2;
    return (a + b);
}

1 个答案:

答案 0 :(得分:2)

使用ASTParser创建的AST是不同的,而不是您正在操作的那个。因此,您必须使用org.eclipse.jdt.core.dom.ASTNode#copySubtree method进行转换。转换后,您可以将其设置为Body的{​​{1}}。

请注意,MethodDeclaration仍然必须添加到MethodDeclaration,但这可能是您未展示的代码的一部分。这是一个显示方法的MCVE:

TypeDeclaration

输出正如预期的那样

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.TextEdit;

public class JDTTest
{
    public static void main(String[] args) throws Exception 
    {
        // Set the stage by creating an empty compilation unit and its AST
        ASTParser parser = ASTParser.newParser(AST.JLS8);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setSource("".toCharArray());
        CompilationUnit compilationUnit =
            (CompilationUnit) parser.createAST(null);
        compilationUnit.recordModifications();
        AST ast = compilationUnit.getAST();

        // Create the AST node with the method body. Note that this AST
        // node will belong to a diferent AST!
        ASTNode astNodeWithMethodBody = createAstNodeWithMethodBody();        

        // Create the MethodDeclaration
        MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
        methodDeclaration.setName(ast.newSimpleName("myMethod"));

        // Convert the AST node with the method body to belong to
        // the desired AST, and set it as the method body
        ASTNode convertedAstNodeWithMethodBody = 
            ASTNode.copySubtree(ast, astNodeWithMethodBody);
        Block block = (Block)convertedAstNodeWithMethodBody;
        methodDeclaration.setBody(block);

        // (If necessary: Create a class declaration that will contain the 
        // newly generated method) 
        TypeDeclaration typeDeclaration = ast.newTypeDeclaration();
        typeDeclaration.setName(ast.newSimpleName("Example"));
        typeDeclaration.bodyDeclarations().add(methodDeclaration);
        compilationUnit.types().add(typeDeclaration);

        // Print the resulting document
        Document document = new Document();
        TextEdit edits = compilationUnit.rewrite(document, null);
        edits.apply(document);
        System.out.println(document.get());
    }

    private static ASTNode createAstNodeWithMethodBody()
    {
        String body = "int a = 1;\n int b = 2;\n return (a + b);";
        ASTParser parser = ASTParser.newParser(AST.JLS8);
        parser.setKind(ASTParser.K_STATEMENTS);
        parser.setSource(body.toCharArray());
        ASTNode result = parser.createAST(null);
        return result;
    }

}