001: /*
002: * Created on 10-Apr-2005
003:
004: */
005: package com.jofti.util;
006:
007: import java.lang.reflect.Array;
008: import java.lang.reflect.Constructor;
009: import java.lang.reflect.Field;
010: import java.lang.reflect.InvocationTargetException;
011: import java.sql.Timestamp;
012: import java.text.DateFormat;
013: import java.text.ParseException;
014: import java.util.Date;
015: import java.util.HashMap;
016: import java.util.Map;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: import com.jofti.exception.JoftiException;
022: import com.jofti.model.ComparableBoolean;
023:
024: import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
025:
026: /**
027: * @author xenephon (xenephon@jofti.com)
028: *
029: */
030: public class ReflectionUtil {
031:
032: private static Log log = LogFactory.getLog(ReflectionUtil.class);
033: private static HashMap primitives = new HashMap();
034: private static HashMap primitiveWrapperClasses = new HashMap();
035: private static Map classMap = new ConcurrentHashMap();
036:
037: static {
038: primitives.put(int.class.getName(), int.class);
039: primitives.put(byte.class.getName(), byte.class);
040: primitives.put(short.class.getName(), short.class);
041: primitives.put(char.class.getName(), char.class);
042: primitives.put(long.class.getName(), long.class);
043: primitives.put(boolean.class.getName(), boolean.class);
044: primitives.put(float.class.getName(), float.class);
045: primitives.put(double.class.getName(), double.class);
046: }
047:
048: static {
049: primitiveWrapperClasses.put(int.class, Integer.class);
050: primitiveWrapperClasses.put(byte.class, Byte.class);
051: primitiveWrapperClasses.put(short.class, Short.class);
052: primitiveWrapperClasses.put(char.class, Character.class);
053: primitiveWrapperClasses.put(long.class, Long.class);
054: primitiveWrapperClasses.put(boolean.class, Boolean.class);
055: primitiveWrapperClasses.put(float.class, Float.class);
056: primitiveWrapperClasses.put(double.class, Double.class);
057: }
058:
059: public static Class classForName(String name)
060: throws ClassNotFoundException {
061: Class clazz = null;
062: try {
063:
064: clazz = (Class) classMap.get(name);
065:
066: if (clazz != null) {
067: return clazz;
068: }
069:
070: ClassLoader classLoader = Thread.currentThread()
071: .getContextClassLoader();
072:
073: if (classLoader != null) {
074: int i = name.indexOf("[]");
075: if (i > -1) {
076: String temp = name.substring(0, i);
077: clazz = (Class) primitives.get(temp);
078: if (clazz == null) {
079: clazz = Class.forName(temp, false, classLoader);
080: }
081:
082: clazz = Array.newInstance(clazz, 0).getClass();
083: classMap.put(name, clazz);
084: return clazz;
085:
086: }
087: //return classLoader.loadClass(name);
088: clazz = Class.forName(name, false, classLoader);
089: classMap.put(name, clazz);
090: return clazz;
091:
092: } else {
093: clazz = Class.forName(name);
094: classMap.put(name, clazz);
095: return clazz;
096: }
097: } catch (Exception e) {
098: clazz = Class.forName(name);
099: classMap.put(name, clazz);
100: return clazz;
101: }
102: }
103:
104: public static Object constructObjectValue(Class clazz, String value)
105: 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
123: .getConstructor(new Class[] { String.class });
124: o = c.newInstance(new Object[] { value });
125: }
126:
127: } catch (NoSuchMethodException e) {
128: // we have to do something here
129: // for special classes
130: throw new JoftiException("Unable to construct " + clazz
131: + " from String");
132: } catch (InstantiationException e2) {
133: // the class is abstract
134: throw new JoftiException(e2);
135: } catch (IllegalAccessException e3) {
136: // we don't have permission to create an instance
137: throw new JoftiException(e3);
138: } catch (InvocationTargetException e4) {
139: // the construct threw an exception
140: throw new JoftiException("Unable to construct " + clazz
141: + " from String " + value, e4);
142: } catch (Exception e) {
143: throw new JoftiException("Unable to construct " + clazz
144: + " from String " + value, e);
145: }
146:
147: return o;
148:
149: }
150:
151: public static Date constructDate(String value)
152: throws JoftiException {
153:
154: Date date = null;
155:
156: try {
157: date = DateFormat.getDateTimeInstance(DateFormat.LONG,
158: DateFormat.LONG).parse(value);
159: return date;
160: } catch (ParseException e) {
161: // we do not care
162: }
163: try {
164: date = DateFormat.getDateTimeInstance(DateFormat.LONG,
165: DateFormat.MEDIUM).parse(value);
166: return date;
167: } catch (ParseException e) {
168: // we do not care
169: }
170: try {
171: date = DateFormat.getDateTimeInstance(DateFormat.LONG,
172: DateFormat.SHORT).parse(value);
173: return date;
174: } catch (ParseException e) {
175: // we do not care
176: }
177:
178: try {
179: date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
180: DateFormat.LONG).parse(value);
181: return date;
182: } catch (ParseException e) {
183: // we do not care
184: }
185: try {
186: date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
187: DateFormat.MEDIUM).parse(value);
188: return date;
189: } catch (ParseException e) {
190: // we do not care
191: }
192: try {
193: date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
194: DateFormat.SHORT).parse(value);
195: return date;
196: } catch (ParseException e) {
197: // we do not care
198: }
199:
200: try {
201: date = DateFormat.getDateTimeInstance(DateFormat.SHORT,
202: DateFormat.LONG).parse(value);
203: return date;
204: } catch (ParseException e) {
205: // we do not care
206: }
207: try {
208: date = DateFormat.getDateTimeInstance(DateFormat.SHORT,
209: DateFormat.MEDIUM).parse(value);
210: return date;
211: } catch (ParseException e) {
212: // we do not care
213: }
214: try {
215: date = DateFormat.getDateTimeInstance(DateFormat.SHORT,
216: DateFormat.SHORT).parse(value);
217: return date;
218: } catch (ParseException e) {
219: // we do not care
220: }
221:
222: try {
223: date = DateFormat.getDateTimeInstance().parse(value);
224: return date;
225: } catch (ParseException e) {
226: // we do not care
227: }
228:
229: try {
230: date = DateFormat.getDateInstance(DateFormat.LONG).parse(
231: value);
232: return date;
233: } catch (ParseException e) {
234: // we do not care
235: }
236: try {
237: date = DateFormat.getDateInstance(DateFormat.MEDIUM).parse(
238: value);
239: return date;
240: } catch (ParseException e) {
241: // we do not care
242: }
243: try {
244: date = DateFormat.getDateInstance(DateFormat.SHORT).parse(
245: value);
246: return date;
247: } catch (ParseException e) {
248: // we do not care
249: }
250: try {
251: date = DateFormat.getDateInstance(DateFormat.DEFAULT)
252: .parse(value);
253: return date;
254: } catch (ParseException e) {
255: // we do not care
256: }
257:
258: throw new JoftiException(
259: "Unable to construct java.util.Date from String "
260: + value);
261:
262: }
263:
264: public static Object getFieldValue(Object obj, String fieldName)
265: throws JoftiException {
266: try {
267: Class c = obj.getClass();
268:
269: // get the reflected object
270: Field field = c.getDeclaredField(fieldName);
271: // set accessible true
272: field.setAccessible(true);
273: // modify the member varaible
274: Object result = field.get(obj);
275:
276: return result;
277: } catch (Throwable t) {
278: throw new JoftiException(t);
279: }
280:
281: }
282:
283: }
|