⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mappingprocessor.java

📁 一个javabean的转换与copy非常的好用希望大家好好研究一下
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        result = new ArrayList(sourceCollectionValue.size());
      }
    }
    Object destValue = null;
    Iterator iter = sourceCollectionValue.iterator();
    Object sourceValue = null;
    Class prevDestEntryType = null;
    while (iter.hasNext()) {
      sourceValue = iter.next();
      if (destEntryType == null
          || (fieldMap.getDestinationTypeHint() != null && fieldMap.getDestinationTypeHint().hasMoreThanOneHint())) {
        if (sourceValue == null) {
          destEntryType = prevDestEntryType;
        } else {
          destEntryType = fieldMap.getDestHintType(sourceValue.getClass());
        }
      }
      destValue = mapOrRecurseObject(sourceValue, destEntryType, classMap, fieldMap, destObj);
      prevDestEntryType = destEntryType;
      if (fieldMap.isGenericFieldMap()) {
        GenericFieldMap gfm = (GenericFieldMap) fieldMap;
        if (gfm.getRelationshipType() == null
            || gfm.getRelationshipType().equals(MapperConstants.RELATIONSHIP_CUMULATIVE)) {
          result.add(destValue);
        } else {
          if (result.contains(destValue)) {
            int index = result.indexOf(destValue);
            // perform an update if complex type - can't map strings
            Object obj = result.get(index);
            // make sure it is not a String
            if (!obj.getClass().isAssignableFrom(String.class)) {
              map(null, obj, destValue, null, fieldMap);
            }
            result.set(index, destValue);
          } else {
            result.add(destValue);
          }
        }
      } else {
        result.add(destValue);
      }
    }
    return result;
  }

  private List addOrUpdateToList(FieldMap fieldMap, Collection sourceCollectionValue, ClassMap classMap, Object destObj)
      throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
      ClassNotFoundException, NoSuchFieldException {
    return addOrUpdateToList(fieldMap, sourceCollectionValue, classMap, destObj, null);
  }

  private Set mapSetToSet(Set sourceCollectionValue, ClassMap classMap, FieldMap fieldMap, Object destObj,
      Class destClass) throws IllegalAccessException, InstantiationException, InvocationTargetException,
      NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {

    Object field = fieldMap.doesFieldExist(destObj, destClass);
    Set result = null;
    // Sets can not contain the same equals() object - by definition as you
    // add you might be updating at the same time
    if (field == null) {
      Class destFieldType = fieldMap.getDestFieldType(destObj.getClass());
      result = collectionUtils.createNewSet(destFieldType);
    } else {
      result = (Set) field;
    }
    Object destValue = null;
    Iterator iter = sourceCollectionValue.iterator();
    while (iter.hasNext()) {
      Object obj = iter.next();
      Class destEntryType = fieldMap.getDestHintType(obj.getClass());
      destValue = mapOrRecurseObject(obj, destEntryType, classMap, fieldMap, destObj);
      result.add(destValue);
    }
    return result;
  }

  private Object mapSetToArray(Collection sourceCollectionValue, ClassMap classMap, FieldMap fieldMap, Object destObj)
      throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
      ClassNotFoundException, NoSuchFieldException {
    return mapListToArray(sourceCollectionValue, classMap, fieldMap, destObj);
  }

  private List mapArrayToList(Object sourceCollectionValue, ClassMap classMap, FieldMap fieldMap, Object destObj)
      throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
      ClassNotFoundException, NoSuchFieldException {
    Class destEntryType = null;
    if (fieldMap.getDestinationTypeHint() != null) {
      destEntryType = fieldMap.getDestinationTypeHint().getHint();
    } else {
      destEntryType = sourceCollectionValue.getClass().getComponentType();
    }
    return addOrUpdateToList(fieldMap, Arrays.asList((Object[]) sourceCollectionValue), classMap, destObj,
        destEntryType);
  }

  private void writeDestinationValue(Object destObj, Object destFieldValue, ClassMap classMap, FieldMap fieldMapping)
      throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException,
      ClassNotFoundException, NoSuchFieldException {
    boolean bypass = false;
    // don't map null to dest field if map-null="false"
    if (destFieldValue == null && !classMap.getDestClass().getMapNull().booleanValue()) {
      bypass = true;
    }

    // don't map "" to dest field if map-empty-string="false"
    if (destFieldValue != null && destFieldValue.getClass().equals(String.class) &&
        StringUtils.isEmpty((String) destFieldValue) && !classMap.getDestClass().getMapEmptyString().booleanValue()) {
        bypass = true;
    }

    if (!bypass) {
      eventMgr.fireEvent(new DozerEvent(MapperConstants.MAPPING_PRE_WRITING_DEST_VALUE, classMap, fieldMapping, null, destObj, destFieldValue));
      
      fieldMapping.writeDestinationValue(destObj, destFieldValue, classMap);
      
      eventMgr.fireEvent(new DozerEvent(MapperConstants.MAPPING_POST_WRITING_DEST_VALUE, classMap, fieldMapping, null, destObj, destFieldValue));
    }
  }

  private Object mapUsingCustomConverter(Class customConverterClass, Class srcFieldClass, Object srcFieldValue,
      Class destFieldClass, Object destFieldValue, FieldMap fieldMap, boolean topLevel) throws IllegalAccessException,
      InvocationTargetException, InstantiationException, NoSuchMethodException, ClassNotFoundException,
      NoSuchFieldException {
    Object converterInstance = null;
    // search our injected customconverters for a match
    if (customConverterObjects != null) {
      Iterator iter = customConverterObjects.iterator();
      while (iter.hasNext()) {
        Object customConverter = (Object) iter.next();
        if (customConverter.getClass().isAssignableFrom(customConverterClass)) {
          // we have a match
          converterInstance = customConverter;
        }
      }
    }
    //if converter object instances were not injected, then create new instance of the 
    //converter for each conversion
    if (converterInstance == null) {
      converterInstance = customConverterClass.newInstance();
    }
    if (!(converterInstance instanceof CustomConverter)) {
      throw new MappingException("Custom Converter does not implement CustomConverter interface");
    }
    CustomConverter theConverter = (CustomConverter) converterInstance;
    // if this is a top level mapping the destObj is the highest level mapping...not a recursive mapping
    if (topLevel) {
      return theConverter.convert(destFieldValue, srcFieldValue, destFieldClass, srcFieldClass);
    }
    Object field = mappingValidator.validateField(fieldMap, destFieldValue, destFieldClass);
    return theConverter.convert(field, srcFieldValue, destFieldClass, srcFieldClass);
  }

  private List checkForSuperTypeMapping(Class sourceClass, Class destClass) {
    // Check cache first
    Cache cache = cacheMgr.getCache(MapperConstants.SUPER_TYPE_CHECK_CACHE);
    Object cacheKey = CacheKeyFactory.createKey(new Object[] { destClass, sourceClass });
    CacheEntry cacheEntry = cache.get(cacheKey);
    if (cacheEntry != null) {
      return (List) cacheEntry.getValue();
    }

    // If no existing cache entry is found, determine super type mapping and store in cache
    Class superSourceClass = sourceClass.getSuperclass();
    Class superDestClass = destClass.getSuperclass();
    List superClasses = new ArrayList();
    boolean stillHasSuperClasses = true;
    while (stillHasSuperClasses) {
      if ((superSourceClass != null && !superSourceClass.getName().equals("java.lang.Object"))
          || (superDestClass != null && !superDestClass.getName().equals("java.lang.Object"))) {
        // see if the source super class is mapped to the dest class
        ClassMap superClassMap = (ClassMap) customMappings.get(ClassMapKeyFactory.createKey(superSourceClass,
            destClass));
        if (superClassMap != null) {
          superClasses.add(superClassMap);
        }
        // now walk up the dest classes super classes with our super
        // source class and our source class
        superDestClass = destClass.getSuperclass();
        boolean stillHasDestSuperClasses = true;
        while (stillHasDestSuperClasses) {
          if (superDestClass != null && !superDestClass.getName().equals("java.lang.Object")) {
            ClassMap superDestClassMap = (ClassMap) customMappings.get(ClassMapKeyFactory.createKey(
                superSourceClass, superDestClass));
            if (superDestClassMap != null) {
              superClasses.add(superDestClassMap);
            }
            ClassMap sourceClassMap = (ClassMap) customMappings.get(ClassMapKeyFactory.createKey(sourceClass,
                superDestClass));
            if (sourceClassMap != null) {
              superClasses.add(sourceClassMap);
            }
            superDestClass = superDestClass.getSuperclass();
          } else {
            break;
          }
        }
        superSourceClass = superSourceClass.getSuperclass();
      } else {
        break;
      }
    }// while

    // multiple levels of custom mapping processed in wrong order - need to reverse
    Collections.reverse(superClasses);
    // Add to cache
    cacheEntry = new CacheEntry(cacheKey, (ArrayList) superClasses);
    cache.put(cacheEntry);

    return superClasses;
  }

  private List processSuperTypeMapping(List superClasses, Object sourceObj, Object destObj, Class sourceClass,
      FieldMap parentFieldMap) throws NoSuchMethodException, NoSuchFieldException, ClassNotFoundException,
      IllegalAccessException, InvocationTargetException, InstantiationException {
    List fieldNamesList = new ArrayList();
    Iterator iter = superClasses.iterator();
    while (iter.hasNext()) {
      ClassMap map = (ClassMap) iter.next();
      map(map, sourceObj, destObj, map, parentFieldMap);
      List fieldMaps = map.getFieldMaps();
      Class destClassToMap = map.getDestClass().getClassToMap();
      Class srcClassToMap = map.getSourceClass().getClassToMap();
      for (int i = 0; i < fieldMaps.size(); i++) {
        FieldMap fieldMapping = (FieldMap) fieldMaps.get(i);
        if (!fieldMapping.isGenericFieldMap() && !(fieldMapping instanceof ExcludeFieldMap)) {
          // do nothing
        } else {
          String methodName = fieldMapping.getDestFieldReadMethodName(destClassToMap);
          String sourceMethodName = fieldMapping.getSourceFieldReadMethodName(srcClassToMap);
          String parentSourceField = null;
          if (parentFieldMap != null) {
            parentSourceField = parentFieldMap.getSourceField().getName();
          }
          String key = mappingUtils.getParentFieldNameKey(parentSourceField, sourceObj, sourceClass.getName(), 
              sourceMethodName, methodName, fieldMapping.getSourceField().getName(), fieldMapping.getDestField().getName());
          if (fieldNamesList.contains(key)) {
            continue;
          } else {
            fieldNamesList.add(key);
          }
        }
      }
    }
    return fieldNamesList;
  }

  private ClassMap getClassMap(Object sourceObj, Class destClass, String mapId, boolean isInstance) {
    ClassMap mapping = classMapFinder.findClassMap(this.customMappings, sourceObj, destClass, mapId, isInstance);

    //If mapping not found in exsting custom mapping collection, create default as an explicit mapping must not exist.
    //The create default class map method will also add all default mappings that it can determine.
    if (mapping == null) {
      mapping = classMapBuilder.createDefaultClassMap(globalConfiguration, sourceObj.getClass(), destClass);
      customMappings.put(ClassMapKeyFactory.createKey(sourceObj.getClass(), destClass), mapping);
    }

    return mapping;
  }
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -