blob: e116dea6f6c6b2c8950e0ff3251632088ac5355a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package bjc.utils.configuration;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* Bind the values in a prepared class to a config file
*
* @author ben
*
*/
public class Configurator {
/**
* Bind the values in a config file to the values in a class,
* substituting default values if none are appropriate
*
* @param <E>
* The type of the object to bind
* @param clasz
* The class of the object to bind
* @param inputSource
* The source to get input from
* @return A instance of the provided class, with values filled in from
* a config file
*/
public static <E> E readConfig(Class<E> clasz,
InputStream inputSource) {
try {
Constructor<E> noArgConstructor = clasz.getConstructor();
E backingStore = noArgConstructor.newInstance();
return backingStore;
} catch (NoSuchMethodException | SecurityException
| InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
|