Clover coverage report - SOJO - 1.0.0
Coverage timestamp: Mo Mai 4 2009 16:26:33 CEST
file stats: LOC: 148   Methods: 8
NCLOC: 97   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ComplexBean2MapConversion.java 100% 100% 100% 100%
coverage
 1    /*
 2    * Copyright 2002-2005 the original author or authors.
 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 net.sf.sojo.core.conversion;
 17   
 18    import java.lang.reflect.AccessibleObject;
 19    import java.util.HashMap;
 20    import java.util.Iterator;
 21    import java.util.Map;
 22    import java.util.Map.Entry;
 23   
 24    import net.sf.sojo.core.ComplexConversion;
 25    import net.sf.sojo.core.IConverter;
 26    import net.sf.sojo.core.IConverterExtension;
 27    import net.sf.sojo.core.NonCriticalExceptionHandler;
 28    import net.sf.sojo.core.UniqueIdGenerator;
 29    import net.sf.sojo.core.filter.ClassPropertyFilterHelper;
 30    import net.sf.sojo.core.reflect.Property;
 31    import net.sf.sojo.core.reflect.ReflectionHelper;
 32    import net.sf.sojo.core.reflect.ReflectionPropertyHelper;
 33    import net.sf.sojo.util.Util;
 34   
 35    /**
 36    * Convert complex bean (this mean a JavaBean) to a <code>java.util.Map</code>.
 37    *
 38    * @author linke
 39    *
 40    */
 41    public class ComplexBean2MapConversion extends ComplexConversion {
 42   
 43    public static final Class DEFAULT_MAP_TYPE = HashMap.class;
 44   
 45    private Class newBeanConversionType = null;
 46   
 47  1603 public ComplexBean2MapConversion() { this(null);}
 48   
 49  1610 public ComplexBean2MapConversion(Class pvBeanType) {
 50  1610 newBeanConversionType = pvBeanType;
 51  1610 if (newBeanConversionType == null) {
 52  1603 newBeanConversionType = DEFAULT_MAP_TYPE;
 53    }
 54  1610 if (Map.class.isAssignableFrom(newBeanConversionType) == false) {
 55  1 throw new IllegalArgumentException("The class: " + newBeanConversionType + " must be implements the java.util.Map interface.");
 56    }
 57  1609 if (newBeanConversionType.isInterface() == true) {
 58  1 throw new IllegalArgumentException("The class: " + newBeanConversionType + " mus be a implementation and not a interface.");
 59    }
 60    }
 61   
 62   
 63  4663 public final boolean isAssignableFrom(Object pvObject) {
 64  4663 return ReflectionHelper.isComplexType(pvObject);
 65    }
 66   
 67  1759 public boolean isAssignableTo(final Class pvToType) {
 68  1759 return ReflectionHelper.isMapType(pvToType);
 69    }
 70   
 71   
 72  735 public Object convert(final Object pvObject, final Class pvToType, final IConverterExtension pvConverter) {
 73  735 Map lvBeanMap = null;
 74  735 try {
 75  735 Class lvToType = ( ( pvToType == null || pvToType.isInterface() ) ? newBeanConversionType : pvToType);
 76   
 77  735 Map lvGetterMap = ReflectionPropertyHelper.getAllGetterProperties(pvObject.getClass(), null);
 78  735 lvBeanMap = (Map) ReflectionHelper.createNewIteratableInstance(lvToType, lvGetterMap.size());
 79   
 80    // filter for synthetik key unique id, this is a specific case
 81  735 if (ClassPropertyFilterHelper.isPropertyToFiltering(classPropertyFilterHandler, pvObject.getClass(), UniqueIdGenerator.UNIQUE_ID_PROPERTY) == false) {
 82  726 String lvUniqueId = pvConverter.getUniqueId(pvObject);
 83  726 lvBeanMap.put(UniqueIdGenerator.UNIQUE_ID_PROPERTY, lvUniqueId);
 84    }
 85   
 86   
 87  735 Iterator it = lvGetterMap.entrySet().iterator();
 88  735 lvBeanMap = (Map) super.iterate(pvObject, lvBeanMap, it, pvConverter);
 89    } catch (Exception e) {
 90    if (NonCriticalExceptionHandler.isNonCriticalExceptionHandlerEnabled()) {
 91    NonCriticalExceptionHandler.handleException(ComplexBean2MapConversion.class, e, "Problem by conver bean to map: " + e);
 92    }
 93    }
 94  735 return lvBeanMap;
 95   
 96    }
 97   
 98  3987 protected Object[] doTransformIteratorObject2KeyValuePair(Object pvIteratorObject) {
 99  3987 Map.Entry lvMapEntry = (Entry) pvIteratorObject;
 100  3987 Object lvKey = lvMapEntry.getKey();
 101  3987 Object lvValue = lvMapEntry.getValue();
 102  3987 return new Object[] { lvKey, lvValue };
 103    }
 104   
 105  3925 protected Object[] doConvert(Object pvSourceObject, final Object pvNewTargetObject, Object pvKey, Object pvValue, IConverter pvConverter) {
 106  3925 String propName = (String) pvKey;
 107  3925 Object lvNewValue = null;
 108    if (Util.getKeyWordClass().equals(propName) == false) {
 109    Object lvValue = null;
 110    AccessibleObject lvAccessibleObject = null;
 111    try {
 112    lvAccessibleObject = (AccessibleObject) pvValue;
 113    lvValue = new Property(lvAccessibleObject).executeGetValue(pvSourceObject);
 114    lvNewValue = pvConverter.convert(lvValue);
 115   
 116    } catch (Exception e) {
 117    if (NonCriticalExceptionHandler.isNonCriticalExceptionHandlerEnabled()) {
 118    NonCriticalExceptionHandler.handleException(ComplexBean2MapConversion.class, e, "Problem by invoke get from property: " + lvAccessibleObject);
 119    }
 120    }
 121   
 122    }
 123  3925 return new Object [] { pvKey, lvNewValue };
 124   
 125    }
 126   
 127   
 128  3516 protected void doAddObject(Object pvSourceObject, Object pvNewTargetObject, Object pvKey, Object pvValue, int pvIteratorPosition) {
 129  3516 Map lvBeanMap = (Map) pvNewTargetObject;
 130  3516 String propName = (String) pvKey;
 131   
 132    if (Util.getKeyWordClass().equals(propName) == false) {
 133    try {
 134    lvBeanMap.put(propName, pvValue);
 135    } catch (NullPointerException e) {
 136    if (NonCriticalExceptionHandler.isNonCriticalExceptionHandlerEnabled()) {
 137    NonCriticalExceptionHandler.handleException(ComplexBean2MapConversion.class, e, "Try to add a null-value to Map: " + lvBeanMap.getClass().getName());
 138    }
 139    }
 140    } else {
 141    // fuer das unmarshalling
 142    lvBeanMap.put(Util.getKeyWordClass(), pvSourceObject.getClass().getName());
 143    }
 144   
 145    }
 146   
 147   
 148    }