Merge remote-tracking branch 'origin/v5-dev' into v5-dev

This commit is contained in:
achao 2021-11-28 18:34:27 +08:00
commit c1c80b3f50
14 changed files with 203 additions and 31 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.17 (2021-11-24)
# 5.7.17 (2021-11-26)
### 🐣新特性
* 【core 】 增加AsyncUtilpr#457@Gitee
@ -25,11 +25,15 @@
* 【db 】 Db.executeBatch标记一个重载为弃用issue#I4JIPH@Gitee
* 【core 】 增加CharSequenceUtil.subPreGbk重载issue#I4JO2E@Gitee
* 【core 】 ReflectUtil.getMethod排除桥接方法pr#1965@Github
* 【http 】 completeFileNameFromHeader在使用path为路径时自动解码issue#I4K0FS@Gitee
* 【core 】 CopyOptions增加override配置issue#I4JQ1N@Gitee
* 【poi 】 SheetRidReader可以获取所有sheet名issue#I4JA3M@Gitee
*
### 🐞Bug修复
* 【core 】 修复FileResource构造fileName参数无效问题issue#1942@Github
* 【cache 】 修复WeakCache键值强关联导致的无法回收问题issue#1953@Github
* 【core 】 修复ZipUtil相对路径父路径获取null问题issue#1961@Github
* 【http 】 修复HttpUtil.normalizeParams未判空导致的问题issue#1975@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -121,18 +121,19 @@ public class PropDesc {
/**
* 检查属性是否可读即是否可以通过{@link #getValue(Object)}获取到值
*
* @param checkTransient 是否检查Transient关键字或注解
* @return 是否可读
* @since 5.4.2
*/
public boolean isReadable(boolean checkTransient){
public boolean isReadable(boolean checkTransient) {
// 检查是否有getter方法或是否为public修饰
if(null == this.getter && false == ModifierUtil.isPublic(this.field)){
if (null == this.getter && false == ModifierUtil.isPublic(this.field)) {
return false;
}
// 检查transient关键字和@Transient注解
if(checkTransient && isTransientForGet()){
if (checkTransient && isTransientForGet()) {
return false;
}
@ -164,7 +165,7 @@ public class PropDesc {
* 首先调用字段对应的Getter方法获取值如果Getter方法不存在则判断字段如果为public则直接获取字段值
*
* @param bean Bean对象
* @param targetType 返回属性值需要转换的类型null表示不转换
* @param targetType 返回属性值需要转换的类型null表示不转换
* @param ignoreError 是否忽略错误包括转换错误和注入错误
* @return this
* @since 5.4.2
@ -190,18 +191,19 @@ public class PropDesc {
/**
* 检查属性是否可读即是否可以通过{@link #getValue(Object)}获取到值
*
* @param checkTransient 是否检查Transient关键字或注解
* @return 是否可读
* @since 5.4.2
*/
public boolean isWritable(boolean checkTransient){
public boolean isWritable(boolean checkTransient) {
// 检查是否有getter方法或是否为public修饰
if(null == this.setter && false == ModifierUtil.isPublic(this.field)){
if (null == this.setter && false == ModifierUtil.isPublic(this.field)) {
return false;
}
// 检查transient关键字和@Transient注解
if(checkTransient && isTransientForSet()){
if (checkTransient && isTransientForSet()) {
return false;
}
@ -239,7 +241,28 @@ public class PropDesc {
* @since 5.4.2
*/
public PropDesc setValue(Object bean, Object value, boolean ignoreNull, boolean ignoreError) {
if (ignoreNull && null == value) {
return setValue(bean, value, ignoreNull, ignoreError, true);
}
/**
* 设置属性值可以自动转换字段类型为目标类型
*
* @param bean Bean对象
* @param value 属性值可以为任意类型
* @param ignoreNull 是否忽略{@code null}true表示忽略
* @param ignoreError 是否忽略错误包括转换错误和注入错误
* @param override 是否覆盖目标值如果不覆盖会先读取bean的值{@code null}则写否则忽略如果覆盖则不判断直接写
* @return this
* @since 5.7.17
*/
public PropDesc setValue(Object bean, Object value, boolean ignoreNull, boolean ignoreError, boolean override) {
if (null == value && ignoreNull) {
return this;
}
// issue#I4JQ1N@Gitee
// 非覆盖模式下如果目标值存在则跳过
if (false == override && null != getValue(bean)) {
return this;
}

View File

@ -140,13 +140,44 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
* Map转Map
*
* @param source 源Map
* @param dest 目标Map
* @param targetMap 目标Map
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void mapToMap(Map source, Map dest) {
if (null != dest && null != source) {
dest.putAll(source);
}
private void mapToMap(Map source, Map targetMap) {
source.forEach((key, value)->{
final CopyOptions copyOptions = this.copyOptions;
final HashSet<String> ignoreSet = (null != copyOptions.ignoreProperties) ? CollUtil.newHashSet(copyOptions.ignoreProperties) : null;
// issue#I4JQ1N@Gitee
// 非覆盖模式下如果目标值存在则跳过
if(false == copyOptions.override && null != targetMap.get(key)){
return;
}
if(key instanceof CharSequence){
if (CollUtil.contains(ignoreSet, key)) {
// 目标属性值被忽略或值提供者无此key时跳过
return;
}
// 对key做映射映射后为null的忽略之
key = copyOptions.editFieldName(copyOptions.getMappedFieldName(key.toString(), false));
if(null == key){
return;
}
value = copyOptions.editFieldValue(key.toString(), value);
}
if ((null == value && copyOptions.ignoreNullValue) || source == value) {
// 当允许跳过空时跳过
//值不能为bean本身防止循环引用此类也跳过
return;
}
targetMap.put(key, value);
});
}
/**
@ -158,11 +189,11 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void beanToMap(Object bean, Map targetMap) {
final HashSet<String> ignoreSet = (null != copyOptions.ignoreProperties) ? CollUtil.newHashSet(copyOptions.ignoreProperties) : null;
final CopyOptions copyOptions = this.copyOptions;
final HashSet<String> ignoreSet = (null != copyOptions.ignoreProperties) ? CollUtil.newHashSet(copyOptions.ignoreProperties) : null;
BeanUtil.descForEach(bean.getClass(), (prop)->{
if(false == prop.isReadable(copyOptions.isTransientSupport())){
if(false == prop.isReadable(copyOptions.transientSupport)){
// 忽略的属性跳过之
return;
}
@ -178,6 +209,12 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
return;
}
// issue#I4JQ1N@Gitee
// 非覆盖模式下如果目标值存在则跳过
if(false == copyOptions.override && null != targetMap.get(key)){
return;
}
Object value;
try {
value = prop.getValue(bean);
@ -230,7 +267,7 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
// 遍历目标bean的所有属性
BeanUtil.descForEach(actualEditable, (prop)->{
if(false == prop.isWritable(this.copyOptions.isTransientSupport())){
if(false == prop.isWritable(this.copyOptions.transientSupport)){
// 字段不可写跳过之
return;
}
@ -270,7 +307,7 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
return;
}
prop.setValue(bean, value, copyOptions.ignoreNullValue, copyOptions.ignoreError);
prop.setValue(bean, value, copyOptions.ignoreNullValue, copyOptions.ignoreError, copyOptions.override);
});
}
}

View File

@ -65,7 +65,11 @@ public class CopyOptions implements Serializable {
/**
* 是否支持transient关键字修饰和@Transient注解如果支持被修饰的字段或方法对应的字段将被忽略
*/
private boolean transientSupport = true;
protected boolean transientSupport = true;
/**
* 是否覆盖目标值如果不覆盖会先读取目标对象的值{@code null}则写否则忽略如果覆盖则不判断直接写
*/
protected boolean override = true;
/**
* 创建拷贝选项
@ -259,7 +263,9 @@ public class CopyOptions implements Serializable {
*
* @return 是否支持
* @since 5.4.2
* @deprecated 无需此方法内部使用直接调用属性
*/
@Deprecated
public boolean isTransientSupport() {
return this.transientSupport;
}
@ -276,6 +282,18 @@ public class CopyOptions implements Serializable {
return this;
}
/**
* 设置是否覆盖目标值如果不覆盖会先读取目标对象的值{@code null}则写否则忽略如果覆盖则不判断直接写
*
* @param override 是否覆盖目标值
* @return this
* @since 5.7.17
*/
public CopyOptions setOverride(boolean override) {
this.override = override;
return this;
}
/**
* 获得映射后的字段名<br>
* 当非反向则根据源字段名获取目标字段名反之根据目标字段名获取源字段名

View File

@ -1,7 +1,7 @@
package cn.hutool.core.collection;
/**
* 集合相关工具类包括数组{@link CollUtil} 的别名工具
* 集合相关工具类包括数组 {@link CollUtil} 的别名工具
*
* @author xiaoleilu
* @see CollUtil

View File

@ -253,8 +253,10 @@ public class Opt<T> {
* String hutool = Opt.ofBlankAble("hutool").mapOrElse(String::toUpperCase, () -> Console.log("yes")).mapOrElse(String::intern, () -> Console.log("Value is not present~")).get();
* }</pre>
*
* @param <U> map后新的类型
* @param mapper 包裹里的值存在时的操作
* @param emptyAction 包裹里的值不存在时的操作
* @return 新的类型的Opt
* @throws NullPointerException 如果包裹里的值存在时执行的操作为 {@code null}, 或者包裹里的值不存在时的操作为 {@code null}则抛出{@code NPE}
*/
public <U> Opt<U> mapOrElse(Function<? super T, ? extends U> mapper, VoidFunc0 emptyAction) {

View File

@ -154,7 +154,7 @@ public class IdcardUtil {
* @return 是否有效
*/
public static boolean isValidCard(String idCard) {
if(StrUtil.isBlank(idCard)){
if (StrUtil.isBlank(idCard)) {
return false;
}
@ -201,9 +201,7 @@ public class IdcardUtil {
* <li>余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2</li>
* <li>通过上面得知如果余数是2就会在身份证的第18位数字上出现罗马数字的如果余数是10身份证的最后一位号码就是2</li>
* </ol>
*
<p>
* <ol>
* <ol>
* <li>香港人在大陆的身份证810000开头同样可以直接获取到 性别出生日期</li>
* <li>81000019980902013X: 文绎循 1998-09-02</li>
* <li>810000201011210153: 辛烨 2010-11-21</li>
@ -218,7 +216,6 @@ public class IdcardUtil {
* <li>830000194609150010: 苏建文 1946-09-14</li>
* <li>83000019810715006X: 刁婉琇 1981-07-15</li>
* </ol>
* </p>
*
* @param idcard 待验证的身份证
* @return 是否有效的18位身份证忽略x的大小写
@ -255,7 +252,7 @@ public class IdcardUtil {
* <li>通过上面得知如果余数是2就会在身份证的第18位数字上出现罗马数字的如果余数是10身份证的最后一位号码就是2</li>
* </ol>
*
* @param idcard 待验证的身份证
* @param idcard 待验证的身份证
* @param ignoreCase 是否忽略大小写{@code true}则忽略X大小写否则严格匹配大写
* @return 是否有效的18位身份证
* @since 5.5.7
@ -576,7 +573,7 @@ public class IdcardUtil {
*/
public static String getProvinceByIdCard(String idcard) {
final String code = getProvinceCodeByIdCard(idcard);
if(StrUtil.isNotBlank(code)){
if (StrUtil.isNotBlank(code)) {
return CITY_CODES.get(code);
}
return null;
@ -617,7 +614,7 @@ public class IdcardUtil {
* @return {@link Idcard}
* @since 5.4.3
*/
public static Idcard getIdcardInfo(String idcard){
public static Idcard getIdcardInfo(String idcard) {
return new Idcard(idcard);
}
@ -761,6 +758,7 @@ public class IdcardUtil {
/**
* 获取年龄
*
* @return 年龄
*/
public int getAge() {

View File

@ -0,0 +1,50 @@
package cn.hutool.core.bean.copier;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
public class BeanCopierTest {
/**
* 测试在非覆盖模式下目标对象有值则不覆盖
*/
@Test
public void beanToBeanNotOverrideTest() {
final A a = new A();
a.setValue("123");
final B b = new B();
b.setValue("abc");
final BeanCopier<B> copier = BeanCopier.create(a, b, CopyOptions.create().setOverride(false));
copier.copy();
Assert.assertEquals("abc", b.getValue());
}
/**
* 测试在覆盖模式下目标对象值被覆盖
*/
@Test
public void beanToBeanOverrideTest() {
final A a = new A();
a.setValue("123");
final B b = new B();
b.setValue("abc");
final BeanCopier<B> copier = BeanCopier.create(a, b, CopyOptions.create());
copier.copy();
Assert.assertEquals("123", b.getValue());
}
@Data
private static class A {
private String value;
}
@Data
private static class B {
private String value;
}
}

View File

@ -182,6 +182,12 @@ public class ArrayUtilTest {
Assert.assertEquals(9, range[9]);
}
@Test(expected = NegativeArraySizeException.class)
public void rangeMinTest() {
//noinspection ResultOfMethodCallIgnored
ArrayUtil.range(0, Integer.MIN_VALUE);
}
@Test
public void maxTest() {
int max = ArrayUtil.max(1, 2, 13, 4, 5);

View File

@ -7,7 +7,6 @@ import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
@ -434,7 +433,10 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
fileName = StrUtil.subSuf(path, path.lastIndexOf('/') + 1);
if (StrUtil.isBlank(fileName)) {
// 编码后的路径做为文件名
fileName = URLUtil.encodeQuery(path, CharsetUtil.CHARSET_UTF_8);
fileName = URLUtil.encodeQuery(path, charset);
} else {
// issue#I4K0FS@Gitee
fileName = URLUtil.decode(fileName, charset);
}
}
return FileUtil.file(targetFileOrDir, fileName);

View File

@ -539,6 +539,9 @@ public class HttpUtil {
* @since 4.5.2
*/
public static String normalizeParams(String paramPart, Charset charset) {
if(StrUtil.isEmpty(paramPart)){
return paramPart;
}
final StrBuilder builder = StrBuilder.create(paramPart.length() + 16);
final int len = paramPart.length();
String name = null;

View File

@ -302,6 +302,12 @@ public class HttpUtilTest {
Assert.assertEquals("%E5%8F%82%E6%95%B0", encodeResult);
}
@Test
public void normalizeBlankParamsTest() {
String encodeResult = HttpUtil.normalizeParams("", CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals("", encodeResult);
}
@Test
public void getMimeTypeTest() {
String mimeType = HttpUtil.getMimeType("aaa.aaa");

View File

@ -202,7 +202,7 @@ public class Excel07SaxReader implements ExcelSaxReader<Excel07SaxReader> {
}
// sheetIndex需转换为rid
final SheetRidReader ridReader = new SheetRidReader().read(xssfReader);
final SheetRidReader ridReader = SheetRidReader.parse(xssfReader);
if (StrUtil.startWithIgnoreCase(idOrRidOrSheetName, SHEET_NAME_PREFIX)) {
// name:开头的被认为是sheet名称直接处理

View File

@ -1,6 +1,7 @@
package cn.hutool.poi.excel.sax;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
@ -13,6 +14,7 @@ import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
@ -32,6 +34,17 @@ import java.util.Map;
*/
public class SheetRidReader extends DefaultHandler {
/**
* {@link XSSFReader}中解析sheet名sheet id等相关信息
*
* @param reader {@link XSSFReader}
* @return SheetRidReader
* @since 5.7.17
*/
public static SheetRidReader parse(XSSFReader reader) {
return new SheetRidReader().read(reader);
}
private final static String TAG_NAME = "sheet";
private final static String RID_ATTR = "r:id";
private final static String SHEET_ID_ATTR = "sheetId";
@ -137,6 +150,16 @@ public class SheetRidReader extends DefaultHandler {
return null;
}
/**
* 获取所有sheet名称
*
* @return sheet名称
* @since 5.7.17
*/
public List<String> getSheetNames() {
return ListUtil.toList(this.NAME_RID_MAP.keySet());
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (TAG_NAME.equalsIgnoreCase(localName)) {