Java 在MethodDeclaration中插入ASTNode
我有一个JDT ASTJava 在MethodDeclaration中插入ASTNode,java,eclipse-jdt,Java,Eclipse Jdt,我有一个JDT ASTMethodDeclaration定义如下: MethodDeclaration md = ast.newMethodDeclaration(); SimpleName sn = ast.newSimpleName("myMethod"); md.setName(sn); String body = "int a = 1;\n int b = 2;\n return (a + b);"; ASTParser parser = ASTParser.newParser(AS
MethodDeclaration
定义如下:
MethodDeclaration md = ast.newMethodDeclaration();
SimpleName sn = ast.newSimpleName("myMethod");
md.setName(sn);
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);
然后,我基于包含一些Java代码的字符串创建了一个ASTNode
,如下所示:
MethodDeclaration md = ast.newMethodDeclaration();
SimpleName sn = ast.newSimpleName("myMethod");
md.setName(sn);
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不同。因此,必须使用。转换后,您可以将其设置为方法声明的正文
请注意,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);
}
}