一个VO转换工具

贴在这里备查

package com.hp.mes.mat.pojo;

/**
* ---------------------------------------------------------------------------------
Confidential and Proprietary
Copyright 2008 By
SGAI & Hewlett-Packard Development Company, L.P.
All Rights Reserved

Project Name : SGAI MES
Class Name : IValueObject.java
Package : com.hp.mes.md.pojo
@version $Id$
@author Daniel
@since 2008-8-6
*/
public interface IValueObject {

/**
* 返回value object属性域名称数组
*
* @return
* @author Daniel
* @since 2008-8-6
*/
public String[] getAttributeName();

/**
* 返回配置文件中定义的属性名称数组,与getAttributeName方法返回域一一对应
*
* @return
* @author Daniel
* @since 2008-8-6
*/
public String[] getAttributeRef();

}



package com.hp.mes.mat.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import com.hp.common.util.StringUtil;
import com.hp.mes.mat.pojo.IValueObject;

/**
* ---------------------------------------------------------------------------------
Confidential and Proprietary
Copyright 2008 By
SGAI & Hewlett-Packard Development Company, L.P.
All Rights Reserved

Project Name : SGAI MES
Class Name : VOConvertUtil.java
Package : com.hp.mes.md.util
@version $Id$
@author Daniel
@since 2008-8-7
*/
public class VOConvertUtil {

private Class valueObjectClass = null;

private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

public VOConvertUtil() {

}

public VOConvertUtil(Class valueObjectClass) {
this.valueObjectClass = valueObjectClass;
}

/**
* 将符合properties格式的字串,转换为一个实现了IValueObject接口的对象
*
* @param text 必须符合properties格式
* @return IValueObject 对象
* @throws Exception 可能抛出的异常
* @author Daniel
* @since 2008-8-13
*/
public IValueObject parse2ValueOject(String text) throws Exception {
Properties prop = new Properties();
ByteArrayInputStream byteStream = new ByteArrayInputStream(text
.getBytes());
try {
prop.load(byteStream);
} catch (IOException e) {
e.printStackTrace();
}

IValueObject valueObject = (IValueObject) valueObjectClass
.newInstance();

// 获得IValueObject的所有属性名称
String[] attrNames = valueObject.getAttributeName();

String[] attrRefs = valueObject.getAttributeRef();

for (int i = 0; i < attrNames.length; i++) {
// 获得属性名
String attrName = attrNames[i];

// 获得对应xml文件中的键值
String refKey = attrRefs[i];
// 从properties中获得对应的值
String refValue = prop.getProperty(refKey);
// 为避免properties中汉字出现乱码,进行编码转换
refValue = new String(refValue.getBytes("ISO8859-1"), "GBK");

// 根据名称找属性类型
Field field = valueObjectClass.getDeclaredField(attrName);
Class fieldType = field.getType();
// 将String类型的值转换成相应类型的值
Object attrValue = castToAttributeValue(fieldType, refValue);

// 取值,调用set方法赋值
String methodName = getSetterMethod(attrName);
Method setterMethod = valueObjectClass.getDeclaredMethod(
methodName, fieldType);
setterMethod.invoke(valueObject, attrValue);
}

return valueObject;
}

/**
* 获得setter方法名称
*
* @param attrName
* @return
* @author Daniel
* @since 2008-8-7
*/
private String getSetterMethod(String attrName) {
String methodName = "set" + attrName.substring(0, 1).toUpperCase()
+ attrName.substring(1);
return methodName;
}

/**
* 获得getter方法名称
*
* @param attrName
* @return
* @author Daniel
* @since 2008-8-7
*/
private String getGetterMethod(String attrName) {
String methodName = "get" + attrName.substring(0, 1).toUpperCase()
+ attrName.substring(1);
return methodName;
}

private Object castToAttributeValue(Class fieldType, String refValue)
throws Exception {
Object value = null;
if (StringUtil.isBlank(refValue)) {
value = null;
} else if (fieldType == String.class) {
value = refValue;
} else if (fieldType == Integer.class) {
value = Integer.valueOf(refValue);
} else if (fieldType == Long.class) {
value = Long.valueOf(refValue);
} else if (fieldType == Double.class) {
value = Double.valueOf(refValue);
} else if (fieldType == BigDecimal.class) {
value = new BigDecimal(refValue);
} else if (fieldType == char.class) {
value = refValue.toCharArray()[0];
} else if (fieldType == short.class) {
value = Short.parseShort(refValue);
} else if (fieldType == int.class) {
value = Integer.parseInt(refValue);
} else if (fieldType == double.class) {
value = Double.valueOf(refValue);
} else if (fieldType == Date.class) {
value = dateFormatter.parse(refValue);
}
return value;
}

/**
* 将一个IValueObject的对象转换为对应的实体对象
*
* @param valueObject
* @return
* @author Daniel
* @throws IllegalAccessException
* @throws InstantiationException
* @since 2008-8-7
*/
public Object parse2Entity(IValueObject valueObject) throws Exception {

Object entity = valueObjectClass.newInstance();
// 获得IValueObject中的所有属性名
String[] attrNames = valueObject.getAttributeName();
for (int i = 0; i < attrNames.length; i++) {
String attrName = attrNames[i];
// 调用ValueObject的getter方法从ValueObject从取值
String getterName = getGetterMethod(attrName);
Method getterMethod = valueObject.getClass().getDeclaredMethod(
getterName);
Object attrValue = getterMethod.invoke(valueObject);

// 调用entity的set方法给Entity赋值
String setterName = getSetterMethod(attrName);
Method setterMethod = valueObjectClass.getDeclaredMethod(
setterName, attrValue.getClass());
setterMethod.invoke(entity, attrValue);
}
return entity;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值