java - JDT : Nesting MethodInvocation

标签 java abstract-syntax-tree eclipse-jdt

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

相关文章:

c++ - 如何获取clang抽象语法树右侧的整个表达式?

Eclipse Kepler 不显示建议

java - 从插件修改 Eclipse UI

java - eclipse JDT : Is there a refactoring to replace direct field accesses with setter/getter methods?

c++ - 虚拟类作为具有 Spirit 的 AST 节点

python - 如何在 Python 中查找特定函数定义的所有引用

java - 使用 javaFX 对齐文本时遇到问题

JAVA - 仅当我使用调试运行它时才会出现异常

java - Spring 具有 IntelliJ 属性 p :prefix is not allowed here

java - OIOSAML 的 Bouncy CasSTLe Maven 依赖项