gpt4 book ai didi

java - JDT : Nesting MethodInvocation

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:59 24 4
gpt4 key购买 nike

我正在尝试使用 Eclipse JDT 的 AST 模型来创建如下命令:

tmpStringBuffer.append("Content: ").append(gateId);

什么有效:创建

tmpStringBuffer.append("Content: ");

使用下面的代码

MethodInvocation mi = ast.newMethodInvocation();
mi.setExpression(ast.newSimpleName("tmpStringBuffer"));
mi.setName(ast.newSimpleName("append"));
sl = ast.newStringLiteral();
sl.setLiteralValue("Content: " );
mi.arguments().add(sl);
bufferBlock.statements().add(ast.newExpressionStatement(mi));

但是如何设置第二个.append(gateId)(以获取上面显示的命令)。它不是第二个添加的 MethodInitation 命令,因为它将生成 tmpStringBuffer.append("Content: ",append(gateId));。但结果应该是 tmpStringBuffer.append("Content: ").append(gateId);

AstView 告诉我它以某种方式嵌套。如何附加?

最佳答案

第二个 MethodInitation 应嵌套为第一个 MethodInovcation 的表达式。尝试下面的代码:

MethodInvocation nestedMI = ast.newMethodInvocation();
nestedMI.setExpression(ast.newSimpleName("tmpStringBuffer"));
nestedMI.setName(ast.newSimpleName("append"));
sl = ast.newStringLiteral();
sl.setLiteralValue("Content: " );
nestedMI.arguments().add(s1);

MethodInvocation mi = ast.newMethodInvocation();
mi.setExpression(nestedMI);
mi.setName(ast.newSimpleName("append"));
mi.arguments().add(ast.newSimpleName("gateId"));

bufferBlock.statements().add(ast.newExpressionStatement(mi));

关于java - JDT : Nesting MethodInvocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207444/

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