public void addListFieldValue ( String fieldName , Object value ) { Object val = value ; if ( value instanceof DomainObject ) val = ( ( DomainObject ) value ) . getRawObject ( ) ; Object lst = getFieldValue ( fieldName , true ) ; // internal DOField fld = getDomainObjectType ( ) . getFieldByName ( fieldName ) ; String ctn = fld . getComponentTypeName ( ) ; Class < ? > clazz ; try { clazz = getDomainObjectType ( ) . getDomainModel ( ) . getClassForName ( ctn ) ; if ( ! clazz . isAssignableFrom ( val . getClass ( ) ) ) throw new RuntimeException ( "value must be of type or subtype of: [" + clazz . getName ( ) + "]" ) ; lst = tryInitListOrArray ( lst , fld , clazz ) ; } catch ( ClassNotFoundException e ) { throw new RuntimeException ( e ) ; } if ( lst instanceof List < ? > ) { @ SuppressWarnings ( "unchecked" ) List < Object > list = ( List < Object > ) lst ; list . add ( val ) ; } else if ( lst != null && lst . getClass ( ) . isArray ( ) ) { int len = Array . getLength ( lst ) ; Object array = Array . newInstance ( clazz , len + 1 ) ; for ( int i = 0 ; i < len ; i ++ ) { Array . set ( array , i , Array . get ( lst , i ) ) ; } Array . set ( array , len , val ) ; } else { if ( ! getDomainObjectType ( ) . getFieldByName ( fieldName ) . isListOrArray ( ) ) throw new RuntimeException ( "field: [" + fieldName + "] is neither list nor array" ) ; if ( lst == null ) throw new RuntimeException ( "field: [" + fieldName + "] has not been initialized as list or array" ) ; } }
Add a value to a list or array field