CIOC
CIOC

Reputation: 1427

Insert ASTNode inside MethodDeclaration

I have a JDT AST MethodDeclaration difined like this:

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

Then I created an ASTNode based on a String that contains some Java code, like this:

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

Now I'm trying to insert the ASTNode into the MethodDeclaration object as the body of the method so at the end it should look likt this:

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

Upvotes: 1

Views: 680

Answers (1)

Marco13
Marco13

Reputation: 54639

The AST that is created by using the ASTParser like that is a different one than the one that you are operating on. Therefore, you have to convert it, using the org.eclipse.jdt.core.dom.ASTNode#copySubtree method. After the conversion, you can set it as the Body of your MethodDeclaration.

Note that the MethodDeclaration will still have to be added to a TypeDeclaration, but maybe this is part of the code that you didn't show. Here is an MCVE showing the approach:

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

}

The output is, as expected

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

Upvotes: 3

Related Questions