| 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 | |
|
| 22 | |
import java.lang.reflect.Method; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 1 | public class XMLBeanFactory implements BeanFactory { |
| 31 | 1 | private static final Class<?>[] emptyArglist = new Class[0]; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public Object createBean(Object srcObj, Class<?> srcObjClass, String beanId) { |
| 44 | |
Object result; |
| 45 | 14 | Class<?> destClass = MappingUtils.loadClass(beanId); |
| 46 | 14 | Class<?>[] innerClasses = destClass.getClasses(); |
| 47 | 14 | Class<?> factory = null; |
| 48 | 32 | for (Class<?> innerClass : innerClasses) { |
| 49 | 18 | if (innerClass.getName().endsWith("Factory")) { |
| 50 | 14 | factory = innerClass; |
| 51 | |
} |
| 52 | |
} |
| 53 | 14 | if (factory == null) { |
| 54 | 0 | MappingUtils.throwMappingException("Factory class of Bean of type " + beanId + " not found."); |
| 55 | |
} |
| 56 | 14 | Method newInstanceMethod = null; |
| 57 | |
try { |
| 58 | 14 | newInstanceMethod = ReflectionUtils.getMethod(factory, "newInstance", emptyArglist); |
| 59 | 0 | } catch (NoSuchMethodException e) { |
| 60 | 0 | MappingUtils.throwMappingException(e); |
| 61 | 14 | } |
| 62 | 14 | result = ReflectionUtils.invoke(newInstanceMethod, null, emptyArglist); |
| 63 | 14 | return result; |
| 64 | |
} |
| 65 | |
} |