Coverage Report - org.dozer.propertydescriptor.PropertyDescriptorFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyDescriptorFactory
71%
20/28
80%
29/36
7.333
 
 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.propertydescriptor;
 17  
 
 18  
 import org.dozer.fieldmap.HintContainer;
 19  
 import org.dozer.util.DozerConstants;
 20  
 import org.dozer.util.MappingUtils;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 import java.util.concurrent.CopyOnWriteArrayList;
 25  
 
 26  
 /**
 27  
  * Internal factory responsible for determining which property descriptor should
 28  
  * be used. Only intended for internal use.
 29  
  * 
 30  
  * @author garsombke.franz
 31  
  */
 32  
 public class PropertyDescriptorFactory {
 33  
 
 34  1
   private static final List<PropertyDescriptorCreationStrategy> pluggedDescriptorCreationStrategies =
 35  
           new ArrayList<PropertyDescriptorCreationStrategy>();
 36  
 
 37  0
   private PropertyDescriptorFactory() {
 38  0
   }
 39  
 
 40  
   public static DozerPropertyDescriptor getPropertyDescriptor(Class<?> clazz, String theGetMethod, String theSetMethod,
 41  
       String mapGetMethod, String mapSetMethod, boolean isAccessible, boolean isIndexed, int index, String name, String key,
 42  
       boolean isSelfReferencing, String oppositeFieldName, HintContainer srcDeepIndexHintContainer,
 43  
       HintContainer destDeepIndexHintContainer, String beanFactory) {
 44  58879
     DozerPropertyDescriptor desc = null;
 45  
 
 46  
     // Raw Map types or custom map-get-method/set specified
 47  58879
     boolean isMapProperty = MappingUtils.isSupportedMap(clazz);
 48  58879
     if (name.equals(DozerConstants.SELF_KEYWORD) &&
 49  
             (mapSetMethod != null || mapGetMethod != null || isMapProperty)) {
 50  1233
       String setMethod = isMapProperty ? "put" : mapSetMethod;
 51  1233
       String getMethod = isMapProperty ? "get" : mapGetMethod;
 52  
 
 53  1233
       desc = new MapPropertyDescriptor(clazz, name, isIndexed, index, setMethod,
 54  
               getMethod, key != null ? key : oppositeFieldName,
 55  
               srcDeepIndexHintContainer, destDeepIndexHintContainer);
 56  
 
 57  
       // Copy by reference(Not mapped backed properties which also use 'this'
 58  
       // identifier for a different purpose)
 59  1233
     } else if (isSelfReferencing) {
 60  350
       desc = new SelfPropertyDescriptor(clazz);
 61  
 
 62  
       // Access field directly and bypass getter/setters
 63  57296
     } else if (isAccessible) {
 64  330
       desc = new FieldPropertyDescriptor(clazz, name, isIndexed, index, srcDeepIndexHintContainer, destDeepIndexHintContainer);
 65  
 
 66  
       // Custom get-method/set specified
 67  56966
     } else if (theSetMethod != null || theGetMethod != null) {
 68  2449
       desc = new CustomGetSetPropertyDescriptor(clazz, name, isIndexed, index, theSetMethod, theGetMethod,
 69  
           srcDeepIndexHintContainer, destDeepIndexHintContainer);
 70  
 
 71  
       // If this object is an XML Bean - then use the XmlBeanPropertyDescriptor  
 72  54517
     } else if (beanFactory != null && beanFactory.equals(DozerConstants.XML_BEAN_FACTORY)) {
 73  120
       desc = new XmlBeanPropertyDescriptor(clazz, name, isIndexed, index, srcDeepIndexHintContainer, destDeepIndexHintContainer);
 74  
     }
 75  
 
 76  58879
     if (desc != null) return desc;
 77  
 
 78  
     for (PropertyDescriptorCreationStrategy propertyDescriptorBuilder :
 79  54397
             new CopyOnWriteArrayList<PropertyDescriptorCreationStrategy>(pluggedDescriptorCreationStrategies)) {
 80  0
       if (propertyDescriptorBuilder.isApplicable(clazz, name)) {
 81  0
         desc = propertyDescriptorBuilder.buildFor(
 82  
                 clazz, name, isIndexed, index, srcDeepIndexHintContainer, destDeepIndexHintContainer);
 83  0
         if (desc != null) break;
 84  
       }
 85  0
     }
 86  
 
 87  54397
     if (desc == null) {
 88  
       // Everything else. It must be a normal bean with normal custom get/set methods
 89  54397
       desc = new JavaBeanPropertyDescriptor(clazz, name, isIndexed, index, srcDeepIndexHintContainer, destDeepIndexHintContainer);
 90  
     }
 91  
 
 92  54397
     return desc;
 93  
   }
 94  
 
 95  
   public static void addPluggedPropertyDescriptorCreationStrategy(PropertyDescriptorCreationStrategy strategy) {
 96  0
     pluggedDescriptorCreationStrategies.add(strategy);
 97  0
   }
 98  
 }