This commit is contained in:
Looly 2022-06-01 12:10:34 +08:00
parent e12c5f170a
commit 8fef1ca19f
5 changed files with 187 additions and 56 deletions

View File

@ -465,36 +465,6 @@ public class BeanUtil {
return toBean(source, clazz, null);
}
/**
* 对象或Map转Bean忽略字段转换时发生的异常
*
* @param <T> 转换的Bean类型
* @param source Bean对象或Map
* @param clazz 目标的Bean类型
* @return Bean对象
* @since 5.4.0
*/
public static <T> T toBeanIgnoreError(final Object source, final Class<T> clazz) {
return toBean(source, clazz, CopyOptions.create().setIgnoreError(true));
}
/**
* 对象或Map转Bean忽略字段转换时发生的异常
*
* @param <T> 转换的Bean类型
* @param source Bean对象或Map
* @param clazz 目标的Bean类型
* @param ignoreError 是否忽略注入错误
* @return Bean对象
* @since 5.4.0
*/
public static <T> T toBeanIgnoreCase(final Object source, final Class<T> clazz, final boolean ignoreError) {
return toBean(source, clazz,
CopyOptions.create()
.setIgnoreCase(true)
.setIgnoreError(ignoreError));
}
/**
* 对象或Map转Bean
*
@ -513,7 +483,7 @@ public class BeanUtil {
* 对象或Map转Bean
*
* @param <T> 转换的Bean类型
* @param source Bean对象或Map
* @param source Bean对象或Map{@link ValueProvider}
* @param targetSupplier 目标的Bean创建器
* @param options 属性拷贝选项
* @return Bean对象
@ -528,22 +498,6 @@ public class BeanUtil {
return target;
}
/**
* ServletRequest 参数转Bean
*
* @param <T> Bean类型
* @param beanClass Bean Class
* @param valueProvider 值提供者
* @param copyOptions 拷贝选项 {@link CopyOptions}
* @return Bean
*/
public static <T> T toBean(final Class<T> beanClass, final ValueProvider<String> valueProvider, final CopyOptions copyOptions) {
if (null == beanClass || null == valueProvider) {
return null;
}
return fillBean(ConstructorUtil.newInstanceIfPossible(beanClass), valueProvider, copyOptions);
}
/**
* 填充Bean的核心方法
*
@ -560,7 +514,6 @@ public class BeanUtil {
return BeanCopier.create(valueProvider, bean, copyOptions).copy();
}
// --------------------------------------------------------------------------------------------- beanToMap
/**
@ -575,7 +528,7 @@ public class BeanUtil {
public static Map<String, Object> beanToMap(final Object bean, final String... properties) {
int mapSize = 16;
Editor<String> keyEditor = null;
if(ArrayUtil.isNotEmpty(properties)){
if (ArrayUtil.isNotEmpty(properties)) {
mapSize = properties.length;
final Set<String> propertiesSet = SetUtil.of(properties);
keyEditor = property -> propertiesSet.contains(property) ? property : null;
@ -685,7 +638,7 @@ public class BeanUtil {
* @return 目标对象
*/
public static <T> T copyProperties(final Object source, final Class<T> tClass, final String... ignoreProperties) {
if(null == source){
if (null == source) {
return null;
}
final T target = ConstructorUtil.newInstanceIfPossible(tClass);
@ -725,7 +678,7 @@ public class BeanUtil {
* @param copyOptions 拷贝选项 {@link CopyOptions}
*/
public static void copyProperties(final Object source, final Object target, final CopyOptions copyOptions) {
if(null == source || null == target){
if (null == source || null == target) {
return;
}
BeanCopier.create(source, target, ObjUtil.defaultIfNull(copyOptions, CopyOptions::create)).copy();

View File

@ -2,6 +2,7 @@ package cn.hutool.core.map;
import cn.hutool.core.bean.BeanPath;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.SetUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
@ -246,7 +247,7 @@ public class Dict extends LinkedHashMap<String, Object> implements BasicTypeGett
* @return vo
*/
public <T> T toBeanIgnoreCase(final Class<T> clazz) {
return BeanUtil.toBeanIgnoreCase(this, clazz, false);
return BeanUtil.toBean(this, clazz, CopyOptions.create().setIgnoreCase(true));
}
/**

View File

@ -116,7 +116,7 @@ public class BeanUtilTest {
// 错误的类型此处忽略
map.put("age", "aaaaaa");
final Person person = BeanUtil.toBeanIgnoreError(map, Person.class);
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setIgnoreError(true));
Assert.assertEquals("Joe", person.getName());
// 错误的类型不copy这个字段使用对象创建的默认值
Assert.assertEquals(0, person.getAge());
@ -128,7 +128,7 @@ public class BeanUtilTest {
map.put("Name", "Joe");
map.put("aGe", 12);
final Person person = BeanUtil.toBeanIgnoreCase(map, Person.class, false);
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setIgnoreCase(true));
Assert.assertEquals("Joe", person.getName());
Assert.assertEquals(12, person.getAge());
}

View File

@ -0,0 +1,176 @@
package cn.hutool.db.meta;
import cn.hutool.db.DbRuntimeException;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
/**
* {@link ResultSetMetaData}中某一列的元数据信息
*
* @author looly
*/
public class ResultColumn {
private final boolean autoIncrement;
private final boolean caseSensitive;
private final boolean searchable;
private final boolean currency;
private final int nullable;
private final boolean signed;
private final int displaySize;
private final String label;
private final String name;
private final String schemaName;
private final int precision;
private final int scale;
private final String tableName;
private final String catalogName;
private final int type;
private final String typeName;
private final boolean readOnly;
private final boolean writable;
private final boolean definitelyWritable;
private final String className;
/**
* 构造
*
* @param metaData {@link ResultSetMetaData}
* @param columnIndexBase1 列序号从1开始即第一列为1第二列为2
* @throws DbRuntimeException SQLException包装
*/
public ResultColumn(final ResultSetMetaData metaData, final int columnIndexBase1) throws DbRuntimeException {
try {
this.autoIncrement = metaData.isAutoIncrement(columnIndexBase1);
this.caseSensitive = metaData.isCaseSensitive(columnIndexBase1);
this.searchable = metaData.isSearchable(columnIndexBase1);
this.currency = metaData.isCurrency(columnIndexBase1);
this.nullable = metaData.isNullable(columnIndexBase1);
this.signed = metaData.isSigned(columnIndexBase1);
this.displaySize = metaData.getColumnDisplaySize(columnIndexBase1);
this.label = metaData.getColumnLabel(columnIndexBase1);
this.name = metaData.getColumnName(columnIndexBase1);
this.schemaName = metaData.getSchemaName(columnIndexBase1);
this.precision = metaData.getPrecision(columnIndexBase1);
this.scale = metaData.getScale(columnIndexBase1);
this.tableName = metaData.getTableName(columnIndexBase1);
this.catalogName = metaData.getCatalogName(columnIndexBase1);
this.type = metaData.getColumnType(columnIndexBase1);
this.typeName = metaData.getColumnTypeName(columnIndexBase1);
this.readOnly = metaData.isReadOnly(columnIndexBase1);
this.writable = metaData.isWritable(columnIndexBase1);
this.definitelyWritable = metaData.isDefinitelyWritable(columnIndexBase1);
this.className = metaData.getColumnClassName(columnIndexBase1);
} catch (final SQLException e) {
throw new DbRuntimeException(e);
}
}
public boolean isAutoIncrement() {
return autoIncrement;
}
public boolean isCaseSensitive() {
return caseSensitive;
}
public boolean isSearchable() {
return searchable;
}
public boolean isCurrency() {
return currency;
}
public int getNullable() {
return nullable;
}
public ColumnNullable getNullableEnum() {
return ColumnNullable.of(getNullable());
}
public boolean isSigned() {
return signed;
}
public int getDisplaySize() {
return displaySize;
}
public String getLabel() {
return label;
}
public String getName() {
return name;
}
public String getSchemaName() {
return schemaName;
}
public int getPrecision() {
return precision;
}
public int getScale() {
return scale;
}
public String getTableName() {
return tableName;
}
public String getCatalogName() {
return catalogName;
}
public int getType() {
return type;
}
public String getTypeName() {
return typeName;
}
public boolean isReadOnly() {
return readOnly;
}
public boolean isWritable() {
return writable;
}
public boolean isDefinitelyWritable() {
return definitelyWritable;
}
public String getClassName() {
return className;
}
public static enum ColumnNullable {
NO_NULLS(ResultSetMetaData.columnNoNulls),
NULLABLE(ResultSetMetaData.columnNullable),
UNKNOWN(ResultSetMetaData.columnNullableUnknown);
final int value;
private ColumnNullable(final int value) {
this.value = value;
}
public static ColumnNullable of(final int nullable) {
switch (nullable) {
case ResultSetMetaData.columnNoNulls:
return NO_NULLS;
case ResultSetMetaData.columnNullable:
return NULLABLE;
default:
return UNKNOWN;
}
}
}
}

View File

@ -1,6 +1,7 @@
package cn.hutool.poi.csv;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import java.util.Collection;
@ -98,7 +99,7 @@ public final class CsvRow implements List<String> {
}
/**
* 一行数据转换为Bean对象
* 一行数据转换为Bean对象忽略转换错误
*
* @param <T> Bean类型
* @param clazz bean类
@ -106,7 +107,7 @@ public final class CsvRow implements List<String> {
* @since 5.3.6
*/
public <T> T toBean(final Class<T> clazz){
return BeanUtil.toBeanIgnoreError(getFieldMap(), clazz);
return BeanUtil.toBean(getFieldMap(), clazz, CopyOptions.create().setIgnoreError(true));
}
/**