【问题标题】:How to check if a visited node is in if part or else part of IfStatement node in Eclipse JDT parsing?如何检查访问的节点是否在 Eclipse JDT 解析中的 if 部分或 IfStatement 节点的一部分中?
【发布时间】: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


【解决方案1】:

如果您使用visit(IfStatement node) 而不是visit(MethodInvocation node),您可以访问 then (getThenStatement()) 和 else (getElseStatement()) 分支单独的访客:

@Override
public boolean visit(IfStatement node) {

    Statement thenBranch = node.getThenStatement(); 
    if (thenBranch != null) {
        thenBranch.accept(new ASTVisitor(false) {
            @Override
            public boolean visit(MethodInvocation node) {
                // handle method invocation in the then branch
                return true; // false, if nested method invocations should be ignored
            }
        }
    }

    Statement elseBranch = node.getElseStatement(); 
    if (elseBranch != null) {
        elseBranch.accept(new ASTVisitor(false) {
            @Override
            public boolean visit(MethodInvocation node) {
                // handle method invocation in the else branch
                return true; // false, if nested method invocations should be ignored
            }
        }
    }

    return true; // false, if nested if statements should be ignored
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多