use of javax.lang.model.element.VariableElement in project realm-java by realm.
the class RealmProxyClassGenerator method emitInitTableMethod.
private void emitInitTableMethod(JavaWriter writer) throws IOException {
writer.beginMethod(// Return type
"Table", // Method name
"initTable", // Modifiers
EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), "SharedRealm", // Argument type & argument name
"sharedRealm");
writer.beginControlFlow("if (!sharedRealm.hasTable(\"" + Constants.TABLE_PREFIX + this.simpleClassName + "\"))");
writer.emitStatement("Table table = sharedRealm.getTable(\"%s%s\")", Constants.TABLE_PREFIX, this.simpleClassName);
// For each field generate corresponding table index constant
for (VariableElement field : metadata.getFields()) {
String fieldName = field.getSimpleName().toString();
String fieldTypeCanonicalName = field.asType().toString();
String fieldTypeSimpleName = Utils.getFieldTypeSimpleName(field);
if (Constants.JAVA_TO_REALM_TYPES.containsKey(fieldTypeCanonicalName)) {
String nullableFlag;
if (metadata.isNullable(field)) {
nullableFlag = "Table.NULLABLE";
} else {
nullableFlag = "Table.NOT_NULLABLE";
}
writer.emitStatement("table.addColumn(%s, \"%s\", %s)", Constants.JAVA_TO_COLUMN_TYPES.get(fieldTypeCanonicalName), fieldName, nullableFlag);
} else if (Utils.isRealmModel(field)) {
writer.beginControlFlow("if (!sharedRealm.hasTable(\"%s%s\"))", Constants.TABLE_PREFIX, fieldTypeSimpleName).emitStatement("%s%s.initTable(sharedRealm)", fieldTypeSimpleName, Constants.PROXY_SUFFIX).endControlFlow().emitStatement("table.addColumnLink(RealmFieldType.OBJECT, \"%s\", sharedRealm.getTable(\"%s%s\"))", fieldName, Constants.TABLE_PREFIX, fieldTypeSimpleName);
} else if (Utils.isRealmList(field)) {
String genericTypeSimpleName = Utils.getGenericTypeSimpleName(field);
writer.beginControlFlow("if (!sharedRealm.hasTable(\"%s%s\"))", Constants.TABLE_PREFIX, genericTypeSimpleName).emitStatement("%s.initTable(sharedRealm)", Utils.getProxyClassName(genericTypeSimpleName)).endControlFlow().emitStatement("table.addColumnLink(RealmFieldType.LIST, \"%s\", sharedRealm.getTable(\"%s%s\"))", fieldName, Constants.TABLE_PREFIX, genericTypeSimpleName);
}
}
for (VariableElement field : metadata.getIndexedFields()) {
String fieldName = field.getSimpleName().toString();
writer.emitStatement("table.addSearchIndex(table.getColumnIndex(\"%s\"))", fieldName);
}
if (metadata.hasPrimaryKey()) {
String fieldName = metadata.getPrimaryKey().getSimpleName().toString();
writer.emitStatement("table.setPrimaryKey(\"%s\")", fieldName);
} else {
writer.emitStatement("table.setPrimaryKey(\"\")");
}
writer.emitStatement("return table");
writer.endControlFlow();
writer.emitStatement("return sharedRealm.getTable(\"%s%s\")", Constants.TABLE_PREFIX, this.simpleClassName);
writer.endMethod().emitEmptyLine();
}
use of javax.lang.model.element.VariableElement in project realm-java by realm.
the class RealmProxyClassGenerator method emitInsertMethod.
private void emitInsertMethod(JavaWriter writer) throws IOException {
writer.beginMethod(// Return type
"long", // Method name
"insert", // Modifiers
EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), // Argument type & argument name
"Realm", // Argument type & argument name
"realm", // Argument type & argument name
qualifiedClassName, // Argument type & argument name
"object", // Argument type & argument name
"Map<RealmModel,Long>", // Argument type & argument name
"cache");
// If object is already in the Realm there is nothing to update
writer.beginControlFlow("if (object instanceof RealmObjectProxy && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath()))").emitStatement("return ((RealmObjectProxy)object).realmGet$proxyState().getRow$realm().getIndex()").endControlFlow();
writer.emitStatement("Table table = realm.getTable(%s.class)", qualifiedClassName);
writer.emitStatement("long tableNativePtr = table.getNativeTablePointer()");
writer.emitStatement("%s columnInfo = (%s) realm.schema.getColumnInfo(%s.class)", columnInfoClassName(), columnInfoClassName(), qualifiedClassName);
if (metadata.hasPrimaryKey()) {
writer.emitStatement("long pkColumnIndex = table.getPrimaryKey()");
}
addPrimaryKeyCheckIfNeeded(metadata, true, writer);
for (VariableElement field : metadata.getFields()) {
String fieldName = field.getSimpleName().toString();
String fieldType = field.asType().toString();
String getter = metadata.getInternalGetter(fieldName);
if (Utils.isRealmModel(field)) {
writer.emitEmptyLine().emitStatement("%s %sObj = ((%s) object).%s()", fieldType, fieldName, interfaceName, getter).beginControlFlow("if (%sObj != null)", fieldName).emitStatement("Long cache%1$s = cache.get(%1$sObj)", fieldName).beginControlFlow("if (cache%s == null)", fieldName).emitStatement("cache%s = %s.insert(realm, %sObj, cache)", fieldName, Utils.getProxyClassSimpleName(field), fieldName).endControlFlow().emitStatement("Table.nativeSetLink(tableNativePtr, columnInfo.%1$sIndex, rowIndex, cache%1$s, false)", fieldName).endControlFlow();
} else if (Utils.isRealmList(field)) {
final String genericType = Utils.getGenericTypeQualifiedName(field);
writer.emitEmptyLine().emitStatement("RealmList<%s> %sList = ((%s) object).%s()", genericType, fieldName, interfaceName, getter).beginControlFlow("if (%sList != null)", fieldName).emitStatement("long %1$sNativeLinkViewPtr = Table.nativeGetLinkView(tableNativePtr, columnInfo.%1$sIndex, rowIndex)", fieldName).beginControlFlow("for (%1$s %2$sItem : %2$sList)", genericType, fieldName).emitStatement("Long cacheItemIndex%1$s = cache.get(%1$sItem)", fieldName).beginControlFlow("if (cacheItemIndex%s == null)", fieldName).emitStatement("cacheItemIndex%1$s = %2$s.insert(realm, %1$sItem, cache)", fieldName, Utils.getProxyClassSimpleName(field)).endControlFlow().emitStatement("LinkView.nativeAdd(%1$sNativeLinkViewPtr, cacheItemIndex%1$s)", fieldName).endControlFlow().endControlFlow().emitEmptyLine();
} else {
if (metadata.getPrimaryKey() != field) {
setTableValues(writer, fieldType, fieldName, interfaceName, getter, false);
}
}
}
writer.emitStatement("return rowIndex");
writer.endMethod().emitEmptyLine();
}
use of javax.lang.model.element.VariableElement in project realm-java by realm.
the class RealmProxyClassGenerator method emitInsertOrUpdateListMethod.
private void emitInsertOrUpdateListMethod(JavaWriter writer) throws IOException {
writer.beginMethod(// Return type
"void", // Method name
"insertOrUpdate", // Modifiers
EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), // Argument type & argument name
"Realm", // Argument type & argument name
"realm", // Argument type & argument name
"Iterator<? extends RealmModel>", // Argument type & argument name
"objects", // Argument type & argument name
"Map<RealmModel,Long>", // Argument type & argument name
"cache");
writer.emitStatement("Table table = realm.getTable(%s.class)", qualifiedClassName);
writer.emitStatement("long tableNativePtr = table.getNativeTablePointer()");
writer.emitStatement("%s columnInfo = (%s) realm.schema.getColumnInfo(%s.class)", columnInfoClassName(), columnInfoClassName(), qualifiedClassName);
if (metadata.hasPrimaryKey()) {
writer.emitStatement("long pkColumnIndex = table.getPrimaryKey()");
}
writer.emitStatement("%s object = null", qualifiedClassName);
writer.beginControlFlow("while (objects.hasNext())");
writer.emitStatement("object = (%s) objects.next()", qualifiedClassName);
writer.beginControlFlow("if(!cache.containsKey(object))");
writer.beginControlFlow("if (object instanceof RealmObjectProxy && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy)object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath()))");
writer.emitStatement("cache.put(object, ((RealmObjectProxy)object).realmGet$proxyState().getRow$realm().getIndex())").emitStatement("continue");
writer.endControlFlow();
addPrimaryKeyCheckIfNeeded(metadata, false, writer);
for (VariableElement field : metadata.getFields()) {
String fieldName = field.getSimpleName().toString();
String fieldType = field.asType().toString();
String getter = metadata.getInternalGetter(fieldName);
if (Utils.isRealmModel(field)) {
writer.emitEmptyLine().emitStatement("%s %sObj = ((%s) object).%s()", fieldType, fieldName, interfaceName, getter).beginControlFlow("if (%sObj != null)", fieldName).emitStatement("Long cache%1$s = cache.get(%1$sObj)", fieldName).beginControlFlow("if (cache%s == null)", fieldName).emitStatement("cache%1$s = %2$s.insertOrUpdate(realm, %1$sObj, cache)", fieldName, Utils.getProxyClassSimpleName(field)).endControlFlow().emitStatement("Table.nativeSetLink(tableNativePtr, columnInfo.%1$sIndex, rowIndex, cache%1$s, false)", fieldName).nextControlFlow("else").emitStatement("Table.nativeNullifyLink(tableNativePtr, columnInfo.%sIndex, rowIndex)", fieldName).endControlFlow();
} else if (Utils.isRealmList(field)) {
final String genericType = Utils.getGenericTypeQualifiedName(field);
writer.emitEmptyLine().emitStatement("long %1$sNativeLinkViewPtr = Table.nativeGetLinkView(tableNativePtr, columnInfo.%1$sIndex, rowIndex)", fieldName).emitStatement("LinkView.nativeClear(%sNativeLinkViewPtr)", fieldName).emitStatement("RealmList<%s> %sList = ((%s) object).%s()", genericType, fieldName, interfaceName, getter).beginControlFlow("if (%sList != null)", fieldName).beginControlFlow("for (%1$s %2$sItem : %2$sList)", genericType, fieldName).emitStatement("Long cacheItemIndex%1$s = cache.get(%1$sItem)", fieldName).beginControlFlow("if (cacheItemIndex%s == null)", fieldName).emitStatement("cacheItemIndex%1$s = %2$s.insertOrUpdate(realm, %1$sItem, cache)", fieldName, Utils.getProxyClassSimpleName(field)).endControlFlow().emitStatement("LinkView.nativeAdd(%1$sNativeLinkViewPtr, cacheItemIndex%1$s)", fieldName).endControlFlow().endControlFlow().emitEmptyLine();
} else {
if (metadata.getPrimaryKey() != field) {
setTableValues(writer, fieldType, fieldName, interfaceName, getter, true);
}
}
}
writer.endControlFlow();
writer.endControlFlow();
writer.endMethod();
writer.emitEmptyLine();
}
use of javax.lang.model.element.VariableElement in project realm-java by realm.
the class RealmProxyClassGenerator method emitCreateRealmObjectSchemaMethod.
private void emitCreateRealmObjectSchemaMethod(JavaWriter writer) throws IOException {
writer.beginMethod(// Return type
"RealmObjectSchema", // Method name
"createRealmObjectSchema", // Modifiers
EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), "RealmSchema", // Argument type & argument name
"realmSchema");
writer.beginControlFlow("if (!realmSchema.contains(\"" + this.simpleClassName + "\"))");
writer.emitStatement("RealmObjectSchema realmObjectSchema = realmSchema.create(\"%s\")", this.simpleClassName);
// For each field generate corresponding table index constant
for (VariableElement field : metadata.getFields()) {
String fieldName = field.getSimpleName().toString();
String fieldTypeCanonicalName = field.asType().toString();
String fieldTypeSimpleName = Utils.getFieldTypeSimpleName(field);
if (Constants.JAVA_TO_REALM_TYPES.containsKey(fieldTypeCanonicalName)) {
String nullableFlag = (metadata.isNullable(field) ? "!" : "") + "Property.REQUIRED";
String indexedFlag = (metadata.isIndexed(field) ? "" : "!") + "Property.INDEXED";
String primaryKeyFlag = (metadata.isPrimaryKey(field) ? "" : "!") + "Property.PRIMARY_KEY";
writer.emitStatement("realmObjectSchema.add(new Property(\"%s\", %s, %s, %s, %s))", fieldName, Constants.JAVA_TO_COLUMN_TYPES.get(fieldTypeCanonicalName), primaryKeyFlag, indexedFlag, nullableFlag);
} else if (Utils.isRealmModel(field)) {
writer.beginControlFlow("if (!realmSchema.contains(\"" + fieldTypeSimpleName + "\"))").emitStatement("%s%s.createRealmObjectSchema(realmSchema)", fieldTypeSimpleName, Constants.PROXY_SUFFIX).endControlFlow().emitStatement("realmObjectSchema.add(new Property(\"%s\", RealmFieldType.OBJECT, realmSchema.get(\"%s\")))", fieldName, fieldTypeSimpleName);
} else if (Utils.isRealmList(field)) {
String genericTypeSimpleName = Utils.getGenericTypeSimpleName(field);
writer.beginControlFlow("if (!realmSchema.contains(\"" + genericTypeSimpleName + "\"))").emitStatement("%s%s.createRealmObjectSchema(realmSchema)", genericTypeSimpleName, Constants.PROXY_SUFFIX).endControlFlow().emitStatement("realmObjectSchema.add(new Property(\"%s\", RealmFieldType.LIST, realmSchema.get(\"%s\")))", fieldName, genericTypeSimpleName);
}
}
writer.emitStatement("return realmObjectSchema");
writer.endControlFlow();
writer.emitStatement("return realmSchema.get(\"" + this.simpleClassName + "\")");
writer.endMethod().emitEmptyLine();
}
use of javax.lang.model.element.VariableElement in project realm-java by realm.
the class RealmProxyClassGenerator method emitPersistedFieldAccessors.
private void emitPersistedFieldAccessors(final JavaWriter writer) throws IOException {
for (final VariableElement field : metadata.getFields()) {
final String fieldName = field.getSimpleName().toString();
final String fieldTypeCanonicalName = field.asType().toString();
if (Constants.JAVA_TO_REALM_TYPES.containsKey(fieldTypeCanonicalName)) {
emitPrimitiveType(writer, field, fieldName, fieldTypeCanonicalName);
} else if (Utils.isRealmModel(field)) {
emitRealmModel(writer, field, fieldName, fieldTypeCanonicalName);
} else if (Utils.isRealmList(field)) {
emitRealmList(writer, field, fieldName, fieldTypeCanonicalName);
} else {
throw new UnsupportedOperationException(String.format("Field \"%s\" of type \"%s\" is not supported.", fieldName, fieldTypeCanonicalName));
}
writer.emitEmptyLine();
}
}
Aggregations