package com.ygsoft.czp.ticketsys.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.ygsoft.czp.model.AcUser;
import com.ygsoft.czp.model.WebCzpCzrw;
import com.ygsoft.czp.test.common.ClassPropertyBean;
/**
* 进行一些JAVABEAN的相关操作 〖功能模块〗: BeanUtil(功能) 〖目 的〗: 〖作 者〗: * 〖创建日期〗: 〖版 本〗: 1.00<br>
* 〖版权信息〗: * 〖更改记录〗: 更改时间、更改人、更改原因、更改内容<br>
*/
public class BeanUtil {
/**
* 将源对象中的数据拷贝到目标对象中
*
* @param target
* @param source
*/
public static void copyProperty(Object target, Object source) {
Field[] tarFields = target.getClass().getDeclaredFields();
Field[] sourFields = source.getClass().getDeclaredFields();
for (int i = 0; i < tarFields.length; i++) {
Field f = tarFields[i];
int n = f.getModifiers();
// 判断是FINAL字段
if (Modifier.isFinal(n)) {
continue;
}
f.setAccessible(true);
String tarType = tarFields[i].getType().toString();
String name = tarFields[i].getName();
for (int j = 0; j < sourFields.length; j++) {
Field temp = sourFields[j];
int m = temp.getModifiers();
if (Modifier.isFinal(m)) {
continue;
}
temp.setAccessible(true);
String sourType = sourFields[j].getType().toString();
String sourName = sourFields[j].getName();
if (name.equalsIgnoreCase(sourName)) {
try {
if (tarType.equals(sourType)) {
f.set(target, temp.get(source));
} else if ((tarType.indexOf("Date") != -1 || (tarType
.indexOf("Timestamp") != -1) )
&& sourType.indexOf("String") != -1) {
String time = (String) temp.get(source);
if (time != null && !(time.trim().equals(""))) {
String dateStr = "yyyy-MM-dd HH:mm:ss";
if (time.trim().length() < 11) {
dateStr = "yyyy-MM-dd";
}
SimpleDateFormat myFmt2 = new SimpleDateFormat( dateStr);
Date date = new Date();
try {
date = myFmt2.parse(time);
} catch (ParseException e) {
System.out
.println("copyProperty 时不能进行字符串到日期的转换");
e.printStackTrace();
}
f.set(target, date);
}
} else if (tarType.indexOf("String") != -1
&& (sourType.indexOf("Date") != -1 || (sourType
.indexOf("Timestamp") != -1)||(sourType
.indexOf("CnDate") != -1))) {
Date time = (Date) temp.get(source);
if (time != null) {
SimpleDateFormat myFmt2 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
f.set(target, myFmt2.format(time));
}
}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Long") != -1){
if(temp.get(source)!=null){
f.set(target, ""+temp.get(source) );
}else{
f.set(target, "");
}
}else if(tarType.indexOf("Long") != -1 && sourType.indexOf("String") != -1){
if(temp.get(source)!=null && !temp.get(source).equals("")){
f.set(target, new Long(temp.get(source).toString()) );
}
}else if(tarType.indexOf("Double") != -1 && sourType.indexOf("String") != -1){
if(temp.get(source)!=null && !temp.get(source).equals("")){
f.set(target, new Double(temp.get(source).toString()) );
}
}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Double") != -1){
if(temp.get(source)!=null){
f.set(target, ""+temp.get(source) );
}else{
f.set(target, "");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
temp.setAccessible(false);
}
f.setAccessible(false);
}
}
/**
* 将源对象中的数据拷贝到目标对象中
*
* @param target
* @param source
* @param namesList 如果传入某个属性,则某个属性不进行COPY
*
*/
public static void copyProperty(Object target, Object source,List namesList) {
Field[] tarFields = target.getClass().getDeclaredFields();
Field[] sourFields = source.getClass().getDeclaredFields();
for (int i = 0; i < tarFields.length; i++) {
Field f = tarFields[i];
int n = f.getModifiers();
// 判断是FINAL字段
if (Modifier.isFinal(n)) {
continue;
}
f.setAccessible(true);
String tarType = tarFields[i].getType().toString();
String name = tarFields[i].getName();
if(namesList.contains(name)){
continue;
}
for (int j = 0; j < sourFields.length; j++) {
Field temp = sourFields[j];
int m = temp.getModifiers();
if (Modifier.isFinal(m)) {
continue;
}
temp.setAccessible(true);
String sourType = sourFields[j].getType().toString();
String sourName = sourFields[j].getName();
if (name.equalsIgnoreCase(sourName)) {
try {
if (tarType.equals(sourType)) {
f.set(target, temp.get(source));
} else if ((tarType.indexOf("Date") != -1 || (tarType
.indexOf("Timestamp") != -1))
&& sourType.indexOf("String") != -1) {
String time = (String) temp.get(source);
if (time != null && !(time.trim().equals(""))) {
String dateStr = "yyyy-MM-dd HH:mm:ss";
if (time.trim().length() < 11) {
dateStr = "yyyy-MM-dd";
}
SimpleDateFormat myFmt2 = new SimpleDateFormat(
dateStr);
Date date = new Date();
try {
date = myFmt2.parse(time);
} catch (ParseException e) {
System.out
.println("copyProperty 时不能进行字符串到日期的转换");
e.printStackTrace();
}
f.set(target, date);
}
} else if (tarType.indexOf("String") != -1
&& (sourType.indexOf("Date") != -1 || (sourType
.indexOf("Timestamp") != -1))) {
Date time = (Date) temp.get(source);
if (time != null) {
SimpleDateFormat myFmt2 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
f.set(target, myFmt2.format(time));
}
}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Long") != -1){
f.set(target, new Long(temp.get(source).toString()) );
}else if(tarType.indexOf("Long") != -1 && sourType.indexOf("String") != -1){
f.set(target, ""+temp.get(source) );
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
temp.setAccessible(false);
}
f.setAccessible(false);
}
}
//
private static ConcurrentHashMap<Class, Field[]> classPropertyMap = new ConcurrentHashMap<Class, Field[]>();
/**
* 由类得到类的所有属性,并进行缓存,使得再次使用时可以取到相关信息。
* @param cla
* @return
*/
public static Field[] getClassProperty(Class cla) {
Field[] fileds= classPropertyMap.get(cla);
if (fileds != null) {
return fileds;
}else {
fileds = cla.getDeclaredFields();
classPropertyMap.put(cla, fileds);
return fileds;
}
}
/**
* 将对象转换为一个XML字符串,返回
*
* @param target
* @param source
*/
public static String convertBeanToXML(Object source,String beanName) {
StringBuffer sb = new StringBuffer();
Field[] sourFields = getClassProperty(source.getClass());
beanName = beanName.equals("")?source.getClass().getName().substring(source.getClass().getName().lastIndexOf(".")+1):beanName;
sb.append("<"+beanName+">");
for (int j = 0; j < sourFields.length; j++) {
Field temp = sourFields[j];
int m = temp.getModifiers();
if (Modifier.isFinal(m)) {
continue;
}
temp.setAccessible(true);
String sourName = sourFields[j].getName();
sb.append("\r\n\t<"+sourName+">");
try {
if(temp.get(source) != null){
sb.append(temp.get(source) );
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sb.append("</"+sourName+">");
temp.setAccessible(false);
}
sb.append("\r\n</"+beanName+">");
return sb.toString();
}
/**
* 将对象转换为一个XML字符串,返回
*
* @param target
* @param source
*/
public static String convertBeanToXML(Object source,String beanName,Map map) {
StringBuffer sb = new StringBuffer();
Field[] sourFields = getClassProperty(source.getClass());
beanName = beanName.equals("")?source.getClass().getName():beanName;
sb.append("<"+beanName+">");
for (int j = 0; j < sourFields.length; j++) {
Field temp = sourFields[j];
int m = temp.getModifiers();
if (Modifier.isFinal(m)) {
continue;
}
temp.setAccessible(true);
String sourName = sourFields[j].getName();
if(map.containsKey(sourName)){
sb.append("\r\n\t<"+sourName+">");
try {
if(temp.get(source) != null){
sb.append(temp.get(source) );
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sb.append("</"+sourName+">");
}
temp.setAccessible(false);
}
sb.append("\r\n</"+beanName+">");
return sb.toString();
}
}
分享到:
相关推荐
2 Copy the source code to the new projects. 3 Import the liberaries as show on top. 4 New the database and be sured the server is open. 4 OK! You can run the ...
IOS property属性 原子性 atomic nonatomic 访问权限 readWrite readOnly 内存管理 strong retain copy assign weak unsafe_unretained 方法命名 getter = setter = <name> 结论 1、原子性 (1...
我们在声明@property 属性时,总是要在括号中写上assign、retain、copy、weak、strong中的一个,很多时候,我们仅仅只是按照习惯去写经常写的那一个,但有时候看代码时又会发现别人用的不尽相同,那这些之间的区别是...
(三)阿拉伯金额数字万位或元位是“0”,或者数字中间连续有几个“0”, 万位、元位也是“0’,但千位、角位不是“0”时,中文大写金额中 可以只写一个零字,也可以不写“零”字。如¥1,680.32,应写成 人民币壹...
详见 http://blog.csdn.net/arttecme/article/details/8566818
3. 请问 property 的 作用,assign,copy,retain 的 区别; assign就是基本赋值 copy是重新创建一个oc对象,计数器是1 retain是对计数器+1 4.请写出实现多线程操作涉及的类及 简单 示例; NSThread, ...
模型属性*/ @property (nonatomic, copy) NSString * city; @property (nonatomic, copy) NSString * content; @property (nonatomic, copy) NSString * create_time; @property (nonatomic, copy) NSString * ...
代码实现根据需要实现的功能,我这里建了一个类,来分别实现不同的功能@interface VideoAudioComposition : NSObject/** 合成后的名字 */@property (nonatomic,copy) NSString *compositionName;/** 合成
想要学习DELPHI的控件开发,这个资料非常的值得一看。 Delphi控件开发 1~8 由CowNew开源团队撰写。 文章将通过一个简单的例子来为初学者介绍编写控件的一些基本方法和模式。 这个例子控件叫TLeiLabel,是在...
Text path 设置: 在 ALLEGRO 视窗 LAYOUT 时,不能执行一些指令:Show element, Tools>report… 1) 应急办法:蒐寻一个相应的 log 文档 copy 到档案同一路径即可. 2) Setup>User Preference 之 Design_Paths>...
Jquery是继prototype之后又一个优秀的Javascrīpt框架。它是轻量级的js库(压缩后只有21k) ,它兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。jQuery使用户能更方便地处理...
将apache-roller-4.0\apache-roller-4.0\webapp\roller\WEB-INF\classes目录下面的除了org目录外的所有文件都copy到自建项目的src目录中. 删除自建项目中: org\apache\roller\weblogger目录下面的pojos目录及该...
'希望大家可以写出一个属于自己的更多功能的记事本 '今天的教程就到这里了 '我们下次见 '以下是代码: '----------------------------------华丽的分割线------------------------------------------------- ...
IOS 中runtime使用方法整理 做iOS的朋友都知道或听说runtime,这个东西很像java的反射机制,但功能远胜于java的反射。通过runtime我们可以动态的向一个类中添加属性、成员...@property(nonatomic,copy) NSString *publi
后来自己写了一个简单的。 实现代码如下 1、创建一个继承UITextView的CjTextView 。 import @interface CjTextView : UITextView @end import CjTextView.h @interface CjTextView () @property (nonatomic, ...
@property (readonly, nonatomic, copy) NSString *name; @property (readonly, nonatomic, strong) NSDate *createdAt; @property (readonly, nonatomic, assign, getter = isMeUser) BOOL ...
这几个文件是Imgadmin.ocx,Imgcmn.dll,Imgedit.ocx,Imgscan.ocx,Imgshl.dll,Imgthumb.ocx,Imgutil.dll,把它们copy到windows\system目录下,然后用regsvr32.exe将它们分别注册。 2. 打开工程,进入资源管理器...
10. 某电影中,只有一个layer1,其上放置一个有两个元件(test1 和test2)组合成的组合体, 选择这个组合体执行打散Ctrl+B,然后右键单击执行Distribute to layers,那末: □ A. 这个电影中将增加两个新层:layer2 ...
0051 一般的I2C设备的从机地址都是一个字节的,因为前两位为0x00(16进制的),后两位为pcf8563移位自后的I2C从机地址0x51(也是16进制的) 压缩包中的PCF8563-CN.pdf,datasheet告诉我们:I2C总线从地址:读,0A3H...