private Expression transformRhsExpressionIfNecessary(DeclarationExpression declExpr) {
    Expression rightExpression = declExpr.getRightExpression();
    // The case of `def foo = foo()` will break if we split it up without changing the target,
    // as groovy would now interpret it as a `foo.call()` invocation on the local variable.
    if (rightExpression instanceof MethodCallExpression) {
      MethodCallExpression methodCallExpression = (MethodCallExpression)rightExpression;
      if (methodCallExpression.isImplicitThis())
        if (declExpr.isMultipleAssignmentDeclaration()) {
          ArgumentListExpression argumentListExpression = (ArgumentListExpression)declExpr.getLeftExpression();
          argumentListExpression.getExpressions().stream()
            .filter(VariableExpression.class::isInstance)
            .map(VariableExpression.class::cast)
            .map(VariableExpression::getName)
            .filter(methodCallExpression.getMethod().getText()::equals)
            .findAny()
            .ifPresent(ignore-> methodCallExpression.setImplicitThis(false));
        } else if (declExpr.getVariableExpression().getName().equals(methodCallExpression.getMethod().getText())) {
          // change to explicit `this` to turn the expression to `foo = this.foo()`
          methodCallExpression.setImplicitThis(false);
        }
    }
    return rightExpression;
  }
A Spec visitor responsible for most of the rewriting of a Spec's AST.

@author Peter Niederwieser
spock-core/src/main/java/org/spockframework/compiler/SpecRewriter.java