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.ArrayList;
027: import java.util.HashMap;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import de.schlund.pfixcore.generator.IWrapper;
033:
034: /**
035: *
036: * @author mleidig@schlund.de
037: *
038: */
039: public class IWrapperToBean {
040:
041: private static Map<Class<?>, BeanDescriptor> descriptors = new HashMap<Class<?>, BeanDescriptor>();
042:
043: private static synchronized <T> BeanDescriptor getBeanDescriptor(
044: Class<T> clazz) {
045: BeanDescriptor desc = descriptors.get(clazz);
046: if (desc == null) {
047: desc = new BeanDescriptor(clazz);
048: descriptors.put(clazz, desc);
049: }
050: return desc;
051: }
052:
053: public static <T> T createBean(IWrapper wrapper, Class<T> beanClass) {
054: try {
055: T instance = beanClass.newInstance();
056: populateBean(wrapper, instance);
057: return instance;
058: } catch (Exception x) {
059: throw new RuntimeException("Can't instantiate bean: "
060: + beanClass.getName(), x);
061: }
062: }
063:
064: public static void populateBean(IWrapper wrapper, Object obj) {
065: Class<?> clazz = obj.getClass();
066: BeanDescriptor beanDesc = getBeanDescriptor(clazz);
067: BeanDescriptor wrapperBeanDesc = getBeanDescriptor(wrapper
068: .getClass());
069: Set<String> properties = wrapperBeanDesc.getProperties();
070: for (String property : properties) {
071: String getterName = createGetterName(property);
072: Method getter = null;
073: try {
074: getter = wrapper.getClass().getMethod(getterName,
075: new Class[0]);
076: } catch (NoSuchMethodException x) {
077: throw new RuntimeException("Can't find getter: "
078: + getterName, x);
079: }
080: Object res = null;
081: try {
082: res = getter.invoke(wrapper, new Object[0]);
083: } catch (Exception x) {
084: throw new RuntimeException("Can't get property: "
085: + property, x);
086: }
087: Method setter = beanDesc.getSetMethod(property);
088: if (setter == null) {
089: Field field = null;
090: field = beanDesc.getDirectAccessField(property);
091: if (field == null)
092: throw new RuntimeException(
093: "Can't find bean property: " + property);
094: try {
095: Type type = beanDesc.getPropertyType(property);
096: if (isList(type)) {
097: res = convertArrayToList(res);
098: }
099: field.set(obj, res);
100: } catch (Exception x) {
101: throw new RuntimeException("Can't set property: "
102: + property, x);
103: }
104: } else {
105: try {
106: Type type = beanDesc.getPropertyType(property);
107: if (isList(type)) {
108: res = convertArrayToList(res);
109: }
110: setter.invoke(obj, res);
111: } catch (Exception x) {
112: throw new RuntimeException("Can't set property: "
113: + property, x);
114: }
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: private static List<Object> convertArrayToList(Object array) {
133: if (array == null)
134: return null;
135: List<Object> list = new ArrayList<Object>();
136: int len = Array.getLength(array);
137: for (int ind = 0; ind < len; ind++) {
138: Object item = Array.get(array, ind);
139: list.add(item);
140: }
141: return list;
142: }
143:
144: private static String createGetterName(String propName) {
145: return "get" + Character.toUpperCase(propName.charAt(0))
146: + propName.substring(1);
147: }
148:
149: }
|