Android 开发中的代码片段(2)复制对象之间的属性值

简介: 前言开发中会遇到这样的一个情况,我们得到一个dto对象,里面有几十个属性值,需要将这几十个属性值的N个通过VO传输另外一个地方,一般我们的做法是:创建VO类,new vo() 对象,通过vo.set(dto.get)的方式不断的设置值。

前言

开发中会遇到这样的一个情况,我们得到一个dto对象,里面有几十个属性值,需要将这几十个属性值的N个通过VO传输另外一个地方,一般我们的做法是:

创建VO类,new vo() 对象,通过vo.set(dto.get)的方式不断的设置值。

但是这种方式在属性少量的情况之下还是没有问题,但是在属性不断变化的情况下就令人烦恼了,但是我们可以选择使用反射来完成A对象拷贝到B对象的这一过程。

其主要原理是通过反射获取到A对象的get方法和数据类型,然后截取get之后的字符串,拼接成set方法,再通过反射调用B的set方法和传值。

代码

   public class CopyPropertiesUtil {
       /**
        * 利用反射实现对象之间属性复制
        *
        * @param from
        * @param to
        */
       public static void copyProperties(Object from, Object to) throws Exception {
            copyPropertiesExclude(from, to, null);
       }

       /**
        * 复制对象属性
        *
        * @param from
        * @param to
        * @param excludsArray 排除属性列表
        * @throws Exception
        */
       @SuppressWarnings("unchecked")
       public static void copyPropertiesExclude(Object from, Object to, String[] excludsArray) throws Exception {
               List<String> excludesList = null;
               if (excludsArray != null && excludsArray.length > 0) {
                   excludesList = Arrays.asList(excludsArray);    //构造列表对象
               }
               Method[] fromMethods = from.getClass().getDeclaredMethods();
               Method[] toMethods = to.getClass().getDeclaredMethods();
               Method fromMethod = null, toMethod = null;
               String fromMethodName = null, toMethodName = null;
               for (int i = 0; i < fromMethods.length; i++) {
                   fromMethod = fromMethods[i];
                   fromMethodName = fromMethod.getName();
                   if (!fromMethodName.contains("get") || fromMethodName.contains("getId"))
                       continue;
                   //排除列表检测
                   if (excludesList != null && excludesList.contains(fromMethodName.substring(3).toLowerCase())) {
                       continue;
                   }
                   toMethodName = "set" + fromMethodName.substring(3);
                   toMethod = findMethodByName(toMethods, toMethodName);
                   if (toMethod == null)
                       continue;
                   Object value = fromMethod.invoke(from, new Object[0]);
                   if (value == null)
                       continue;
                   //集合类判空处理
                   if (value instanceof Collection) {
                       Collection newValue = (Collection) value;
                       if (newValue.size() <= 0)
                           continue;
                   }
                   toMethod.invoke(to, new Object[]{value});
               }
       }

       /**
        * 对象属性值复制,仅复制指定名称的属性值
        *
        * @param from
        * @param to
        * @param includsArray
        * @throws Exception
        */
       @SuppressWarnings("unchecked")
       public static void copyPropertiesInclude(Object from, Object to, String[] includsArray) throws Exception {
               List<String> includesList = null;
               if (includsArray != null && includsArray.length > 0) {
                   includesList = Arrays.asList(includsArray);    //构造列表对象
               } else {
                   return;
               }
               Method[] fromMethods = from.getClass().getDeclaredMethods();
               Method[] toMethods = to.getClass().getDeclaredMethods();
               Method fromMethod = null, toMethod = null;
               String fromMethodName = null, toMethodName = null;
               for (int i = 0; i < fromMethods.length; i++) {
                   fromMethod = fromMethods[i];
                   fromMethodName = fromMethod.getName();
                   if (!fromMethodName.contains("get"))
                       continue;
                   //排除列表检测
                   String str = fromMethodName.substring(3);
                   if (!includesList.contains(str.substring(0, 1).toLowerCase() + str.substring(1))) {
                       continue;
                   }
                   toMethodName = "set" + fromMethodName.substring(3);
                   toMethod = findMethodByName(toMethods, toMethodName);
                   if (toMethod == null)
                       continue;
                   Object value = fromMethod.invoke(from, new Object[0]);
                   if (value == null)
                       continue;
                   //集合类判空处理
                   if (value instanceof Collection) {
                       Collection newValue = (Collection) value;
                       if (newValue.size() <= 0)
                           continue;
                   }
                   toMethod.invoke(to, new Object[]{value});
               }
       }


       /**
        * 从方法数组中获取指定名称的方法
        *
        * @param methods
        * @param name
        * @return
        */
       public static Method findMethodByName(Method[] methods, String name) {
               for (int j = 0; j < methods.length; j++) {
                   if (methods[j].getName().equals(name))
                       return methods[j];
               }
               return null;
       }
}

最后

未完待续、敬请期待!
我的博客地址

img_1ee92a858822d3b1d90a45e40e7b1042.jpe
FullScreenDeveloper
目录
相关文章
|
7天前
|
Shell API Android开发
android queries属性
android queries属性
15 2
|
11天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
13天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
14天前
|
监控 Java Android开发
安卓应用开发:打造高效用户界面的五大策略
【4月更文挑战第29天】 在安卓应用开发的世界中,构建一个既美观又高效的用户界面(UI)对于吸引和保留用户至关重要。本文将深入探讨五种策略,这些策略可以帮助开发者优化安卓应用的UI性能。我们将从布局优化讲起,逐步过渡到绘制优化、内存管理、异步处理以及最终的用户交互细节调整。通过这些实践技巧,你将能够为用户提供流畅而直观的体验,确保你的应用在竞争激烈的市场中脱颖而出。
|
3天前
|
Java Android开发
Android开发--Intent-filter属性详解
Android开发--Intent-filter属性详解
|
4天前
|
物联网 Java 开发工具
安卓应用开发:打造未来移动生活
【5月更文挑战第10天】 随着科技的飞速发展,智能手机已成为我们日常生活中不可或缺的一部分。作为智能手机市场的两大巨头,安卓和iOS分别占据了一定的市场份额。在这篇文章中,我们将重点关注安卓应用开发,探讨如何利用先进的技术和创新思维,为用户打造更加便捷、智能的移动生活。文章将涵盖安卓应用开发的基本概念、关键技术、以及未来发展趋势等方面的内容。
|
4天前
|
XML Android开发 数据格式
Android五大布局对象---FrameLayout,LinearLayout ,Absolute
Android五大布局对象---FrameLayout,LinearLayout ,Absolute
|
5天前
|
Java API 开发工具
java与Android开发入门指南
java与Android开发入门指南
12 0
|
6天前
|
Android开发 Kotlin
Kotlin开发Android之基础问题记录
Kotlin开发Android之基础问题记录
16 1
|
6天前
|
Java Android开发
Android开发@IntDef完美替代Enum
Android开发@IntDef完美替代Enum
13 0