降低JDT代码时checkNewChild引发错误 [英] checkNewChild throws error when lowering JDT code

查看:77
本文介绍了降低JDT代码时checkNewChild引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的编译器类中,我们使用JDT表示Java的子集。我已经可以进行作业了,因此我认为通过将其降低至作业来实现增量/减量是一个好主意。我在键入此代码时意识到,由于表达式增加可能会产生影响,因此这不是100%有效的。

So in my compilers class we are using JDT to represent our subset of Java. I already have assignment working and so I thought it would be a good idea to implement increment/decrement by lowering it to assignment. I realized while typing this that because the expression being incremented might have an effect this is not 100% valid. Still I want to do something like this for for loops as well.

所以我有这段代码

    @Override
    public boolean visit(final PostfixExpression node) {
        //in this we lower an inc/dec expression to an assignment
        NumberLiteral one = node.getAST().newNumberLiteral();
        one.setToken(new Integer(1).toString());
        InfixExpression ie = node.getAST().newInfixExpression();
        ie.setLeftOperand(node.getOperand());
        ie.setRightOperand(one);
        if(node.getOperator() == PostfixExpression.Operator.INCREMENT) {
          ie.setOperator(InfixExpression.Operator.PLUS);
        } else {
          ie.setOperator(InfixExpression.Operator.MINUS);
        }
        Assignment a = node.getAST().newAssignment();
        a.setLeftHandSide(node.getOperand());
        a.setRightHandSide(ie);
        //finally just lower the increment to the assignment
        return this.visit(a);
    }

但是当它执行时,我尝试设置中缀表达式的左操作数。

but when it executes I get an error as soon as I try to set the left operand of the infix expression.

错误是

java.lang.IllegalArgumentException
at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:2087)
at org.eclipse.jdt.core.dom.ASTNode.preReplaceChild(ASTNode.java:2149)
at org.eclipse.jdt.core.dom.InfixExpression.setLeftOperand(InfixExpression.java:437)
...

所以我最好的猜测是孩子必须是唯一的。是这样吗在这种情况下,如何实施降低?如果不是这种情况,这是怎么回事?

So my best guess is that children are required to be unique. Is this the case? If this is the case, how is lowering implemented? If this is not the case, what is going on here?

推荐答案

也遇到了这个问题。找出它很简单:

Also faced this issue. It was simple to find out:


java.lang.IllegalArgumentException at
org.eclipse.jdt.core.dom.ASTNode .checkNewChild(ASTNode.java:2087)

java.lang.IllegalArgumentException at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:2087)

转到源文件并检查第2087行。

Go to sources and check for line 2087.

    if (newChild.getParent() != null) {
        // new child currently has a different parent
        throw new IllegalArgumentException();
    }

问题是如果节点已经存在,则无法将节点添加到树中(这是是树结构所必需的)。我看到以下方法可以解决此问题:

The problem is you cannot add node to tree if node is already there (this is required by tree structure). I see the following ways to solve this problem:


  1. 复制节点及其为带有 ASTNode.copySubtree(astNode的子树。 getAST(),astNode)

  2. 使用 astNode.delete()
  1. Copy node and it's subtree with ASTNode.copySubtree(astNode.getAST(), astNode)
  2. Delete node from tree with astNode.delete()

这篇关于降低JDT代码时checkNewChild引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆