001: package org.shiftone.cache.config;
002:
003: import org.shiftone.cache.util.Log;
004:
005: import java.lang.reflect.InvocationTargetException;
006: import java.lang.reflect.Method;
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: /**
011: * This class treats methods case-insensitive. This can cause a problem if
012: * their are two setters with the same name in different case. Don't do that.
013: * @version $Revision: 1.6 $
014: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
015: */
016: public class BeanWrapper {
017:
018: private static final Log LOG = new Log(BeanWrapper.class);
019: private final Object object;
020: private final Class klass;
021: private Method[] methods;
022: private Map setters = new HashMap();
023:
024: public BeanWrapper(Object object) {
025:
026: this .object = object;
027: this .klass = object.getClass();
028: this .methods = klass.getMethods();
029:
030: for (int i = 0; i < methods.length; i++) {
031: Method method = methods[i];
032: String name = method.getName().toLowerCase();
033:
034: if ((name.startsWith("set")) //
035: && (method.getParameterTypes().length == 1) //
036: && (method.getReturnType().equals(Void.TYPE))) {
037: setters.put(name.substring(3), method);
038:
039: // LOG.info(name.substring(3) + " " + method.getReturnType());
040: }
041: }
042: }
043:
044: public Class getWrappedObjectClass() {
045: return klass;
046: }
047:
048: public Object getWrappedObject() {
049: return object;
050: }
051:
052: public Method getSetter(String name) throws NoSuchMethodException {
053:
054: Method method = (Method) setters.get(name.toLowerCase());
055:
056: if (method == null) {
057: throw new NoSuchMethodException("no setter for : " + name);
058: }
059:
060: return method;
061: }
062:
063: public Class getType(String name) throws NoSuchMethodException {
064: return getSetter(name).getParameterTypes()[0];
065: }
066:
067: public void setProperty(String name, String value)
068: throws NoSuchMethodException, IllegalAccessException,
069: IllegalArgumentException, InvocationTargetException,
070: ClassNotFoundException {
071: setProperty(name, convert(value, getType(name)));
072: }
073:
074: public void setProperty(String name, Object objectValue)
075: throws NoSuchMethodException, IllegalAccessException,
076: IllegalArgumentException, InvocationTargetException {
077: LOG.debug(klass.getName() + " SET " + name + " -> "
078: + objectValue);
079: getSetter(name).invoke(object, new Object[] { objectValue });
080: }
081:
082: private Object convert(String val, Class type)
083: throws IllegalArgumentException, ClassNotFoundException {
084:
085: String v = val.trim();
086:
087: if (val == null) {
088: return null;
089: } else if (Class.class.isAssignableFrom(type)) {
090: return Class.forName(val);
091: } else if (String.class.isAssignableFrom(type)) {
092: return val;
093: } else if (Integer.TYPE.isAssignableFrom(type)
094: || Integer.class.isAssignableFrom(type)) {
095: return new Integer(v);
096: } else if (Long.TYPE.isAssignableFrom(type)
097: || Long.class.isAssignableFrom(type)) {
098: return new Long(v);
099: } else if (Boolean.TYPE.isAssignableFrom(type)
100: || Boolean.class.isAssignableFrom(type)) {
101: if ("true".equalsIgnoreCase(v)) {
102: return Boolean.TRUE;
103: } else if ("false".equalsIgnoreCase(v)) {
104: return Boolean.FALSE;
105: }
106: }
107:
108: throw new IllegalArgumentException("unable to convert '" + v
109: + "' to '" + type.getName() + "' on class '"
110: + klass.getName() + "'");
111: }
112: }
|