@Test void testReadValue() { Instant datetime = Instant.ofEpochSecond(12, 34); Duration duration = Duration.ofSeconds(56, 78); Map<String, Literal> inputMap = new HashMap<>(); inputMap.put("i", literalOf(Primitive.ofInteger(123L))); inputMap.put("f", literalOf(Primitive.ofFloat(123.0))); inputMap.put("s", literalOf(Primitive.ofString("123"))); inputMap.put("b", literalOf(Primitive.ofBoolean(true))); inputMap.put("t", literalOf(Primitive.ofDatetime(datetime))); inputMap.put("d", literalOf(Primitive.ofDuration(duration))); inputMap.put("l", Literal.of(singletonList(literalOf(Primitive.ofString("123"))))); inputMap.put("m", Literal.of(singletonMap("marco", literalOf(Primitive.ofString("polo"))))); AutoValueInput input = AutoValueReflection.readValue(inputMap, AutoValueInput.class); assertThat(input.i(), equalTo(123L)); assertThat(input.f(), equalTo(123.0)); assertThat(input.s(), equalTo("123")); assertThat(input.b(), equalTo(true)); assertThat(input.t(), equalTo(datetime)); assertThat(input.d(), equalTo(duration)); assertThat(input.l(), equalTo(singletonList("123"))); assertThat(input.m(), equalTo(singletonMap("marco", "polo"))); }
static <T> T readValue(Map<String, Literal> inputs, Class<T> cls) { Map<String, Object> inputValues = toJavaMap(inputs); Constructor<T> constructor = getAutoValueConstructor(cls); Object[] paramValues = Arrays.stream(constructor.getParameters()) .map(param -> getParamValue(inputValues, param)) .toArray(); try { if (!constructor.isAccessible()) { constructor.setAccessible(true); } return constructor.newInstance(paramValues); } catch (Exception e) { throw new IllegalArgumentException( String.format( "Couldn't instantiate class [%s] with values [%s]", constructor.getDeclaringClass(), inputs), e); } }
AutoValueReflection { static <T> T readValue(Map<String, Literal> inputs, Class<T> cls) { Map<String, Object> inputValues = toJavaMap(inputs); Constructor<T> constructor = getAutoValueConstructor(cls); Object[] paramValues = Arrays.stream(constructor.getParameters()) .map(param -> getParamValue(inputValues, param)) .toArray(); try { if (!constructor.isAccessible()) { constructor.setAccessible(true); } return constructor.newInstance(paramValues); } catch (Exception e) { throw new IllegalArgumentException( String.format( "Couldn't instantiate class [%s] with values [%s]", constructor.getDeclaringClass(), inputs), e); } } }
AutoValueReflection { static <T> T readValue(Map<String, Literal> inputs, Class<T> cls) { Map<String, Object> inputValues = toJavaMap(inputs); Constructor<T> constructor = getAutoValueConstructor(cls); Object[] paramValues = Arrays.stream(constructor.getParameters()) .map(param -> getParamValue(inputValues, param)) .toArray(); try { if (!constructor.isAccessible()) { constructor.setAccessible(true); } return constructor.newInstance(paramValues); } catch (Exception e) { throw new IllegalArgumentException( String.format( "Couldn't instantiate class [%s] with values [%s]", constructor.getDeclaringClass(), inputs), e); } } private  AutoValueReflection(); }
AutoValueReflection { static <T> T readValue(Map<String, Literal> inputs, Class<T> cls) { Map<String, Object> inputValues = toJavaMap(inputs); Constructor<T> constructor = getAutoValueConstructor(cls); Object[] paramValues = Arrays.stream(constructor.getParameters()) .map(param -> getParamValue(inputValues, param)) .toArray(); try { if (!constructor.isAccessible()) { constructor.setAccessible(true); } return constructor.newInstance(paramValues); } catch (Exception e) { throw new IllegalArgumentException( String.format( "Couldn't instantiate class [%s] with values [%s]", constructor.getDeclaringClass(), inputs), e); } } private  AutoValueReflection();  }
AutoValueReflection { static <T> T readValue(Map<String, Literal> inputs, Class<T> cls) { Map<String, Object> inputValues = toJavaMap(inputs); Constructor<T> constructor = getAutoValueConstructor(cls); Object[] paramValues = Arrays.stream(constructor.getParameters()) .map(param -> getParamValue(inputValues, param)) .toArray(); try { if (!constructor.isAccessible()) { constructor.setAccessible(true); } return constructor.newInstance(paramValues); } catch (Exception e) { throw new IllegalArgumentException( String.format( "Couldn't instantiate class [%s] with values [%s]", constructor.getDeclaringClass(), inputs), e); } } private  AutoValueReflection();   }