我们可以访问kotlin中任何属性的PropertyMetaData吗?

时间:2015-08-03 14:29:24

标签: kotlin

kotlin有没有办法从房产外面访问PropertyMetaData?更具体地说,来自代表团? 寻找这样的东西:

编辑(现在更合适的例子)

class Obj(var name : String = "")

class Bar {
 val prop1 : Obj by Inject.inject() // <- can inject() access prop1 without passing it explicitly?
 val prop2 : Obj by Inject.inject()
}

object Inject {

 val injected = arrayListOf<ReadOnlyProperty<Any, Obj>>()

 fun inject() : ReadOnlyProperty<Any, Obj> {

    val delegate = Delegates.lazy {
        Obj("NEED THE DELEGATED PROPERTY'S NAME HERE") // <- like "prop1" "prop2" ...
    }
    injected.add(delegate)
    return delegate
 }
}

编辑(1.0.0-beta-1038之后)

此代码片段之前的工作方式类似于魅力(注意:使用以前版本的kotlin时未显式传递字符串“scrollPlace”,因为属性名称之前已“隐式”传递):

val scrollPlace by injectBy<Image>("scrollPlace").replaceBy {

    val scrollPane = ScrollPane(VerticalGroup().space(12f).center())
    scrollPane.setPosition(it.x, it.y)
    scrollPane.setSize(it.width, it.height)
    scrollPane
}

在这个例子中,我正在使用一些链接。除了传递自定义字符串之外,扩展函数injectBy(基于1中建议的模式)接受了答案,创建了一个O2dInjectionClosure(ReadOnlyProperty)实例。解决方案1)当然会工作,即使它不像使用propertyDelgated那样方便,但链接会带来一些麻烦。至少还没有找到一个合适的解决方案。扩展函数replaceBy创建另一个新的O2dInjectionClosure实例:

 public final fun <T : Actor, R : Actor> O2dInjectionClosure<T>.replaceBy(replacer : (replaced : T) -> R) : O2dInjectionClosure<R> {
    val originalInitializer = this.initializer
    return O2dInjectionClosure { propName ->

        ... some transferring of the previous O2dInjectionClosure to this one
        new
    }
}

因此,我不知何故想在链接的最后一次调用中做必要的东西,但是方便的方式:)。 我希望它在某种程度上是合理的

1 个答案:

答案 0 :(得分:2)

显然,您需要在属性的委托对象上调用一些初始化代码。有几种解决方案:

1)使用属性引用表达式(如bashor所述)获取属性的名称并将其传递给inject

val prop1: Obj by Inject.inject(Bar::prop1.name)
val prop2: Obj by Inject.inject(Bar::prop2.name)

虽然明确,但这有点冗长且容易出错。

2)首次访问属性时执行初始化逻辑。因此Inject本身成为属性委托,维护已注册属性的映射。但是,语义上的细微变化可能不适用于您的用例:要在get中注册该属性,至少需要一个Inject

class Bar {
    val prop1: Obj by Inject
    val prop2: Obj by Inject
}

object Inject {
    val injected = hashMapOf<String, ReadOnlyProperty<Any, Obj>>()

    fun get(obj: Any, metadata: PropertyMetadata): Obj {
        // getOrPut computes and stores the value for the key if it's not present in the map
        val property = injected.getOrPut(metadata.name) {
            Delegates.lazy {
                Obj(metadata.name)
            }
        }

        return property[obj, metadata]
    }
}

3)(UPD:不再支持此功能。)使用{em>隐藏和实验功能的propertyDelegated方法,该功能允许使用元数据初始化属性委托。将在访问时使用此委托的属性。请注意,此方法可能会在未来版本的Kotlin中重命名,重新设计甚至删除。

class Bar {
    val prop1: Obj by Inject
    val prop2: Obj by Inject
}

object Inject {
    val injected = hashMapOf<String, ReadOnlyProperty<Any, Obj>>()

    // Experimental method recognized and called by Kotlin on delegated property initialization
    fun propertyDelegated(metadata: PropertyMetadata) {
        injected[metadata.name] = Delegates.lazy {
            Obj(metadata.name)
        }
    }

    fun get(obj: Any, metadata: PropertyMetadata): Obj {
        return injected[metadata.name]!!.get(obj, metadata)
    }
}