Skip to content

Instantly share code, notes, and snippets.

@tylertreat
Created December 31, 2014 21:39
Show Gist options
  • Save tylertreat/3b2ecff2895ac64ae348 to your computer and use it in GitHub Desktop.
Save tylertreat/3b2ecff2895ac64ae348 to your computer and use it in GitHub Desktop.
public <T> T createFromCursor(Cursor cursor, Class<T> modelClass) throws ModelConfigurationException, InfinitumRuntimeException {
return createFromCursorRec(cursor, modelClass);
}
@SuppressWarnings("unchecked")
private <T> T createFromCursorRec(Cursor cursor, Class<T> modelClass) throws ModelConfigurationException, InfinitumRuntimeException {
SqliteResult result = new SqliteResult(cursor);
T ret = (T) mClassReflector.getClassInstance(modelClass);
List<Field> fields = mPersistencePolicy.getPersistentFields(modelClass);
for (Field field : fields) {
if (!mPersistencePolicy.isRelationship(field)) {
SqliteTypeAdapter<?> resolver = mMapper.resolveType(field.getType());
int index = result.getColumnIndex(mPersistencePolicy.getFieldColumnName(field));
try {
resolver.mapToObject(result, index, field, ret);
} catch (IllegalArgumentException e) {
throw new InfinitumRuntimeException("Could not map '" + field.getType().getName() + "'");
} catch (IllegalAccessException e) {
throw new InfinitumRuntimeException("Could not map '" + field.getType().getName() + "'");
}
}
}
int objHash = mPersistencePolicy.computeModelHash(ret);
if (mSession.checkCache(objHash))
return (T) mSession.searchCache(objHash);
mSession.cache(objHash, ret);
loadRelationships(ret);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment