コード例 #1
0
ファイル: TypeConversionTest.java プロジェクト: avast/syringe
  @Test
  public void testBasic() throws Exception {
    Assert.assertEquals(true, TypeConversion.convert("true", Boolean.class).booleanValue());
    Assert.assertEquals(true, TypeConversion.convert("1", Boolean.class).booleanValue());

    Assert.assertEquals(1234567890, TypeConversion.convert("1234567890", Integer.class).intValue());

    Assert.assertEquals(
        12345678901234L, TypeConversion.convert("12345678901234", Long.class).longValue());

    Assert.assertEquals(
        TypeConversionTest.class,
        TypeConversion.convert(
            "com.avast.syringe.config.internal.TypeConversionTest", Class.class));
  }
コード例 #2
0
  @NotNull
  public Instantiator<?> findInstantiator(@NotNull Type type, @NotNull NamedTypeList types) {
    // First check if we have an immediate conversion registered. If so, we'll just use that.
    if (types.size() == 1) {
      TypeConversion conversion = findConversionFromDbValue(types.getType(0), type).orElse(null);
      if (conversion != null) return args -> conversion.convert(args.getSingleValue());
    }

    Class<?> cl = rawType(type);

    Instantiator<?> instantiator = findExplicitInstantiatorFor(cl, types).orElse(null);
    if (instantiator != null) return instantiator;

    if (!isPublic(cl.getModifiers()))
      throw new InstantiationFailureException(
          type
              + " can't be instantiated reflectively because it is not public or missing a @DalesbredConstructor-annotation");

    return candidateConstructorsSortedByDescendingParameterCount(cl)
        .map(ctor -> implicitInstantiatorFrom(ctor, types).orElse(null))
        .filter(Objects::nonNull)
        .findFirst()
        .orElseThrow(
            () ->
                new InstantiationFailureException(
                    "could not find a way to instantiate " + type + " with parameters " + types));
  }
コード例 #3
0
  @Nullable
  public Object valueToDatabase(@Nullable Object value) {
    if (value == null) return null;

    TypeConversion conversion =
        typeConversionRegistry.findConversionToDb(value.getClass()).orElse(null);
    if (conversion != null) return conversion.convert(value);
    else if (value instanceof Enum<?>) return dialect.valueToDatabase(((Enum<?>) value).name());
    else return dialect.valueToDatabase(value);
  }