Kotlin 便å©ãªãã©ã°ã¤ã³ã2ã¤ç»å ´ãRealmãæããã
Kotiln 1.0.6ããªãªã¼ã¹ããã¾ããï¼ åæã«é¢ç½ããã©ã°ã¤ã³ã2ã¤ãªãªã¼ã¹ããã¾ããã all-open compiler pluginã¨no-arg compiler pluginã§ãã
all-open compiler pluginã¯ãæå®ããã¢ããã¼ã·ã§ã³ãã¤ããã¯ã©ã¹ãèªåã§open
æå®ãããã¨ããæ©è½ãæã¡ã¾ãã
no-arg compiler pluginã¯ãæå®ããã¢ããã¼ã·ã§ã³ãã¤ããã¯ã©ã¹ã«ãèªåã§ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ï¼å¼æ°ãæããªãã³ã³ã¹ãã©ã¯ã¿ï¼ãçæãã¦ããã¾ãã
å°å ¥æ¹æ³ã¯ãåé ã«ç¤ºããKotlinå ¬å¼ããã°ãåç §ãã¦ãã ããã
ä»åã¯ãããã®ãã©ã°ã¤ã³ãç¨ãããã¨ã§ãRealmã使ãããããªããã¨ã示ãããã¨æãã¾ãã
ãã©ã°ã¤ã³ãªã
ã¾ãã¯æ®éã«Kotlinã ãã使ã£ã¦ãRealmãªãã¸ã§ã¯ããå®ç¾©ãã¦ã¿ã¾ãã
open User(@PrimaryKey open var id: Long = 0, open var name: String = ""): RealmObject
ãã²ãã¼ããã¾ããªãã ããã§ããã
open
ã3åãç»å ´ãã¦ãã¾ãããRealmããã®User
ã¯ã©ã¹ãç¶æ¿ãã¦é¢ç½ãæ©è½ã追å ããããã«å¿
è¦ãªã®ã§ãã
ã¾ããRealmã¯ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãè¦æ±ãã¾ãã
ãã®ããUser
ã¯ã©ã¹ã®ãã©ã¤ããªã³ã³ã¹ãã©ã¯ã¿ã§ããã©ã«ãå¼æ°ãä¸ãããã¨ã§ãå¼æ°ãªãã®ã³ã³ã¹ãã©ã¯ã¿ãæä¾ãã¦ããã¨ããããã§ãã
ã¨ããããæ¬æ¥ã§ããã°ç¡è¦ãã¦ããããã¨ã«æ³¨æããªããã°ãªããªãã®ã§ãããã
all-open compiler pluginã使ã
all-open compiler pluginã使ã£ã¦ãopen
修飾åãéæ²»ãã¾ãããã
ãã©ã°ã¤ã³ã®è¨å®ã¨ãã¦ãä¸è¨ãbuild.gradleã«è¨è¿°ãã¾ãã
allOpen {
annotation('io.realm.annotations.RealmClass')
}
ãã®è¨å®ã«ããã@RealmClass
ã¢ããã¼ã·ã§ã³ãä»ããã¯ã©ã¹ããèªåã§open
æå®ã¨ãªãã¾ãã
@RealmClass User(@PrimaryKey var id: Long = 0, var name: String = ""): RealmObject
no-arg compiler pluginã使ã
次ã«no-arg compiler pluginã使ã£ã¦ãããã©ã«ãå¼æ°ãæ¯åè¨è¿°ããéå±ãªä½æ¥ãã解æ¾ããã¾ãããã ãã©ã°ã¤ã³ã®è¨å®ã¨ãã¦ãä¸è¨ãbuild.gradleã«è¨è¿°ãã¾ãã
noArg {
annotation('io.realm.annotations.RealmClass')
}
all-openã®ã¨ãã¨åãããã«ã@RealmClass
ãä»ããã¯ã©ã¹ã対象ã«ãããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ã®èªåçæãè¨å®ãã¾ãã
ã¤ãã«ããã¾ããï¼
è¬ã®open
ãããã©ã«ãå¼æ°ãããã¬ã¤ã«æ¶ããã³ã¼ããæã«å
¥ãã¾ããï¼
@RealmClass class User(@PrimaryKey var id: Long, var name: String) : RealmObject()
ãã¾ã
no-argã§èªåçæããããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ã¯ãKotlinã®ãªãã¬ã¯ã·ã§ã³æ©è½ã使ã£ã¦ãã¢ã¯ã»ã¹ãããã¨ã¯ã§ããªãããã§ãã ä¸æ¹Javaã®ãªãã¬ã¯ã·ã§ã³æ©è½ã使ãã¨ã¢ã¯ã»ã¹ã§ãã¾ãã
@MyAnnotation // ãã®ã¢ããã¼ã·ã§ã³ã«å¯¾ãã¦no-argè¨å® data class Hoge(val value: String)
Hoge::class.constructors.forEach(::println) println("----------") Hoge::class.java.constructors.forEach(::println) println("----------") println(Hoge::class.java.newInstance())
å®è¡ããã¨...
fun <init>(kotlin.String): Hoge
----------
public Hoge()
public Hoge(java.lang.String)
----------
Hoge(value=null)