attributemap.java

来自「根据Scott W. Ambler在1998年写的关于ORM Persisten」· Java 代码 · 共 135 行

JAVA
135
字号
package pl.map;

import java.beans.*;
import java.lang.reflect.*;
import pl.*;


/**
 * 属性映射类
 * 对类属性与表字段进行映射
 */
public class AttributeMap {
  private final static Object[] emptyParameters = new Object[0];
  private java.lang.String name = null;
  private ColumnMap columnMap = null;
  private java.lang.reflect.Method getter = null;
  private java.lang.reflect.Method setter = null;
  private AttributeMap reference = null;

  /**
   * 创建AttributeMap
   */
  public AttributeMap(String name) {
    super();

    this.name = name;
  }

  /**
   * 返回属性对应的ColumnMap对象
   * 如果这个属性没有映射到任何Column则返回null
   * @return
   */
  public ColumnMap getColumnMap() {
    return columnMap;
  }

  /**
   * 返回属性名
   *
   * @return java.lang.String
   */
  public java.lang.String getName() {
    return name;
  }

  /**
   * Return the AttributeMap for the referenced attribute of this attribute.
   *
   * @return pl.map.AttributeMap
   */
  public AttributeMap getReference() {
    return reference;
  }

  /**
   * Inits the getter and setter methods for this attribute.
   *
   * @param mapObjectClass the Class of the attribute's object
   */
  void initAccessors(Class mapObjectClass) throws PlException {
    try {
      PropertyDescriptor descriptor = null;
      BeanInfo info = java.beans.Introspector.getBeanInfo(mapObjectClass);
      for (int i = 0; i < info.getPropertyDescriptors().length; i++) {
        descriptor = info.getPropertyDescriptors()[i];
        if (descriptor.getName().equals(getName())) {
          getter = descriptor.getReadMethod();
          setter = descriptor.getWriteMethod();
          break;
        }
      }

      if (getter == null)
        throw new PlException("No getter method for attribute " + getName() +
                              " in class " + mapObjectClass.getName());

      if (setter == null)
        throw new PlException("No setter method for attribute " + getName() +
                              " in class " + mapObjectClass.getName());

      // Init type of the attribute
      if (getColumnMap() != null) {
        getColumnMap().setPlType(pl.sql.PlTypes.getPlType(descriptor.
            getPropertyType()));
      }
    }
    catch (IntrospectionException e) {
      throw PlException.toPlException(e);
    }
  }

  /**
   * 设置属性对于的ColumnMap对象
   *
   */
  public void setColumnMap(ColumnMap columnMap) {
    this.columnMap = columnMap;
  }

  /**
   * Sets the referenced AttributeMap for this attribute.
   *
   * @param newReference new reference attribute for this attribute
   */
  public void setReference(AttributeMap reference) {
    this.reference = reference;
  }

  public Object getValue(Object target) throws PlException {
    try {
      return getter.invoke(target, emptyParameters);
    }
    catch (IllegalAccessException e) {
      throw new PlException(e);
    }
    catch (InvocationTargetException e) {
      throw new PlException(e);
    }
  }

  public void setValue(Object target, Object value) throws PlException {
    try {
      Object[] values = new Object[] {
          value};
      setter.invoke(target, values);
    }
    catch (IllegalAccessException e) {
      throw new PlException(e);
    }
    catch (InvocationTargetException e) {
      throw new PlException(e);
    }
  }
}

⌨️ 快捷键说明

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