/*
   * Generates the initializers (AST) for all values that are shared between states (e.g. all instance factories
   * shared by AddItems overrides).
   */
  private StatementListNode genSharedFactoriesAST(
      StatesModel model, NodeFactory nodeFactory, StatementListNode statementList) {
    Map<String, SharedObject> shared = model.sharedObjects;

    StatementListNode result = statementList;

    for (Iterator<String> iter = shared.keySet().iterator(); iter.hasNext(); ) {
      SharedObject symbol = shared.get(iter.next());

      String varName = ((String) symbol.name + _FACTORY).intern();
      String typeName = NameFormatter.retrieveClassName(DEFERREDINSTANCEFROMFUNCTION);
      String factory = symbol.name + (symbol.model.isDeclared() ? _I : _C);
      String resetFunc = symbol.name + _R;

      MemberExpressionNode memberExpression =
          AbstractSyntaxTreeUtil.generateGetterSelector(nodeFactory, factory, true);

      ArgumentListNode callExpressionArgumentList =
          nodeFactory.argumentList(null, memberExpression);

      if (symbol.model.getIsTransient()) {
        memberExpression =
            AbstractSyntaxTreeUtil.generateGetterSelector(nodeFactory, resetFunc, true);
        callExpressionArgumentList =
            nodeFactory.argumentList(callExpressionArgumentList, memberExpression);
      }

      QualifiedIdentifierNode qualifiedIdentifier =
          AbstractSyntaxTreeUtil.generateQualifiedIdentifier(
              nodeFactory, standardDefs.getCorePackage(), typeName, false);

      CallExpressionNode callExpression =
          (CallExpressionNode)
              nodeFactory.callExpression(qualifiedIdentifier, callExpressionArgumentList);
      callExpression.is_new = true;
      callExpression.setRValue(false);

      MemberExpressionNode ad = nodeFactory.memberExpression(null, callExpression);

      VariableDefinitionNode variableDefinition =
          AbstractSyntaxTreeUtil.generateVariable(nodeFactory, varName, typeName, false, ad);
      result = nodeFactory.statementList(result, variableDefinition);
    }
    return result;
  }
  /*
   * Generate all necessary binding instance AST for any data bound overrides.
   */
  private StatementListNode genBindingInitializersAST(
      NodeFactory nodeFactory, StatementListNode statementList) {
    StatementListNode result = statementList;

    for (Iterator<StatesModel.Override> iter = bindingsQueue.iterator(); iter.hasNext(); ) {
      StatesModel.Override symbol = (StatesModel.Override) iter.next();

      QualifiedIdentifierNode qualifiedIdentifier =
          AbstractSyntaxTreeUtil.generateQualifiedIdentifier(
              nodeFactory, standardDefs.getBindingPackage(), BINDINGMANAGER, false);

      GetExpressionNode bindExpression = nodeFactory.getExpression(qualifiedIdentifier);

      MemberExpressionNode lvalue = nodeFactory.memberExpression(null, bindExpression);

      ArgumentListNode execArgs = nodeFactory.argumentList(null, nodeFactory.thisExpression(0));
      String decl = symbol.declaration.intern();
      execArgs = nodeFactory.argumentList(execArgs, nodeFactory.literalString(decl, false));
      IdentifierNode rvalIdentifier = nodeFactory.identifier(decl, false);
      GetExpressionNode getExpression = nodeFactory.getExpression(rvalIdentifier);
      MemberExpressionNode rvalue = nodeFactory.memberExpression(null, getExpression);
      execArgs = nodeFactory.argumentList(execArgs, rvalue);

      IdentifierNode bindIdentifier = nodeFactory.identifier(EXECUTEBINDINGS, false);

      CallExpressionNode selector =
          (CallExpressionNode) nodeFactory.callExpression(bindIdentifier, execArgs);
      selector.setRValue(false);

      MemberExpressionNode base = nodeFactory.memberExpression(lvalue, selector);
      ListNode list = nodeFactory.list(null, base);
      ExpressionStatementNode expressionStatement = nodeFactory.expressionStatement(list);
      result = nodeFactory.statementList(result, expressionStatement);
    }
    return result;
  }