cmelchior / realmfieldnameshelper

Realm extension library used to create more type-safe queries.
Apache License 2.0
173 stars 21 forks source link

private fields of RealmObject in library module not recognized #25

Closed yshrsmz closed 6 years ago

yshrsmz commented 7 years ago

sample: https://github.com/cmelchior/realmfieldnameshelper/compare/master...yshrsmz:kotlin-class

Kotlin 1.1.4-3 Realm 3.7.2 RealmFieldsNameHelper 1.1.1

In the above example, I added a LibraryModel2 in example-library module which looks like this:

open class LibraryModel2 :RealmObject(){
    open var test: String = ""
}

and modified Person class as below:

public class Person extends RealmObject {

    public String name;
    public boolean hasDogs; // camel case naming gets converted to uppercase separated by "_"
    public boolean mHasCats; // Hungarian notation is evil and not supported (yet).
    public boolean has_fish; // fields already using "_" are just converted as they are.
    public RealmList<Dog> dogs;
    public Dog favoriteDog;
    public LibraryModel libraryRef;
    public LibraryModel2 libraryRef2;

    @Ignore
    public int random;
}

generated fields class looks like this.

public final class PersonFields {
  public static final String HAS_DOGS = "hasDogs";

  public static final String HAS_FISH = "has_fish";

  public static final String HAS_CATS = "mHasCats";

  public static final String NAME = "name";

  public static final class DOGS {
    public static final String $ = "dogs";

    public static final String AGE = "dogs.age";

    public static final String FOO = "dogs.foo";

    public static final String NAME = "dogs.name";

    public static final String OWNER = "dogs.owner";
  }

  public static final class FAVORITE_DOG {
    public static final String $ = "favoriteDog";

    public static final String AGE = "favoriteDog.age";

    public static final String FOO = "favoriteDog.foo";

    public static final String NAME = "favoriteDog.name";

    public static final String OWNER = "favoriteDog.owner";
  }

  public static final class LIBRARY_REF {
    public static final String $ = "libraryRef";

    public static final String LIBRARY_FIELD = "libraryRef.libraryField";
  }

  public static final class LIBRARY_REF2 {
    public static final String $ = "libraryRef2";
  }
}

Apparently, PersonFields does not contain field names of LibraryModel2

yshrsmz commented 7 years ago

It looks like Class#getFields(which is used to retrieve fields from library classes) does not return private fields. So if we use Class#getDeclaredFields(which returns all declared fields regardless of its modifier), it should be ok.