1 20 package fr.dyade.aaa.util; 21 22 import java.util.Hashtable ; 23 import java.lang.reflect.Method ; 24 25 public class ConfigVariable{ 26 27 static Hashtable primitives = new Hashtable (); 28 static{ 29 primitives.put("int", Integer .class); 30 primitives.put("long", Long .class); 31 primitives.put("double", Double .class); 32 primitives.put("float", Float .class); 33 primitives.put("short", Short .class); 34 primitives.put("boolean", Boolean .class); 35 } 36 37 private String name; 38 private Class type; 39 private Object defaultValue; 40 private String info; 41 42 ConfigVariable(String name, Class type, Object defaultValue, String info){ 43 this.name = name; 44 this. type = type; 45 this.defaultValue = defaultValue; 46 this.info = info; 47 } 48 49 public final String getName(){ 50 return name; 51 } 52 53 public final Class getType(){ 54 return type; 55 } 56 57 public final Object getDefault(){ 58 return defaultValue; 59 } 60 61 public final String getInfo(){ 62 return info; 63 } 64 65 66 public static Class getClass(String type) throws Exception { 67 if (type == null || type.equals("")) 68 throw new Exception ("Type not defined"); 69 Object o = primitives.get(type); 70 if (o != null) 71 return (Class )o; 72 try{ 73 return Class.forName(type); 74 }catch(ClassNotFoundException e){ 75 return Class.forName("java.lang." + type); 77 } 78 } 79 80 public static Object getObject(Class c, String value) throws Exception { 81 if (c.equals(java.lang.String .class)) 82 return value;Class cp[] = new Class [1]; 83 cp[0] = java.lang.String .class; 84 Method m = c.getMethod("valueOf", cp); 85 Object op[] = new Object [1]; 86 op[0] = value; 87 Object res = m.invoke(null, op); 88 return res; 89 } 90 91 92 } 93 | Popular Tags |