java反射为各参数赋随机初始值
import org. apache. commons. lang3. StringUtils;
import java. lang. reflect. Field;
import java. lang. reflect. Method;
import java. lang. reflect. ParameterizedType;
import java. lang. reflect. Type;
import java. math. BigDecimal;
import java. util. * ;
public class Initparam {
private static final int arrayNum = 3 ;
public static void main ( String[ ] args) throws Exception{
BizReq< Animal> bizReq = new BizReq < > ( ) ;
setAllParameter ( bizReq, Animal. class ) ;
System. out. println ( bizReq) ;
}
private static < T > void setAllParameter ( BizReq< T > bizReq, Class< T > clazz) throws Exception{
T t = clazz. getDeclaredConstructor ( ) . newInstance ( ) ;
if ( bizReq. getBody ( ) == null ) {
bizReq. setBody ( getInstance ( clazz) ) ;
}
setParameter ( bizReq. getClass ( ) , bizReq) ;
}
private static void setParameter ( Class clazz , Object o) throws Exception{
Field[ ] declaredFields = clazz. getDeclaredFields ( ) ;
for ( Field field : declaredFields) {
field. setAccessible ( true ) ;
Object targetValue = field. get ( o) ;
if ( targetValue != null && StringUtils. isNotBlank ( targetValue. toString ( ) ) ) {
Class targetClass = targetValue. getClass ( ) ;
if ( isCommonClass ( targetValue) ) {
continue ;
}
setParameter ( targetClass, targetValue) ;
}
setNotBlankValue ( clazz, field. getName ( ) , o, null ) ;
}
return ;
}
private static void setNotBlankValue ( Class clazz , String name, Object o, Object defaultValue)
throws Exception{
String setMethod = "set" + name. substring ( 0 , 1 ) . toUpperCase ( ) + name. substring ( 1 ) ;
Field declaredField = clazz. getDeclaredField ( name) ;
Class< ? > type = declaredField. getType ( ) ;
if ( type. isAssignableFrom ( Object. class ) ) {
return ;
}
Object instanceValue = getInstance ( type) ;
Method method = clazz. getMethod ( setMethod, type) ;
if ( defaultValue != null ) {
method. invoke ( o, defaultValue) ;
setSuperClassValue ( clazz. getSuperclass ( ) , o) ;
return ;
}
if ( isCommonClass ( instanceValue) && ( ! ( instanceValue instanceof List ) ) ) {
method. invoke ( o, instanceValue) ;
setSuperClassValue ( clazz. getSuperclass ( ) , o) ;
return ;
}
if ( instanceValue instanceof List ) {
Type genericType = declaredField. getGenericType ( ) ;
if ( null == genericType) {
throw new Exception ( "未获取到list的泛型类型" ) ;
}
if ( genericType instanceof ParameterizedType ) {
ParameterizedType pt = ( ParameterizedType) genericType;
Class< ? > actualTypeArgument = ( Class< ? > ) pt. getActualTypeArguments ( ) [ 0 ] ;
Object actualType = getInstance ( actualTypeArgument) ;
method. invoke ( o, set2list ( actualType) ) ;
}
setSuperClassValue ( clazz. getSuperclass ( ) , o) ;
return ;
}
if ( true ) {
Field[ ] fields = type. getDeclaredFields ( ) ;
Object childObject = getInstance ( type) ;
for ( Field field : fields) {
field. setAccessible ( true ) ;
Object targetValue = field. get ( childObject) ;
if ( targetValue != null && StringUtils. isNotBlank ( targetValue. toString ( ) ) ) {
if ( isCommonClass ( targetValue) ) {
continue ;
}
setParameter ( targetValue. getClass ( ) , targetValue) ;
}
setNotBlankValue ( type, field. getName ( ) , childObject, null ) ;
setSuperClassValue ( clazz. getSuperclass ( ) , o) ;
}
setNotBlankValue ( clazz, name, o, childObject) ;
}
}
private static < T > List< T > set2list ( T t) throws Exception{
List< T > list = new ArrayList < > ( ) ;
if ( isCommonClass ( t) && ( ! ( t instanceof List ) ) ) {
for ( int i = 0 ; i < arrayNum; i++ ) {
list. add ( ( T ) getInstance ( t. getClass ( ) ) ) ;
}
return list;
}
Class< ? > clazz = t. getClass ( ) ;
Field[ ] field = clazz. getDeclaredFields ( ) ;
for ( int i = 0 ; i < arrayNum; i++ ) {
for ( int j = 0 ; j < field. length; j++ ) {
String fieldName = field[ j] . getName ( ) ;
setNotBlankValue ( clazz, fieldName, t, null ) ;
}
list. add ( t) ;
}
return list;
}
private static void setSuperClassValue ( Class clazz , Object o) throws Exception{
if ( clazz != null ) {
setParameter ( clazz, o) ;
}
}
private static < T > T getInstance ( Class< T > clazz) throws Exception{
if ( clazz. isAssignableFrom ( Object. class ) ) {
return ( T ) new Object ( ) ;
}
if ( clazz. isAssignableFrom ( String. class ) ) {
return ( T ) UUID . randomUUID ( ) . toString ( ) ;
}
if ( clazz. isAssignableFrom ( int. class ) ) {
return ( T ) ( Integer) ( new Random ( ) . nextInt ( 200000 ) ) ;
}
if ( clazz. isAssignableFrom ( Integer. class ) ) {
return ( T ) ( Integer) ( new Random ( ) . nextInt ( 200000 ) ) ;
}
if ( clazz. isAssignableFrom ( Date. class ) ) {
return ( T ) new Date ( ) ;
}
if ( clazz. isAssignableFrom ( boolean. class ) ) {
return ( T ) ( Boolean) true ;
}
if ( clazz. isAssignableFrom ( Boolean. class ) ) {
return ( T ) ( Boolean) true ;
}
if ( clazz. isAssignableFrom ( Double. class ) ) {
return ( T ) ( Double. valueOf ( new Random ( ) . nextDouble ( ) ) ) ;
}
if ( clazz. isAssignableFrom ( Float. class ) ) {
return ( T ) ( Float. valueOf ( new Random ( ) . nextFloat ( ) ) ) ;
}
if ( clazz. isAssignableFrom ( double. class ) ) {
return ( T ) ( Double. valueOf ( new Random ( ) . nextDouble ( ) ) ) ;
}
if ( clazz. isAssignableFrom ( Long. class ) ) {
return ( T ) ( Long. valueOf ( new Random ( ) . nextLong ( ) ) ) ;
}
if ( clazz. isAssignableFrom ( long. class ) ) {
return ( T ) ( Long. valueOf ( new Random ( ) . nextLong ( ) ) ) ;
}
if ( clazz. isAssignableFrom ( float. class ) ) {
return ( T ) ( Float. valueOf ( new Random ( ) . nextFloat ( ) ) ) ;
}
if ( clazz. isAssignableFrom ( BigDecimal. class ) ) {
return ( T ) new BigDecimal ( String. valueOf ( new Random ( ) . nextInt ( 200000 ) ) ) ;
}
if ( clazz. isAssignableFrom ( List. class ) ) {
return ( T ) new ArrayList < > ( ) ;
}
if ( clazz. isAssignableFrom ( String[ ] . class ) ) {
String[ ] strs = new String [ arrayNum] ;
for ( int i = 0 ; i < arrayNum; i++ ) {
strs[ i] = UUID . randomUUID ( ) . toString ( ) ;
}
return ( T ) strs;
}
Object o = null ;
try {
o = clazz. getDeclaredConstructor ( ) . newInstance ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
return ( T ) o;
}
private static boolean isCommonClass ( Object o) throws Exception{
if ( o. getClass ( ) . isAssignableFrom ( Object. class ) ) {
return true ;
}
if ( o instanceof String || o instanceof String [ ] || o instanceof Integer || o instanceof Date || o instanceof Boolean
|| o instanceof Double || o instanceof Float || o instanceof Long || o instanceof BigDecimal || o instanceof List ) {
return true ;
}
return false ;
}
}