001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.generator.iwrpgen;
020:
021: import java.lang.reflect.Array;
022: import java.lang.reflect.Field;
023: import java.lang.reflect.Method;
024: import java.lang.reflect.ParameterizedType;
025: import java.lang.reflect.Type;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import de.schlund.pfixcore.generator.IWrapper;
032:
033: /**
034: * Helper class to populate IWrapper instances with data from Java beans.
035: *
036: * @author mleidig@schlund.de
037: */
038: public class BeanToIWrapper {
039:
040: private static Map<Class<?>, BeanDescriptor> descriptors = new HashMap<Class<?>, BeanDescriptor>();
041:
042: private static synchronized <T> BeanDescriptor getBeanDescriptor(
043: Class<T> clazz) {
044: BeanDescriptor desc = descriptors.get(clazz);
045: if (desc == null) {
046: desc = new BeanDescriptor(clazz);
047: descriptors.put(clazz, desc);
048: }
049: return desc;
050: }
051:
052: /**
053: * Populates an IWrapper instance with data from a Java bean.
054: *
055: * @param obj -
056: * the Java bean to populate the IWrapper
057: * @param wrapper -
058: * the IWrapper to be populated
059: */
060: public static void populateIWrapper(Object obj, IWrapper wrapper) {
061: Class<?> clazz = obj.getClass();
062: BeanDescriptor beanDesc = getBeanDescriptor(clazz);
063: BeanDescriptor wrapperBeanDesc = getBeanDescriptor(wrapper
064: .getClass());
065: Set<String> properties = wrapperBeanDesc.getProperties();
066: for (String property : properties) {
067: Object res = null;
068: Method getter = beanDesc.getGetMethod(property);
069: if (getter != null) {
070: try {
071: res = getter.invoke(obj, new Object[0]);
072: } catch (Exception x) {
073: throw new RuntimeException("Can't get property: "
074: + property, x);
075: }
076: } else {
077: Field field = beanDesc.getDirectAccessField(property);
078: if (field == null)
079: throw new RuntimeException(
080: "Can't find bean property: " + property);
081: try {
082: res = field.get(obj);
083: } catch (Exception x) {
084: throw new RuntimeException("Can't get property: "
085: + property, x);
086: }
087: }
088: Method setter = null;
089: try {
090: setter = wrapperBeanDesc.getSetMethod(property);
091: Type type = beanDesc.getPropertyType(property);
092: if (res != null && isList(type)) {
093: Class<?> compType = null;
094: Class<?>[] paramTypes = setter.getParameterTypes();
095: if (paramTypes.length == 1
096: && paramTypes[0].isArray())
097: compType = paramTypes[0].getComponentType();
098: if (compType == null)
099: throw new RuntimeException(
100: "Can't get array component type for property: "
101: + property);
102: List<?> list = (List<?>) res;
103: Object array = Array.newInstance(compType, list
104: .size());
105: for (int ind = 0; ind < list.size(); ind++) {
106: Object item = list.get(ind);
107: Array.set(array, ind, item);
108: }
109: res = array;
110: }
111: setter.invoke(wrapper, res);
112: } catch (Exception x) {
113: throw new RuntimeException("Can't set param: "
114: + property, x);
115: }
116: }
117: }
118:
119: private static boolean isList(Type type) {
120: if (type instanceof ParameterizedType) {
121: ParameterizedType ptype = (ParameterizedType) type;
122: Type rawType = ptype.getRawType();
123: if (rawType instanceof Class) {
124: Class<?> c = (Class<?>) rawType;
125: if (List.class.isAssignableFrom(c))
126: return true;
127: }
128: }
129: return false;
130: }
131:
132: }
|