SqueezyMo SqueezyMo - 1 month ago 11
Android Question

Kotlin: @Ignore'd properties persisted by Sugar ORM

In my Android app, I'm using Kotlin in conjunction with SugarORM and I have encountered an issue trying to prevent some properties from being persisted. Ironically, the

@com.orm.dsl.Ignore
annotation seems to be ignored when used in Kotlin classes.

As an example,

1) let's declare two seemingly identical models:

// JavaUser.java
public class JavaUser extends SugarRecord {
public String login = "login";
@Ignore public String password = "password";
}

// KotlinUser.kt
class KotlinUser : SugarRecord() {
var login: String = "login"
@Ignore var password: String = "password"
}


2) persist their instances

JavaUser().save()
KotlinUser().save()


3) and take a look at what's actually being persisted:

sqlite> select * from java_user;
ID|LOGIN
1|login

sqlite> select * from kotlin_user;
ID|LOGIN|PASSWORD
1|login|password


I realize that it may have something to do with Kotlin annotation processing but I'm just not sure how I can go about it. Any suggestions are most welcome.

Answer

The core difference between your Java and Kotlin code is that in Java you use fields, but in Kotlin you use properties. See the Properties and Fields section in documentation.

You may try the following solutions and see what works best with SugarORM:

1. Make Kotlin expose fields:

@Ignore @JvmField var password: String = "password"

2. Apply your annotation to the private backing field:

@field:Ignore var password: String = "password"
Comments