package io.realm; import android.annotation.TargetApi; import android.os.Build; import android.util.JsonReader; import android.util.JsonToken; import io.realm.RealmObjectSchema; import io.realm.RealmSchema; import io.realm.exceptions.RealmMigrationNeededException; import io.realm.internal.ColumnInfo; import io.realm.internal.LinkView; import io.realm.internal.OsObject; import io.realm.internal.RealmObjectProxy; import io.realm.internal.Row; import io.realm.internal.SharedRealm; import io.realm.internal.Table; import io.realm.internal.android.JsonUtils; import io.realm.log.RealmLog; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class SimpleRealmProxy extends some.test.Simple implements RealmObjectProxy, SimpleRealmProxyInterface { static final class SimpleColumnInfo extends ColumnInfo { long nameIndex; long ageIndex; SimpleColumnInfo(SharedRealm realm, Table table) { super(2); this.nameIndex = addColumnDetails(table, "name", RealmFieldType.STRING); this.ageIndex = addColumnDetails(table, "age", RealmFieldType.INTEGER); } SimpleColumnInfo(ColumnInfo src, boolean mutable) { super(src, mutable); copy(src, this); } @Override protected final ColumnInfo copy(boolean mutable) { return new SimpleColumnInfo(this, mutable); } @Override protected final void copy(ColumnInfo rawSrc, ColumnInfo rawDst) { final SimpleColumnInfo src = (SimpleColumnInfo) rawSrc; final SimpleColumnInfo dst = (SimpleColumnInfo) rawDst; dst.nameIndex = src.nameIndex; dst.ageIndex = src.ageIndex; } } private SimpleColumnInfo columnInfo; private ProxyState<some.test.Simple> proxyState; private static final List<String> FIELD_NAMES; static { List<String> fieldNames = new ArrayList<String>(); fieldNames.add("name"); fieldNames.add("age"); FIELD_NAMES = Collections.unmodifiableList(fieldNames); } SimpleRealmProxy() { proxyState.setConstructionFinished(); } @Override public void realm$injectObjectContext() { if (this.proxyState != null) { return; } final BaseRealm.RealmObjectContext context = BaseRealm.objectContext.get(); this.columnInfo = (SimpleColumnInfo) context.getColumnInfo(); this.proxyState = new ProxyState<some.test.Simple>(this); proxyState.setRealm$realm(context.getRealm()); proxyState.setRow$realm(context.getRow()); proxyState.setAcceptDefaultValue$realm(context.getAcceptDefaultValue()); proxyState.setExcludeFields$realm(context.getExcludeFields()); } @Override @SuppressWarnings("cast") public String realmGet$name() { proxyState.getRealm$realm().checkIfValid(); return (java.lang.String) proxyState.getRow$realm().getString(columnInfo.nameIndex); } @Override public void realmSet$name(String value) { if (proxyState.isUnderConstruction()) { if (!proxyState.getAcceptDefaultValue$realm()) { return; } final Row row = proxyState.getRow$realm(); if (value == null) { row.getTable().setNull(columnInfo.nameIndex, row.getIndex(), true); return; } row.getTable().setString(columnInfo.nameIndex, row.getIndex(), value, true); return; } proxyState.getRealm$realm().checkIfValid(); if (value == null) { proxyState.getRow$realm().setNull(columnInfo.nameIndex); return; } proxyState.getRow$realm().setString(columnInfo.nameIndex, value); } @Override @SuppressWarnings("cast") public int realmGet$age() { proxyState.getRealm$realm().checkIfValid(); return (int) proxyState.getRow$realm().getLong(columnInfo.ageIndex); } @Override public void realmSet$age(int value) { if (proxyState.isUnderConstruction()) { if (!proxyState.getAcceptDefaultValue$realm()) { return; } final Row row = proxyState.getRow$realm(); row.getTable().setLong(columnInfo.ageIndex, row.getIndex(), value, true); return; } proxyState.getRealm$realm().checkIfValid(); proxyState.getRow$realm().setLong(columnInfo.ageIndex, value); } public static RealmObjectSchema createRealmObjectSchema(RealmSchema realmSchema) { if (!realmSchema.contains("Simple")) { RealmObjectSchema realmObjectSchema = realmSchema.create("Simple"); realmObjectSchema.add("name", RealmFieldType.STRING, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED); realmObjectSchema.add("age", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED); return realmObjectSchema; } return realmSchema.get("Simple"); } public static SimpleColumnInfo validateTable(SharedRealm sharedRealm, boolean allowExtraColumns) { if (!sharedRealm.hasTable("class_Simple")) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "The 'Simple' class is missing from the schema for this Realm."); } Table table = sharedRealm.getTable("class_Simple"); final long columnCount = table.getColumnCount(); if (columnCount != 2) { if (columnCount < 2) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is less than expected - expected 2 but was " + columnCount); } if (allowExtraColumns) { RealmLog.debug("Field count is more than expected - expected 2 but was %1$d", columnCount); } else { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is more than expected - expected 2 but was " + columnCount); } } Map<String, RealmFieldType> columnTypes = new HashMap<String, RealmFieldType>(); for (long i = 0; i < columnCount; i++) { columnTypes.put(table.getColumnName(i), table.getColumnType(i)); } final SimpleColumnInfo columnInfo = new SimpleColumnInfo(sharedRealm, table); if (table.hasPrimaryKey()) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Primary Key defined for field " + table.getColumnName(table.getPrimaryKey()) + " was removed."); } if (!columnTypes.containsKey("name")) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'name' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn()."); } if (columnTypes.get("name") != RealmFieldType.STRING) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'String' for field 'name' in existing Realm file."); } if (!table.isColumnNullable(columnInfo.nameIndex)) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'name' is required. Either set @Required to field 'name' or migrate using RealmObjectSchema.setNullable()."); } if (!columnTypes.containsKey("age")) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'age' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn()."); } if (columnTypes.get("age") != RealmFieldType.INTEGER) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'int' for field 'age' in existing Realm file."); } if (table.isColumnNullable(columnInfo.ageIndex)) { throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'age' does support null values in the existing Realm file. Use corresponding boxed type for field 'age' or migrate using RealmObjectSchema.setNullable()."); } return columnInfo; } public static String getTableName() { return "class_Simple"; } public static List<String> getFieldNames() { return FIELD_NAMES; } @SuppressWarnings("cast") public static some.test.Simple createOrUpdateUsingJsonObject(Realm realm, JSONObject json, boolean update) throws JSONException { final List<String> excludeFields = Collections.<String> emptyList(); some.test.Simple obj = realm.createObjectInternal(some.test.Simple.class, true, excludeFields); if (json.has("name")) { if (json.isNull("name")) { ((SimpleRealmProxyInterface) obj).realmSet$name(null); } else { ((SimpleRealmProxyInterface) obj).realmSet$name((String) json.getString("name")); } } if (json.has("age")) { if (json.isNull("age")) { throw new IllegalArgumentException("Trying to set non-nullable field 'age' to null."); } else { ((SimpleRealmProxyInterface) obj).realmSet$age((int) json.getInt("age")); } } return obj; } @SuppressWarnings("cast") @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static some.test.Simple createUsingJsonStream(Realm realm, JsonReader reader) throws IOException { some.test.Simple obj = new some.test.Simple(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (false) { } else if (name.equals("name")) { if (reader.peek() == JsonToken.NULL) { reader.skipValue(); ((SimpleRealmProxyInterface) obj).realmSet$name(null); } else { ((SimpleRealmProxyInterface) obj).realmSet$name((String) reader.nextString()); } } else if (name.equals("age")) { if (reader.peek() == JsonToken.NULL) { reader.skipValue(); throw new IllegalArgumentException("Trying to set non-nullable field 'age' to null."); } else { ((SimpleRealmProxyInterface) obj).realmSet$age((int) reader.nextInt()); } } else { reader.skipValue(); } } reader.endObject(); obj = realm.copyToRealm(obj); return obj; } public static some.test.Simple copyOrUpdate(Realm realm, some.test.Simple object, boolean update, Map<RealmModel,RealmObjectProxy> cache) { if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().threadId != realm.threadId) { throw new IllegalArgumentException("Objects which belong to Realm instances in other threads cannot be copied into this Realm instance."); } if (object instanceof RealmObjectProxy && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) { return object; } final BaseRealm.RealmObjectContext objectContext = BaseRealm.objectContext.get(); RealmObjectProxy cachedRealmObject = cache.get(object); if (cachedRealmObject != null) { return (some.test.Simple) cachedRealmObject; } else { return copy(realm, object, update, cache); } } public static some.test.Simple copy(Realm realm, some.test.Simple newObject, boolean update, Map<RealmModel,RealmObjectProxy> cache) { RealmObjectProxy cachedRealmObject = cache.get(newObject); if (cachedRealmObject != null) { return (some.test.Simple) cachedRealmObject; } else { // rejecting default values to avoid creating unexpected objects from RealmModel/RealmList fields. some.test.Simple realmObject = realm.createObjectInternal(some.test.Simple.class, false, Collections.<String>emptyList()); cache.put(newObject, (RealmObjectProxy) realmObject); ((SimpleRealmProxyInterface) realmObject).realmSet$name(((SimpleRealmProxyInterface) newObject).realmGet$name()); ((SimpleRealmProxyInterface) realmObject).realmSet$age(((SimpleRealmProxyInterface) newObject).realmGet$age()); return realmObject; } } public static long insert(Realm realm, some.test.Simple object, Map<RealmModel,Long> cache) { if (object instanceof RealmObjectProxy && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) { return ((RealmObjectProxy)object).realmGet$proxyState().getRow$realm().getIndex(); } Table table = realm.getTable(some.test.Simple.class); long tableNativePtr = table.getNativePtr(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.schema.getColumnInfo(some.test.Simple.class); long rowIndex = OsObject.createRow(realm.sharedRealm, table); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface)object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name, false); } Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface)object).realmGet$age(), false); return rowIndex; } public static void insert(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel,Long> cache) { Table table = realm.getTable(some.test.Simple.class); long tableNativePtr = table.getNativePtr(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.schema.getColumnInfo(some.test.Simple.class); some.test.Simple object = null; while (objects.hasNext()) { object = (some.test.Simple) objects.next(); if(!cache.containsKey(object)) { if (object instanceof RealmObjectProxy && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) { cache.put(object, ((RealmObjectProxy)object).realmGet$proxyState().getRow$realm().getIndex()); continue; } long rowIndex = OsObject.createRow(realm.sharedRealm, table); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface)object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name, false); } Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface)object).realmGet$age(), false); } } } public static long insertOrUpdate(Realm realm, some.test.Simple object, Map<RealmModel,Long> cache) { if (object instanceof RealmObjectProxy && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) { return ((RealmObjectProxy)object).realmGet$proxyState().getRow$realm().getIndex(); } Table table = realm.getTable(some.test.Simple.class); long tableNativePtr = table.getNativePtr(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.schema.getColumnInfo(some.test.Simple.class); long rowIndex = OsObject.createRow(realm.sharedRealm, table); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface)object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name, false); } else { Table.nativeSetNull(tableNativePtr, columnInfo.nameIndex, rowIndex, false); } Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface)object).realmGet$age(), false); return rowIndex; } public static void insertOrUpdate(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel,Long> cache) { Table table = realm.getTable(some.test.Simple.class); long tableNativePtr = table.getNativePtr(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.schema.getColumnInfo(some.test.Simple.class); some.test.Simple object = null; while (objects.hasNext()) { object = (some.test.Simple) objects.next(); if(!cache.containsKey(object)) { if (object instanceof RealmObjectProxy && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) { cache.put(object, ((RealmObjectProxy)object).realmGet$proxyState().getRow$realm().getIndex()); continue; } long rowIndex = OsObject.createRow(realm.sharedRealm, table); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface)object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name, false); } else { Table.nativeSetNull(tableNativePtr, columnInfo.nameIndex, rowIndex, false); } Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface)object).realmGet$age(), false); } } } public static some.test.Simple createDetachedCopy(some.test.Simple realmObject, int currentDepth, int maxDepth, Map<RealmModel, CacheData<RealmModel>> cache) { if (currentDepth > maxDepth || realmObject == null) { return null; } CacheData<RealmModel> cachedObject = cache.get(realmObject); some.test.Simple unmanagedObject; if (cachedObject != null) { // Reuse cached object or recreate it because it was encountered at a lower depth. if (currentDepth >= cachedObject.minDepth) { return (some.test.Simple)cachedObject.object; } else { unmanagedObject = (some.test.Simple)cachedObject.object; cachedObject.minDepth = currentDepth; } } else { unmanagedObject = new some.test.Simple(); cache.put(realmObject, new RealmObjectProxy.CacheData<RealmModel>(currentDepth, unmanagedObject)); } ((SimpleRealmProxyInterface) unmanagedObject).realmSet$name(((SimpleRealmProxyInterface) realmObject).realmGet$name()); ((SimpleRealmProxyInterface) unmanagedObject).realmSet$age(((SimpleRealmProxyInterface) realmObject).realmGet$age()); return unmanagedObject; } @Override public ProxyState<?> realmGet$proxyState() { return proxyState; } }