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

📄 fieldmap.java

📁 一个javabean的转换与copy非常的好用希望大家好好研究一下
💻 JAVA
字号:
/*
 * 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.fieldmap;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import net.sf.dozer.util.mapping.propertydescriptor.DozerPropertyDescriptorIF;
import net.sf.dozer.util.mapping.propertydescriptor.PropertyDescriptorFactory;
import net.sf.dozer.util.mapping.util.MapperConstants;
import net.sf.dozer.util.mapping.util.MappingUtils;

import org.apache.log4j.Logger;

/**
 * @author garsombke.franz
 * @author sullins.ben
 * @author tierney.matt
 * 
 */
public abstract class FieldMap implements Cloneable {
  private static final Logger log = Logger.getLogger(FieldMap.class);

  private Field sourceField;

  private Field destField;

  private Hint sourceTypeHint;

  private Hint destinationTypeHint;

  private String type;

  private boolean copyByReference;

  private boolean copyByReferenceOveridden;

  private final Map sourcePropertyDescriptors = new HashMap();

  private DozerPropertyDescriptorIF destinationPropertyDescriptor;

  private String mapId;

  private final MappingUtils mappingUtils = new MappingUtils();
  
  private DozerPropertyDescriptorIF getSourcePropertyDescriptor(Class sourceClass) throws NoSuchFieldException {
    if (!sourcePropertyDescriptors.containsKey(sourceClass)) {
      sourcePropertyDescriptors.put(sourceClass, PropertyDescriptorFactory.getPropertyDescriptor(sourceField, sourceClass));
    }
    return (DozerPropertyDescriptorIF) sourcePropertyDescriptors.get(sourceClass);
  }
  
  private DozerPropertyDescriptorIF getDestinationPropertyDescriptor(Class destClass) throws NoSuchFieldException {
    if (destinationPropertyDescriptor == null) {
      destinationPropertyDescriptor = PropertyDescriptorFactory.getPropertyDescriptor(destField, destClass);
    }
    
    return destinationPropertyDescriptor;
  }

  public String getSourceFieldReadMethodName(Class sourceClass) throws NoSuchFieldException {
    return getSourcePropertyDescriptor(sourceClass).getReadMethodName();
  }

  public String getDestFieldReadMethodName(Class destClass) throws NoSuchFieldException {
    return getDestinationPropertyDescriptor(destClass).getReadMethodName();
  }

  public Class getDestHintType(Class sourceClass) {
    if (getDestinationTypeHint() != null) {
      if (getSourceTypeHint() != null) {
        return getDestinationTypeHint().getHint(sourceClass, getSourceTypeHint().getHints());
      } else {
        return getDestinationTypeHint().getHint();
      }
    } else {
      return sourceClass;
    }
  }

  public Class getDestFieldType(Class destClass) throws NoSuchMethodException, ClassNotFoundException,
      NoSuchFieldException {
    return getDestinationPropertyDescriptor(destClass).getPropertyType();
  }

  public Method getDestFieldWriteMethod(Class destClass) throws NoSuchMethodException, ClassNotFoundException,
      NoSuchFieldException {
    return getDestinationPropertyDescriptor(destClass).getWriteMethod();
  }

  public Object getSrcFieldValue(Class srcClass, Object srcObj) throws IllegalAccessException, InvocationTargetException,
      NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
    // this is mainly for Maps...we cant use the selfdescriptor...so we have to cheat and use this
    if (isSourceSelfReferencing()) {
      return srcObj;
    }
    return getSourcePropertyDescriptor(srcClass).getPropertyValue(srcObj);
  }

  public void writeDestinationValue(Object destObj, Object destFieldValue, ClassMap classMap)
      throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException,
      ClassNotFoundException, NoSuchFieldException {
    if (log.isDebugEnabled()) {
      log.debug("Getting ready to invoke write method on the destination object.  Dest Obj: "
          + mappingUtils.getClassNameWithoutPackage(destObj.getClass()) + ", Dest value: " + destFieldValue);
    }
    DozerPropertyDescriptorIF propDescriptor = getDestinationPropertyDescriptor(destObj.getClass()); 
    propDescriptor.setPropertyValue(destObj, destFieldValue, getDestinationTypeHint(), classMap);
  }

  public Object getDestinationObject(Class destClass, Object destObj) throws IllegalAccessException, InvocationTargetException,
      InstantiationException, NoSuchFieldException {
    return getDestinationPropertyDescriptor(destClass).getPropertyValue(destObj);
  }
  
  public Object doesFieldExist(Object destObj, Class destClass) throws IllegalAccessException,
      InvocationTargetException, InstantiationException, NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
    Object field = null;
    if (!isGenericFieldMap()) {
      //then do no validation
    } else {
      // call the getXX method to see if the field is already instantiated
      // for deep mapping need the 'real' destination class.
      if (destClass == null) {
        destClass = destObj.getClass();
      }
      field = getDestinationObject(destClass, destObj);
    }
    return field;
  }  
  
  public String getDestKey() {
    String key;
    if (getDestField().getKey() != null) {
      key = getDestField().getKey();
    } else {
      key = getSourceField().getName();
    }
    return key;
  }

  public String getSourceKey() {
    String key;
    if (getSourceField().getKey() != null) {
      key = getSourceField().getKey();
    } else {
      key = getDestField().getName();
    }
    return key;
  }
  
  public Hint getDestinationTypeHint() {
    return destinationTypeHint;
  }

  public void setDestinationTypeHint(Hint destHint) {
    this.destinationTypeHint = destHint;
  }

  public Hint getSourceTypeHint() {
    return sourceTypeHint;
  }

  public void setSourceTypeHint(Hint sourceHint) {
    this.sourceTypeHint = sourceHint;
  }

  public Field getDestField() {
    return destField;
  }

  public void setDestField(Field destField) {
    this.destField = destField;
  }

  public Field getSourceField() {
    return sourceField;
  }

  public void setSourceField(Field sourceField) {
    this.sourceField = sourceField;
  }

  public Object clone() throws CloneNotSupportedException {
    return super.clone();
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public boolean getCopyByReference() {
    return copyByReference;
  }

  public void setCopyByReference(boolean copyByReference) {
    this.copyByReference = copyByReference;
    this.copyByReferenceOveridden = true;
  }

  /**
   * Return true if is self referencing. Is considered self referencing where no other sources are specified, i.e., no
   * source properties or #CDATA in the xml def.
   */
  protected boolean isSourceSelfReferencing() {
    return sourceField.getName().equals(MapperConstants.SELF_KEYWORD);
  }

  public boolean getCopyByReferenceOveridden() {
    return copyByReferenceOveridden;
  }

  public void setCopyByReferenceOveridden(boolean copyByReferenceOveridden) {
    this.copyByReferenceOveridden = copyByReferenceOveridden;
  }

  public String getMapId() {
    return mapId;
  }

  public void setMapId(String mapId) {
    this.mapId = mapId;
  }
  
  public boolean isGenericFieldMap() {
    return this instanceof GenericFieldMap ? true : false;
  }

}

⌨️ 快捷键说明

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