From 15acd796058ab8348996f6fda863ae37de5255c1 Mon Sep 17 00:00:00 2001 From: 王彬 Date: Tue, 29 Dec 2020 13:37:58 +0800 Subject: [PATCH] objectutil upgrade --- build.gradle | 2 +- src/main/java/com/taover/util/UtilObject.java | 441 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 2 files changed, 256 insertions(+), 187 deletions(-) diff --git a/build.gradle b/build.gradle index 2dece05..1cebfea 100644 --- a/build.gradle +++ b/build.gradle @@ -62,7 +62,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '1.1.122' + version '1.1.124' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/util/UtilObject.java b/src/main/java/com/taover/util/UtilObject.java index df8b105..f5f1525 100644 --- a/src/main/java/com/taover/util/UtilObject.java +++ b/src/main/java/com/taover/util/UtilObject.java @@ -3,6 +3,8 @@ package com.taover.util; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -11,376 +13,443 @@ public class UtilObject { /** * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同; * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer + * * @param source * @param dest * @param allowNull * @param exceptFieldNameArray * @return 如果source==dest或者source==null或者dest==null,则返回false */ - public static Object fieldCopy(Object source, Object dest, boolean allowNull, String exceptFieldNameArray[], boolean isNewInstance){ - //引用检查 - if(source==null || dest==null || source==dest){ + public static Object fieldCopy(Object source, Object dest, boolean allowNull, String exceptFieldNameArray[], + boolean isNewInstance) { + // 引用检查 + if (source == null || dest == null || source == dest) { return null; } - - //获取Field数组 - Field[] sourceField = source.getClass().getDeclaredFields(); - Field[] destField = dest.getClass().getDeclaredFields(); - - //获取目标对象 + + // 获取Field数组 + Field[] sourceField = getAndCachedFields(source.getClass()); + Field[] destField = getAndCachedFields(dest.getClass()); + + // 获取目标对象 String[] destFieldNameArray = new String[destField.length]; String[] destFieldTypeArray = new String[destField.length]; - for(int i=0; i cachedField = new HashMap(); + + private static Field[] getAndCachedFields(Class infoClass) { + String infoClassName = infoClass.getName(); + if (cachedField.containsKey(infoClassName)) { + return cachedField.get(infoClassName); + } else { + List recurseField = new ArrayList(); + loadClassFieldRecurse(infoClass, recurseField); + Field[] data = new Field[recurseField.size()]; + recurseField.toArray(data); + cachedField.put(infoClassName, data); + return data; + } + } + + private static void loadClassFieldRecurse(Class infoClass, List recurseField) { + // Object对象不处理 + if ("java.lang.Object".equals(infoClass.getName())) { + return; + } + + // 父类处理 + Class parentClass = infoClass.getSuperclass(); + if (parentClass != null || !"java.lang.Object".equals(parentClass.getName())) { + loadClassFieldRecurse(parentClass, recurseField); + } + + recurseField.addAll(Arrays.asList(infoClass.getDeclaredFields())); + } + /** * 将源对象source字段赋值到对象dest字段,条件1字段名相同,条件2字段类型相同; * 注:source与dest可以是两个没有关系的类,不接受基本封装类型,例如Integer + * * @param source * @param destClass * @param allowNull * @param exceptFieldNameArray * @return 如果source==null或者destClass==null,则返回false */ - public static E fieldCopy(Object source, Class destClass, boolean allowNull, String exceptFieldNameArray[]) throws Exception{ - //引用检查 - if(source==null || destClass==null){ + public static E fieldCopy(Object source, Class destClass, boolean allowNull, String exceptFieldNameArray[]) + throws Exception { + // 引用检查 + if (source == null || destClass == null) { return null; } - - //获取Field数组 - Field[] sourceField = source.getClass().getDeclaredFields(); - Field[] destField = destClass.getDeclaredFields(); - - //获取目标对象 + + // 获取Field数组 + Field[] sourceField = getAndCachedFields(source.getClass()); + Field[] destField = getAndCachedFields(destClass); + + // 获取目标对象 String[] destFieldNameArray = new String[destField.length]; String[] destFieldTypeArray = new String[destField.length]; - for(int i=0; i hello_word + * 在字符串驼峰命名转化成下划线形式 如 helloWord -> hello_word + * * @param source * @param isCopyNull * @param exceptFieldNameArray * @return */ - public static List objecFieldToListToLowerCase(Object source, boolean isCopyNull, String exceptFieldNameArray[]){ + public static List objecFieldToListToLowerCase(Object source, boolean isCopyNull, + String exceptFieldNameArray[]) { List result = new ArrayList(); - //参数检验 - if(source == null){ + // 参数检验 + if (source == null) { return result; } - - //获取Field数组 - Field[] sourceField = source.getClass().getDeclaredFields(); - //遍历源field数组,处理源field数组 - for(int i=0; i fieldToArray(Object source, boolean allowNull, String exceptFieldNameArray[]){ + public static List fieldToArray(Object source, boolean allowNull, String exceptFieldNameArray[]) { List result = new ArrayList(); - //参数检验 - if(source == null){ + // 参数检验 + if (source == null) { return result; } - - //获取Field数组 - Field[] sourceField = source.getClass().getDeclaredFields(); - //遍历源field数组,处理源field数组 - for(int i=0; i E mapToObject(Map dataMap, Class beanClass, boolean isCopyNull, String exceptFieldNameArray[]) throws Exception{ - if(beanClass == null){ + } + + public static E mapToObject(Map dataMap, Class beanClass, boolean isCopyNull, + String exceptFieldNameArray[]) throws Exception { + if (beanClass == null) { return null; } E result = beanClass.newInstance(); Iterator keyIter = dataMap.keySet().iterator(); - while(keyIter.hasNext()){ + while (keyIter.hasNext()) { String keyName = keyIter.next(); String camelName = UtilString.camelName(keyName); Object keyValue = dataMap.get(keyName); - - if(keyValue == null){ + + if (keyValue == null) { + continue; + } + + Field keyField = findDeclaredField(beanClass, camelName); + if (keyField == null) { continue; } - - Field keyField = beanClass.getDeclaredField(camelName); String keyTypeName = keyField.getType().getSimpleName(); - - //如果是final关键词修饰,则返回 - if(Modifier.isFinal(keyField.getModifiers())){ + + // 如果是final关键词修饰,则返回 + if (Modifier.isFinal(keyField.getModifiers())) { continue; } - - if(keyTypeName.equals("String")){ + + if (keyTypeName.equals("String")) { keyField.set(result, keyValue.toString()); - }else if(keyTypeName.equals("Integer")){ + } else if (keyTypeName.equals("Integer")) { keyField.set(result, Integer.valueOf(keyValue.toString())); } } return result; } - - public static E mapToObject(Map dataMap, Class beanClass) throws Exception{ - if(beanClass == null){ + + public static E mapToObject(Map dataMap, Class beanClass) throws Exception { + if (beanClass == null) { return null; } E result = beanClass.newInstance(); Iterator keyIter = dataMap.keySet().iterator(); - while(keyIter.hasNext()){ + while (keyIter.hasNext()) { String keyName = keyIter.next(); String camelName = UtilString.camelName(keyName); Object keyValue = dataMap.get(keyName); - - if(keyValue == null){ + + if (keyValue == null) { continue; } - + try { - Field keyField = beanClass.getDeclaredField(camelName); - - //如果是final关键词修饰,则返回 - if(Modifier.isFinal(keyField.getModifiers())){ + Field keyField = findDeclaredField(beanClass, camelName); + if (keyField == null) { + continue; + } + + // 如果是final关键词修饰,则返回 + if (Modifier.isFinal(keyField.getModifiers())) { continue; } - + keyField.setAccessible(true); keyField.set(result, keyValue); - } catch (Exception e) { } + } catch (Exception e) { + } } return result; } - - public static void main(String args[]){ - - class Temp2{ + + private static Field findDeclaredField(Class beanClass, String camelName) { + Field[] dataArr = getAndCachedFields(beanClass); + if (dataArr == null || dataArr.length == 0) { + return null; + } + for (Field item : dataArr) { + if (item.getName().equals(camelName)) { + return item; + } + } + return null; + } + + public static void main(String args[]) { + + class Temp2 { final Integer a; Integer c; - public Temp2(Integer a, Integer c){ + + public Temp2(Integer a, Integer c) { this.a = a; this.c = c; } + @Override - public String toString(){ - return "a="+a+":c="+c; + public String toString() { + return "a=" + a + ":c=" + c; } } - class Temp{ + class Temp extends Temp2 { Integer a; Integer b; - public Temp(Integer a, Integer b){ + + public Temp(Integer a, Integer b) { + super(a, b); this.a = a; this.b = b; } + @Override - public String toString(){ - return "a="+a+":b="+b; + public String toString() { + return "a=" + a + ":b=" + b; } } - - Temp temp1 = new Temp(null,2); - Temp temp2 = new Temp(3,4); - Temp temp3 = new Temp(5,6); - Temp2 temp4 = new Temp2(7,8); - + + Temp temp1 = new Temp(null, 2); + Temp temp2 = new Temp(3, 4); + Temp temp3 = new Temp(5, 6); + Temp2 temp4 = new Temp2(7, 8); + try { - Field a= temp4.getClass().getDeclaredField("a"); - System.out.print(Modifier.isFinal(a.getModifiers())); - } catch (NoSuchFieldException e) { - // TODO Auto-generated catch block - e.printStackTrace(); +//// Field a= temp4.getClass().getDeclaredField("a"); +//// System.out.print(Modifier.isFinal(a.getModifiers())); +// getAndCachedFields(Temp.class); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } + - /*fieldCopy(temp1, temp2, false, null,false); - System.out.println(temp1.toString()); - System.out.println(temp2.toString()); - - fieldCopy(temp3, temp4, false, null,false); - System.out.println(temp3.toString()); - System.out.println(temp4.toString()); - - List temp = UtilObject.fieldToArray(temp1, false, new String[]{"b"}); - for(int i=0; i temp = UtilObject.fieldToArray(temp1, false, new + String[]{"b"}); for(int i=0; i