gu.sql2java.excel.BaseBeanParser Maven / Gradle / Ivy
package gu.sql2java.excel;
import static com.google.common.base.Preconditions.checkNotNull;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import com.google.common.base.Throwables;
import gu.sql2java.BaseBean;
/**
* {@link CustomBeanParser}接口实现,将Map转为{@link #beanClass}指定的BaseBean实例
* @author guyadong
*
* @param
* @since 3.30.0
*/
public class BaseBeanParser implements CustomBeanParser{
private final Class beanClass;
public BaseBeanParser(Class beanClass) {
this.beanClass = checkNotNull(beanClass,"beanClass is null") ;
}
@SuppressWarnings("unchecked")
protected BaseBeanParser() {
Type superClass = getClass().getGenericSuperclass();
this.beanClass = (Class) getRawClass(((ParameterizedType) superClass).getActualTypeArguments()[0]);
}
private static Class> getRawClass(Type type){
if(type instanceof Class>){
return (Class>) type;
} else if(type instanceof ParameterizedType){
return getRawClass(((ParameterizedType) type).getRawType());
} else{
throw new IllegalArgumentException("invalid type");
}
}
@Override
public Object parse(Map input) {
try {
return beanClass.newInstance().copyNoFilter(input);
} catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
}