diff --git a/common/pom.xml b/common/pom.xml index a703a9741f26918865cb59b122d130b13620650e..8f587e4ddf816b3dfaf54cde689682304738eb4c 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -294,6 +294,13 @@ org.springframework spring-test + + + com.jeesite + jeesite-framework + 4.1.8-SNAPSHOT + compile + diff --git a/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java b/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java index 40666582ab6b485ea6bd73f6da924f1da8335234..4970602696fab0f756e33cdebc429c5b6aa753ab 100644 --- a/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java +++ b/common/src/main/java/com/jeesite/common/reflect/ReflectUtils.java @@ -14,6 +14,7 @@ import java.lang.reflect.Type; import java.util.Date; import java.util.Map; +import com.jeesite.common.entity.DataEntity; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.apache.poi.ss.usermodel.DateUtil; @@ -88,7 +89,46 @@ public class ReflectUtils { } } } - + + + @SuppressWarnings("unchecked") + public static void invokeSetterV2(Object obj, String propertyName, E value) throws NoSuchFieldException, IllegalAccessException, InstantiationException { + Object object = obj; + String[] names = StringUtils.split(propertyName, "."); + for (int i = 0; i < names.length; i++) { + if (i < names.length - 1) { + if (obj instanceof Map) { + object = ((Map) obj).get(names[i]); + } else { + assert object != null; + Field declaredField = object.getClass().getDeclaredField(names[i]); + declaredField.setAccessible(true); + Object o1 = declaredField.get(object); + if (o1 == null) { + Class type = declaredField.getType(); + Object o = type.newInstance(); + if (o instanceof DataEntity) { + declaredField.set(object, o); + object = o; + continue; + } + } + object = o1; + } + } else { + if (obj instanceof Map) { + ((Map) obj).put(names[i], value); + } else { + String methodName = SETTER_PREFIX + StringUtils.capitalize(names[i]); + invokeMethodByName(object, methodName, new Object[]{value}); + } + } + } + //赋值给obj,让obj拥有值 + obj = object; + } + + /** * 直接读取对象属性值,无视private/protected修饰符,不经过getter函数 */ diff --git a/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java b/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java index 72a852ac4d0bb234b39ce7d8cd86fd81d2a38d02..6533563c670c9aef8d7ec5c5070d234409c6385a 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java @@ -318,7 +318,7 @@ public class ExcelImport implements Closeable { * @param cls 导入对象类型 * @param groups 导入分组 */ - public List getDataList(Class cls, String... groups) throws InstantiationException, IllegalAccessException{ + public List getDataList(Class cls, String... groups) throws InstantiationException, IllegalAccessException, NoSuchFieldException { return getDataList(cls, false, groups); } @@ -328,7 +328,7 @@ public class ExcelImport implements Closeable { * @param isThrowException 遇见错误是否抛出异常 * @param groups 导入分组 */ - public List getDataList(Class cls, final boolean isThrowException, String... groups) throws InstantiationException, IllegalAccessException{ + public List getDataList(Class cls, final boolean isThrowException, String... groups) throws InstantiationException, IllegalAccessException, NoSuchFieldException { return getDataList(cls, new MethodCallback() { @Override public Object execute(Object... params) { @@ -348,7 +348,7 @@ public class ExcelImport implements Closeable { * @param isThrowException 遇见错误是否抛出异常 * @param groups 导入分组 */ - public List getDataList(Class cls, MethodCallback exceptionCallback, String... groups) throws InstantiationException, IllegalAccessException{ + public List getDataList(Class cls, MethodCallback exceptionCallback, String... groups) throws InstantiationException, IllegalAccessException, NoSuchFieldException { List annotationList = ListUtils.newArrayList(); // Get annotation field Field[] fs = cls.getDeclaredFields(); @@ -475,7 +475,7 @@ public class ExcelImport implements Closeable { } // set entity value if (StringUtils.isNotBlank(ef.attrName())){ - ReflectUtils.invokeSetter(e, ef.attrName(), val); + ReflectUtils.invokeSetterV2(e, ef.attrName(), val); }else{ if (os[1] instanceof Field){ ReflectUtils.invokeSetter(e, ((Field)os[1]).getName(), val);