TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
应用的场景是:
页面上一个javaBean已经有数据了,再创建一个JavaBean,想将这些数据复制到这个新的Bean中
有了这个方法,就不用自己手动的写这些代码了:
bean2.setXxx(bean1.getXxx())
...
...
...
代码如下:
- import java.lang.reflect.Method;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.ListIterator;
-
- /**
- * 复制相似Row之间的数据,比如讲一个有数据的Row的所有数据选择性的复制到一个空的Row中,通过属性的set、get方法
-
- * desRow和srcRow的类型是一样的
-
- * 只能复制RowImpl类型的对象
-
- * 注意:栏位以VO中Attribute标签里的列表为准,desRow中存在的属性则srcRow中必须存在,
- * 否则在needlessColumn_List加入srcRow中不存在的属性
- */
- public class CloneUtil {
- private Object desRow; // 目标Row
- private Object srcRow; // 源Row
- private List
-
- needlessColumn_List; // 需要跳过的属性的List
-
- public CloneUtil(Object desRow, Object srcRow,
- List
-
- needlessColumn_List) {
- this.desRow = desRow;
- this.srcRow = srcRow;
- this.needlessColumn_List = needlessColumn_List;
- }
-
- /**
- * 主方法
- *
- * @throws Exception
- */
- public void deepClone() {
- Method setMethod = null; // 源 Bean中的属性的set方法
- Method getMethod = null; // 源 Bean中的属性的get方法
-
- Class desRowClass = desRow.getClass(); // 得到desRow的Class
- // 所有Public范围的方法,包括从父类继承的和从接口实现的公共方法
- Method[] publicMethods = desRowClass.getMethods();
- // 本类定义的所有范围的方法,不包括从父类继承的和从接口实现的方法
- Method[] exceptedSuperMethods = desRowClass.getDeclaredMethods();
- List
-
- neededMethodsList = new ArrayList
-
- (); // 保存Bean有对应属性的方法的List
- fetchNeededMethods(publicMethods, exceptedSuperMethods,
- neededMethodsList); // 只将Bean的属性的set方法放入neededMethodsList中以便处理
- try {
- for (Method neededMethod : neededMethodsList) { // 循环处理此desRow的Class对应的所有方法
- String neededMethodName = neededMethod.getName();
- // 跳过不需要复制的属性
- boolean inList = false;
- if (this.needlessColumn_List != null
- && !needlessColumn_List.isEmpty()) {
- inList = checkNeedlessColumn(neededMethodName); // 检查不需要复制的属性
- if (inList == false) { // 如果当前属性名称存在于needlessColumn_List中,则跳过
- continue;
- }
- }
- // 得到源Bean中的属性的set方法
- setMethod = srcRow.getClass().getMethod(neededMethodName,
- neededMethod.getParameterTypes());
- // 得到源Bean中的属性的get方法
- getMethod = srcRow.getClass().getMethod(convertName(setMethod.getName()), null);
- Object item = getMethod.invoke(srcRow, null); // 执行源 Bean的get方法,得到返回值
- neededMethod.invoke(desRow, item); // 执行目标Bean的set方法
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 只将VO的属性的set方法放入neededMethodsList中以便处理
- * 因为publicMethods和exceptedSuperMethods中可能有很多不需要方法
- *
- * @param publicMethods
- * @param exceptedSuperMethods
- * @param neededMethodsList
- */
- private void fetchNeededMethods(Method[] publicMethods,
- Method[] exceptedSuperMethods, List
-
- neededMethodsList) {
- for (Method publicMethod : publicMethods) { // 遍历publicMethods
- String publicMethodName = publicMethod.getName(); // publicMethods中的每一个方法名
- if (publicMethodName.startsWith("set")) { // 只处理以“set”开头的方法
- for (Method exceptedSuperMethod : exceptedSuperMethods) { // 遍历methodsExceptSuper
- String exceptedSuperMethodName = exceptedSuperMethod.getName();
- // 如果publicMethodName与exceptedSuperMethodName相等
- if (publicMethodName.equals(exceptedSuperMethodName))
- neededMethodsList.add(publicMethod); // 将符合条件的方法放入此List中
- }
- }
- }
- }
-
- /**
- * 过滤needlessColumn_List中的条目
- *
- * @param methodName
- * desRow的Class中以“set”开头的方法
- * @return 如果methodName存在于此List中 则返回false
- */
- private boolean checkNeedlessColumn(String methodName) {
- ListIterator
-
- it = needlessColumn_List.listIterator();
- while (it.hasNext()) {
- String needlessColumn = it.next();
- String uuidCName = "set" + needlessColumn;
- if (uuidCName.equals(methodName)) {
- needlessColumn_List.remove(needlessColumn);
- return false;
- }
- }
- return true;
- }
-
- /**
- * 将setXxx方法名称转换成getXxx方法名称
- *
- * @param methodName
- * set开头的方法名称
- * @return 对应的get方法
- */
- private String convertName(String methodName) {
- if (methodName == null) {
- return null;
- }
- String s = methodName.substring(3, methodName.length());
- return "get" + s;
- }
- }
- 测试代码: public class Test {
-
- public static void main(String[] args) {
- Bean src = new Bean();
- src.setName("DOTA");
-
- Bean des = new Bean();
- System.out.println("复制前的Name = " + des.getName());
- CloneUtil cu = new CloneUtil(des, src, null);
- cu.deepClone();
- System.out.println("复制后的Name = " + des.getName());
- }
- }
-
- class Bean {
- private String name;
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
- }
-
- 运行:
-
- C:java>java Test
-
- 复制前的Name = null
-
- 复制后的Name = DOTA
-
-
-
-
-
-
复制代码
源码下载:http://file.javaxxz.com/2014/10/28/235631953.zip |
|