public void validate() { if (srcField == null) { MappingUtils.throwMappingException("src field must be specified"); } if (destField == null) { MappingUtils.throwMappingException("dest field must be specified"); } }
public String createFieldMappingSuccessMsg(Class<?> srcClass, Class<?> destClass, String srcFieldName, String destFieldName, Object srcFieldValue, Object destFieldValue, String classMapId) { String srcClassStr = MappingUtils.getClassNameWithoutPackage(srcClass); String destClassStr = MappingUtils.getClassNameWithoutPackage(destClass); return "MAPPED: " + srcClassStr + "." + srcFieldName + " --> " + destClassStr + "." + destFieldName + " VALUES: " + getLogOutput(srcFieldValue) + " --> " + getLogOutput(destFieldValue) + " MAPID: " + (classMapId != null ? classMapId : ""); }
public static List<Class<?>> getSuperClassesAndInterfaces(Class<?> srcClass) { List<Class<?>> superClasses = new ArrayList<Class<?>>(); Class<?> realClass = getRealClass(srcClass); // Add all super classes first Class<?> superClass = getRealClass(realClass).getSuperclass(); while (!isBaseClass(superClass)) { superClasses.add(superClass); superClass = superClass.getSuperclass(); } // Now add all interfaces of the passed in class and all it's super classes // Linked hash set so duplicated are not added but insertion order is kept LinkedHashSet<Class<?>> interfaces = new LinkedHashSet<Class<?>>(); interfaces.addAll(getInterfaceHierarchy(realClass)); for (Class<?> clazz : superClasses) { interfaces.addAll(getInterfaceHierarchy(clazz)); } superClasses.addAll(interfaces); return superClasses; }
public static Object prepareIndexedCollection(Class<?> collectionType, Object existingCollection, Object collectionEntry, int index) { Object result = null; if (collectionType.isArray()) { result = prepareIndexedArray(collectionType, existingCollection, collectionEntry, index); } else if (Collection.class.isAssignableFrom(collectionType)) { result = prepareIndexedCollectionType(collectionType, existingCollection, collectionEntry, index); } else { throwMappingException("Only types java.lang.Object[] and java.util.Collection are supported for indexed properties."); } return result; }
public void validate() { if (srcField == null || MappingUtils.isBlankOrNull(srcField.getName())) { MappingUtils.throwMappingException("src field must be specified"); } if (destField == null || MappingUtils.isBlankOrNull(destField.getName())) { MappingUtils.throwMappingException("dest field must be specified"); } }
if (!MappingUtils.isBlankOrNull(classMap.getMapId())) { if (mapIds.contains(classMap.getMapId())) { throw new IllegalArgumentException("Duplicate Map Id's Found. Map Id: " + classMap.getMapId()); MappingUtils.reverseFields(classMap, classMapPrime); if ((isSupportedMap(classMap.getDestClassToMap()) && !isSupportedMap(classMap .getSrcClassToMap())) || (isSupportedMap(classMap.getSrcClassToMap()) && !isSupportedMap(classMap .getDestClassToMap())) || (isSupportedMap(fieldMap.getDestFieldType(classMap .getDestClassToMap())) && !isSupportedMap(fieldMap.getSrcFieldType(classMap .getSrcClassToMap()))) || (isSupportedMap(fieldMap.getSrcFieldType(classMap .getSrcClassToMap()))) && !isSupportedMap(fieldMap.getDestFieldType(classMap .getDestClassToMap()))) { FieldMap fm = new MapFieldMap(fieldMap); MappingUtils.reverseFields(fieldMap, fieldMapPrime); MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMap, classMap); MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMapPrime, classMapPrime); MappingUtils.reverseFields(fieldMap, fieldMapPrime); .throwMappingException("custom converter should be provided for n to 1 field mapping"); MappingUtils.applyGlobalCopyByReference(globalConfiguration, oneWayFieldMap, classMap); MappingUtils.reverseFields(oneWayFieldMap, fieldMapPrime);
CustomConverter converter = MappingUtils.determineCustomConverter(fieldMap, converterByDestTypeCache, customConverterObjects, fieldMap.getClassMap().getCustomConverters(), srcFieldClass, destFieldType); boolean isSrcFieldClassSupportedMap = MappingUtils.isSupportedMap(srcFieldClass); boolean isDestFieldTypeSupportedMap = MappingUtils.isSupportedMap(destFieldType); if (isSrcFieldClassSupportedMap && isDestFieldTypeSupportedMap) { return mapMap(srcObj, (Map<?, ?>) srcFieldValue, fieldMap, destObj, context); if (MappingUtils.isSupportedCollection(srcFieldClass) && (MappingUtils.isSupportedCollection(destFieldType))) { return mapCollection(srcObj, srcFieldValue, fieldMap, destObj, context); if (MappingUtils.isSupportedCollection(destFieldType) && (!MappingUtils.isBlankOrNull(fieldMap.getCollectionItemDiscriminator()) || !MappingUtils.isBlankOrNull(fieldMap.getCollectionItemDiscriminatorId()))) { Object arrayValue = new Object[]{srcFieldValue}; return mapCollection(srcObj, arrayValue, fieldMap, destObj, context); if (MappingUtils.isEnumType(srcFieldClass, destFieldType)) { return mapEnum((Enum) srcFieldValue, (Class<Enum>) destFieldType);
srcObj = MappingUtils.deProxy(srcObj); Class<?> converterClass = MappingUtils.findCustomConverter(converterByDestTypeCache, classMap.getCustomConverters(), srcObj .getClass(), destType); MappingUtils.throwMappingException(e);
private void map(ClassMap classMap, Object srcObj, Object destObj, boolean bypassSuperMappings, List<String> mappedParentFields, String mapId) { srcObj = MappingUtils.deProxy(srcObj); Class<?> converterClass = MappingUtils.findCustomConverter(converterByDestTypeCache, classMap.getCustomConverters(), srcClass, destClass); if (converterClass != null) { String key = MappingUtils.getMappedParentFieldKey(destObj, fieldMapping); if (mappedParentFields != null && mappedParentFields.contains(key)) { continue;
public Class<?> getHint(Class<?> clazz, List<Class<?>> clazzHints) { List<Class<?>> hints = getHints(); int hintsSize = hints.size(); if (hintsSize == 1) { return getHint(); } // validate sizes if (clazzHints.size() != hintsSize) { MappingUtils .throwMappingException("When using multiple source and destination hints there must be exactly the same number of hints on the source and the destination."); } int count = 0; String myClazName = MappingUtils.getRealClass(clazz).getName(); int size = clazzHints.size(); for (int i = 0; i < size; i++) { Class<?> element = clazzHints.get(i); if (element.getName().equals(myClazName)) { return hints.get(count); } count++; } return clazz; }
.throwMappingException("<field type=\"iterate\"> must have a source or destination type hint"); CustomConverter converter = MappingUtils.findCustomConverter(converterByDestTypeCache, customConverterObjects, fieldMapping.getClassMap().getCustomConverters(), value.getClass(), fieldMapping.getDestHintContainer().getHint());
private List<String> getFieldMapKeys(Object destObj, List<FieldMap> fieldMaps, String mapId) { List<String> keys = new ArrayList<String>(); for (FieldMap fieldMap : fieldMaps) { // Check that field map is applicable for current mapping using // mapId value. If mapId parameter is provided by user and field map // has different mapId value we should skip this field map. if (MappingUtils.isBlankOrNull(fieldMap.getMapId()) || fieldMap.getMapId().equals(mapId)) { String key = MappingUtils.getMappedParentFieldKey(destObj, fieldMap); if (!keys.contains(key)) { keys.add(key); } } } return keys; }
public String createKey(Class<?> srcClass, Class<?> destClass, String mapId) { Class<?> srcRealClass = MappingUtils.getRealClass(srcClass); Class<?> destRealClass = MappingUtils.getRealClass(destClass); StringBuilder result = new StringBuilder(140); result.append("SRC-CLASS->"); result.append(srcRealClass.getName()); result.append(" DST-CLASS->"); result.append(destRealClass.getName()); if (StringUtils.isNotEmpty(mapId)) { result.append(" MAP-ID->"); result.append(mapId); } return result.toString(); }
private void processSuperTypeMapping(Collection<ClassMap> superClasses, Object srcObj, Object destObj, List<String> mappedParentFields, String mapId) { for (ClassMap map : superClasses) { map(map, srcObj, destObj, true, mappedParentFields ,mapId); for (FieldMap fieldMapping : map.getFieldMaps()) { String key = MappingUtils.getMappedParentFieldKey(destObj, fieldMapping); mappedParentFields.add(key); } } }
public static void addGenericMapping(ClassMap classMap, Configuration configuration, String srcName, String destName) { FieldMap fieldMap = new GenericFieldMap(classMap); fieldMap.setSrcField(new DozerField(srcName, null)); fieldMap.setDestField(new DozerField(destName, null)); // add CopyByReferences per defect #1728159 MappingUtils.applyGlobalCopyByReference(configuration, fieldMap, classMap); classMap.addFieldMapping(fieldMap); } }
public static Class<?> determineCustomConverter(FieldMap fieldMap, Cache converterByDestTypeCache, CustomConverterContainer customConverterContainer, Class<?> srcClass, Class<?> destClass) { if (customConverterContainer == null) { return null; } // This method is messy. Just trying to isolate the junk into this one method instead of spread across the mapping // processor until a better solution can be put into place // For indexed mapping, need to use the actual class at index to determine the custom converter. if (fieldMap != null && fieldMap.isDestFieldIndexed()) { if (destClass.isArray()) { destClass = destClass.getComponentType(); } else if (destClass.isAssignableFrom(Collection.class) && fieldMap.getDestHintContainer() != null && !fieldMap.getDestHintContainer().hasMoreThanOneHint()) { // use hint when trying to find a custom converter destClass = fieldMap.getDestHintContainer().getHint(); } } return findCustomConverter(converterByDestTypeCache, customConverterContainer, srcClass, destClass); }
private Collection<ClassMap> checkForSuperTypeMapping(Class<?> srcClass, Class<?> destClass) { // Check cache first Object cacheKey = CacheKeyFactory.createKey(destClass, srcClass); Collection<ClassMap> cachedResult = (Collection<ClassMap>) superTypeCache.get(cacheKey); if (cachedResult != null) { return cachedResult; } // If no existing cache entry is found, determine super type mappings. // Recursively walk the inheritance hierarchy. List<ClassMap> superClasses = new ArrayList<ClassMap>(); // Need to call getRealSuperclass because proxied data objects will not return correct // superclass when using basic reflection List<Class<?>> superSrcClasses = MappingUtils.getSuperClassesAndInterfaces(srcClass); List<Class<?>> superDestClasses = MappingUtils.getSuperClassesAndInterfaces(destClass); // add the actual classes to check for mappings between the original and the opposite // super classes superSrcClasses.add(0, srcClass); superDestClasses.add(0, destClass); for (Class<?> superSrcClass : superSrcClasses) { for (Class<?> superDestClass : superDestClasses) { if (!(superSrcClass.equals(srcClass) && superDestClass.equals(destClass))) { checkForClassMapping(superSrcClass, superClasses, superDestClass); } } } Collections.reverse(superClasses); // Done so base classes are processed first superTypeCache.put(cacheKey, superClasses); return superClasses; }
private Object mapCustomObject(FieldMap fieldMap, Object destObj, Class<?> destFieldType, Object srcFieldValue) { srcFieldValue = MappingUtils.deProxy(srcFieldValue);
if (!MappingUtils.isBlankOrNull(classMap.getMapId())) { if (mapIds.contains(classMap.getMapId())) { throw new IllegalArgumentException("Duplicate Map Id's Found. Map Id: " + classMap.getMapId()); MappingUtils.reverseFields(classMap, classMapPrime); if ((isSupportedMap(classMap.getDestClassToMap()) && !isSupportedMap( classMap.getSrcClassToMap())) || (isSupportedMap( classMap.getSrcClassToMap()) && !isSupportedMap( classMap.getDestClassToMap())) || (isSupportedMap( fieldMap.getDestFieldType(classMap.getDestClassToMap())) && !isSupportedMap( fieldMap.getSrcFieldType(classMap.getSrcClassToMap()))) || (isSupportedMap( fieldMap .getSrcFieldType(classMap.getSrcClassToMap()))) && !isSupportedMap( fieldMap.getDestFieldType(classMap.getDestClassToMap()))) { FieldMap fm = new MapFieldMap(fieldMap); MappingUtils.reverseFields(fieldMap, fieldMapPrime); MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMap, classMap); MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMapPrime, classMapPrime); MappingUtils.reverseFields(fieldMap, fieldMapPrime); .throwMappingException("custom converter should be provided for n to 1 field mapping");
CustomConverter converter = MappingUtils.determineCustomConverter(fieldMap, converterByDestTypeCache, customConverterObjects, boolean isSrcFieldClassSupportedMap = MappingUtils.isSupportedMap(srcFieldClass); boolean isDestFieldTypeSupportedMap = MappingUtils.isSupportedMap(destFieldType); if (isSrcFieldClassSupportedMap && isDestFieldTypeSupportedMap) { return mapMap(srcObj, (Map<?, ?>) srcFieldValue, fieldMap, destObj, context); if (MappingUtils.isSupportedCollection(srcFieldClass) && (MappingUtils.isSupportedCollection(destFieldType))) { return mapCollection(srcObj, srcFieldValue, fieldMap, destObj, context); if (MappingUtils.isSupportedCollection( destFieldType) && (!MappingUtils.isBlankOrNull(fieldMap.getCollectionItemDiscriminator()) || !MappingUtils .isBlankOrNull(fieldMap.getCollectionItemDiscriminatorId()))) { Object arrayValue = new Object[] { srcFieldValue }; return mapCollection(srcObj, arrayValue, fieldMap, destObj, context); if (MappingUtils.isEnumType(srcFieldClass, destFieldType)) { return mapEnum((Enum) srcFieldValue, (Class<Enum>) destFieldType);