Как проверить, находится ли посещаемый узел, если он является частью или частью узла IfStatement при разборе Eclipse JDT? - PullRequest
0 голосов
/ 23 мая 2018

Когда я посещаю узел MethodInvocation во время обхода AST, я хочу знать, лежит ли он в части IfStatement затем или else или в выражении часть.Часть then может быть полным блоком кода, но я думаю, что мой код обрабатывает только один оператор then.

Вот фрагмент кода для посещения вызова метода

@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;
                }
            }
        }
    }

1 Ответ

0 голосов
/ 23 мая 2018

Если вы используете visit(IfStatement node) вместо visit(MethodInvocation node), вы можете посетить ветку , затем (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
}
...