贴在这里备查
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;
}
}
分享到:
相关推荐
简单的vopo转换功能及依赖jar
后台VO转换的基类nc.bs.pf.change.VOConversion 检查引用类nc.bs.trade.comchkref.CheckRef 业务异常nc.vo.pub.BusinessException类 第二节 开发问题集 UI工厂分为哪几大基本模块? 怎样配置eclipse参数? 单据开发...
“自动生成字段声明,对象转换属性设置”脚本工具 开发过程中,从pm给出接口文档,到我们定义接口数据模型,总有许多的重复无意义的体力劳动,比如把文档里字段和描述转换成程序里的数据模型定义。以及DTO,VO,...
转换为对象 xmlToObject.vo.Request(自定义) 里面还包含其他对象。 转成的对象 已JSON格式后台输出 {"body":{"class":"xmlToObject.vo.ReqBody","list":[{"class":"xmlToObject.vo.Foot","name":"红豆","rmb":"10...
对于interlaced是先将一副图像的奇偶场的数据单独提取,然后先放奇场数据,再放偶场数据,再其拼成一副图像,进行转换。 所以: interlaced数据的存储为。 如果是pacekd模式 例如YUYV格式。就是奇,偶场顺序放YUYVe....
"CRUD代码生成器(Java)" 是一个强大的工具,旨在帮助开发者根据数据库中的数据表自动生成基础CRUD代码。这包括实体类(Entity)、数据访问对象(DAO)、数据传输对象(DTO)、值对象(VO)、查询对象(QO)、转换器...
4、这个工具同时又是一个简洁易用的数据库客户端工具,它提倡开发人员将每个表和列都加上注释,实现表及注释的平面化管理,是数据库管理员和项目开发、管理人员的最佳选择。 对于一个新的J2EE工程,本...
链接数据库后生成对应的实体、vo,service,xml,基础sql文件
4、这个工具同时又是一个简洁易用的数据库客户端工具,它提倡开发人员将每个表和列都加上注释,实现表及注释的平面化管理,是数据库管理员和项目开发、管理人员的最佳选择。 对于一个新的J2EE工程,本工具的使用...
于是一怒之下,自己动手丰衣足食,于是就自己用Swing写了一个基于数据库的自动化生成工具,支持MySQL、Oracle、SQLServce、PostgreSQL,完美支持JPA注解,可以同时生成Entity和DTO等,可以自动去除表前缀,支持单个...
抽取查询对象,简化查询,自动生成QueryModel自动驼峰装换生成VO对象,提供特定注解,@ View @AssociationView @CollectionView @MapView可在开发中运行(增量运行可随时修改,增加,删除类属性后继续运行,此时将...
dozer是一种JavaBean的映射工具,类似于Apache的BeanUtils。它可以灵活地处理复杂类型之间的映射。不但可以进行简单的属性映射、复杂的类型映射、双向映射、递归映射等,并且可以通过XML配置文件进行灵活的配置。 ...
目前,该插件将读取您现有类的所有字段,并将它们复制到一个目标类,该类的名称与您当前类的名称相同,前缀为DTO 。 它将为您的所有字段生成 getter 和 setter。 注意:如果一个属性扩展了 Collection 类,它将为您...
创建一个新项目 voltar create MyAwesomeGame 启动开发服务器 voltar server :默认情况下需要支持ES6的浏览器,但重建速度非常快 voltar server -es5 :每次更改都可转换为ES5 ,重建速度较慢,但可以在任何...
该软件包还适用于将某些VO数据模型类转换为相应的AstroPY类型的适配器。 例如:meas:Point-> astropy:SkyCoord 例如:meas:Time-> astropy:Time 该软件包由Omar Laurino开发,并由我更新为当前数据模型内容。...
浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是于当前页同域名的路径,这能有效的阻止跨站攻击。因此:跨域问题 是针对ajax的一种限制。 解决跨域问题的方案 CORS 规范化的跨域请求解决方案...
Jakob Voß(GBV 集团总部),2020-()前言经过较长时间的研究,开发了一种先进的书目信息存储格式; PICA 格式。 - 看科斯特 (1979) PICA 格式已经使用了 40 多年,迄今为止还没有被关系数据库系统或知识图等更...
摘 要 随着微电子技术的应用,市场上使用的传统称重工具已经满足不了人们的要求。为了 改变传统称重工具在使用上存在的问题,在本设计中将智能化、自动化、人性化用在了 电子秤重的控制系统中。...vo