1 5 package com.jofti.util; 6 7 import java.lang.reflect.Array ; 8 import java.lang.reflect.Constructor ; 9 import java.lang.reflect.Field ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.sql.Timestamp ; 12 import java.text.DateFormat ; 13 import java.text.ParseException ; 14 import java.util.Date ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 import com.jofti.exception.JoftiException; 22 import com.jofti.model.ComparableBoolean; 23 24 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 25 26 27 31 public class ReflectionUtil { 32 33 34 private static Log log = LogFactory.getLog(ReflectionUtil.class); 35 private static HashMap primitives = new HashMap (); 36 private static HashMap primitiveWrapperClasses = new HashMap (); 37 private static Map classMap = new ConcurrentHashMap(); 38 39 static { 40 primitives.put(int.class.getName(), int.class); 41 primitives.put(byte.class.getName(), byte.class); 42 primitives.put(short.class.getName(), short.class); 43 primitives.put(char.class.getName(), char.class); 44 primitives.put(long.class.getName(), long.class); 45 primitives.put(boolean.class.getName(), boolean.class); 46 primitives.put(float.class.getName(), float.class); 47 primitives.put(double.class.getName(), double.class); 48 } 49 50 static { 51 primitiveWrapperClasses.put(int.class, Integer .class); 52 primitiveWrapperClasses.put(byte.class, Byte .class); 53 primitiveWrapperClasses.put(short.class, Short .class); 54 primitiveWrapperClasses.put(char.class, Character .class); 55 primitiveWrapperClasses.put(long.class, Long .class); 56 primitiveWrapperClasses.put(boolean.class, Boolean .class); 57 primitiveWrapperClasses.put(float.class, Float .class); 58 primitiveWrapperClasses.put(double.class, Double .class); 59 } 60 61 public static Class classForName(String name) throws ClassNotFoundException { 62 Class clazz = null; 63 try { 64 65 clazz = (Class )classMap.get(name); 66 67 if (clazz != null){ 68 return clazz; 69 } 70 71 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 72 73 if(classLoader!=null) { 74 int i= name.indexOf("[]"); 75 if (i>-1){ 76 String temp = name.substring(0,i); 77 clazz = (Class )primitives.get(temp); 78 if (clazz == null){ 79 clazz = Class.forName(temp,false,classLoader); 80 } 81 82 clazz = Array.newInstance(clazz,0).getClass(); 83 classMap.put(name,clazz); 84 return clazz; 85 86 } 87 clazz = Class.forName(name,false,classLoader); 89 classMap.put(name,clazz); 90 return clazz; 91 92 } else { 93 clazz = Class.forName(name); 94 classMap.put(name,clazz); 95 return clazz; 96 } 97 } 98 catch (Exception e) { 99 clazz = Class.forName(name); 100 classMap.put(name,clazz); 101 return clazz; 102 } 103 } 104 105 public static Object constructObjectValue(Class clazz, String value) throws JoftiException { 106 107 Object o =null; 108 109 try { 110 Class temp = (Class )primitiveWrapperClasses.get(clazz); 111 if (temp != null){ 112 clazz =temp; 113 } 114 if(clazz.equals(Timestamp .class)){ 115 log.info("constructing timestamp"); 116 o= Timestamp.valueOf(value); 117 }else if (clazz.isAssignableFrom(Date .class)){ 118 o = constructDate(value); 119 }else if (clazz.equals(Boolean .class)){ 120 o = new ComparableBoolean(value); 121 }else{ 122 Constructor c = clazz.getConstructor( new Class [] { String .class } ); 123 o = c.newInstance( new Object [] { value} ); 124 } 125 126 } catch ( NoSuchMethodException e ) { 127 throw new JoftiException("Unable to construct "+ clazz + " from String"); 130 } catch ( InstantiationException e2 ) { 131 throw new JoftiException (e2); 133 } catch ( IllegalAccessException e3 ) { 134 throw new JoftiException (e3); 136 } catch ( InvocationTargetException e4 ) { 137 throw new JoftiException ("Unable to construct "+ clazz + " from String " +value,e4); 139 } catch (Exception e){ 140 throw new JoftiException ("Unable to construct "+ clazz + " from String " +value,e); 141 } 142 143 return o; 144 145 } 146 147 148 149 public static Date constructDate(String value) throws JoftiException{ 150 151 Date date = null; 152 153 154 try { 155 date =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).parse(value); 156 return date; 157 } catch (ParseException e) { 158 } 160 try { 161 date =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.MEDIUM).parse(value); 162 return date; 163 } catch (ParseException e) { 164 } 166 try { 167 date =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT).parse(value); 168 return date; 169 } catch (ParseException e) { 170 } 172 173 174 175 try { 176 date =DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.LONG ).parse(value); 177 return date; 178 } catch (ParseException e) { 179 } 181 try { 182 date =DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM ).parse(value); 183 return date; 184 } catch (ParseException e) { 185 } 187 try { 188 date =DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT ).parse(value); 189 return date; 190 } catch (ParseException e) { 191 } 193 194 195 try { 196 date =DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.LONG).parse(value); 197 return date; 198 } catch (ParseException e) { 199 } 201 try { 202 date =DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM).parse(value); 203 return date; 204 } catch (ParseException e) { 205 } 207 try { 208 date =DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT).parse(value); 209 return date; 210 } catch (ParseException e) { 211 } 213 214 try { 215 date =DateFormat.getDateTimeInstance().parse(value); 216 return date; 217 } catch (ParseException e) { 218 } 220 221 222 try { 223 date =DateFormat.getDateInstance(DateFormat.LONG).parse(value); 224 return date; 225 } catch (ParseException e) { 226 } 228 try { 229 date =DateFormat.getDateInstance(DateFormat.MEDIUM).parse(value); 230 return date; 231 } catch (ParseException e) { 232 } 234 try { 235 date =DateFormat.getDateInstance(DateFormat.SHORT).parse(value); 236 return date; 237 } catch (ParseException e) { 238 } 240 try { 241 date =DateFormat.getDateInstance(DateFormat.DEFAULT).parse(value); 242 return date; 243 } catch (ParseException e) { 244 } 246 247 throw new JoftiException("Unable to construct java.util.Date from String " +value); 248 249 } 250 251 public static Object getFieldValue(Object obj, String fieldName) throws JoftiException{ 252 try { 253 Class c = obj.getClass(); 254 255 Field field = c.getDeclaredField(fieldName); 257 field.setAccessible(true); 259 Object result = field.get(obj); 261 262 return result; 263 } catch (Throwable t){ 264 throw new JoftiException(t); 265 } 266 267 } 268 269 } 270 | Popular Tags |