org.apache.commons.beanutils.BaseRowPropertyDescriptor Maven / Gradle / Ivy
package org.apache.commons.beanutils;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import com.google.common.base.Throwables;
import gu.sql2java.BaseBean;
import gu.sql2java.RowMetaData;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* 实现对{@link gu.sql2java.BaseBean}接口实例的属性封装
* @author guyadong
*
*/
public class BaseRowPropertyDescriptor extends PropertyDescriptor implements MorePropertyDescriptor {
private final Method readMethod;
private final Method writeMethod;
private final Class> propertyType;
public BaseRowPropertyDescriptor(Class> propertyType, String name)
throws IntrospectionException {
super(checkNotNull(name,"field is null"), null, null);
this.propertyType = checkNotNull(propertyType,"propertyType of %s is null",name);
try {
readMethod = BaseRowPropertyDescriptor.class.getMethod("readMethod", Object.class,String.class);
writeMethod = BaseRowPropertyDescriptor.class.getMethod("writeMethod", Object.class,String.class,Object.class);
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
public BaseRowPropertyDescriptor(RowMetaData rowMetaData, String name)
throws IntrospectionException {
this(checkNotNull(rowMetaData,"rowMetaData is null").columnTypeOf(name), name);
}
/**
* 字段读取方法实现
* @param bean 读取目标对象
* @param name 字段名
* @return 字段值
*/
public static Object readMethod(Object bean,String name){
if(bean instanceof BaseBean){
return ((BaseBean)bean).getValue(name);
}
return null;
}
/**
* 字段写入方法实现
* @param bean 写入目标对象
* @param name 字段名
* @param value 要写入的值
*/
public static void writeMethod(Object bean,String name,Object value){
if(bean instanceof BaseBean){
((BaseBean)bean).setValue(name, value);
}
}
@Override
public Method getReadMethod() {
return readMethod;
}
@Override
public Method getWriteMethod() {
return writeMethod;
}
@Override
public Class> getPropertyType() {
return propertyType;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy