realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

RealmObject: Impossible to implement interfaces

Open ousenko opened this issue 11 years ago • 7 comments

Having added "implements" clause to one of my RealmObjects, I am facing the following exception:

Caused by: io.realm.exceptions.RealmException: Could not find io.realm.RealmJsonImpl

...

Caused by: java.lang.ClassNotFoundException: io.realm.RealmJsonImpl

...

Caused by: java.lang.ClassNotFoundException: Didn't find class "io.realm.RealmJsonImpl" on path: DexPathList[[zip file "/data/app/com.example.app-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.app-2/lib/x86, /vendor/lib, /system/lib]]

Removing implements clause makes my app useable again.

So it sounds like we can't add implements clause to an object extending RealmObject, can we?

PS. The code is like:

public class AwesomeStuff extends RealmObject implements IAwesomeInterface {

//some fields
private String field;

//some getters/setters for them, standard java getter/setter naming convention

public String getField(){
    return field;
}    

@Overrides
public String someAwesomeMethod(){
    //call getter method to return some value
    return getField();
}

}

ousenko avatar Apr 03 '15 10:04 ousenko

Custom methods are not allowed in RealmObjects for now. So interfaces would not work. Follow #637 for details. What version are you using by the way? 0.80.0 should not even build with this setup.

kboyarshinov avatar Apr 03 '15 12:04 kboyarshinov

Thanks for pointing out. I use 0.80.0, no error/warning is issued in build log.

At least, I hope I got it right that static methods are supported in RealmObject subclasses now? It is not transparent from documentation whether they are allowed, though it works for me. Wouldn't want to fall victim of this ambiguity in future.

ousenko avatar Apr 03 '15 12:04 ousenko

Provide more details about your classes and build log. It may be a bug that no error or warning emitted.

static methods are supported.

kboyarshinov avatar Apr 03 '15 13:04 kboyarshinov

In my code I am able to implement interfaces. The interfaces just can't have methods different from those generated by realm

iflatness avatar Apr 03 '15 17:04 iflatness

Hi @ousenko What @iflatness says is true. Currently we don't allow any custom class methods except getters and setters. Using interfaces with no methods should work, the same with static methods/fields.

And it looks like we should update the documentation to be more clear :)

cmelchior avatar Apr 07 '15 07:04 cmelchior

One thing I would like to add: interfaces with methods matching the getters and setters do work. @cmelchior can you confirm this?

gergokajtar avatar Oct 19 '15 13:10 gergokajtar

Yes, empty interfaces or interfaces with methods that match getters and setters work just fine currently.

cmelchior avatar Oct 19 '15 13:10 cmelchior