常用的类
apache下beanutils工具类
org.apache.commons.beanutils.BeanUtils
/**
* 复制对象obj,类似于值传递,非引用
*/
private Object cloneObject(Object obj) throws Exception {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(obj);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
return in.readObject();
}
/**
* 比较同一类型class,orig 中非NULL值的属性和dest不同的值到resultBean里面
*
* @param dest 要比较的BEAN
* @param orig 原来BEAN
* @param result 结果BEAN
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static void differBeanNotNullPropertyToOtherBean(Object dest, Object orig , Object result) throws IllegalAccessException,
InvocationTargetException {
// Validate existence of the specified beans
if (dest == null) {
throw new IllegalArgumentException("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
if (result == null) {
throw new IllegalArgumentException("No result bean specified");
}
if (!dest.getClass().toString().equals(orig.getClass().toString())) {
throw new IllegalArgumentException("No same bean class");
}
if (orig instanceof Map) {
throw new IllegalArgumentException("No support map");
}
/* if (orig is a standard JavaBean) */
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) {
try {
Object value1 = PropertyUtils.getSimpleProperty(orig, name);
Object value2 = PropertyUtils.getSimpleProperty(dest, name);
if (value2 != null && !value2.equals(value1)) {
if (PropertyUtils.isReadable(result, name) && PropertyUtils.isWriteable(result, name)) {
BeanUtils.copyProperty(result, name, value2);
}
}
} catch (NoSuchMethodException e) {
}
}
}
}
/**
* 比较一个对象,如是这个对象的某一个属性不为空,把他copy到另一个有这个属性的bean中
*
* @param result 要copy到的bean
* @param orig 原来BEAN
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static void copyOrigNotNullPropertyToDestBean(Object result, Object orig) throws IllegalAccessException,
InvocationTargetException {
// Validate existence of the specified beans
if (result == null) {
throw new IllegalArgumentException("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
if (orig instanceof Map) {
throw new IllegalArgumentException("No support map");
}
/* if (orig is a standard JavaBean) */
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(result, name)) {
try {
Object value1 = PropertyUtils.getSimpleProperty(orig, name);
if (value1 != null) {
if (PropertyUtils.isReadable(result, name) && PropertyUtils.isWriteable(result, name)) {
BeanUtils.copyProperty(result, name, value1);
}
}
} catch (NoSuchMethodException e) {
}
}
}
}
分享到:
相关推荐
对象的克隆操作过程,展示了对象中数据字段是如何是实现克隆的
深度复制Java对象实例,复制后对象属性值改变不影响被复制对象,有注释
本文将从流的分类、流的转换、对象序列化、字节流和字符流的区别、对象克隆等方面对 Java IO 进行总结。 1. 流的分类 Java 中有两种类型的流:输入流(inputStream)和输出流(outputStream)。输入流用于从外部...
提供了对于JavaBean进行各种操作,克隆对象,属性等等. 2.commons-digester.jar:方便地将XML文档所定义的元素转化为JAVA对象。 3.commons-lang.jar; 4.commons-collection.jar包: 5.commons-io.jar包:FileUtil....
031111_【第11章:Java常用类库】_对象克隆技术笔记.pdf 031112_【第11章:Java常用类库】_Arrays笔记.pdf 031113_【第11章:Java常用类库】_比较器(Comparable、Comparator)笔记.pdf 031114_【第11章:Java常用...
浅克隆 :克隆出来的对象实例一模一样,对象的属性如果是引用数据类型,那么他么指向同一个地址值。无论是修改原来的对象,还是修改克隆出来的对象,只要是引用数据类型修改了,那么两个对象同时被修改,因为他们...
编写一个java应用程序。用户从键盘输入一个1-9999之间的数,程序将判断这个数是几位数,并判断这个数是否回...将一个类的对象(例如:Calendar)写入到文件,然后顺序读出该对象,并验证读出的对象是否原始对象的克隆.doc
A.1.3 传递和使用Java对象 A.1.4 JNI和Java违例 A.1.5 JNI和线程处理 A.1.6 使用现成代码 A.2 微软的解决方案 A.3 J/Direct A.3.1 @dll.import引导命令 A.3.2 com.ms.win32包 A.3.3 汇集 A.3.4 编写回调函数 A.3.5 ...
A.1.3 传递和使用Java对象 A.1.4 JNI和Java违例 A.1.5 JNI和线程处理 A.1.6 使用现成代码 A.2 微软的解决方案 A.3 J/Direct A.3.1 @dll.import引导命令 A.3.2 com.ms.win32包 A.3.3 汇集 A.3.4 编写回调函数 A.3.5 ...
A.1.3 传递和使用Java对象 A.1.4 JNI和Java违例 A.1.5 JNI和线程处理 A.1.6 使用现成代码 A.2 微软的解决方案 A.3 J/Direct A.3.1 @dll.import引导命令 A.3.2 com.ms.win32包 A.3.3 汇集 A.3.4 编写回调函数 A.3.5 ...
当可用内存不足以让Java虚拟机分配给一个对象时抛出该错误。 34.java.lang.StackOverflowError 堆栈溢出错误。当一个应用递归调用的层次太深而导致堆栈溢出时抛出该错误。 35.java.lang.ThreadDeath 线程结束。当...
A.1.3 传递和使用Java对象 A.1.4 JNI和Java违例 A.1.5 JNI和线程处理 A.1.6 使用现成代码 A.2 微软的解决方案 A.3 J/Direct A.3.1 @dll.import引导命令 A.3.2 com.ms.win32包 A.3.3 汇集 A.3.4 编写回调函数 A.3.5 ...
A.1.3 传递和使用Java对象 A.1.4 JNI和Java违例 A.1.5 JNI和线程处理 A.1.6 使用现成代码 A.2 微软的解决方案 A.3 J/Direct A.3.1 @dll.import引导命令 A.3.2 com.ms.win32包 A.3.3 汇集 A.3.4 编写回调函数 A.3.5 ...
这样在用户的前后连接或不同用户之间的连接中,可以对此对象的同一属性进行操作;在任何地方对此对象属性 的操作,都将影响到其他用户对此的访问。服务器的启动和关闭决定了application对象的生命。它是...
深度克隆:所有元素或属性均完全克隆,并于原引用类型完全独立,即,在后面修改对象的属性的时候,原对象不会被修改。 代码如下: function cloneObject(obj){ var o = obj.constructor === Array ? [] : {}; for...
A.1.3 传递和使用Java对象 A.1.4 JNI和Java违例 A.1.5 JNI和线程处理 A.1.6 使用现成代码 A.2 微软的解决方案 A.3.1 @dll.import引导命令 A.3.2 com.ms.win32包 A.3.3 汇集 A.3.4 编写回调函数 A.3.5 其他J/Direct...
6.2对象克隆 6.3接口与回调 6.4内部类 6.4.1使用内部类访问对象状态 6.4.2内部类的特殊语法规则 6.4.3内部类是否实用、必要和安全 6.4.4局部内部类 6.4.5匿名内部类 6.4.6静态内部类 6.5...
7.13.5 通过系统剪贴板传递Java对象 7.13.6 使用本地剪贴板来传递对象引用 7.14 拖放操作 7.14.1 Swing对数据传递的支持 7.14.2 拖曳源 7.14.3 放置目标 7.15 平台集成 7.15.1 闪屏 7.15.2 启动桌面应用程序 7.15.3 ...