Coverage Report - org.dozer.factory.JAXBBeanFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
JAXBBeanFactory
89%
17/19
100%
2/2
3
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * Public custom bean factory that can be used by applition code when mapping JAXB data objects
 28  
  * 
 29  
  * @author Vincent Jassogne
 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  
    * Create a bean implementation of a jaxb interface.
 38  
    * 
 39  
    * @param srcObj
 40  
    *          The source object
 41  
    * @param srcObjClass
 42  
    *          The source object class
 43  
    * @param beanId
 44  
    *          the name of the destination interface class
 45  
    * @return A implementation of the destination interface
 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  
 }