| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.factory; |
| 17 | |
|
| 18 | |
import org.dozer.BeanFactory; |
| 19 | |
import org.dozer.util.MappingUtils; |
| 20 | |
import org.dozer.util.ReflectionUtils; |
| 21 | |
import org.slf4j.Logger; |
| 22 | |
import org.slf4j.LoggerFactory; |
| 23 | |
|
| 24 | |
import java.lang.reflect.Method; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 5 | public class JAXBBeanFactory implements BeanFactory { |
| 32 | |
|
| 33 | 5 | private final Logger log = LoggerFactory.getLogger(JAXBBeanFactory.class); |
| 34 | |
private static final char INNER_CLASS_DELIMETER = '$'; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public Object createBean(Object srcObj, Class<?> srcObjClass, String beanId) { |
| 48 | 14 | log.debug("createBean(Object, Class, String) - start [{}]", beanId); |
| 49 | |
|
| 50 | 14 | int indexOf = beanId.indexOf(INNER_CLASS_DELIMETER); |
| 51 | 21 | while (indexOf > 0) { |
| 52 | 7 | beanId = beanId.substring(0, indexOf) + beanId.substring(indexOf + 1); |
| 53 | 7 | log.debug("createBean(Object, Class, String) - HAS BEEN CHANGED TO [{}]", beanId); |
| 54 | 7 | indexOf = beanId.indexOf(INNER_CLASS_DELIMETER); |
| 55 | |
} |
| 56 | |
Object result; |
| 57 | |
|
| 58 | 14 | Class<?> objectFactory = MappingUtils.loadClass(beanId.substring(0, beanId.lastIndexOf(".")) + ".ObjectFactory"); |
| 59 | 13 | Object factory = ReflectionUtils.newInstance(objectFactory); |
| 60 | 13 | Method method = null; |
| 61 | |
try { |
| 62 | 13 | method = ReflectionUtils.getMethod(objectFactory, "create" + beanId.substring(beanId.lastIndexOf(".") + 1), new Class[] {}); |
| 63 | 0 | } catch (NoSuchMethodException e) { |
| 64 | 0 | MappingUtils.throwMappingException(e); |
| 65 | 13 | } |
| 66 | 13 | Object returnObject = ReflectionUtils.invoke(method, factory, new Object[] {}); |
| 67 | 13 | log.debug("createBean(Object, Class, String) - end [{}]", returnObject.getClass().getName()); |
| 68 | 13 | result = returnObject; |
| 69 | |
|
| 70 | 13 | return result; |
| 71 | |
} |
| 72 | |
|
| 73 | |
} |