private static ExpressionWriter write()

in src/main/java/net/hydromatic/linq4j/expressions/ConstantExpression.java [74:172]


  private static ExpressionWriter write(ExpressionWriter writer,
      final Object value, Type type) {
    if (value == null) {
      return writer.append("null");
    }
    if (type == null) {
      type = value.getClass();
      if (Primitive.isBox(type)) {
        type = Primitive.ofBox(type).primitiveClass;
      }
    }
    if (value instanceof String) {
      escapeString(writer.getBuf(), (String) value);
      return writer;
    }
    final Primitive primitive = Primitive.of(type);
    if (primitive != null) {
      switch (primitive) {
      case BYTE:
        return writer.append("(byte)").append(((Byte) value).intValue());
      case CHAR:
        return writer.append("(char)")
            .append((int) ((Character) value).charValue());
      case SHORT:
        return writer.append("(short)").append(((Short) value).intValue());
      case LONG:
        return writer.append(value).append("L");
      case FLOAT:
        return writer.append(value).append("F");
      case DOUBLE:
        return writer.append(value).append("D");
      default:
        return writer.append(value);
      }
    }
    final Primitive primitive2 = Primitive.ofBox(type);
    if (primitive2 != null) {
      writer.append(primitive2.boxClass.getSimpleName() + ".valueOf(");
      write(writer, value, primitive2.primitiveClass);
      return writer.append(")");
    }
    if (value instanceof Enum) {
      return writer.append(((Enum) value).getDeclaringClass())
          .append('.')
          .append(((Enum) value).name());
    }
    if (value instanceof BigDecimal) {
      BigDecimal bigDecimal = ((BigDecimal) value).stripTrailingZeros();
      try {
        final int scale = bigDecimal.scale();
        final long exact = bigDecimal.scaleByPowerOfTen(scale).longValueExact();
        writer.append("new java.math.BigDecimal(").append(exact).append("L");
        if (scale != 0) {
          writer.append(", ").append(scale);
        }
        return writer.append(")");
      } catch (ArithmeticException e) {
        return writer.append("new java.math.BigDecimal(\"").append(
            bigDecimal.toString()).append("\")");
      }
    }
    if (value instanceof BigInteger) {
      BigInteger bigInteger = (BigInteger) value;
      return writer.append("new java.math.BigInteger(\"").append(
          bigInteger.toString()).append("\")");
    }
    if (value instanceof Class) {
      Class clazz = (Class) value;
      return writer.append(clazz.getCanonicalName()).append(".class");
    }
    if (value instanceof Types.RecordType) {
      final Types.RecordType recordType = (Types.RecordType) value;
      return writer.append(recordType.getName()).append(".class");
    }
    if (value.getClass().isArray()) {
      writer.append("new ").append(value.getClass().getComponentType());
      list(writer, Primitive.asList(value), "[] {\n", ",\n", "}");
      return writer;
    }
    Constructor constructor = matchingConstructor(value);
    if (constructor != null) {
      final Field[] fields = value.getClass().getFields();
      writer.append("new ").append(value.getClass());
      list(writer,
          Functions.adapt(fields,
              new Function1<Field, Object>() {
                public Object apply(Field field) {
                  try {
                    return field.get(value);
                  } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                  }
                }
              }),
          "(\n", ",\n", ")");
      return writer;
    }
    return writer.append(value);
  }