ClassUtil

来源:互联网 发布:mixly软件下载 编辑:程序博客网 时间:2024/06/01 23:27
package com.ics.common.util;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Set;


import com.ics.common.CommonConstants;
import com.ics.common.utils.DataTypeTrans;
import com.ics.common.utils.DateUtil;
import com.ics.common.utils.StringUtil;


/**
 * @title :源对象操作类
 * @description :对对象进行操作,完成对象之间的转换,以及字段到方法的转换等功能
 * @author: 
 * @data: 2011-02-24
 */
public class ClassUtil {


/**
* @description: 由一个值对象转换成另外一个值对象
* @param srcObj:将要被转换的源对象
* @param destObj:将要被转换成的目标对象
* @return:转换成功则返回true,否则抛出异常
* @throws Exception:抛出Exception异常
* @deprecated
*/
public static boolean obj2Obj(Object srcObj, Object destObj)
{
Class srcClassType = srcObj.getClass();
Class destClassType = destObj.getClass();
Field fields[] = destClassType.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getMethodName = "get" + firstLetter + fieldName.substring(1);
String setMethodName = "set" + firstLetter + fieldName.substring(1);
try{
Method getMethod = srcClassType.getMethod(getMethodName,
new Class[] {});
Method setMethod = destClassType.getMethod(setMethodName,
new Class[] { field.getType() });
Object value = getMethod.invoke(srcObj, new Object[] {});
setMethod.invoke(destObj, new Object[] { value });
}catch (SecurityException e) {
                //throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                //throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                //throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                //throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                //throw new RuntimeException(e);
            }
}
return true;
}

/**
* @description: 由全部是字符串的bo对象转换成无嵌入自定义引用对象的po
* 要求:属性名一致:仅能java.util.Date与String/BigDecimal与String之间转换
* @param bo: 原值对象
* @param po: 目标值对象
* @return: 转换成功则返回true,否则抛出异常
* @throws Exception:抛出Exception异常
*/
public static boolean bo2Po(Object bo, Object po) {
Class srcClass = bo.getClass();
Class destClass = po.getClass();
Field fields[] = destClass.getDeclaredFields();
Field destfield = null;
Field srcField = null;
String fieldName = null;
Method getMethod = null;
Method setMethod = null;
for (int i = 0; i < fields.length; i++) {
destfield = fields[i];
fieldName = destfield.getName();
try{
srcField=srcClass.getDeclaredField(fieldName);
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getMethodName = "get" + firstLetter + fieldName.substring(1);
String setMethodName = "set" + firstLetter + fieldName.substring(1);
getMethod = srcClass.getMethod(getMethodName,new Class[] {});
Object value = getMethod.invoke(bo, new Object[] {});
setMethod = destClass.getMethod(setMethodName,
new Class[] { destfield.getType() });
if (srcField.getType()==String.class &&  destfield.getType() == java.util.Date.class){
value= value==null ? null:value.toString().length() >10 ? DateUtil.getUtilDate(value.toString(), CommonConstants.YY_MM_DD):DateUtil.getUtilDate(value.toString(), CommonConstants.YY_MM_DD);
}else if(srcField.getType()==String.class &&  destfield.getType() == BigDecimal.class){
value= value==null || value.equals("") ? null :new BigDecimal(value.toString());
}
if (value!=null) setMethod.invoke(po, new Object[] { value });
   } catch(java.lang.NoSuchFieldException e) {
                //throw new RuntimeException(e);
} catch (SecurityException e) {
                //throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                //throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                //throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                //throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                //throw new RuntimeException(e);
            }
}
return true;
}
public static boolean bo2Po(Object bo, Object po,Set fields) {
Class srcClass = bo.getClass();
Class destClass = po.getClass();
Field destfield = null;
Field srcField = null;
String fieldName = null;
Method getMethod = null;
Method setMethod = null;
for (Object field :fields) {
fieldName = field.toString();
try{
srcField=srcClass.getDeclaredField(fieldName);
} catch(java.lang.NoSuchFieldException e) {
try{
srcField=srcClass.getDeclaredField(fieldName+"2");
} catch(java.lang.NoSuchFieldException e1) {
}
}
try{
destfield=destClass.getDeclaredField(fieldName);
} catch(java.lang.NoSuchFieldException e) {
try{
destfield=destClass.getDeclaredField(fieldName+"2");
} catch(java.lang.NoSuchFieldException e1) {
}
}
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getMethodName = "get" + firstLetter + fieldName.substring(1);
String setMethodName = "set" + firstLetter + fieldName.substring(1);
try{
getMethod = srcClass.getMethod(getMethodName,new Class[] {});
}catch (SecurityException e) {
            }catch(java.lang.NoSuchMethodException e){
            try{
            getMethod = srcClass.getMethod(getMethodName+"2",new Class[] {});
            }catch (SecurityException e1) {
                }catch(java.lang.NoSuchMethodException e1){
    }
}
Object value=null;
try {
value = getMethod.invoke(bo, new Object[] {});
} catch (IllegalArgumentException e1) {
//e1.printStackTrace();
} catch (IllegalAccessException e1) {
//e1.printStackTrace();
} catch (InvocationTargetException e1) {
//e1.printStackTrace();
}
try{
setMethod = destClass.getMethod(setMethodName,new Class[] { destfield.getType() });
}catch (SecurityException e) {
           }catch(java.lang.NoSuchMethodException e){
            try{
            setMethod = destClass.getMethod(setMethodName+"2",new Class[] { destfield.getType() });
            }catch (SecurityException e1) {
               }catch(java.lang.NoSuchMethodException e1){
    }
}

if (srcField.getType()==String.class &&  destfield.getType() == java.util.Date.class){
value= value==null ? null:value.toString().length() >10 ? DateUtil.getUtilDate(value.toString(), CommonConstants.YY_MM_DD):DateUtil.getUtilDate(value.toString(), CommonConstants.YY_MM_DD);
}else if(srcField.getType()==String.class &&  destfield.getType() == BigDecimal.class){
value= value==null || value.equals("") ? null :new BigDecimal(value.toString());
}
try {
setMethod.invoke(po, new Object[] { value });
} catch (IllegalArgumentException e) {
//e.printStackTrace();
} catch (IllegalAccessException e) {
//e.printStackTrace();
} catch (InvocationTargetException e) {
//e.printStackTrace();
}
}
return true;
}

/**
* @description: 由无嵌入自定义引用对象的po转换为全部是字符串的bo对象
* 要求:属性名一致:仅能java.util.Date与String/BigDecimal与String之间转换
* @param bo: 原值对象
* @param po: 目标值对象
* @return: 转换成功则返回true,否则抛出异常
* @throws Exception:抛出Exception异常
*/
public static boolean po2Bo(Object po, Object bo) {
Class srcClass = po.getClass();
Class destClass = bo.getClass();
Field fields[] = srcClass.getDeclaredFields();
Field destfield = null;
Field srcField = null;
String fieldName = null;
Method getMethod = null;
Method setMethod = null;
for (int i = 0; i < fields.length; i++) {
srcField = fields[i];
fieldName = srcField.getName();
try{
destfield=destClass.getDeclaredField(fieldName);
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getMethodName = "get" + firstLetter + fieldName.substring(1);
String setMethodName = "set" + firstLetter + fieldName.substring(1);
getMethod = srcClass.getMethod(getMethodName,new Class[] {});
Object value = getMethod.invoke(po, new Object[] {});
setMethod = destClass.getMethod(setMethodName,
new Class[] { destfield.getType() });
if (srcField.getType()==java.util.Date.class &&  destfield.getType() == String.class){
value= value==null ? null:DateUtil.getUtilDateString((java.util.Date)value, CommonConstants.YY_MM_DD);
}else if(srcField.getType()==BigDecimal.class &&  destfield.getType() == String.class){
value= value==null ? null :value.toString();
}
setMethod.invoke(bo, new Object[] { value });
} catch(java.lang.NoSuchFieldException e){
                //throw new RuntimeException(e);
} catch (SecurityException e) {
                //throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                //throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                //throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                //throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                //throw new RuntimeException(e);
            }
}
return true;
}


/**
* @description: 字段转换成方法名
* @param fieldCode:数据库表字段英文代码
* @param frontCode:get或set方法前辍
* @return:返回java命名规范的属性get或set方法
* @author: liuyy
* @data: 2011-02-24
*/
public static String fieldToFunc(String fieldCode, String frontCode) {
String[] fieldSectArray = fieldCode.toLowerCase().split("_");
fieldCode = "";
for (int i = 0; i < fieldSectArray.length; i++) {
if (i == 1 && fieldSectArray[0].length() < 2)
fieldCode = fieldCode + fieldSectArray[i];
else
fieldCode = fieldCode
+ fieldSectArray[i].substring(0, 1).toUpperCase()
+ fieldSectArray[i].substring(1);
}
return frontCode.toLowerCase() + fieldCode;
}




/**
* @description:  get方法翻转调用
* @param obj:将要被转换的对象
* @param funcName:将要被转换的函数名
* @return:转换后的调用接口
* @throws Exception:抛出Exception异常
*/
public static Object getInvoke(Object obj, String funcName)
throws Exception {
Class classType = obj.getClass();
Method getMethod = classType.getMethod(funcName, new Class[] {});
Object value = getMethod.invoke(obj, new Object[] {});
return value;
}


/**
* @description: set方法翻转调用,field变量指对象属性名
* @param obj:将要被转换的对象
* @param funcName:将要被转换的函数名
* @param value:set方法的参数
* @throws Exception:抛出Exception异常
*/
 public static void setInvoke(Object obj, String funcName, Object value)
throws Exception {
Class classType = obj.getClass();
Field field = classType.getDeclaredField(funcName.substring(3)
.substring(0, 1).toLowerCase()
+ funcName.substring(4));
Method setMethod = classType.getMethod(funcName, new Class[] { field
.getType() });
if ("class java.math.BigDecimal".equals(field.getType().toString())) {
setMethod.invoke(obj, new Object[] { DataTypeTrans
.Str2BigDecimal(value == null ? "" : value.toString()) });
} else if ("class java.sql.Date".equals(field.getType().toString())) {
setMethod.invoke(obj, new Object[] { StringUtil.str2SqlDate(
value == null ? DateUtil.getSqlDate().toString() : value
.toString(), "yyyy-MM-dd") });
} else {
setMethod.invoke(obj, new Object[] { value });
}
  }
}
0 0
原创粉丝点击