2

I made a switch to Realm last week. Now, I've got this issue when using reflection:

Normally I'd use reflection to fetch field, which when available I'd use to replace url params in our API url f/e: blog/{blogId}/comments <- not an actual url but an example.

The code would check if there is a field called blogId, if yes it'll invoke the getter (getBlogId) and replace the value in the path. Now with realm I get a "BlogRealmProxy" which does not contain the properties I was expecting on my RealmObject, it has fields like INDEX_BLOGID (or alike).

Any idea how to use reflection still? I need this to have generic functions available.

Update:

Well I found a way, by doing something quite silly, any better options?:

Class clazz = obj.getClass();

    if (clazz.getName().endsWith("Proxy")) {
        clazz = clazz.getSuperclass();
    }

I'm 100% sure objects will never end with Proxy ;)

4
  • you need to call the getter, the fields will be null. May 18, 2015 at 14:05
  • Yes that was quite obvious, I do call the getter, since the fields are private. What I was wondering is how to figure out what the fields are with reflection, which is was the question is about not if I should call a property or getter to find out which it is. May 18, 2015 at 14:48
  • no, it's not obvious, you can access private fields with reflection, but you wouldn't get the right result with that here. You could iterate through the methods and whatever starts with "get", the second half of it is the field name. May 18, 2015 at 17:09
  • True, I do feel that my current solution is a bit less code and calculations though. May 18, 2015 at 18:12

1 Answer 1

3

Well I found a way, by doing something quite silly, any better options?:

Class clazz = obj.getClass();

if (clazz.getName().endsWith("Proxy")) {
    clazz = clazz.getSuperclass();
}

I'm 100% sure objects will never end with Proxy ;) in my app.

1
  • Well, what Realm does is that they check for endsWith("RealmProxy"), so it's not a bad solution :P Nov 9, 2016 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.