Coverage Report - org.dozer.factory.DestBeanCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
DestBeanCreator
66%
12/18
83%
5/6
1.571
 
 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  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.Arrays;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.concurrent.CopyOnWriteArrayList;
 25  
 
 26  
 /**
 27  
  * Internal class that contains the logic used to create a new instance of the destination object being mapped. Performs
 28  
  * various checks to determine how the destination object instance is created. Only intended for internal use.
 29  
  *
 30  
  * @author tierney.matt
 31  
  * @author garsombke.franz
 32  
  * @author dmitry.buzdin
 33  
  */
 34  
 public final class DestBeanCreator {
 35  
 
 36  1
   static final List<BeanCreationStrategy> pluggedStrategies = new ArrayList<BeanCreationStrategy>();
 37  
 
 38  
   // order in this collection determines resolving priority
 39  1
   static final BeanCreationStrategy[] availableStrategies = new BeanCreationStrategy[]{
 40  
           ConstructionStrategies.byCreateMethod(),
 41  
           ConstructionStrategies.byGetInstance(),
 42  
           ConstructionStrategies.xmlGregorianCalendar(),
 43  
           ConstructionStrategies.byInterface(),
 44  
           ConstructionStrategies.xmlBeansBased(),
 45  
           ConstructionStrategies.byFactory(),
 46  
           ConstructionStrategies.byConstructor()
 47  
   };
 48  
 
 49  0
   private DestBeanCreator() {
 50  0
   }
 51  
 
 52  
   public static <T> T create(Class<T> targetClass) {
 53  131274
     return (T) create(targetClass, null);
 54  
   }
 55  
 
 56  
   public static Object create(Class<?> targetClass, Class<?> alternateClass) {
 57  131316
     return create(new BeanCreationDirective(null, null, targetClass, alternateClass, null, null, null));
 58  
   }
 59  
 
 60  
   public static Object create(BeanCreationDirective directive) {
 61  172955
     Object result = applyStrategies(directive, pluggedStrategies);
 62  172955
     if (result == null) result = applyStrategies(directive, Arrays.asList(availableStrategies));
 63  172951
     return result;
 64  
   }
 65  
 
 66  
   private static Object applyStrategies(BeanCreationDirective directive, List<BeanCreationStrategy> strategies) {
 67  
     // TODO create method lookup by annotation/convention
 68  
     // TODO Cache ConstructionStrategy (reuse caching infrastructure)
 69  
     // TODO Resolve JAXB by XmlType Annotation
 70  
     // TODO Check resulting type in each method
 71  
     // TODO Directive toString()
 72  
     // TODO review and document
 73  
 
 74  345910
     for (BeanCreationStrategy strategy : new CopyOnWriteArrayList<BeanCreationStrategy>(strategies)) {
 75  1210421
       if (strategy.isApplicable(directive)) {
 76  172955
         return strategy.create(directive);
 77  
       }
 78  1037466
     }
 79  172955
     return null;
 80  
   }
 81  
 
 82  
   public static void setStoredFactories(Map<String, BeanFactory> factories) {
 83  0
     ConstructionStrategies.byFactory().setStoredFactories(factories);
 84  0
   }
 85  
 
 86  
   public static void addPluggedStrategy(BeanCreationStrategy strategy) {
 87  0
     pluggedStrategies.add(strategy);
 88  0
   }
 89  
 
 90  
 }