001: package net.sourceforge.jaxor.util;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005:
006: /*
007: * User: Mike
008: * Date: Sep 2, 2003
009: * Time: 10:12:30 PM
010: */
011:
012: public class JavaBean {
013:
014: private final Object _bean;
015:
016: public JavaBean(Object bean) {
017: _bean = bean;
018: }
019:
020: public JavaBean(Class clzz) {
021: try {
022: _bean = clzz.newInstance();
023: } catch (InstantiationException e) {
024: throw new SystemException(e);
025: } catch (IllegalAccessException e) {
026: throw new SystemException(e);
027: }
028: }
029:
030: public void add(String propertyName, Object value) {
031: Method found = findMethod("add" + propertyName);
032: try {
033: found.invoke(_bean, new Object[] { value });
034: } catch (IllegalAccessException e) {
035: throw new SystemException(e);
036: } catch (IllegalArgumentException e) {
037: throw new SystemException(e);
038: } catch (InvocationTargetException e) {
039: throw new SystemException(e);
040: }
041: }
042:
043: public void set(String property, String value) {
044: Method found = findMethod("set" + property);
045: Converter converter = this .findConverter(found
046: .getParameterTypes()[0]);
047: try {
048: found.invoke(_bean, new Object[] { converter
049: .getValue(value) });
050: } catch (IllegalAccessException e) {
051: throw new SystemException(e);
052: } catch (IllegalArgumentException e) {
053: throw new SystemException(e);
054: } catch (InvocationTargetException e) {
055: throw new SystemException(e);
056: }
057: }
058:
059: private Method findMethod(String methodName) {
060: Method[] methods = _bean.getClass().getMethods();
061: for (int i = 0; i < methods.length; i++) {
062: Method method = methods[i];
063: String name = method.getName();
064: int length = method.getParameterTypes().length;
065: if (name.equalsIgnoreCase(methodName) && length == 1) {
066: method.setAccessible(true);
067: return method;
068: }
069: }
070: throw new MethodNotFound(methodName, _bean);
071: }
072:
073: public Object get(String propertyName) {
074: String name = "get" + propertyName;
075: try {
076: Method m = _bean.getClass().getMethod(name, new Class[0]);
077: m.setAccessible(true);
078: return m.invoke(_bean, new Object[0]);
079: } catch (NoSuchMethodException e) {
080: throw new MethodNotFound(name, _bean);
081: } catch (SecurityException e) {
082: throw new SystemException(e);
083: } catch (IllegalAccessException e) {
084: throw new SystemException(e);
085: } catch (IllegalArgumentException e) {
086: throw new SystemException(e);
087: } catch (InvocationTargetException e) {
088: throw new SystemException(e);
089: }
090: }
091:
092: private Converter findConverter(Class param) {
093: if (param.equals(String.class))
094: return new StringMethod();
095: if (param.equals(boolean.class) || param.equals(Boolean.class))
096: return new BooleanMethod();
097: if (param.equals(int.class))
098: return new IntMethod();
099: throw new SystemException(
100: "Failed to find converter method for class: " + param);
101: }
102:
103: public Object getWrappedObject() {
104: return _bean;
105: }
106:
107: private static class IntMethod implements Converter {
108: public Object getValue(String value) {
109: return new Integer(value);
110: }
111: }
112:
113: private static class BooleanMethod implements Converter {
114: public Object getValue(String value) {
115: return new Boolean(value);
116: }
117: }
118:
119: private static class StringMethod implements Converter {
120: public Object getValue(String value) {
121: return value;
122: }
123: }
124:
125: private interface Converter {
126: Object getValue(String value);
127: }
128: }
|