cool.scx.jdbc.bean_builder.FieldSetter Maven / Gradle / Ivy
package cool.scx.jdbc.bean_builder;
import cool.scx.jdbc.type_handler.TypeHandler;
import cool.scx.util.reflect.FieldUtils;
import java.lang.reflect.Field;
import java.util.function.Function;
/**
* BeanBuilder interface.
*
* @author scx567888
* @version 0.2.1
*/
final class FieldSetter {
private final Field javaField;
private final String columnName;
private TypeHandler> typeHandler;
/**
*
*/
FieldSetter(Field javaField, String columnName) {
this.javaField = javaField;
this.columnName = columnName;
this.typeHandler = null;
}
static FieldSetter of(Field field, Function columnNameMapping) {
field.setAccessible(true);
var columnName = columnNameMapping.apply(field);
//若 columnNameMapping 提供空值, 则回退到 field.getName()
if (columnName == null) {
columnName = field.getName();
}
return new FieldSetter(field, columnName);
}
static FieldSetter[] ofArray(Class> type, Function columnNameMapping) {
var fields = FieldUtils.findFields(type);
var fieldSetters = new FieldSetter[fields.length];
for (int i = 0; i < fields.length; i = i + 1) {
fieldSetters[i] = of(fields[i], columnNameMapping);
}
return fieldSetters;
}
public void bindTypeHandler(TypeHandler
© 2015 - 2024 Weber Informatics LLC | Privacy Policy