001package com.typesafe.config; 002 003import com.typesafe.config.impl.ConfigBeanImpl; 004 005/** 006 * Factory for automatically creating a Java class from a {@link Config}. 007 * 008 * Example usage: 009 * 010 * <pre> 011 * Config configSource = ConfigFactory.load().getConfig("foo"); 012 * FooConfig config = ConfigBeanFactory.create(configSource, FooConfig.class); 013 * </pre> 014 * 015 * The Java class should follow JavaBean conventions. Field types 016 * can be any of the types you can normally get from a {@link 017 * Config}, including <code>java.time.Duration</code> or {@link 018 * ConfigMemorySize}. Fields may also be another JavaBean-style 019 * class. 020 * 021 * Fields are mapped to config by converting the config key to 022 * camel case. So the key <code>foo-bar</code> becomes JavaBean 023 * setter <code>setFooBar</code>. 024 */ 025public class ConfigBeanFactory { 026 027 /** 028 * Creates an instance of a class, initializing its fields from a {@link Config}. 029 * @param config source of config information 030 * @param clazz class to be instantiated 031 * @param <T> the type of the class to be instantiated 032 * @return an instance of the class populated with data from the config 033 * @throws ConfigException.BadBean 034 * If something is wrong with the JavaBean 035 * @throws ConfigException.ValidationFailed 036 * If the config doesn't conform to the bean's implied schema 037 * @throws ConfigException 038 * Can throw the same exceptions as the getters on <code>Config</code> 039 */ 040 public static <T> T create(Config config, Class<T> clazz) { 041 return ConfigBeanImpl.createInternal(config, clazz); 042 } 043}