Coverage Report - org.dozer.util.DeepHierarchyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DeepHierarchyUtils
0%
0/48
0%
0/28
4.25
 
 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.util;
 17  
 
 18  
 import org.dozer.fieldmap.HintContainer;
 19  
 import org.dozer.propertydescriptor.DozerPropertyDescriptor;
 20  
 import org.dozer.propertydescriptor.PropertyDescriptorFactory;
 21  
 
 22  
 import java.util.Collection;
 23  
 import java.util.StringTokenizer;
 24  
 
 25  
 /**
 26  
  * @author Dmitry Spikhalskiy
 27  
  */
 28  0
 public class DeepHierarchyUtils {
 29  
   // Copy-paste from GetterSetterPropertyDescriptor
 30  
   public static Object getDeepFieldValue(Object srcObj, String fieldName, boolean isIndexed, int index, HintContainer srcDeepIndexHintContainer) {
 31  
     // follow deep field hierarchy. If any values are null along the way, then return null
 32  0
     Object parentObj = srcObj;
 33  0
     Object hierarchyValue = parentObj;
 34  0
     DozerPropertyDescriptor[] hierarchy = getDeepFieldHierarchy(srcObj.getClass(), fieldName, srcDeepIndexHintContainer);
 35  
 
 36  0
     for (DozerPropertyDescriptor hierarchyElement : hierarchy) {
 37  0
       hierarchyValue = hierarchyElement.getPropertyValue(parentObj);
 38  0
       parentObj = hierarchyValue;
 39  0
       if (hierarchyValue == null) {
 40  0
         break;
 41  
       }
 42  
     }
 43  
 
 44  0
     if (isIndexed) {
 45  0
       hierarchyValue = MappingUtils.getIndexedValue(hierarchyValue, index);
 46  
     }
 47  
 
 48  0
     return hierarchyValue;
 49  
   }
 50  
 
 51  
   public static Class<?> getDeepFieldType(Class<?> clazz, String fieldName, HintContainer deepIndexHintContainer) {
 52  
     // follow deep field hierarchy. If any values are null along the way, then return null
 53  0
     DozerPropertyDescriptor[] hierarchy = getDeepFieldHierarchy(clazz, fieldName, deepIndexHintContainer);
 54  0
     return hierarchy[hierarchy.length - 1].getPropertyType();
 55  
   }
 56  
 
 57  
   public static Class<?> getDeepGenericType(Class<?> clazz, String fieldName, HintContainer deepIndexHintContainer) {
 58  
     // follow deep field hierarchy. If any values are null along the way, then return null
 59  0
     DozerPropertyDescriptor[] hierarchy = getDeepFieldHierarchy(clazz, fieldName, deepIndexHintContainer);
 60  0
     return hierarchy[hierarchy.length - 1].genericType();
 61  
   }
 62  
 
 63  
   private static DozerPropertyDescriptor[] getDeepFieldHierarchy(Class<?> parentClass, String field, HintContainer deepIndexHintContainer) {
 64  0
     if (!MappingUtils.isDeepMapping(field)) {
 65  0
       MappingUtils.throwMappingException("Field does not contain deep field delimiter");
 66  
     }
 67  
 
 68  0
     StringTokenizer toks = new StringTokenizer(field, DozerConstants.DEEP_FIELD_DELIMITER);
 69  0
     Class<?> latestClass = parentClass;
 70  0
     DozerPropertyDescriptor[] hierarchy = new DozerPropertyDescriptor[toks.countTokens()];
 71  0
     int index = 0;
 72  0
     int hintIndex = 0;
 73  0
     while (toks.hasMoreTokens()) {
 74  0
       String aFieldName = toks.nextToken();
 75  0
       String theFieldName = aFieldName;
 76  0
       int collectionIndex = -1;
 77  
 
 78  0
       if (aFieldName.contains("[")) {
 79  0
         theFieldName = aFieldName.substring(0, aFieldName.indexOf("["));
 80  0
         collectionIndex = Integer.parseInt(aFieldName.substring(aFieldName.indexOf("[") + 1, aFieldName.indexOf("]")));
 81  
       }
 82  
 
 83  0
       DozerPropertyDescriptor propDescriptor = PropertyDescriptorFactory.getPropertyDescriptor(latestClass, null, null, null, null, false,
 84  
               collectionIndex > -1, collectionIndex, theFieldName, null,
 85  
               false, null, null, null, null); //we can pass null as a hint container - if genericType return null - we will use hintContainer in the underlying if
 86  
 
 87  0
       if (propDescriptor == null) {
 88  0
         MappingUtils.throwMappingException("Exception occurred determining deep field hierarchy for Class --> "
 89  
                 + parentClass.getName() + ", Field --> " + field + ".  Unable to determine property descriptor for Class --> "
 90  
                 + latestClass.getName() + ", Field Name: " + aFieldName);
 91  
       }
 92  
 
 93  0
       latestClass = propDescriptor.getPropertyType();
 94  0
       if (toks.hasMoreTokens()) {
 95  0
         if (latestClass.isArray()) {
 96  0
           latestClass = latestClass.getComponentType();
 97  0
         } else if (Collection.class.isAssignableFrom(latestClass)) {
 98  0
           Class<?> genericType = propDescriptor.genericType();
 99  
 
 100  0
           if (genericType == null && deepIndexHintContainer == null) {
 101  0
             MappingUtils
 102  
                     .throwMappingException("Hint(s) or Generics not specified.  Hint(s) or Generics must be specified for deep mapping with indexed field(s). Exception occurred determining deep field hierarchy for Class --> "
 103  
                             + parentClass.getName()
 104  
                             + ", Field --> "
 105  
                             + field
 106  
                             + ".  Unable to determine property descriptor for Class --> "
 107  
                             + latestClass.getName() + ", Field Name: " + aFieldName);
 108  
           }
 109  0
           if (genericType != null) {
 110  0
             latestClass = genericType;
 111  
           } else {
 112  0
             latestClass = deepIndexHintContainer.getHint(hintIndex);
 113  0
             hintIndex += 1;
 114  
           }
 115  
         }
 116  
       }
 117  0
       hierarchy[index++] = propDescriptor;
 118  0
     }
 119  
 
 120  0
     return hierarchy;
 121  
   }
 122  
 }