mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
55b433c4ca
commit
12a3679c20
@ -133,7 +133,9 @@ public class DynaBean implements Cloneable, Serializable {
|
||||
} else {
|
||||
final PropDesc prop = BeanUtil.getBeanDesc(beanClass).getProp(fieldName);
|
||||
if (null == prop) {
|
||||
throw new BeanException("No public field or get method for {}", fieldName);
|
||||
// 节点字段不存在,类似于Map无key,返回null而非报错
|
||||
return null;
|
||||
//throw new BeanException("No public field or get method for {}", fieldName);
|
||||
}
|
||||
return (T) prop.getValue(bean);
|
||||
}
|
||||
|
@ -18,6 +18,8 @@ package org.dromara.hutool.core.bean.path.node;
|
||||
|
||||
import org.dromara.hutool.core.bean.DynaBean;
|
||||
import org.dromara.hutool.core.math.NumberUtil;
|
||||
import org.dromara.hutool.core.reflect.ClassUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
|
||||
/**
|
||||
* 处理名称节点或序号节点,如:
|
||||
@ -58,7 +60,12 @@ public class NameNode implements Node {
|
||||
if ("$".equals(name)) {
|
||||
return bean;
|
||||
}
|
||||
return DynaBean.of(bean).get(this.name);
|
||||
Object value = DynaBean.of(bean).get(this.name);
|
||||
if(null == value && StrUtil.lowerFirst(ClassUtil.getClassName(bean, true)).equals(this.name)){
|
||||
// 如果bean类名与属性名相同,则返回bean本身
|
||||
value = bean;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,7 +22,6 @@ import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.exception.HutoolException;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.math.NumberUtil;
|
||||
import org.dromara.hutool.core.text.StrPool;
|
||||
import org.dromara.hutool.core.text.placeholder.StrTemplate;
|
||||
@ -354,7 +353,7 @@ public class NamedPlaceholderStrTemplate extends StrTemplate {
|
||||
* @return 格式化字符串
|
||||
*/
|
||||
public String format(final Map<String, ?> map) {
|
||||
if (MapUtil.isEmpty(map)) {
|
||||
if (null == map) {
|
||||
return getTemplate();
|
||||
}
|
||||
return format(map::get, map::containsKey);
|
||||
|
@ -334,6 +334,21 @@ public class BeanUtilTest {
|
||||
assertEquals("sub名字", subName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPropertyWithClassNameTest() {
|
||||
final SubPerson person = new SubPerson();
|
||||
person.setAge(14);
|
||||
person.setOpenid("11213232");
|
||||
person.setName("测试A11");
|
||||
person.setSubName("sub名字");
|
||||
|
||||
// 获取Bean属性时,如果用户传入名称以对象名开头,则自动去掉对象名,获取剩余部分的属性值
|
||||
final Object name = BeanUtil.getProperty(person, "subPerson.name");
|
||||
assertEquals("测试A11", name);
|
||||
final Object subName = BeanUtil.getProperty(person, "subPerson.subName");
|
||||
assertEquals("sub名字", subName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public void getNullPropertyTest() {
|
||||
|
@ -43,6 +43,9 @@ public class DynaBeanTest {
|
||||
//执行指定方法
|
||||
final Object invoke = bean2.invoke("testMethod");
|
||||
Assertions.assertEquals("test for 李华", invoke);
|
||||
|
||||
// 不存在的字段测试
|
||||
Assertions.assertNull(bean.get("notExist"));
|
||||
}
|
||||
|
||||
|
||||
|
@ -190,7 +190,7 @@ public class TemplateContext {
|
||||
} else {
|
||||
// 模板中存在多个变量或模板填充,直接赋值为String
|
||||
// 没有找到值的变量保留原样
|
||||
cellValue = StrUtil.formatByBean(templateStr, rowDataBean, false);
|
||||
cellValue = StrUtil.formatByBean(templateStr, rowDataBean, true);
|
||||
if (ObjUtil.equals(cellValue, templateStr)) {
|
||||
// 模板无修改,说明没有变量替换,跳过填充
|
||||
return false;
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.dromara.hutool.poi.excel.writer;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.poi.excel.ExcelUtil;
|
||||
@ -77,6 +79,28 @@ public class TemplateWriterTest {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 错位数据,即变量不在一行上
|
||||
*/
|
||||
@Test
|
||||
void writeBeanTest() {
|
||||
final ExcelWriter writer = ExcelUtil.getWriter("templateWithFooterNoneOneLine.xlsx");
|
||||
//writer.getConfig().setInsertRow(true);
|
||||
|
||||
// 单个替换的变量
|
||||
writer.fillOnce(MapUtil
|
||||
.builder("date", (Object)"2024-01-01")
|
||||
.build());
|
||||
|
||||
// 列表替换
|
||||
for (int i = 0; i < 10; i++) {
|
||||
writer.writeRow(new User("李四", 25, "某个街道"), false);
|
||||
}
|
||||
|
||||
writer.flush(FileUtil.file(targetDir + "templateWriteWithBeanResult.xlsx"), true);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private static Map<String, Object> createRow(){
|
||||
return MapUtil
|
||||
.builder("user.name", (Object)"张三")
|
||||
@ -84,9 +108,16 @@ public class TemplateWriterTest {
|
||||
.put("year", 2024)
|
||||
.put("month", 8)
|
||||
.put("day", 24)
|
||||
.put("day", 24)
|
||||
.put("user.area123", "某某市")
|
||||
.put("invalid", "不替换")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
private static class User{
|
||||
private String name;
|
||||
private int age;
|
||||
private String area123;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user