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

📄 mappingprocessor.java

📁 一个javabean的转换与copy非常的好用希望大家好好研究一下
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * Copyright 2005-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.dozer.util.mapping;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.dozer.util.mapping.cache.Cache;
import net.sf.dozer.util.mapping.cache.CacheEntry;
import net.sf.dozer.util.mapping.cache.CacheKeyFactory;
import net.sf.dozer.util.mapping.cache.CacheManagerIF;
import net.sf.dozer.util.mapping.converters.CustomConverter;
import net.sf.dozer.util.mapping.converters.CustomConverterContainer;
import net.sf.dozer.util.mapping.event.DozerEvent;
import net.sf.dozer.util.mapping.event.DozerEventManager;
import net.sf.dozer.util.mapping.event.EventManagerIF;
import net.sf.dozer.util.mapping.fieldmap.ClassMap;
import net.sf.dozer.util.mapping.fieldmap.Configuration;
import net.sf.dozer.util.mapping.fieldmap.ExcludeFieldMap;
import net.sf.dozer.util.mapping.fieldmap.FieldMap;
import net.sf.dozer.util.mapping.fieldmap.GenericFieldMap;
import net.sf.dozer.util.mapping.fieldmap.MapFieldMap;
import net.sf.dozer.util.mapping.stats.StatisticTypeConstants;
import net.sf.dozer.util.mapping.stats.StatisticsManagerIF;
import net.sf.dozer.util.mapping.util.ClassMapBuilder;
import net.sf.dozer.util.mapping.util.ClassMapFinder;
import net.sf.dozer.util.mapping.util.ClassMapKeyFactory;
import net.sf.dozer.util.mapping.util.CollectionUtils;
import net.sf.dozer.util.mapping.util.DateFormatContainer;
import net.sf.dozer.util.mapping.util.DestBeanCreator;
import net.sf.dozer.util.mapping.util.LogMsgFactory;
import net.sf.dozer.util.mapping.util.MapperConstants;
import net.sf.dozer.util.mapping.util.MappingUtils;
import net.sf.dozer.util.mapping.util.MappingValidator;
import net.sf.dozer.util.mapping.util.ReflectionUtils;
import net.sf.dozer.util.mapping.converters.PrimitiveOrWrapperConverter;

import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.collections.set.ListOrderedSet;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * Dozer's Internal Mapping Engine
 * 
 * @author garsombke.franz
 * @author sullins.ben
 * @author tierney.matt
 * 
 */
public class MappingProcessor implements MapperIF {

  private static final Logger log = Logger.getLogger(MappingProcessor.class);

  /*
   * Not Accessible 
   */
  private List superListOfFieldNames = null;
  private final transient Map customMappings;
  private final Configuration globalConfiguration;
  private final List customConverterObjects;//actual converter object instances
  private final CacheManagerIF cacheMgr;
  private final StatisticsManagerIF statsMgr;
  private final EventManagerIF eventMgr;
  private final ClassMapFinder classMapFinder = new ClassMapFinder();
  private final MappingUtils mappingUtils = new MappingUtils();
  private final ReflectionUtils reflectionUtils = new ReflectionUtils();
  private final CollectionUtils collectionUtils = new CollectionUtils();
  private final MappingValidator mappingValidator = new MappingValidator();
  private final ClassMapBuilder classMapBuilder = new ClassMapBuilder();
  private final LogMsgFactory logMsgFactory = new LogMsgFactory();
  private final DestBeanCreator destBeanCreator = new DestBeanCreator(MappingUtils.storedFactories);//only temp use the static var.  the stored factories need to be relocated
  private final PrimitiveOrWrapperConverter primitiveOrWrapperConverter = new PrimitiveOrWrapperConverter();
  
  public MappingProcessor(Map mappings, Configuration globalConfiguration, CacheManagerIF cacheMgr,
      StatisticsManagerIF statsMgr, List customConverterObjects, List eventListeners) {
    this.customMappings = mappings;
    this.globalConfiguration = globalConfiguration;
    this.cacheMgr = cacheMgr;
    this.statsMgr = statsMgr;
    this.customConverterObjects = customConverterObjects;
    this.eventMgr = new DozerEventManager(eventListeners);
  }

  public Object map(Object sourceObj, Class destClass) {
    //map-id is optional, so just pass in null
    return map(sourceObj, destClass, (String) null);
  }

  public Object map(Object sourceObj, Class destClass, String mapId) {
    Object destObj = null;
    ClassMap classMap = null;
    try {
      mappingValidator.validateMappingRequest(sourceObj, destClass);
      classMap = getClassMap(sourceObj, destClass, mapId, false);
      
      //Check to see if custom converter has been specified for this mapping combination.  If so, just use it.
      CustomConverterContainer customConverterContainer = classMap.getConfiguration().getCustomConverters();
      Class converterClass = mappingUtils.getCustomConverterFromContainer(cacheMgr, 
          customConverterContainer, sourceObj.getClass(), destClass);
      if (converterClass != null) {
        eventMgr.fireEvent(new DozerEvent(MapperConstants.MAPPING_STARTED_EVENT, classMap, null, sourceObj, destObj, null));
        return mapUsingCustomConverter(converterClass, sourceObj.getClass(), sourceObj, destClass, destObj, null, true);
      }
      
      //Create destination object.  It will be populated in the call to map()
      destObj = destBeanCreator.create(sourceObj, classMap, destClass);
      
      //Fire event to any listeners
      eventMgr.fireEvent(new DozerEvent(MapperConstants.MAPPING_STARTED_EVENT, classMap, null, sourceObj, destObj, null));
      
      //Map src values to dest object
      map(classMap, sourceObj, destObj, null, null);
    } catch (Throwable e) {
      mappingUtils.throwMappingException(e);
    }
    eventMgr.fireEvent(new DozerEvent(MapperConstants.MAPPING_FINISHED_EVENT, classMap, null, sourceObj, destObj, null));
    return destObj;
  }

  public void map(Object sourceObj, Object destObj) {
    map(sourceObj, destObj, null);
  }

  public void map(Object sourceObj, Object destObj, String mapId) {
    ClassMap classMap = null;
    try {
      //validate request and find the appropriate class map for the source/dest obj combination
      mappingValidator.validateMappingRequest(sourceObj, destObj);
      classMap = getClassMap(sourceObj, destObj.getClass(), mapId, true);

      //Check to see if custom converter has been specified for this mapping combination.  If so, just use it.
      CustomConverterContainer customConverterContainer = classMap.getConfiguration().getCustomConverters();
      Class converterClass = mappingUtils.getCustomConverterFromContainer(cacheMgr, customConverterContainer, sourceObj.getClass(), destObj.getClass());
      eventMgr.fireEvent(new DozerEvent(MapperConstants.MAPPING_STARTED_EVENT, classMap, null, sourceObj, destObj, null));

      if (converterClass != null) {
        mapUsingCustomConverter(converterClass, sourceObj.getClass(), sourceObj, destObj.getClass(), destObj, null,
            true);
        return;
      }
      
      //Map src values to dest object
      map(classMap, sourceObj, destObj, null, null);
    } catch (Throwable e) {
      mappingUtils.throwMappingException(e);
    }
    eventMgr.fireEvent(new DozerEvent(MapperConstants.MAPPING_FINISHED_EVENT, classMap, null, sourceObj, destObj, null));
  }

  private void map(ClassMap classMap, Object sourceObj, Object destObj, ClassMap parentClassMap, FieldMap parentFieldMap)
      throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException,
      InvocationTargetException, InstantiationException {
    mappingValidator.validateMappingRequest(sourceObj, destObj);
    Class sourceClass = sourceObj.getClass();
    Class destClass = destObj.getClass();
    String mapId = null;

    // see if we need to pull a referenced mapId
    if (parentFieldMap != null) {
      mapId = parentFieldMap.getMapId();
    }

    //If class map hasnt already been determined, find the appropriate one for the src/dest object combination
    if (classMap == null) {
      classMap = getClassMap(sourceObj, destObj.getClass(), mapId, true);
    }

    //What is this and why here?
    if (parentClassMap != null) {
      if (superListOfFieldNames == null) {
        superListOfFieldNames = new ArrayList();
      }
    }

    //Check to see if custom converter has been specified for this mapping combination.  If so, just use it.
    CustomConverterContainer customConverterContainer = classMap.getConfiguration().getCustomConverters();
    Class converterClass = mappingUtils.getCustomConverterFromContainer(cacheMgr, customConverterContainer, sourceObj.getClass(), destClass);
    if (converterClass != null) {
      Object rvalue = mapUsingCustomConverter(converterClass, sourceObj.getClass(), sourceObj, destClass, destObj,
          null, true);
      if (rvalue != null) {
        destObj = rvalue;
      }
      return;
    }

    /*
     * Now check for super class mappings as a convenience -assuming for now that super class mappings are at the same
     * level
     */
    List parentFieldNames = null;
    if (parentClassMap == null) {
      // check for super classes
      List superClasses = checkForSuperTypeMapping(sourceClass, destClass);
      // check for interfaces
      superClasses.addAll(classMapFinder.findInterfaceMappings(this.customMappings, sourceClass, destClass));
      if (superClasses != null && superClasses.size() > 0) {
        parentFieldNames = processSuperTypeMapping(superClasses, sourceObj, destObj, sourceClass, parentFieldMap);
      }
    }

    // Perform mappings for each field. Iterate through Fields Maps for this class mapping
    List fieldMaps = classMap.getFieldMaps();
    for (int i = 0; i < fieldMaps.size(); i++) {
      FieldMap fieldMapping = (FieldMap) fieldMaps.get(i);
      mapField(fieldMapping, classMap, sourceObj, destObj, parentClassMap, parentFieldMap, parentFieldNames);
    }
  }

  private void mapField(FieldMap fieldMapping, ClassMap classMap, Object sourceObj, Object destObj,
      ClassMap parentClassMap, FieldMap parentFieldMap, List parentFieldNames) {

    //The field has been explicitly excluded from mapping.  So just return, as no further processing is needed for this field 
    if (fieldMapping instanceof ExcludeFieldMap) {
      return;
    }

    Class sourceClass = sourceObj.getClass();
    Class destClass = destObj.getClass();
    Object sourceFieldValue = null;
    try {
      sourceFieldValue = fieldMapping.getSrcFieldValue(sourceClass, sourceObj);
      // check for super class names
      String methodName = fieldMapping.getDestFieldReadMethodName(destClass);
      String sourceMethodName = fieldMapping.getSourceFieldReadMethodName(sourceClass);
      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 (parentClassMap != null) {
        if (superListOfFieldNames.contains(key)) {
          return;
        } else {
          superListOfFieldNames.add(key);
        }
      }
      // check for parent field names
      if (parentFieldNames != null && parentFieldNames.size() > 0) {

⌨️ 快捷键说明

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