方法1:把JavaBean的from的值自动set给to,省略了自己从from中get然后再set给to

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public static Object convertBean2Bean(Object from, Object to) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(to.getClass());
PropertyDescriptor[] ps = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor p : ps) {
Method getMethod = p.getReadMethod();
Method setMethod = p.getWriteMethod();
if (getMethod != null && setMethod != null) {
try {
Object result = getMethod.invoke(from);
setMethod.invoke(to, result);
} catch (Exception e) {
// 如果from没有此属性的get方法,跳过
continue;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} return to;
} 方法2:
/**
* 不支持to继承(to是其他bean的子类)
*/

import java.lang.reflect.Field;
import java.lang.reflect.Method;

   public static Object coverBean2Bean(Object from, Object to) {
Class fClass = from.getClass();
Class tClass = to.getClass();
// 拿to所有属性(如果有继承,父类属性拿不到)
Field[] cFields = tClass.getDeclaredFields();
try {
for (Field field : cFields) {
String cKey = field.getName();
// 确定第一个字母大写
cKey = cKey.substring(0, 1).toUpperCase() + cKey.substring(1); Method fMethod;
Object fValue;
try {
fMethod = fClass.getMethod("get" + cKey);// public方法
fValue = fMethod.invoke(from);// 取getfKey的值
} catch (Exception e) {
// 如果from没有此属性的get方法,跳过
continue;
} try {
// 用fMethod.getReturnType(),而不用fValue.getClass()
// 为了保证get方法时,参数类型是基本类型而传入对象时会找不到方法
Method cMethod = tClass.getMethod("set" + cKey, fMethod.getReturnType());
cMethod.invoke(to, fValue);
} catch (Exception e) {
// 如果to没有此属性set方法,跳过
continue;
}
}
} catch (Exception e) {
e.printStackTrace();
} return to;
} 3.打印bean /** * 打印bean信息
*/
public static void printInvoke(Object object) {
Method[] ms = object.getClass().getMethods();
String str = spare;
str += "start log object: " + object.getClass().getSimpleName() + "\r\n";
str += spare;
System.out.print(str); for (int i = 0; i < ms.length; i++) {
if (ms[i].getName().indexOf("get") != -1
&& !ms[i].getName().equals("getClass")) {
try {
System.out.println(ms[i].getName() + " = "
+ ms[i].invoke(object));
} catch (Exception e) {
e.printStackTrace();
}
}
} System.out.println(spare);
}

最新文章

  1. Android学习九:屏幕自适应
  2. sockaddr与sockaddr_in结构体简介
  3. asp.net 网站 或者web Api 发布
  4. PAT乙级 1023. 组个最小数 (20)
  5. C++ 操作 MySQL
  6. memcache command
  7. python基础之数据类型/字符串/元组/列表/字典
  8. solr7.2安装实例,中文分词器
  9. iOS - UIView 动画
  10. YII框架CGridView sql有条件分页实现
  11. [Swift]LeetCode888. 公平的糖果交换 | Fair Candy Swap
  12. group by 用法解析
  13. day-10初级函数
  14. suricata HTTP关键字
  15. 这不是我想要的ABAP开发者
  16. pta l2-28(秀恩爱分得快)
  17. 微信小程序——豆瓣电影——(2):小程序运行部署
  18. 小程序通过用户授权获取手机号之getPhoneNumber
  19. c3p0----获取不到链接
  20. NetCore+Dapper WebApi架构搭建(三):添加实体和仓储

热门文章

  1. MySQL学习笔记(4) - 创建数据库
  2. C# 操作系统回收站
  3. HTML5简单入门系列(三)
  4. php的一些小笔记--字符串
  5. 重读LPTHW-Lesson1-14
  6. WSGI的理解
  7. postgresql的psql命令
  8. .net 拉姆达 groupby(p =&gt; p.X) order by count(c.Count())
  9. SQL Server索引的维护 - 索引碎片、填充因子 &lt;第三篇&gt;
  10. UESTC_酱神的旅行 2015 UESTC Training for Dynamic Programming&lt;Problem M&gt;