public static < T > DBObject getBsonFromObject ( T t ) throws IllegalAccessException , InstantiationException , InvocationTargetException { Field [ ] fields = AnnotationUtils . filterDeepFields ( t . getClass ( ) ) ; DBObject bson = new BasicDBObject ( ) ; for ( Field field : fields ) { Method method = Utils . findGetter ( field . getName ( ) , t . getClass ( ) ) ; Object object = method . invoke ( t ) ; if ( object != null ) { if ( Collection . class . isAssignableFrom ( field . getType ( ) ) ) { Collection c = ( Collection ) object ; Iterator iterator = c . iterator ( ) ; List innerBsonList = new ArrayList <> ( ) ; while ( iterator . hasNext ( ) ) { innerBsonList . add ( getBsonFromObject ( iterator . next ( ) ) ) ; } bson . put ( AnnotationUtils . deepFieldName ( field ) , innerBsonList ) ; } else if ( IDeepType . class . isAssignableFrom ( field . getType ( ) ) ) { bson . put ( AnnotationUtils . deepFieldName ( field ) , getBsonFromObject ( ( IDeepType ) object ) ) ; } else { bson . put ( AnnotationUtils . deepFieldName ( field ) , object ) ; } } } return bson ; }
converts from an entity class with deep s anotations to BsonObject .