1.
2.自定义一个异常类DefaultException
3.
package com.jdnis.constant;
public class Datatpye {
public final static String STRING="java.lang.String";
public final static String INTEGER="java.lang.Integer";
public final static String DOUBLE="java.lang.Double";
public final static String FLOAT="java.lang.Float";
public final static String LONG="java.lang.Long";
public final static String BIGDECIMAL = "java.math.BigDecimal";
public final static String DATE = "java.util.Date";
public final static String TIMESTMAP = "java.sql.Timestamp";
public final static String BYTE = "java.lang.Byte";
public final static String BOOLEAN = "jjava.lang.Boolean";
public final static String INT="int";
public final static String FLOAT_l="float";
public final static String DOUBLE_l="double";
public final static String LONG_l="long";
public final static String BYTE_l="byte";
public final static String BOOLEAN_l="boolean";
}
2.自定义一个异常类DefaultException
3.
package com.jdnis.utils;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import com.jdnis.constant.Datatpye;
import com.jdnis.exception.DefaultException;
import com.jdnis.po.PersonPO;
import com.jdnis.vo.Person;
public class BeanConverter {
private static HashMap hmSetMethodsBeans = new HashMap();
private static HashMap hmGetMethodsBeans = new HashMap();
public static final String VOTOPO="voToPo";
public static final String POTOVO="poToVo";
/**
* @param instance
* @param cls
* @return Object
* @throws ClassCastException
*/
public static Object copyVoToPo(BaseVO instance) throws ClassCastException {
Object obj = null;
Class cls = null;
try {
cls = BeanUtils.getPoByVoUseName(instance).getClass();
} catch (DefaultException e1) {
e1.printStackTrace();
}
HashMap hmGetMethods = getGetMethods(instance.getClass());
HashMap hmSetMethods = getSetMethods(cls);
Set keySet = null;
keySet = hmSetMethods.keySet(); //得到的是一组键(key,value)的集合
Iterator it = keySet.iterator();
while (it.hasNext()){
String _propertyName = (String)it.next();//得到key()
Method getMethod = (Method)hmGetMethods.get(_propertyName);//得到key对应的get方法名
Method setMethod = (Method)hmSetMethods.get(_propertyName); //得到key对应的set方法名
if (setMethod != null && getMethod != null) {
try {
if (obj == null)
obj = cls.newInstance();
//得到VO中方法的返回值
Object param = getMethod.invoke(instance, null);
//求得(PO)setter的参数---数据类型
String paramTypeName = setMethod.getParameterTypes()[0].getName();
setMethod.invoke(obj,new Object[]{changeType(param, paramTypeName, VOTOPO)});
} catch (Exception e){
e.printStackTrace();
throw new ClassCastException();
}
}
}
return obj;
}
/**
* @param instance
* @param cls
* @return Object
* @throws ClassCastException
*/
public static Object copyVoToPo(Object instance, Class cls) throws ClassCastException {
Object obj = null;
HashMap hmGetMethods = getGetMethods(instance.getClass());
HashMap hmSetMethods = getSetMethods(cls);
Set keySet = null;
keySet = hmSetMethods.keySet(); //得到的是一组键(key,value)的集合
Iterator it = keySet.iterator();
while (it.hasNext()){
String _propertyName = (String)it.next();//得到key()
Method getMethod = (Method)hmGetMethods.get(_propertyName);//得到key对应的get方法名
Method setMethod = (Method)hmSetMethods.get(_propertyName); //得到key对应的set方法名
if (setMethod != null && getMethod != null) {
try {
if (obj == null)
obj = cls.newInstance();
//得到VO中方法的返回值
Object param = getMethod.invoke(instance, null);
//求得(PO)setter的参数---数据类型
String paramTypeName = setMethod.getParameterTypes()[0].getName();
setMethod.invoke(obj,new Object[]{changeType(param, paramTypeName, VOTOPO)});
} catch (Exception e){
e.printStackTrace();
throw new ClassCastException();
}
}
}
return obj;
}
/**
* @param instance
* @param cls
* @return Object
* @throws ClassCastException
*/
public static Object copyPoToVo(BasePO instance) throws ClassCastException {
Object obj = null;
Class cls = null;
try {
cls = BeanUtils.getVoByPoUseName(instance).getClass();
} catch (DefaultException e1) {
e1.printStackTrace();
}
HashMap hmGetMethods = getGetMethods(instance.getClass());
HashMap hmSetMethods = getSetMethods(cls);
Set keySet = null;
keySet = hmSetMethods.keySet(); //得到的是一组键(key,value)的集合
Iterator it = keySet.iterator();
while (it.hasNext()){
String _propertyName = (String)it.next();//得到key()
Method getMethod = (Method)hmGetMethods.get(_propertyName);//得到key对应的get方法名
Method setMethod = (Method)hmSetMethods.get(_propertyName); //得到key对应的set方法名
if (setMethod != null && getMethod != null) {
try {
if (obj == null)
obj = cls.newInstance();
//得到PO中方法的返回值
Object param = getMethod.invoke(instance, null);
//求得setter的参数---数据类型
setMethod.invoke(obj,new String[]{(String)changeType(param, "java.lang.String", POTOVO)});
} catch (Exception e){
e.printStackTrace();
throw new ClassCastException();
}
}
}
return obj;
}
/**
* @param instance
* @param cls
* @return Object
* @throws ClassCastException
*/
public static Object copyPoToVo(Object instance, Class cls) throws ClassCastException {
Object obj = null;
HashMap hmGetMethods = getGetMethods(instance.getClass());
HashMap hmSetMethods = getSetMethods(cls);
Set keySet = null;
keySet = hmSetMethods.keySet(); //得到的是一组键(key,value)的集合
Iterator it = keySet.iterator();
while (it.hasNext()){
String _propertyName = (String)it.next();//得到key()
Method getMethod = (Method)hmGetMethods.get(_propertyName);//得到key对应的get方法名
Method setMethod = (Method)hmSetMethods.get(_propertyName); //得到key对应的set方法名
if (setMethod != null && getMethod != null) {
try {
if (obj == null)
obj = cls.newInstance();
//得到PO中方法的返回值
Object param = getMethod.invoke(instance, null);
//求得setter的参数---数据类型
setMethod.invoke(obj,new String[]{(String)changeType(param, "java.lang.String", POTOVO)});
} catch (Exception e){
e.printStackTrace();
throw new ClassCastException();
}
}
}
return obj;
}
/**
* 将srcObject对象转换成为type类型的数据
* @param srcObject
* @param type
* @return Object
* @throws ClassCastException
*/
public static Object changeType(Object srcObject, String type, String str) throws DefaultException{
if(POTOVO.equals(str)){
return BeanUtils.changeToType(srcObject, "java.lang.String");
}else if(VOTOPO.equals(str)){
return BeanUtils.changeToType(srcObject, type);
}
return null;
}
/**
* 解析Javabean,取得这个bean class的属性和Set方法
*/
public static HashMap getSetMethods(Class cls) throws ClassCastException{
return getSetMethods(cls,Object.class);
}
public static HashMap getSetMethods(Class cls, Class stopClass) throws ClassCastException {
String clsName = cls.getName();
HashMap hmBean = (HashMap)hmSetMethodsBeans.get(clsName);
if (hmBean != null)
return hmBean;
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(cls, stopClass);
} catch (IntrospectionException e) {
e.printStackTrace();
throw new ClassCastException();
}
PropertyDescriptor[] props;
HashMap hm = new HashMap();
props = beanInfo.getPropertyDescriptors();
for (int i = 0; i < props.length; i++) {
Method setMethod = props[i].getWriteMethod();
if (setMethod != null){
String field = props[i].getName().toLowerCase();
hm.put(field, setMethod);
}
}
hmSetMethodsBeans.put(clsName, hm);
return hm;
}
/**
* 解析Javabean,取得这个bean class的属性和Set方法
*/
public static HashMap getGetMethods(Class cls)throws ClassCastException{
return getGetMethods(cls,Object.class);
}
public static HashMap getGetMethods(Class cls, Class stopClass) throws ClassCastException {
String clsName = cls.getName();//得到类的名称
HashMap hmBean = (HashMap)hmGetMethodsBeans.get(clsName);//得到方法对象的集合
if (hmBean != null){
return hmBean;
}
BeanInfo drbeanInfo = null;//bean实例的相关信息
try {
drbeanInfo = Introspector.getBeanInfo(cls);
} catch (IntrospectionException e) {
e.printStackTrace();
throw new ClassCastException();
}
PropertyDescriptor[] props;//属性列表
HashMap hm = new HashMap();
props = drbeanInfo.getPropertyDescriptors();
for (int i = 0; i < props.length; i++) {
Method getMethod = props[i].getReadMethod();
if (getMethod != null); {
String field = props[i].getName().toLowerCase();
hm.put(field, getMethod);
}
}
hmGetMethodsBeans.put(clsName, hm);
return hm;
}
}
public static void main(String[] args){
Person person = new Person("123","456","789","42.02");
PersonPO po = new PersonPO();
po = (PersonPO)BeanConverter.copyVoToPo(person, po.getClass());
System.out.println(person.getName()+" " +person.getAge()+" "+person.getNum()+" "+person.getScore());
System.out.println(po.getName()+" " +po.getAge()+" "+po.getNum()+" "+po.getScore());
}
}