private static Object getListValue(Class<?> beanClass, Type parameterType, Class<?> parameterClass, Config config, String configPropName) { Type elementType = ((ParameterizedType) parameterType).getActualTypeArguments()[0]; if (elementType == Boolean.class) { return config.getBooleanList(configPropName); } else if (elementType == Integer.class) { return config.getIntList(configPropName); } else if (elementType == Double.class) { return config.getDoubleList(configPropName); } else if (elementType == Long.class) { return config.getLongList(configPropName); } else if (elementType == String.class) { return config.getStringList(configPropName); } else if (elementType == Duration.class) { return config.getDurationList(configPropName); } else if (elementType == ConfigMemorySize.class) { return config.getMemorySizeList(configPropName); } else if (elementType == Object.class) { return config.getAnyRefList(configPropName); } else if (elementType == Config.class) { return config.getConfigList(configPropName); } else if (elementType == ConfigObject.class) { return config.getObjectList(configPropName); } else if (elementType == ConfigValue.class) { return config.getList(configPropName); } else { throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported list element type " + elementType); } }
private static ConfigValueType getValueTypeOrNull(Class<?> parameterClass) { if (parameterClass == Boolean.class || parameterClass == boolean.class) { return ConfigValueType.BOOLEAN; } else if (parameterClass == Integer.class || parameterClass == int.class) { return ConfigValueType.NUMBER; } else if (parameterClass == Double.class || parameterClass == double.class) { return ConfigValueType.NUMBER; } else if (parameterClass == Long.class || parameterClass == long.class) { return ConfigValueType.NUMBER; } else if (parameterClass == String.class) { return ConfigValueType.STRING; } else if (parameterClass == Duration.class) { return null; } else if (parameterClass == ConfigMemorySize.class) { return null; } else if (parameterClass == List.class) { return ConfigValueType.LIST; } else if (parameterClass == Map.class) { return ConfigValueType.OBJECT; } else if (parameterClass == Config.class) { return ConfigValueType.OBJECT; } else if (parameterClass == ConfigObject.class) { return ConfigValueType.OBJECT; } else if (parameterClass == ConfigList.class) { return ConfigValueType.LIST; } else { return null; } }
private void assertPojoIsCorrect(TestPojo pojo) { Assert.assertTrue(pojo.isTestBoolean()); Assert.assertTrue(pojo.isTestYesBoolean()); Assert.assertEquals(12345679123l, pojo.getTestLong()); Assert.assertEquals(1, pojo.getTestInt()); Assert.assertEquals(123, pojo.getTestByte()); Assert.assertEquals(2.0, pojo.getTestDouble(), 0.001); Assert.assertEquals(2.0f, pojo.getTestFloat(), 0.001); Assert.assertEquals("test", pojo.getTestString()); Assert.assertEquals(Duration.of(10, ChronoUnit.SECONDS), pojo.getTestDuration()); Assert.assertEquals(ConfigMemorySize.ofBytes(524288), pojo.getTestSize()); NestedPojo nestedListPojo = pojo.getTestListOfNested().get(0); Assert.assertEquals(3, nestedListPojo.getNestInt()); Map<String, Integer> testMap = pojo.getTestMap(); Assert.assertEquals(1, testMap.get("one").intValue()); Map<Integer, String> testMapIntkey = pojo.getTestMapIntkey(); Assert.assertEquals("one", testMapIntkey.get("1")); Assert.assertEquals(Arrays.asList(true, false, true), pojo.getTestListOfBoolean()); Assert.assertEquals(Arrays.asList(1, 2, 3), pojo.getTestListOfInteger()); Assert.assertEquals(Arrays.asList(1.1, 2.2, 3.3), pojo.getTestListOfDouble()); Assert.assertEquals(Arrays.asList(12345679121L, 12345679122L, 12345679123L), pojo.getTestListOfLong()); Assert.assertEquals(Arrays.asList("a", "b", "c"), pojo.getTestListOfString()); Assert.assertEquals(Arrays.asList(Duration.of(1, ChronoUnit.SECONDS), Duration.of(2, ChronoUnit.SECONDS), Duration.of(3, ChronoUnit.SECONDS)), pojo.getTestListOfDuration()); Assert.assertEquals(Arrays.asList(ConfigMemorySize.ofBytes(524288), ConfigMemorySize.ofBytes(1048576), ConfigMemorySize.ofBytes(1073741824)), pojo.getTestListOfSize()); }
private static Object getValue(Class<?> beanClass, Type parameterType, Class<?> parameterClass, Config config, String configPropName) { if (parameterClass == Boolean.class || parameterClass == boolean.class) { return config.getBoolean(configPropName); } else if (parameterClass == Integer.class || parameterClass == int.class) { return config.getInt(configPropName); } else if (parameterClass == Double.class || parameterClass == double.class) { return config.getDouble(configPropName); } else if (parameterClass == Long.class || parameterClass == long.class) { return config.getLong(configPropName); } else if (parameterClass == String.class) { return config.getString(configPropName); } else if (parameterClass == Duration.class) { return config.getDuration(configPropName); } else if (parameterClass == ConfigMemorySize.class) { return config.getMemorySize(configPropName); } else if (parameterClass == Object.class) { return config.getAnyRef(configPropName); } else if (parameterClass == List.class) { return getListValue(beanClass, parameterType, parameterClass, config, configPropName); } else if (parameterClass == Map.class) { // we could do better here, but right now we don't. Type[] typeArgs = ((ParameterizedType) parameterType).getActualTypeArguments(); if (typeArgs[0] != String.class || typeArgs[1] != Object.class) { throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported Map<" + typeArgs[0] + "," + typeArgs[1] + ">, only Map<String,Object> is supported right now"); } return config.getObject(configPropName).unwrapped(); } else if (parameterClass == Config.class) { return config.getConfig(configPropName); } else if (parameterClass == ConfigObject.class) { return config.getObject(configPropName); } else if (parameterClass == ConfigValue.class) { return config.getValue(configPropName); } else if (parameterClass == ConfigList.class) { return config.getList(configPropName); } else if (hasAtLeastOneBeanProperty(parameterClass)) { return createInternal(config.getConfig(configPropName), parameterClass); } else { throw new ConfigException.BadBean("Bean property " + configPropName + " of class " + beanClass.getName() + " has unsupported type " + parameterType); } }
@SuppressWarnings("unchecked") private void setProperty(Method setter, String name, Config config) throws PropertySetterException { Class<?>[] paramTypes = setter.getParameterTypes(); final Object arg; try { Class<?> type = paramTypes[0]; if (String.class.isAssignableFrom(type)) { arg = config.getString(name); } else if (Integer.TYPE.isAssignableFrom(type)) { arg = new Integer(config.getInt(name)); } else if (Long.TYPE.isAssignableFrom(type)) { arg = new Long(config.getLong(name)); } else if (Float.TYPE.isAssignableFrom(type)) { arg = new Float(config.getDouble(name)); } else if (Double.TYPE.isAssignableFrom(type)) { arg = new Double(config.getDouble(name)); } else if (Boolean.TYPE.isAssignableFrom(type)) { arg = new Boolean(config.getBoolean(name)); } else if (Config.class.isAssignableFrom(type)) { arg = config.getConfig(name); } else if (Duration.class.isAssignableFrom(type)) { arg = config.getDuration(name); } else if (ConfigMemorySize.class.isAssignableFrom(type)) { arg = config.getMemorySize(name); } else if (type.isEnum()) { arg = convertToEnum(config.getString(name), (Class<? extends Enum<?>>) type); } else if (followsTheValueOfConvention(type)) { arg = convertByValueOfMethod(type, config.getString(name)); } else if (isOfTypeCharset(type)) { arg = convertToCharset(config.getString(name)); } else { arg = null; } } catch (Throwable t) { throw new PropertySetterException("Conversion to type [" + paramTypes[0] + "] failed. ", t); } if (arg == null) { throw new PropertySetterException("Conversion to type [" + paramTypes[0] + "] failed."); } try { setter.invoke(obj, arg); } catch (Exception ex) { throw new PropertySetterException(ex); } }
@SuppressWarnings("unchecked") private void addProperty(Method adder, String name, Config config) throws PropertySetterException { Class<?>[] paramTypes = adder.getParameterTypes(); final List<? extends Object> arg; try { Class<?> type = paramTypes[0]; if (String.class.isAssignableFrom(type)) { arg = config.getStringList(name); } else if (Integer.TYPE.isAssignableFrom(type)) { arg = config.getIntList(name); } else if (Long.TYPE.isAssignableFrom(type)) { arg = config.getLongList(name); } else if (Float.TYPE.isAssignableFrom(type)) { arg = config.getDoubleList(name); } else if (Double.TYPE.isAssignableFrom(type)) { arg = config.getDoubleList(name); } else if (Boolean.TYPE.isAssignableFrom(type)) { arg = config.getBooleanList(name); } else if (Config.class.isAssignableFrom(type)) { arg = config.getConfigList(name); } else if (Duration.class.isAssignableFrom(type)) { arg = config.getDurationList(name); } else if (ConfigMemorySize.class.isAssignableFrom(type)) { arg = config.getMemorySizeList(name); } else if (type.isEnum()) { arg = config.getStringList(name).stream().map(s -> convertToEnum(s, (Class<? extends Enum<?>>) type)) .collect(Collectors.toList()); } else if (followsTheValueOfConvention(type)) { arg = config.getStringList(name).stream().map(s -> convertByValueOfMethod(type, s)) .collect(Collectors.toList()); } else if (isOfTypeCharset(type)) { arg = config.getStringList(name).stream().map(s -> convertToCharset(s)).collect(Collectors.toList()); } else { arg = Collections.emptyList(); } } catch (Throwable t) { throw new PropertySetterException("Conversion to type [" + paramTypes[0] + "] failed. ", t); } if (arg == null) { throw new PropertySetterException("Conversion to type [" + paramTypes[0] + "] failed."); } try { for (Object o : arg) { adder.invoke(obj, o); } } catch ( Exception ex) { throw new PropertySetterException(ex); } }
@Test public void testSizeProperty() { TestBean bean = new TestBean(); ConfigPropertySetter propertySetter = new ConfigPropertySetter(beanCache, bean); Config config = ConfigFactory.load("bean"); Assert.assertEquals(null, bean.getSize()); propertySetter.setProperty("size", config); Assert.assertEquals(ConfigMemorySize.ofBytes(2048l * 1024l * 1024l), bean.getSize()); }
public ConfigMemorySize getSize() { return size; }
public void setSize(ConfigMemorySize size) { this.size = size; }
@Before public void setup() { Config testConf = ConfigFactory.load("conf/test.conf"); Module testModule = new AbstractModule() { @Override protected void configure() { bind(ConstructorInjectedPojo.class).asEagerSingleton(); bind(FieldInjectedPojo.class).asEagerSingleton(); bind(MethodInjectedPojo.class).asEagerSingleton(); } @Provides @Singleton ProvidedPojo providePojo( @TypesafeConfig("provided.boolean") boolean testBoolean, @TypesafeConfig("provided.yesBoolean") boolean testYesBoolean, @TypesafeConfig("provided.long") long testLong, @TypesafeConfig("provided.byte") byte testByte, @TypesafeConfig("provided.int") int testInt, @TypesafeConfig("provided.double") double testDouble, @TypesafeConfig("provided.float") float testFloat, @TypesafeConfig("provided.string") String testString, @TypesafeConfig("provided.list.boolean") List<Boolean> testListOfBoolean, @TypesafeConfig("provided.list.integer") List<Integer> testListOfInteger, @TypesafeConfig("provided.list.double") List<Double> testListOfDouble, @TypesafeConfig("provided.list.long") List<Long> testListOfLong, @TypesafeConfig("provided.list.string") List<String> testListOfString, @TypesafeConfig("provided.list.duration") List<Duration> testListOfDuration, @TypesafeConfig("provided.list.size") List<ConfigMemorySize> testListOfSize, @TypesafeConfig("provided.list.nested") List<NestedPojo> testListOfNested, @TypesafeConfig("provided.duration") Duration testDuration, @TypesafeConfig("provided.size") ConfigMemorySize testSize, @TypesafeConfig("provided.map") Map<String, Integer> testMap, @TypesafeConfig("provided.map.intkey") Map<Integer, String> testMapIntkey, @TypesafeConfig("provided.nested") NestedPojo testNestedPojo ) { return new ProvidedPojo(testBoolean, testYesBoolean, testLong, testByte, testInt, testDouble, testFloat, testString, testListOfBoolean, testListOfInteger, testListOfDouble, testListOfLong, testListOfString, testListOfDuration, testListOfSize, testListOfNested, testDuration, testSize, testMap, testMapIntkey, testNestedPojo); } }; injector = Guice.createInjector( TypesafeConfigModule.fromConfigWithPackage(testConf, "com.github.racc"), testModule ); }
@Inject public void setTestListOfSize(@TypesafeConfig("method.list.size") List<ConfigMemorySize> testListOfSize) { this.testListOfSize = testListOfSize; }
@Inject public void setTestSize(@TypesafeConfig("method.size") ConfigMemorySize testSize) { this.testSize = testSize; }
public List<ConfigMemorySize> getTestListOfSize() { return testListOfSize; }
public ConfigMemorySize getTestSize() { return testSize; }
public ProvidedPojo( boolean testBoolean, boolean testYesBoolean, long testLong, byte testByte, int testInt, double testDouble, float testFloat, String testString, List<Boolean> testListOfBoolean, List<Integer> testListOfInteger, List<Double> testListOfDouble, List<Long> testListOfLong, List<String> testListOfString, List<Duration> testListOfDuration, List<ConfigMemorySize> testListOfSize, List<NestedPojo> testListOfNested, Duration testDuration, ConfigMemorySize testSize, Map<String, Integer> testMap, Map<Integer, String> testMapIntkey, NestedPojo testNestedPojo ) { this.testBoolean = testBoolean; this.testYesBoolean = testYesBoolean; this.testLong = testLong; this.testByte = testByte; this.testInt = testInt; this.testDouble = testDouble; this.testFloat = testFloat; this.testString = testString; this.testListOfBoolean = testListOfBoolean; this.testListOfInteger = testListOfInteger; this.testListOfDouble = testListOfDouble; this.testListOfLong = testListOfLong; this.testListOfString = testListOfString; this.testListOfDuration = testListOfDuration; this.testListOfSize = testListOfSize; this.testListOfNested = testListOfNested; this.testDuration = testDuration; this.testSize = testSize; this.testMap = testMap; this.testMapIntkey = testMapIntkey; this.testNestedPojo = testNestedPojo; }
@Inject public ConstructorInjectedPojo( @TypesafeConfig("constructor.boolean") boolean testBoolean, @TypesafeConfig("constructor.boolean") boolean testBooleanAgain, @TypesafeConfig("constructor.yesBoolean") boolean testYesBoolean, @TypesafeConfig("constructor.long") long testLong, @TypesafeConfig("constructor.byte") byte testByte, @TypesafeConfig("constructor.int") int testInt, @TypesafeConfig("constructor.double") double testDouble, @TypesafeConfig("constructor.float") float testFloat, @TypesafeConfig("constructor.string") String testString, @TypesafeConfig("constructor.list.boolean") List<Boolean> testListOfBoolean, @TypesafeConfig("constructor.list.integer") List<Integer> testListOfInteger, @TypesafeConfig("constructor.list.double") List<Double> testListOfDouble, @TypesafeConfig("constructor.list.long") List<Long> testListOfLong, @TypesafeConfig("constructor.list.string") List<String> testListOfString, @TypesafeConfig("constructor.list.duration") List<Duration> testListOfDuration, @TypesafeConfig("constructor.list.size") List<ConfigMemorySize> testListOfSize, @TypesafeConfig("constructor.list.nested") List<NestedPojo> testListOfNested, @TypesafeConfig("constructor.duration") Duration testDuration, @TypesafeConfig("constructor.size") ConfigMemorySize testSize, @TypesafeConfig("constructor.map") Map<String, Integer> testMap, @TypesafeConfig("constructor.map.intkey") Map<Integer, String> testMapIntkey, @TypesafeConfig("constructor.nested") NestedPojo testNestedPojo ) { this.testBoolean = testBoolean; this.testYesBoolean = testYesBoolean; this.testLong = testLong; this.testByte = testByte; this.testInt = testInt; this.testDouble = testDouble; this.testFloat = testFloat; this.testString = testString; this.testListOfBoolean = testListOfBoolean; this.testListOfInteger = testListOfInteger; this.testListOfDouble = testListOfDouble; this.testListOfLong = testListOfLong; this.testListOfString = testListOfString; this.testListOfDuration = testListOfDuration; this.testListOfSize = testListOfSize; this.testListOfNested = testListOfNested; this.testDuration = testDuration; this.testSize = testSize; this.testMap = testMap; this.testMapIntkey = testMapIntkey; this.testNestedPojo = testNestedPojo; }
public List<ConfigMemorySize> getTestListOfSize();
public ConfigMemorySize getTestSize();