Exemplo n.º 1
0
  protected String toFormStringExpression(JParameter argument, Style classStyle)
      throws UnableToCompleteException {
    JType type = argument.getType();
    String expr = argument.getName();

    if (type.isPrimitive() != null) {
      return "\"\"+" + expr;
    }
    if (STRING_TYPE == type) {
      return expr;
    }
    if (type.isClass() != null && isOverlayArrayType(type.isClass())) {
      return "(new " + JSON_ARRAY_CLASS + "(" + expr + ")).toString()";
    }
    if (type.isClass() != null && OVERLAY_VALUE_TYPE.isAssignableFrom(type.isClass())) {
      return "(new " + JSON_OBJECT_CLASS + "(" + expr + ")).toString()";
    }
    if (type.getQualifiedBinaryName().startsWith("java.lang.")) {
      return String.format("(%s != null ? %s.toString() : null)", expr, expr);
    }

    Json jsonAnnotation = argument.getAnnotation(Json.class);
    final Style style = jsonAnnotation != null ? jsonAnnotation.style() : classStyle;

    return locator.encodeExpression(type, expr, style) + ".toString()";
  }
Exemplo n.º 2
0
  /**
   * Compares an unresolved JDT type to a TypeOracle type to see if they match.
   *
   * @param jdtType
   * @param toType
   * @return true if the two type objects resolve to the same
   */
  private static boolean typeMatches(TypeReference jdtType, JType toType) {
    List<char[]> toNameComponents = getClassChain(toType.getQualifiedBinaryName());
    int toLen = toNameComponents.size();
    char[][] jdtNameComponents = jdtType.getTypeName();
    int jdtLen = jdtNameComponents.length;
    int maxToCompare = Math.min(toLen, jdtLen);

    // compare from the end
    for (int i = 1; i <= maxToCompare; ++i) {
      if (!Arrays.equals(jdtNameComponents[jdtLen - i], toNameComponents.get(toLen - i))) {
        return false;
      }
    }
    return true;
  }