如何使用 ASTRewrite 使用 JDT 插入/更新方法体?

发布于 2024-11-29 11:14:29 字数 1357 浏览 2 评论 0原文

我想使用 JDT 的 ASTRewrite 在方法内部编写代码。我尝试使用 ASTRewrite 但它不起作用。请帮忙。我的 ASTRewrite 的示例代码如下:

public void implementMethod(MethodDeclaration methodToBeImplemented) {
    astOfMethod = methodToBeImplemented.getAST(); 
    ASTRewrite astRewrite = ASTRewrite.create(astOfMethod);

    Block body = astOfMethod.newBlock();
    methodToBeImplemented.setBody(body);

    MethodInvocation newMethodInvocation = astOfMethod.newMethodInvocation();  
    QualifiedName name = astOfMethod.newQualifiedName(astOfMethod  
            .newSimpleName("System"), astOfMethod.newSimpleName("out"));  
    newMethodInvocation.setExpression(name);  
    newMethodInvocation.setName(astOfMethod.newSimpleName("println"));

    ExpressionStatement expressionStatement = astOfMethod.newExpressionStatement(newMethodInvocation);  
    body.statements().add(expressionStatement);

    astRewrite.set(oldBody, MethodDeclaration.BODY_PROPERTY, body, null);

    ctcObj.document = new Document(ctcObj.source);
    edit = astRewrite.rewriteAST(ctcObj.document, null);
    try {
        edit.apply(ctcObj.document);
    } catch (MalformedTreeException e) {    
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

我尝试使用不同类型的 ASTRewrite.set() 但它会生成编译时错误,指出非法参数或运行时异常。

I want to write code inside the method using JDT's ASTRewrite. I tried using ASTRewrite but its not working. Kindly help. Sample code of my ASTRewrite is below:

public void implementMethod(MethodDeclaration methodToBeImplemented) {
    astOfMethod = methodToBeImplemented.getAST(); 
    ASTRewrite astRewrite = ASTRewrite.create(astOfMethod);

    Block body = astOfMethod.newBlock();
    methodToBeImplemented.setBody(body);

    MethodInvocation newMethodInvocation = astOfMethod.newMethodInvocation();  
    QualifiedName name = astOfMethod.newQualifiedName(astOfMethod  
            .newSimpleName("System"), astOfMethod.newSimpleName("out"));  
    newMethodInvocation.setExpression(name);  
    newMethodInvocation.setName(astOfMethod.newSimpleName("println"));

    ExpressionStatement expressionStatement = astOfMethod.newExpressionStatement(newMethodInvocation);  
    body.statements().add(expressionStatement);

    astRewrite.set(oldBody, MethodDeclaration.BODY_PROPERTY, body, null);

    ctcObj.document = new Document(ctcObj.source);
    edit = astRewrite.rewriteAST(ctcObj.document, null);
    try {
        edit.apply(ctcObj.document);
    } catch (MalformedTreeException e) {    
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}

I tried using different types of ASTRewrite.set() but it generates either compile time error saying illegal parameters or run time exceptions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李白 2024-12-06 11:14:30

您还需要执行一步:写入文件。编辑(应用)不写入文件。
示例参见:
重写方法错误地重写更改为 ICompilationUnit 第二次重写更新< /a>

You need one more step: Write to file. edit(apply) does not write to file.
Example see:
Rewrite method incorrectly rewrite change to ICompilationUnit the second rewrite update

猫七 2024-12-06 11:14:30

(由于缺少 oldBody 的声明,我假设以下声明是正确的。)


必须删除以下行:

methodToBeImplemented.setBody(body);

通过上面的行,您可以手动更改应该是 < 的目标的节点代码>ASTRewrite。通常不建议这样做。

接下来,您的调用

astRewrite.set(oldBody, MethodDeclaration.BODY_PROPERTY, body, null);

失败,因为目标节点(第一个参数)和目标节点属性(第二个参数)必须与节点类匹配。但在您的情况下,它是 Block (oldBody)MethodDeclaration (BODY_PROPERTY)。正确的调用是:

astRewrite.set(methodToBeImplemented, MethodDeclaration.BODY_PROPERTY, body, null);

ASTRewrite.set() 的替代解决方案是这样的调用:

astRewrite.replace(oldBody, body, null);

(As the declaration of oldBody is missing I'm assuming in the following a correct declaration.)


The following line must be removed:

methodToBeImplemented.setBody(body);

With the above line you manually changing a node which should be a target of ASTRewrite. Usually that is not recommended.

Next, your call

astRewrite.set(oldBody, MethodDeclaration.BODY_PROPERTY, body, null);

fails because the target node (1st parameter) and target node property (2nd parameter) must be matching regarding the node class. But in your case it is Block (oldBody) and MethodDeclaration (BODY_PROPERTY). A proper call is:

astRewrite.set(methodToBeImplemented, MethodDeclaration.BODY_PROPERTY, body, null);

An alternative solution to ASTRewrite.set() would be this call:

astRewrite.replace(oldBody, body, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文