Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/realm/realm-java into typ…
Browse files Browse the repository at this point in the history
…o_minor_example_fixes

Conflicts:
	changelog.txt
  • Loading branch information
nhachicha committed Jun 8, 2015
2 parents 4eadf96 + d8b22d3 commit 190e447
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* RealmList.add(), RealmList.addAt() and RealmList.set() now copy standalone objects transparently into Realm.
* Realm now works with Kotlin (M12+). (thanks @cypressious)
* Fix a performance regression introduced in 0.80.3 occurring during the validation of the Realm schema.
* Fix unchecked cast warnings when building with Realm.
* Clean examples (remove old test project)

0.80.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Map<String, Set<ClassMetaData>> getAllModules() {
}

/**
* Returns true if the DefaultRealmModule.java file should be created.
* Returns {@code true} if the DefaultRealmModule.java file should be created.
*/
public boolean shouldCreateDefaultModule() {
return shouldCreateDefaultModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void emitNewInstanceMethod(JavaWriter writer) throws IOException {
emitMediatorSwitch(new ProxySwitchStatement() {
@Override
public void emitStatement(int i, JavaWriter writer) throws IOException {
writer.emitStatement("return (E) new %s()", proxyClasses.get(i));
writer.emitStatement("return clazz.cast(new %s())", proxyClasses.get(i));
}
}, writer);
writer.endMethod();
Expand Down Expand Up @@ -243,13 +243,14 @@ private void emitCopyToRealmMethod(JavaWriter writer) throws IOException {
EnumSet.of(Modifier.PUBLIC),
"Realm", "realm", "E", "obj", "boolean", "update", "Map<RealmObject, RealmObjectProxy>", "cache"
);

writer.emitStatement("Class<E> clazz = (Class<E>) ((obj instanceof RealmObjectProxy) ? obj.getClass().getSuperclass() : obj.getClass())");
writer.emitSingleLineComment("This cast is correct because obj is either ");
writer.emitSingleLineComment("generated by RealmProxy or the original type extending directly from RealmObject");
writer.emitStatement("@SuppressWarnings(\"unchecked\") Class<E> clazz = (Class<E>) ((obj instanceof RealmObjectProxy) ? obj.getClass().getSuperclass() : obj.getClass())");
writer.emitEmptyLine();
emitMediatorSwitch(new ProxySwitchStatement() {
@Override
public void emitStatement(int i, JavaWriter writer) throws IOException {
writer.emitStatement("return (E) %s.copyOrUpdate(realm, (%s) obj, update, cache)", proxyClasses.get(i), simpleModelClasses.get(i));
writer.emitStatement("return clazz.cast(%s.copyOrUpdate(realm, (%s) obj, update, cache))", proxyClasses.get(i), simpleModelClasses.get(i));
}
}, writer, false);
writer.endMethod();
Expand All @@ -268,7 +269,7 @@ private void emitCreteOrUpdateUsingJsonObject(JavaWriter writer) throws IOExcept
emitMediatorSwitch(new ProxySwitchStatement() {
@Override
public void emitStatement(int i, JavaWriter writer) throws IOException {
writer.emitStatement("return (E) %s.createOrUpdateUsingJsonObject(realm, json, update)", proxyClasses.get(i));
writer.emitStatement("return clazz.cast(%s.createOrUpdateUsingJsonObject(realm, json, update))", proxyClasses.get(i));
}
}, writer);
writer.endMethod();
Expand All @@ -287,7 +288,7 @@ private void emitCreateUsingJsonStream(JavaWriter writer) throws IOException {
emitMediatorSwitch(new ProxySwitchStatement() {
@Override
public void emitStatement(int i, JavaWriter writer) throws IOException {
writer.emitStatement("return (E) %s.createUsingJsonStream(realm, reader)", proxyClasses.get(i));
writer.emitStatement("return clazz.cast(%s.createUsingJsonStream(realm, reader))", proxyClasses.get(i));
}
}, writer);
writer.endMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public <E extends RealmObject> E newInstance(Class<E> clazz) {
checkClass(clazz);

if (clazz.equals(AllTypes.class)) {
return (E) new AllTypesRealmProxy();
return clazz.cast(new AllTypesRealmProxy());
} else {
throw getMissingProxyClassException(clazz);
}
Expand All @@ -99,10 +99,12 @@ public Map<String, Long> getColumnIndices(Class<? extends RealmObject> clazz) {

@Override
public <E extends RealmObject> E copyOrUpdate(Realm realm, E obj, boolean update, Map<RealmObject, RealmObjectProxy> cache) {
Class<E> clazz = (Class<E>) ((obj instanceof RealmObjectProxy) ? obj.getClass().getSuperclass() : obj.getClass());
// This cast is correct because obj is either
// generated by RealmProxy or the original type extending directly from RealmObject
@SuppressWarnings("unchecked") Class<E> clazz = (Class<E>) ((obj instanceof RealmObjectProxy) ? obj.getClass().getSuperclass() : obj.getClass());

if (clazz.equals(AllTypes.class)) {
return (E) AllTypesRealmProxy.copyOrUpdate(realm, (AllTypes) obj, update, cache);
return clazz.cast(AllTypesRealmProxy.copyOrUpdate(realm, (AllTypes) obj, update, cache));
} else {
throw getMissingProxyClassException(clazz);
}
Expand All @@ -114,7 +116,7 @@ public <E extends RealmObject> E createOrUpdateUsingJsonObject(Class<E> clazz, R
checkClass(clazz);

if (clazz.equals(AllTypes.class)) {
return (E) AllTypesRealmProxy.createOrUpdateUsingJsonObject(realm, json, update);
return clazz.cast(AllTypesRealmProxy.createOrUpdateUsingJsonObject(realm, json, update));
} else {
throw getMissingProxyClassException(clazz);
}
Expand All @@ -126,7 +128,7 @@ public <E extends RealmObject> E createUsingJsonStream(Class<E> clazz, Realm rea
checkClass(clazz);

if (clazz.equals(AllTypes.class)) {
return (E) AllTypesRealmProxy.createUsingJsonStream(realm, reader);
return clazz.cast(AllTypesRealmProxy.createUsingJsonStream(realm, reader));
} else {
throw getMissingProxyClassException(clazz);
}
Expand Down
9 changes: 7 additions & 2 deletions realm/src/main/java/io/realm/internal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ public String execute(long parm1) {
* was a RealmProxy class.
*/
public static Class<? extends RealmObject> getOriginalModelClass(Class<? extends RealmObject> clazz) {
Class<?> superclass = clazz.getSuperclass();
//This cast is correct because 'clazz' is either the type
//generated by RealmProxy or the original type extending directly from RealmObject
@SuppressWarnings("unchecked")
Class<? extends RealmObject> superclass = (Class<? extends RealmObject>) clazz.getSuperclass();

if (!superclass.equals(RealmObject.class)) {
clazz = (Class<? extends RealmObject>) superclass;
clazz = superclass;
}

return clazz;
}
}

0 comments on commit 190e447

Please sign in to comment.