package com.frank.utils;
import cn.hutool.core.util.RandomUtil;
import com.frank.reflect.Person;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BeanTools {
public static <T> T getInstance(Class<T> c) throws InstantiationException, IllegalAccessException, InvocationTargetException {
T t = c.newInstance();
for(Method method:t.getClass().getDeclaredMethods()){
if(method.getName().startsWith("set")){
Class<?> parameterType = method.getParameterTypes()[0];
Object randomData = getRandomData(parameterType);
method.invoke(t,randomData);
}
}
return t;
}
@Test
public static <T> T getRandomData(Class<T> t){
String name = t.getName();
if(name.endsWith("String")){
return (T) RandomUtil.randomString(10);
} else if (name.endsWith("Integer")) {
return (T)new Integer(RandomUtil.randomInt(10));
} else if (name.endsWith("Float")) {
return (T) "12.5";
}else{
return null;
}
}
}