app/src/main/java/db/juhaku/juhakudb/core/android/EntityConverter.java
/**
     * Convert's cursor's row to entity of model class provided.
     *
     * @param cursor {@link Cursor}'s row to be converted to entity of model class.
     * @param model {@link Class} of database model where to convert cursor's row.
     * @return Fully converted entity from cursor's row.
     *
     * @since 1.2.0
     *
     * @hide
     */
Convert's cursor's row to entity of model class provided.
@param cursor  Cursor's row to be converted to entity of model class.
@param model  Class of database model where to convert cursor's row.
@return Fully converted entity from cursor's row.



@hide
Convert ' s cursor ' s row to entity of model class provided . @param cursor Cursor ' s row to be converted to entity of model class . @param model Class of database model where to convert cursor ' s row . @return Fully converted entity from cursor ' s row . @hide
private <T> T convertCursorToEntity(Cursor cursor, Class<?> model) {
        Object entity = instantiateByDefaultConstructor(model);
        String[] names = cursor.getColumnNames();
        int entityIndex = index.get();
        int fieldCount = countDeclaredFields(model);
        for (Field field : model.getDeclaredFields()) {
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            /*
             * Ignore fields that has primary key as reverse join because there is no such column
             * in database thus nothing to do.
             */
            if (hasPrimaryKeyJoin(field)) {
                continue;
            }
            String name = resolveName(field);
            int fieldIndex = getColumnIndex(entityIndex, fieldCount, names, name);
            Class<?> type = ReflectionUtils.getFieldType(field);
            if (type.isAnnotationPresent(Entity.class)) {
                /*
                 * For entities get id fields type in order to obtain correct id value.
                 */
                Field idField = ReflectionUtils.findIdField(type);
                Object value = getColumnValue(cursor, ReflectionUtils.getFieldType(idField), fieldIndex);
                if (value != null) {
                    Object fieldEntity = instantiateByDefaultConstructor(type);
                    ReflectionUtils.setFieldValue(ReflectionUtils.findIdField(fieldEntity.getClass()), fieldEntity, value);
                    ReflectionUtils.setFieldValue(field.getName(), entity, fieldEntity);
                }
            } else {
                Object value = getColumnValue(cursor, type, fieldIndex);
                ReflectionUtils.setFieldValue(field.getName(), entity, value);
            }
            index.incrementAndGet();
            field.setAccessible(accessible); 
        }
        return (T) entity;
    }
private < T > T convertCursorToEntity ( Cursor cursor , Class < ? > model ) { Object entity = instantiateByDefaultConstructor ( model ) ; String [ ] names = cursor . getColumnNames ( ) ; int entityIndex = index . get ( ) ; int fieldCount = countDeclaredFields ( model ) ; for ( Field field : model . getDeclaredFields ( ) ) { boolean accessible = field . isAccessible ( ) ; field . setAccessible ( true ) ; /*
             * Ignore fields that has primary key as reverse join because there is no such column
             * in database thus nothing to do.
             */ if ( hasPrimaryKeyJoin ( field ) ) { continue ; } String name = resolveName ( field ) ; int fieldIndex = getColumnIndex ( entityIndex , fieldCount , names , name ) ; Class < ? > type = ReflectionUtils . getFieldType ( field ) ; if ( type . isAnnotationPresent ( Entity . class ) ) { /*
                 * For entities get id fields type in order to obtain correct id value.
                 */ Field idField = ReflectionUtils . findIdField ( type ) ; Object value = getColumnValue ( cursor , ReflectionUtils . getFieldType ( idField ) , fieldIndex ) ; if ( value != null ) { Object fieldEntity = instantiateByDefaultConstructor ( type ) ; ReflectionUtils . setFieldValue ( ReflectionUtils . findIdField ( fieldEntity . getClass ( ) ) , fieldEntity , value ) ; ReflectionUtils . setFieldValue ( field . getName ( ) , entity , fieldEntity ) ; } } else { Object value = getColumnValue ( cursor , type , fieldIndex ) ; ReflectionUtils . setFieldValue ( field . getName ( ) , entity , value ) ; } index . incrementAndGet ( ) ; field . setAccessible ( accessible ) ; } return ( T ) entity ; }
Convert's cursor's row to entity of model class provided.
Convert ' s cursor ' s row to entity of model class provided .
// instantiate model // Double check that column name in index matches to the required column name //            if (!name.equals(names[index.get()])) { //                throw new ConversionException("Field's name and indexed table column name does not match: " + name + " <> " + names[index.get()]); //            } // Add entity with value to the mapping entity if value is found from database query. // Instantiate new entity // Get the value and add a new resource to result set. // restore original status