public static < T > T buildInstanceForMap ( Class < T > clazz , Map < String , Object > values , MyReflectionDifferenceHandler differenceHandler ) throws InstantiationException , IllegalAccessException , IntrospectionException , IllegalArgumentException , InvocationTargetException { log . debug ( "Building new instance of Class " + clazz . getName ( ) ) ; T instance = clazz . newInstance ( ) ; for ( String key : values . keySet ( ) ) { Object value = values . get ( key ) ; if ( value == null ) { log . debug ( "Value for field " + key + " is null, so ignoring it..." ) ; continue ; } log . debug ( "Invoke setter for " + key + " (" + value . getClass ( ) + " / " + value . toString ( ) + ")" ) ; Method setter = null ; try { setter = new PropertyDescriptor ( key . replace ( ' ' , ' ' ) , clazz ) . getWriteMethod ( ) ; } catch ( Exception e ) { throw new IllegalArgumentException ( "Setter for field " + key + " was not found" , e ) ; } Class < ? > argumentType = setter . getParameterTypes ( ) [ 0 ] ; if ( argumentType . isAssignableFrom ( value . getClass ( ) ) ) { setter . invoke ( instance , value ) ; } else { Object newValue = differenceHandler . handleDifference ( value , setter . getParameterTypes ( ) [ 0 ] ) ; setter . invoke ( instance , newValue ) ; } } return instance ; }
Builds a instance of the class for a map containing the values