1 16 package net.sf.dozer.util.mapping.factory; 17 18 import java.lang.reflect.Method ; 19 20 import net.sf.dozer.util.mapping.BeanFactoryIF; 21 import net.sf.dozer.util.mapping.MappingException; 22 25 public class XMLBeanFactory implements BeanFactoryIF { 26 private static Class [] emptyArglist = new Class [0]; 27 38 public Object createBean(Object srcObj, Class srcObjClass, String beanId) { 39 Object result = null; 40 try { 41 Class destClass; 42 destClass = Class.forName(beanId); 43 Class [] innerClasses = destClass.getClasses(); 44 Class factory = null; 45 for (int i = 0; i < innerClasses.length; i++) { 46 if (innerClasses[i].getName().endsWith("Factory")) { 47 factory = innerClasses[i]; 48 } 49 } 50 if (factory == null) { 51 throw new MappingException("Factory class of Bean of type " + beanId + " not found."); 52 } 53 Method newInstance = factory.getMethod("newInstance", emptyArglist); 54 result = newInstance.invoke(null, emptyArglist); 55 } catch (ClassNotFoundException e) { 56 throw new MappingException("Bean of type " + beanId + " not found.", e); 57 } catch (Exception e) { 58 throw new MappingException(e); 59 } 60 return result; 61 } 62 } | Popular Tags |