ConfigAccessor { public <T> T getBean(String key, Class<? extends ExtractableBeanBuilder<T>> beanBuilderClass) { try { JSONObject jsonBean = get(key, null, new JSONObjectExtractor()); ConfigAccessor jsonBeanAccessor = new ConfigAccessor(jsonBean); Object beanBuilder = beanBuilderClass.newInstance(); for (Method m : beanBuilderClass.getMethods()) { if (m.getName().toLowerCase().startsWith("set")) { FieldExtractor extractor = m.getAnnotation(FieldExtractor.class); if (extractor != null) { if (!Extractor.class.isAssignableFrom(extractor.extractorClass())) { String message = String.format("extractor %s does not extend Extractor.class", extractor); LOG.error(message); throw new ConfigException(message); } if (!extractor.optional() || jsonBeanAccessor.hasKey(extractor.key())) { Object value = jsonBeanAccessor.get( extractor.key(), null, (Extractor) extractor.extractorClass().newInstance()); m.invoke(beanBuilder, value); } } else { LOG.warn("unable to find annotation for setter method " + m.getName()); } } } return ((ExtractableBeanBuilder<T>) beanBuilder).build(); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new ConfigException(e); } } ConfigAccessor(JSONObject jsonObject); static ConfigAccessor emptyConfig(); T getBean(String key, Class<? extends ExtractableBeanBuilder<T>> beanBuilderClass); Class<?> getClass(String key, Class<?> defaultValue); boolean hasKey(String key); Duration getDuration(String key, String defaultValue); long getDurationSeconds(String key, String defaultValue); long getDurationMillis(String key, String defaultValue); long getSizeBytes(String key, String defaultValue); long getSizeBytes(String key, Long defaultValue); Boolean getBoolean(String key); Boolean getBoolean(String key, Boolean defaultValue); String getString(String key); String getString(String key, String defaultValue); int getInt(String key); int getInt(String key, Integer defaultValue); long getLong(String key); long getLong(String key, Long defaultValue); double getDouble(String key); double getDouble(String key, Double defaultValue); Map<String, String> getStringMap(String key); List<T> getList(String key, Mapper<String, T> converter); List<String> getStringList(String key); List<ByteArray> getByteArrayList(String key); List<String> getKeys(); Map<String, String> getConfigAsMap(); @Override String toString(); String toString(int indentFactor);  }
@Test(groups = "fast") public void testExtractBean() throws Exception { Fuu bean = configAccessor.getBean("fuu", Fuu.FuuBuilder.class); Assert.assertEquals(bean.getX(), 600); Assert.assertEquals(bean.getY(), 6000000000L); Assert.assertEquals(bean.getSomeClass(), Object.class); Assert.assertEquals(bean.getS(), "baar"); }