自己通过反射写的一个属性copy类
package com.xxx.beancopier; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier; /**
* @Type BeanCopier
* @Desc 属性copy类
* @author
* @date 2017-01-06
* @Version V1.0
*/
public class BeanCopier {
private BeanCopier() {
} public static <DestType> DestType propertiesCopy(Object orig, DestType dest) {
if (orig == null || dest == null) {
throw new RuntimeException("参数不能为null");
} Method[] destMethods = dest.getClass().getMethods();
for (Method destMethod : destMethods) {
if (needCopy(destMethod)) {
MethodCopy methodCopy = new MethodCopy(destMethod);
methodCopy.copy(orig, dest);
}
}
return dest;
} private static boolean needCopy(Method method) {
PropertyCopy annotation = method.getAnnotation(PropertyCopy.class);
if (annotation != null && annotation.ignore())
return false;
return method.getName().startsWith("set") && method.getParameterTypes().length == 1
&& Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers());
} static class MethodCopy {
private Method method;
private String methodName;
private Method getterMethod; public MethodCopy(Method destMethod) {
method = destMethod;
methodName = destMethod.getName();
} private void copy(Object orig, Object dest) {
try {
this.getterMethod = orig.getClass().getMethod("get" + methodName.substring(3));
} catch (NoSuchMethodException e) {
throw new RuntimeException("对象【" + orig + "】中缺失【" + methodName.replaceFirst("set", "get") + "()】方法!");
}
Object propertyValue = invokeMethod(orig, getterMethod);
if (propertyValue != null) {
invokeMethod(dest, method, propertyValue);
}
} private Object invokeMethod(Object targerObject, Method method, Object... args) {
if (method == null) {
throw new RuntimeException("被调用方法不能为空!");
}
// 验证参数数量
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length != args.length) {
throw new RuntimeException("方法【" + method + "】的参数数量【" + parameterTypes.length + "】和反射调用时的参数数量【"
+ args.length + "】不相符,无法完成调用!");
} try {
method.setAccessible(true);
return method.invoke(targerObject, args);
} catch (Throwable e) {
throw new RuntimeException("调用对象【" + targerObject + "】的【" + method + "】方法,参数为【" + args + "】时发生异常:", e);
}
} }
} @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
@Documented
@interface PropertyCopy {
/**
* 被标注方法是否被忽略拷贝
* @return
*/
boolean ignore() default false;
}
使用如下:
StudentDTO studentDTO1 = BeanCopier.propertiesCopy(student, new StudentDTO());
自己通过反射写的一个属性copy类的更多相关文章
- 我写的一个ExcelHelper通用类,可用于读取或生成数据
读取或生成EXCEL数据的方法有很多,一般常见的有: 1.通过OFFICE EXCEL组件,优点:读取与生成EXCEL文件方便,缺点:服务器上必须安装OFFICE软件,且进程无法及时释放 2.通过第三 ...
- 我写了一个java实体类,implements了Serializable接口,然后我如何让serialversionUID自动生成
写了一个java实体类,implements了Serializable接口,让serialversionUID自动生成方法: 1.点击类旁边的警告符号: 2.选择Add generated seria ...
- 自己用反射写的一个request.getParameter工具类
适用范围:当我们在jsp页面需要接收很多值的时候,如果用request.getParameter(属性名)一个一个写的话那就太麻烦了,于是我想是 否能用反射写个工具类来简化这样的代码,经过1个小时的代 ...
- entity framework 6 我写了一个公用数据类
public class BaseDAL { string strConn = ""; public BaseDAL(string connString) { strConn = ...
- 同事写了一个疯狂的类构造器,我要疯了,Builder 模式都不会么?!
疯狂的类构造器 最近栈长在做 Code Review 时,发现一段创建对象的方法: Task task = new Task(112, "紧急任务", "处理一下这个任务 ...
- 新手写的一个DBCP工具类
package com.xx.questionnaire.util.dao; import java.io.IOException; import java.sql.Connection; impor ...
- BeanUtils对象属性copy的性能对比以及源码分析
1. 对象属性拷贝的常见方式及其性能 在日常编码中,经常会遇到DO.DTO对象之间的转换,如果对象本身的属性比较少的时候,那么我们采用硬编码手工setter也还ok,但如果对象的属性比较多的情况下,手 ...
- JavaScript写一个表格排序类
依稀记得那是上个星期六的下午,我参加了网易暑期实习生招聘笔试.考得相当糟糕,编程题3个题通过了2个,简答题没做对,选择题貌似是20个题猜了6-7个,99%是挂了,唉唉唉!生活不只眼前的苟且,学习的脚步 ...
- 如何写好一个UITableView
本文是直播分享的简单文字整理,直播共分为上.下两部分. 第一部分: 优酷 :http://v.youku.com/v_show/id_XMTUzNzQzMDU0NA%3Cmark%3E.html Or ...
随机推荐
- Android过滤Logcat输出
logcat和grep配合使用 1.打印特定tag的log,如打印Tag为Adm的Log adb logcat | grep Adm adb logcat | grep - ...
- PHP 数组拼接成字符串
PHP[知识分享] 数组拼接成字符串 <?php // 格式: [二维数组] Array ( [0] => Array ( [topicid] => 1 ) [1] => Ar ...
- launch failed.Binary not found
1.在eclipse官网中下载已经集成了CDT的eclipse.(http://www.eclipse.org/downloads/download.php?file=/technology/epp/ ...
- 从零开始学C++之重载 operator new 和 operator delete 实现一个简单内存泄漏跟踪器
先来说下实现思路:可以实现一个Trace类,调用 operator new 的时候就将指向分配内存的指针.当前文件.当前行等信息添加进Trace 成员map容器内,在调用operator delete ...
- poj 3897 Maze Stretching 二分+A*搜索
题意,给你一个l,和一个地图,让你从起点走到终点,使得路程刚好等于l. 你可以选择一个系数,把纵向的地图拉伸或收缩,比如你选择系数0.5,也就是说现在上下走一步消耗0.5的距离,如果选择系数3,也就是 ...
- Wireshark抓包介绍和TCP三次握手分析
wireshark介绍 wireshark的官方下载网站: http://www.wireshark.org/ wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示 ...
- 【deep learning学习笔记】最近读的几个ppt(四)
这几个ppt都是在微博上看到的,是百度的一个员工整理的. <Deep Belief Nets>,31页的一个ppt 1. 相关背景 还是在说deep learning好啦,如特征表示云云. ...
- iOS开发-大文件下载与断点下载思路
大文件下载方案一:利用NSURLConnection和它的代理方法,及NSFileHandle(iOS9后不建议使用)相关变量: @property (nonatomic,strong) NSFile ...
- Golang爬虫示例包系列教程(一):pedaily.com投资界爬虫
Golang爬虫示例包 文件结构 自己用Golang原生包封装了一个爬虫库,源码见go get -u -v github.com/hunterhug/go_tool/spider ---- data ...
- OC之OC与C的比较
1. 从编写.编译.链接的流程. 1). 创建1个.m的源文件. 2). 在这个文件中写上符合OC语法规范的源代码. 3). 编译. a. 预编译: 执行预处理代码. b. 检查语法. c. 生成目标 ...