Kotlin data class of RealmObject

Realm doesn’t support Data classes currently. You can see an example of how to write Realm compatible model classes in Kotlin here: https://github.com/realm/realm-java/tree/master/examples/kotlinExample/src/main/kotlin/io/realm/examples/kotlin/model public open class Person( @PrimaryKey public open var name: String = “”, public open var age: Int = 0, public open var dog: Dog? = null, public open var cats: RealmList<Cat> = … Read more

only classes are allowed on the left hand side of a class literal

You can’t use generics with class, as easily observable here: List<Int>::class.java It gives you the same error. To use the generic typ in GSON deserialization, do what is suggested here: https://stackoverflow.com/a/5554296/8073652 EDIT: In Kotlin it looks like this: val type: Type = object : TypeToken<ServiceCall<SurveyListModel>>() {}.type Gson().fromJson<ServiceCall<SurveyListModel>>(json, type).result Here’s a small proof of concept, I’ve … Read more

Proguard – do not obfuscate Kotlin data classes

To fix the problem I moved the model classes to model package and added new ProGuard rule for the package. -keep class com.company.myfeature.model.** { *; } Another solution would be to use @Keep annotation from support library to disable the obfuscation for the class: @Keep data class MyRequestBody(val value: String) Using @Keep may cause problems … Read more

Property include/exclude on Kotlin data classes

I’ve used this approach. data class Person(val id: String, val name: String) { override fun equals(other: Person) = EssentialData(this) == EssentialData(other) override fun hashCode() = EssentialData(this).hashCode() override fun toString() = EssentialData(this).toString().replaceFirst(“EssentialData”, “Person”) } private data class EssentialData(val id: String) { constructor(person: Person) : this(id = person.id) }

Kotlin data class: how to read the value of property if I don’t know its name at compile time?

Here is a function to read a property from an instance of a class given the property name (throws exception if property not found, but you can change that behaviour): import kotlin.reflect.KProperty1 import kotlin.reflect.full.memberProperties @Suppress(“UNCHECKED_CAST”) fun <R> readInstanceProperty(instance: Any, propertyName: String): R { val property = instance::class.members // don’t cast here to <Any, R>, it … Read more

kotlin data class + bean validation jsr 303

You need to use Annotation use-site targets since the default for a property declared in the constructor is to target the annotation on the constructor parameter instead of the getter (which will be seen by JavaBeans compliant hosts) when there are multiple options available. Also using a data class might be inappropriate here (see note … Read more

Kotlin data class copy method not deep copying all members

The copy method of Kotlin is not supposed to be a deep copy at all. As explained in the reference doc (https://kotlinlang.org/docs/reference/data-classes.html), for a class such as: data class User(val name: String = “”, val age: Int = 0) the copy implementation would be: fun copy(name: String = this.name, age: Int = this.age) = User(name, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)