gpt4 book ai didi

java - 在 MethodDeclaration 中插入 ASTNode

转载 作者:行者123 更新时间:2023-11-30 02:27:38 27 4
gpt4 key购买 nike

我有一个 JDT AST MethodDeclaration 定义如下:

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

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

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

最佳答案

使用这样的 ASTParser 创建的 AST 与您正在操作的 AST 不同。因此,您必须使用 org.eclipse.jdt.core.dom.ASTNode#copySubtree method 对其进行转换。 。转换后,您可以将其设置为 MethodDeclarationBody

请注意,MethodDeclaration 仍然需要添加到 TypeDeclaration 中,但也许这是您未显示的代码的一部分。以下是展示该方法的 MCVE:

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

}

输出符合预期

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

关于java - 在 MethodDeclaration 中插入 ASTNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45224940/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com