/[projects]/WebConfig/src/dk/thoerup/webconfig/ConfigLoader.java
ViewVC logotype

Contents of /WebConfig/src/dk/thoerup/webconfig/ConfigLoader.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1369 - (show annotations) (download)
Wed Apr 20 21:20:58 2011 UTC (13 years, 1 month ago) by torben
File size: 2982 byte(s)
Add support for enum types
1 package dk.thoerup.webconfig;
2
3
4 import java.lang.reflect.Field;
5 import java.lang.reflect.Method;
6
7
8
9 abstract public class ConfigLoader {
10 // TODO: should this class be enhanced with a saveConfig() and a setValue() ??
11
12
13 public abstract String getValue(String paramName);
14
15 public void loadConfig(Object configObject) {
16
17 Field fields[] = configObject.getClass().getDeclaredFields();
18
19 for(Field field : fields) {
20 field.setAccessible(true);
21
22 ConfigVariable anno = field.getAnnotation(ConfigVariable.class);
23 if (anno == null)
24 continue;
25
26 String propertyName = anno.propertyname().equals("") ? field.getName() : anno.propertyname();
27
28 String value = getValue( propertyName );
29 if (value == null) {
30 //TODO: find another exception to throw here
31 throw new RuntimeException("Could not find a value to this field: " + field.getName() );
32 }
33
34 setFieldValue(field,configObject,value);
35 }
36
37 }
38
39 public static void setFieldValue(Field field, Object configObject, String value) {
40
41 field.setAccessible(true);
42 try {
43
44 if (field.getType().getName().equals("int") ) {
45 field.setInt(configObject, Integer.parseInt(value) );
46 } else if (field.getType().getName().equals("boolean") ) {
47 value = value.toLowerCase();
48 if (! (value.equals("true") || value.equals("false"))) {
49 //TODO: Find another exception to throw here
50 throw new RuntimeException("Only 'true' or 'false' as values for boolean field");
51 }
52 field.setBoolean(configObject, Boolean.parseBoolean(value) );
53 } else if (field.getType().getName().equals("long") ) {
54 field.setLong(configObject, Long.parseLong(value) );
55 } else if (field.getType().getName().equals("short") ) {
56 field.setShort(configObject, Short.parseShort(value) );
57 } else if (field.getType().getName().equals("double") ) {
58 field.setDouble(configObject, Double.parseDouble(value) );
59 } else if (field.getType().getName().equals("float") ) {
60 field.setFloat(configObject, Float.parseFloat(value) );
61 } else if (field.getType().getName().equals("char") ) {
62 field.setFloat(configObject, value.charAt(0) );
63 } else if (field.getType().isEnum() ) {
64 //throw new RuntimeException("Enums are not supported (yet)");
65 Object enumval = parseEnumValue(field.getType(), configObject,value);
66 field.set(configObject, enumval );
67 } else {
68 System.out.print(" >" + field.getType().getName() );
69 field.set(configObject, value );
70 }
71 } catch (IllegalAccessException e) {
72 //this should never happen since we already have called setAccessible(true)
73 throw new RuntimeException("How did we get here?", e);
74 }
75 }
76
77 protected static Object parseEnumValue(Class<?> cls, Object configObject, String value) {
78 try {
79 Class<?> argTypes[] = {String.class};
80 Object args[] = {value};
81
82 Method valueOf = cls.getMethod("valueOf", argTypes);
83 return valueOf.invoke(configObject, args);
84
85 } catch (Exception e) {
86 throw new RuntimeException(e);
87 }
88 }
89
90 }

  ViewVC Help
Powered by ViewVC 1.1.20