KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > util > ReflectionUtil


1 /*
2  * Created on 10-Apr-2005
3
4  */

5 package com.jofti.util;
6
7 import java.lang.reflect.Array JavaDoc;
8 import java.lang.reflect.Constructor JavaDoc;
9 import java.lang.reflect.Field JavaDoc;
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.sql.Timestamp JavaDoc;
12 import java.text.DateFormat JavaDoc;
13 import java.text.ParseException JavaDoc;
14 import java.util.Date JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
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 /**
28  * @author xenephon (xenephon@jofti.com)
29  *
30  */

31 public class ReflectionUtil {
32     
33
34     private static Log log = LogFactory.getLog(ReflectionUtil.class);
35     private static HashMap JavaDoc primitives = new HashMap JavaDoc();
36     private static HashMap JavaDoc primitiveWrapperClasses = new HashMap JavaDoc();
37     private static Map JavaDoc 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 JavaDoc.class);
52         primitiveWrapperClasses.put(byte.class, Byte JavaDoc.class);
53         primitiveWrapperClasses.put(short.class, Short JavaDoc.class);
54         primitiveWrapperClasses.put(char.class, Character JavaDoc.class);
55         primitiveWrapperClasses.put(long.class, Long JavaDoc.class);
56         primitiveWrapperClasses.put(boolean.class, Boolean JavaDoc.class);
57         primitiveWrapperClasses.put(float.class, Float JavaDoc.class);
58         primitiveWrapperClasses.put(double.class, Double JavaDoc.class);
59     }
60     
61      public static Class JavaDoc classForName(String JavaDoc name) throws ClassNotFoundException JavaDoc {
62         Class JavaDoc clazz = null;
63         try {
64             
65             clazz = (Class JavaDoc)classMap.get(name);
66             
67             if (clazz != null){
68                 return clazz;
69             }
70             
71             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
72             
73             if(classLoader!=null) {
74                 int i= name.indexOf("[]");
75                 if (i>-1){
76                     String JavaDoc temp = name.substring(0,i);
77                      clazz = (Class JavaDoc)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                 //return classLoader.loadClass(name);
88
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 JavaDoc e) {
99             clazz = Class.forName(name);
100              classMap.put(name,clazz);
101              return clazz;
102         }
103     }
104      
105      public static Object JavaDoc constructObjectValue(Class JavaDoc clazz, String JavaDoc value) throws JoftiException {
106         
107         Object JavaDoc o =null;
108
109         try {
110             Class JavaDoc temp = (Class JavaDoc)primitiveWrapperClasses.get(clazz);
111             if (temp != null){
112                 clazz =temp;
113             }
114             if(clazz.equals(Timestamp JavaDoc.class)){
115                 log.info("constructing timestamp");
116                 o= Timestamp.valueOf(value);
117             }else if (clazz.isAssignableFrom(Date JavaDoc.class)){
118                 o = constructDate(value);
119             }else if (clazz.equals(Boolean JavaDoc.class)){
120                 o = new ComparableBoolean(value);
121             }else{
122                 Constructor JavaDoc c = clazz.getConstructor( new Class JavaDoc [] { String JavaDoc.class } );
123                 o = c.newInstance( new Object JavaDoc [] { value} );
124             }
125              
126         } catch ( NoSuchMethodException JavaDoc e ) {
127             // we have to do something here
128
// for special classes
129
throw new JoftiException("Unable to construct "+ clazz + " from String");
130         } catch ( InstantiationException JavaDoc e2 ) {
131             // the class is abstract
132
throw new JoftiException (e2);
133         } catch ( IllegalAccessException JavaDoc e3 ) {
134             // we don't have permission to create an instance
135
throw new JoftiException (e3);
136         } catch ( InvocationTargetException JavaDoc e4 ) {
137             // the construct threw an exception
138
throw new JoftiException ("Unable to construct "+ clazz + " from String " +value,e4);
139         } catch (Exception JavaDoc 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 JavaDoc constructDate(String JavaDoc value) throws JoftiException{
150          
151          Date JavaDoc date = null;
152
153          
154             try {
155                 date =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG).parse(value);
156                 return date;
157             } catch (ParseException JavaDoc e) {
158                 // we do not care
159
}
160             try {
161                 date =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.MEDIUM).parse(value);
162                 return date;
163             } catch (ParseException JavaDoc e) {
164                 // we do not care
165
}
166             try {
167                 date =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT).parse(value);
168                 return date;
169             } catch (ParseException JavaDoc e) {
170                 // we do not care
171
}
172          
173             
174             
175             try {
176                 date =DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.LONG ).parse(value);
177                 return date;
178             } catch (ParseException JavaDoc e) {
179                 // we do not care
180
}
181             try {
182                 date =DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM ).parse(value);
183                 return date;
184             } catch (ParseException JavaDoc e) {
185                 // we do not care
186
}
187             try {
188                 date =DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT ).parse(value);
189                 return date;
190             } catch (ParseException JavaDoc e) {
191                 // we do not care
192
}
193             
194             
195             try {
196                 date =DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.LONG).parse(value);
197                 return date;
198             } catch (ParseException JavaDoc e) {
199                 // we do not care
200
}
201             try {
202                 date =DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM).parse(value);
203                 return date;
204             } catch (ParseException JavaDoc e) {
205                 // we do not care
206
}
207             try {
208                 date =DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT).parse(value);
209                 return date;
210             } catch (ParseException JavaDoc e) {
211                 // we do not care
212
}
213             
214             try {
215                 date =DateFormat.getDateTimeInstance().parse(value);
216                 return date;
217             } catch (ParseException JavaDoc e) {
218                 // we do not care
219
}
220             
221             
222             try {
223                 date =DateFormat.getDateInstance(DateFormat.LONG).parse(value);
224                 return date;
225             } catch (ParseException JavaDoc e) {
226                 // we do not care
227
}
228             try {
229                 date =DateFormat.getDateInstance(DateFormat.MEDIUM).parse(value);
230                 return date;
231             } catch (ParseException JavaDoc e) {
232                 // we do not care
233
}
234             try {
235                 date =DateFormat.getDateInstance(DateFormat.SHORT).parse(value);
236                 return date;
237             } catch (ParseException JavaDoc e) {
238                 // we do not care
239
}
240             try {
241                 date =DateFormat.getDateInstance(DateFormat.DEFAULT).parse(value);
242                 return date;
243             } catch (ParseException JavaDoc e) {
244                 // we do not care
245
}
246             
247             throw new JoftiException("Unable to construct java.util.Date from String " +value);
248
249      }
250      
251      public static Object JavaDoc getFieldValue(Object JavaDoc obj, String JavaDoc fieldName) throws JoftiException{
252             try {
253                 Class JavaDoc c = obj.getClass();
254
255                 // get the reflected object
256
Field JavaDoc field = c.getDeclaredField(fieldName);
257                 // set accessible true
258
field.setAccessible(true);
259                 // modify the member varaible
260
Object JavaDoc result = field.get(obj);
261                 
262                 return result;
263             } catch (Throwable JavaDoc t){
264                 throw new JoftiException(t);
265             }
266                 
267         }
268      
269 }
270
Popular Tags