0

I have the following code to analyze

a++;
return a++;

Now, I have the following implementation of method isToBeProcessed

public boolean isToBeProcessed(CtUnaryOperator<? extends CtElement> candidate) {
    if (!super.validMutationSpot(candidate)) {
        LOGGER.log(Level.FINER, "{0} is not a valid mutation spot", new Object[]{candidate});
        return false;
    }
    if (    candidate.getKind().compareTo(UnaryOperatorKind.POSTDEC) == 0
            ||
            candidate.getKind().compareTo(UnaryOperatorKind.POSTINC) == 0
            ||
            candidate.getKind().compareTo(UnaryOperatorKind.PREDEC) == 0
            ||
            candidate.getKind().compareTo(UnaryOperatorKind.PREINC) == 0) {
        //check that is not a statement
        if (candidate.getParent() instanceof CtStatement) {
            LOGGER.log(Level.FINER, "{0} is a statement {1}", new Object[]{candidate, candidate.getParent()});
            return false;
        } else {
            return true;
        }
    } else {
        LOGGER.log(Level.FINER, "{0} operator is not a pre/post inc or dec", new Object[]{candidate});
        return false;
    }
}

The problem is that when this method is called with the first a++ the getParent() method should return a++; but it's returning the whole block. Why?

DrStein
  • 75
  • 2
  • 13
  • In this case, the getparent method returns the parent of the unary operator in the AST which seems rto be the whole block or the expresion. – El Marce Jan 19 '17 at 14:43
  • check the type of the parent. It could also be of type CtExpression. – El Marce Jan 19 '17 at 14:44
  • I found the issue in this which at first seemed unrelated to this one but I found out that this problems was caused by the issue in the other question. link to the other question : http://stackoverflow.com/questions/41732525/ctunaryoperator-extends-ctstatement Issue is that a `CtStatement` not always refers to an actual java statement. For example `a++` is an instance of `CtStatement` – DrStein Jan 26 '17 at 21:04

0 Answers0