Example #1
0
  public static <T> T createFromArg(Class<T> clz, Object value) {
    if (value == null) {
      return null;
    }
    ClassMeta meta = ClassMeta.classMeta(clz);
    List<ConstructorAccess> constructors = meta.oneArgumentConstructors();

    if (constructors.size() == 0) {
      return null;
    } else if (constructors.size() == 1) {
      ConstructorAccess constructorAccess = constructors.get(0);
      Class<?> arg1Type = constructorAccess.parameterTypes()[0];
      if (arg1Type.isInstance(value)) {
        return (T) constructorAccess.create(value);
      } else {
        return (T) constructorAccess.create(coerce(arg1Type, value));
      }
    } else {
      for (ConstructorAccess c : constructors) {
        Class<?> arg1Type = c.parameterTypes()[0];
        if (arg1Type.isInstance(value)) {
          return (T) c.create(value);
        }
      }

      for (ConstructorAccess c : constructors) {
        Class<?> arg1Type = c.parameterTypes()[0];
        if (arg1Type.isAssignableFrom(value.getClass())) {
          return (T) c.create(value);
        }
      }
    }
    return null;
  }
Example #2
0
  private void hasOneInstantiatorDefinitionWithEmptyConstructorOnImpl(
      ClassMeta<?> classMeta, Class<?> impl) throws NoSuchMethodException {
    assertTrue(classMeta instanceof ArrayClassMeta);
    final List<InstantiatorDefinition> instantiatorDefinitions =
        classMeta.getInstantiatorDefinitions();

    assertEquals(1, instantiatorDefinitions.size());
    final ExecutableInstantiatorDefinition instantiatorDefinition =
        (ExecutableInstantiatorDefinition) instantiatorDefinitions.get(0);

    assertEquals(0, instantiatorDefinition.getParameters().length);
    assertEquals(impl.getDeclaredConstructor(), instantiatorDefinition.getExecutable());
  }
Example #3
0
  public static <T> T coerceWithFlag(
      TypeType coerceTo, Class<T> clz, boolean[] flag, Object value) {

    flag[0] = true;
    if (value == null) {
      return null;
    }

    if (clz.isInstance(value)) {
      return (T) value;
    }

    switch (coerceTo) {
      case STRING:
      case CHAR_SEQUENCE:
        return (T) value.toString();

      case INT:
      case INTEGER_WRAPPER:
        Integer i = toInt(value);
        if (i == Integer.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) i;

      case SHORT:
      case SHORT_WRAPPER:
        Short s = toShort(value);
        if (s == Short.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) s;

      case BYTE:
      case BYTE_WRAPPER:
        Byte by = toByte(value);
        if (by == Byte.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) by;

      case CHAR:
      case CHAR_WRAPPER:
        Character ch = toChar(value);
        if (ch == (char) 0) {
          flag[0] = false;
        }
        return (T) ch;

      case LONG:
      case LONG_WRAPPER:
        Long l = toLong(value);
        if (l == Long.MIN_VALUE) {
          flag[0] = false;
        }

        return (T) l;

      case DOUBLE:
      case DOUBLE_WRAPPER:
        Double d = toDouble(value);
        if (d == Double.MIN_VALUE) {
          flag[0] = false;
        }

        return (T) d;

      case FLOAT:
      case FLOAT_WRAPPER:
        Float f = toFloat(value);
        if (f == Float.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) f;

      case DATE:
        return (T) toDate(value);

      case BIG_DECIMAL:
        return (T) toBigDecimal(value);

      case BIG_INT:
        return (T) toBigInteger(value);

      case CALENDAR:
        return (T) toCalendar(toDate(value));

      case BOOLEAN:
      case BOOLEAN_WRAPPER:
        return (T) (Boolean) toBooleanOrDie(value);

      case MAP:
        return (T) toMap(value);

      case ARRAY:
      case ARRAY_INT:
      case ARRAY_BYTE:
      case ARRAY_SHORT:
      case ARRAY_FLOAT:
      case ARRAY_DOUBLE:
      case ARRAY_LONG:
      case ARRAY_STRING:
      case ARRAY_OBJECT:
        return toPrimitiveArrayIfPossible(clz, value);

      case COLLECTION:
        return toCollection(clz, value);

      case INSTANCE:
        if (value instanceof Map) {
          return MapObjectConversion.fromMap((Map<String, Object>) value, clz);
        } else if (value instanceof List) {
          return MapObjectConversion.fromList((List<Object>) value, clz);
        } else if (clz.isInstance(value)) {
          return (T) value;
        } else {
          ClassMeta meta = ClassMeta.classMeta(clz);
          List<ConstructorAccess> constructors = meta.oneArgumentConstructors();

          if (constructors.size() == 0) {
            return null;
          } else if (constructors.size() == 1) {
            ConstructorAccess constructorAccess = constructors.get(0);
            Class<?> arg1Type = constructorAccess.parameterTypes()[0];
            if (arg1Type.isInstance(value)) {
              return (T) constructorAccess.create(value);
            } else {
              return (T) constructorAccess.create(coerce(arg1Type, value));
            }
          } else {
            for (ConstructorAccess c : constructors) {
              Class<?> arg1Type = c.parameterTypes()[0];
              if (arg1Type.isInstance(value)) {
                return (T) c.create(value);
              }
            }

            for (ConstructorAccess c : constructors) {
              Class<?> arg1Type = c.parameterTypes()[0];
              if (arg1Type.isAssignableFrom(value.getClass())) {
                return (T) c.create(value);
              }
            }

            flag[0] = false;
            break;
          }
        }

      case ENUM:
        return (T) toEnum((Class<? extends Enum>) clz, value);

      case CLASS:
        return (T) toClass(value);

      case TIME_ZONE:
        return (T) toTimeZone(flag, value);

      case UUID:
        return (T) toUUID(flag, value);

      case CURRENCY:
        return (T) toCurrency(value);

      case OBJECT:
        return (T) value;

      default:
        flag[0] = false;
        break;
    }
    return null;
  }