Coverage Report - org.dozer.classmap.generator.BeanMappingGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanMappingGenerator
88%
24/27
85%
12/14
2
BeanMappingGenerator$1
100%
3/3
N/A
2
BeanMappingGenerator$BeanFieldsDetector
N/A
N/A
2
 
 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.classmap.generator;
 17  
 
 18  
 import org.dozer.classmap.ClassMap;
 19  
 import org.dozer.classmap.ClassMapBuilder;
 20  
 import org.dozer.classmap.Configuration;
 21  
 import org.dozer.util.CollectionUtils;
 22  
 
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 import java.util.Set;
 26  
 import java.util.concurrent.CopyOnWriteArrayList;
 27  
 
 28  
 /**
 29  
 * @author Dmitry Spikhalskiy
 30  
 */
 31  3
 public class BeanMappingGenerator implements ClassMapBuilder.ClassMappingGenerator {
 32  
 
 33  1
   static final List<BeanFieldsDetector> pluggedFieldDetectors = new ArrayList<BeanFieldsDetector>();
 34  
 
 35  1
   static final List<BeanFieldsDetector> availableFieldDetectors = new ArrayList<BeanFieldsDetector>() {{
 36  1
     add(new JavaBeanFieldsDetector());
 37  1
   }};
 38  
 
 39  
   public boolean accepts(ClassMap classMap) {
 40  8647
     return true;
 41  
   }
 42  
 
 43  
   public boolean apply(ClassMap classMap, Configuration configuration) {
 44  8647
     Class<?> srcClass = classMap.getSrcClassToMap();
 45  8647
     Class<?> destClass = classMap.getDestClassToMap();
 46  
 
 47  8647
     Set<String> destFieldNames = getAcceptsFieldsDetector(destClass).getWritableFieldNames(destClass);
 48  8647
     Set<String> srcFieldNames = getAcceptsFieldsDetector(srcClass).getReadableFieldNames(srcClass);
 49  8647
     Set<String> commonFieldNames = CollectionUtils.intersection(srcFieldNames, destFieldNames);
 50  
 
 51  8647
     for (String fieldName : commonFieldNames) {
 52  16130
       if (GeneratorUtils.shouldIgnoreField(fieldName, srcClass, destClass)) {
 53  8
         continue;
 54  
       }
 55  
 
 56  
       // If field has already been accounted for, then skip
 57  16122
       if (classMap.getFieldMapUsingDest(fieldName) != null || classMap.getFieldMapUsingSrc(fieldName) != null) {
 58  315
         continue;
 59  
       }
 60  
 
 61  13395
       GeneratorUtils.addGenericMapping(classMap, configuration, fieldName, fieldName);
 62  13395
     }
 63  8647
     return false;
 64  
   }
 65  
 
 66  
   private static BeanFieldsDetector getAcceptsFieldsDetector(Class<?> clazz) {
 67  17294
     BeanFieldsDetector detector = getAcceptsFieldDetector(clazz, pluggedFieldDetectors);
 68  17294
     if (detector == null) detector = getAcceptsFieldDetector(clazz, availableFieldDetectors);
 69  17294
     return detector;
 70  
   }
 71  
 
 72  
   private static BeanFieldsDetector getAcceptsFieldDetector(Class<?> clazz, List<BeanFieldsDetector> detectors) {
 73  34588
     for (BeanFieldsDetector detector : new CopyOnWriteArrayList<BeanFieldsDetector>(detectors)) {
 74  17294
       if (detector.accepts(clazz)) return detector;
 75  0
     }
 76  17294
     return null;
 77  
   }
 78  
 
 79  
   public static void addPluggedFieldDetector(BeanFieldsDetector protobufBeanFieldsDetector) {
 80  0
     pluggedFieldDetectors.add(protobufBeanFieldsDetector);
 81  0
   }
 82  
 
 83  3
   protected interface BeanFieldsDetector {
 84  
     boolean accepts(Class<?> clazz);
 85  
     Set<String> getReadableFieldNames(Class<?> clazz);
 86  
     Set<String> getWritableFieldNames(Class<?> clazz);
 87  
   }
 88  
 }