private Class writeLiteralOrSubexpression(Object stmt, Class desiredTarget, Class knownIngressType) {
    if (stmt instanceof ExecutableLiteral) {
        Object literalValue = ((ExecutableLiteral) stmt).getLiteral();

      // Handle the case when the literal is null MVEL-312 
      if(literalValue == null){
        mv.visitInsn(ACONST_NULL);
        return null;
      }        
        
      Class type = literalValue == null ? desiredTarget : literalValue.getClass();

      assert debug("*** type:" + type + ";desired:" + desiredTarget);

      if (type == Integer.class && desiredTarget == int.class) {
        intPush(((ExecutableLiteral) stmt).getInteger32());
        type = int.class;
      }
      else if (desiredTarget != null && desiredTarget != type) {
        assert debug("*** Converting because desiredType(" + desiredTarget.getClass() + ") is not: " + type);


        if (!DataConversion.canConvert(type, desiredTarget)) {
          throw new CompileException("was expecting type: " + desiredTarget.getName()
              + "; but found type: " + type.getName(), expr, st);
        }
        writeOutLiteralWrapped(convert(literalValue, desiredTarget));
      }
      else {
        writeOutLiteralWrapped(literalValue);
      }

      return type;
    }
    else {
      literal = false;

      addSubstatement((ExecutableStatement) stmt);

      Class type;
      if (knownIngressType == null) {
        type = ((ExecutableStatement) stmt).getKnownEgressType();
      }
      else {
        type = knownIngressType;
      }

      if (desiredTarget != null && type != desiredTarget) {
        if (desiredTarget.isPrimitive()) {
          if (type == null) throw new OptimizationFailure("cannot optimize expression: " + new String(expr) +
              ": cannot determine ingress type for primitive output");

          checkcast(type);
          unwrapPrimitive(desiredTarget);
        }
      }

      return type;
    }
  }