【发布时间】:2018-05-23 09:22:12
【问题描述】:
当我在 AST 遍历期间访问 MethodInvocation 节点时,我想知道它是否位于 IfStatement then 部分或 else 部分或 表达式 部分。 then 部分可以是完整的代码块,但我认为我的代码只处理一个 then 语句。
这是访问方法调用的代码sn-p
@Override
public boolean visit(MethodInvocation node)
{
StructuralPropertyDescriptor location = node.getLocationInParent();
setNodeRegion(location);
这是我想为 IfStatement 的每个区域设置标志的方式
private void setNodeRegion(StructuralPropertyDescriptor location) {
if(location == IfStatement.EXPRESSION_PROPERTY ||
location == IfStatement.THEN_STATEMENT_PROPERTY)
{
ParseContextAction.ifBlockRegion = true;
}
else
{
if(location == IfStatement.ELSE_STATEMENT_PROPERTY)
{
ParseContextAction.elseBlockRegion = true;
}
else
{
if(location == CatchClause.BODY_PROPERTY)
{
ParseContextAction.catchBlockRegion = true;
}
else
{
ParseContextAction.basicBlockRegion = true;
}
}
}
}
【问题讨论】:
-
另一个替代问题:是否可以分别访问 IfStatement 的表达式部分、然后部分和 else 部分来设置标志?
-
检查
node的祖先(node.getParent(),node.getParent().getParent(),...)是否是IfStatement的实例,它具有getThenStatement()和getElseStatement()。 -
@howlger 我也有同样的想法,但递归获取父部分直到遇到 IfStatement 或到达根节点,似乎是一个昂贵的解决方案。
-
您还可以实现/覆盖在 if 语句中包含的
visit(MethodInvocation node)之前调用的 AST 访问者的visit(IfStatement node)。 -
再次“是否可以分别访问 IfStatement 的表达式部分、然后部分和 else 部分以设置标志?” if 语句由三个部分组成,then 部分和 else 部分包含多个语句。我想确定特定方法调用是否位于 then 部分或 else 部分。除非可以单独访问 if 的各个部分,否则访问 IfStatement 节点是没有用的。
标签: eclipse location eclipse-jdt