package com.syaccp.ajax.json;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;
@SuppressWarnings("unchecked")
public class PropertyUtil {
private static String[] objs;
public static String[] getPropertyNames(Object obj){
Class clazz = obj.getClass();
Field[] field = clazz.getDeclaredFields();
int len = field.length;
objs = new String[len];
for (int i = 0; i < len; i++) {
objs[i] = field[i].getName();
}
return objs;
}
public static Object copy(Object obj, Map map) {
Class clazz= obj.getClass();
Field[] field = clazz.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
String propertyName = field[i].getName();
String value=((String[])map.get(propertyName))[0];
PropertyUtil.setPropertyValue(obj,propertyName,value);
}
return obj;
}
private static PropertyDescriptor getPropertyDescriptor(Class clazz,
String name) {
PropertyDescriptor propertyDescriptor = null;
String propertyName = null;
Method writeMethod = null;
Method readMethod = null;
String first = null;
String two = null;
StringBuffer buffer = new StringBuffer();
String getString = null;
String setString = null;
try {
Field field = clazz.getDeclaredField(name);
if (field != null) {
first = name.substring(0, 1);
first = first.toUpperCase();
two = name.substring(1);
buffer.append("set").append(first).append(two);
setString = buffer.toString();
Class[] cla = { field.getType() };
writeMethod = clazz.getMethod(setString, cla);
buffer.delete(0, buffer.length()); //
buffer.append("get").append(first).append(two);
getString = buffer.toString();
readMethod = clazz.getMethod(getString, null);
propertyDescriptor = new PropertyDescriptor(name, readMethod,
writeMethod);
}
} catch (Exception e) {
e.printStackTrace();
}
return propertyDescriptor;
}
public static void setPropertyValue(Object obj, String name, Object value) {
try {
Class clazz = obj.getClass();
PropertyDescriptor desc = getPropertyDescriptor(clazz, name);
Method read = desc.getWriteMethod();
Object[] objArr = { PropertyUtil.getConvert(obj,name,value.toString()) };
obj = read.invoke(obj, objArr);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Object getConvert(Object obj1, String name, String value) {
String nameType = null;
Object obj = null;
try {
Class clazz = obj1.getClass();
Field field = clazz.getDeclaredField(name);
nameType = field.getType().getName();
if (field != null) {
if ("java.lang.Integer".equals(nameType)) {
obj = new Integer(value);
return obj;
} else if ("java.sql.Date".equals(nameType)) {
java.sql.Date date = java.sql.Date.valueOf(value);
obj = date;
return obj;
} else if ("java.util.Date".equals(nameType)) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
obj = format.parse(value);
return obj;
} else if ("java.lang.Long".equals(nameType)) {
obj = new Long(value);
return obj;
} else if ("int".equals(nameType)) {
obj = Integer.parseInt(value);
return obj;
} else if ("java.lang.Double".equals(nameType)) {
obj = Double.parseDouble(value);
return obj;
}else if ("java.lang.String".equals(nameType)){
obj = value.toString();
return obj;
}
}
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static Object getPropertyValue(Object object, String name) {
Object obj = null;
try {
Class clazz = object.getClass();
PropertyDescriptor desc = getPropertyDescriptor(clazz, name);
Method read = desc.getReadMethod();
obj = read.invoke(object, null);
if (obj == "Integer") {
obj = Integer.parseInt(obj.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static Object setValue(Object obj, String name) {
Object reust = null;
try {
Class clazz = obj.getClass();
PropertyDescriptor desc = getPropertyDescriptor(clazz, name);
Method read = desc.getWriteMethod();
reust = read.invoke(obj, null);
} catch (Exception e) {
e.printStackTrace();
}
return reust;
}
}
Struts +json
需积分: 0 68 浏览量
2008-09-12
14:01:15
上传
评论
收藏 3KB RAR 举报
tangy_q
- 粉丝: 0
- 资源: 4
最新资源
- 2%EF%BC%9A%E9%99%95%E8%A5%BF%E
- yyspdz62_944.apk
- SAP公司间采购EDI配置-如何触发自动MIRO.docx
- python197基于图像识别的仪表实时监控系统.rar
- I2C驱动SHT30温湿度传感器和LCD12864使用例程(RSCG12864B)
- python193中学地理-中国的江河湖泊教学网(django).rar
- python191基于时间序列分析的大气污染预测软件(django).rar
- python190基于人脸识别智能化小区门禁管理系统.rar
- python189某医院体检挂号系统.rar
- python179的企业物流管理系统(django).rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0