我正在尝试使用 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/