Пример #1
0
 private boolean canBeInlinedWithOtherArrayElements(Object value) {
   if (value.getClass().isPrimitive()) {
     return true;
   }
   for (Class<?> clazz : WRAPPER_TYPES) {
     if (clazz.isAssignableFrom(value.getClass())) {
       return true;
     }
   }
   return false;
 }
Пример #2
0
  @Override
  protected StringBuilder appendInternal(
      ObjectLocator locator, StringBuilder stringBuilder, Object value) {
    insideArray = insideArrayStack.peek();
    insideArrayStack.push(false);
    try {
      if (value instanceof String && ((String) value).isEmpty()) {
        appendNullText(stringBuilder);
        return stringBuilder;
      }
      if (!canBeInlinedWithOtherArrayElements(value)) {
        appendNewLine(stringBuilder);
      }
      if (value instanceof Map) {
        indentationValue++;
        for (Object key : ((Map) value).keySet()) {
          appendNewLine(
              indent(stringBuilder).append(key).append(": ").append(((Map) value).get(key)));
        }
        indentationValue--;
        return stringBuilder;
      }
      if (value instanceof XProperty
          || value instanceof XPartSpecElement
          || value instanceof XTimePartSpecElement
          || value instanceof ResultColumn) {
        removeLastArrayStart(stringBuilder);
        if (value instanceof ResultColumn) {
          ResultColumn column = (ResultColumn) value;
          indent(stringBuilder).append(column.getName()).append(": ").append(column.getType());
        }
        if (value instanceof XProperty) {
          XProperty property = (XProperty) value;
          indent(stringBuilder).append(property.getName()).append(": ").append(property.getValue());
        }
        if (value instanceof XPartSpecElement) {
          XPartSpecElement partSpecElement = (XPartSpecElement) value;
          indent(stringBuilder)
              .append(partSpecElement.getKey())
              .append(": ")
              .append(partSpecElement.getValue());
        }
        if (value instanceof XTimePartSpecElement) {
          XTimePartSpecElement partSpecElement = (XTimePartSpecElement) value;
          indent(stringBuilder)
              .append(partSpecElement.getKey())
              .append(": ")
              .append(partSpecElement.getValue());
        }
        return appendNewLine(stringBuilder);
      }

      if (value instanceof XJoinEdge) {
        XJoinEdge edge = (XJoinEdge) value;
        XTableReference from = edge.getFrom();
        XTableReference to = edge.getTo();
        stringBuilder.setLength(stringBuilder.length() - 2);
        stringBuilder
            .append(from.getTable())
            .append(".")
            .append(from.getColumn())
            .append(from.isMapsToMany() ? "(many)" : "")
            .append("=")
            .append(to.getTable())
            .append(".")
            .append(to.getColumn())
            .append(to.isMapsToMany() ? "(many)" : "");
        return appendNewLine(stringBuilder);
      }
      NameableContext context = getNameableContext(value);
      if (context != null) {
        String heading = context.getHeading();
        if (isBlank(heading)) {
          heading = "-";
        }
        if (insideArray) {
          stringBuilder.setLength(stringBuilder.length() - 2);
        }
        String details = context.getDetails();
        stringBuilder.append(heading);
        if (details.charAt(0) != '\n') {
          stringBuilder.append(" ");
        }
        stringBuilder.append(details);
        return appendNewLine(stringBuilder);
      }
      // some other way of getting heading
      if (value instanceof Collection) {
        Collection collection = (Collection) value;
        // try inline
        StringBuilder allElements = super.appendInternal(locator, new StringBuilder(), value);
        if (collection.size() != 0
            && canBeInlinedWithOtherArrayElements((collection.iterator().next()))
            && allElements.length() < 120) {
          stringBuilder.setLength(stringBuilder.length() - 1);
          String sep = " ";
          for (Object singleElement : collection) {
            stringBuilder.append(sep);
            appendInternal(locator, stringBuilder, singleElement);
            sep = ", ";
          }
          return stringBuilder;
        } else {
          return stringBuilder.append(allElements);
        }
      }
      // If this class is just a wrapper over a another object
      Field[] fields = value.getClass().getDeclaredFields();
      int nonStaticFields = 0;
      String fieldName = null;
      for (Field field : fields) {
        if (!Modifier.isStatic(field.getModifiers())) {
          nonStaticFields++;
          fieldName = field.getName();
        }
      }
      if (nonStaticFields == 1) {
        Class<?> claz = value.getClass();
        String getterName =
            "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        try {
          Object wrappedValue = claz.getDeclaredMethod(getterName).invoke(value);
          return appendNewLine(appendInternal(locator, stringBuilder, wrappedValue));
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
          log.trace(
              "getter access failed for {}#{}. Going the usual way", claz.getName(), getterName, e);
        }
      }
      return super.appendInternal(locator, stringBuilder, value);
    } finally {
      insideArrayStack.pop();
    }
  }