Kotlin-获取KProperty1 <t,out =“” r =“”>的属性类型

时间:2019-01-13 05:27:40

标签: reflection kotlin

给出以下代码

class Foo(val bar: String)
val p = Foo::bar

如何从String获取属性类型p

2 个答案:

答案 0 :(得分:2)

如果通过获取的意思是要检查该属性是否为String类型,则可以将该属性的returnType与任何其他KType进行比较< / p>

示例

check(p.returnType == String::class.createType())

答案 1 :(得分:-1)

Consider the following function:


fun <A, B, C> compose(f: (B) -> C, g: (A) -> B): (A) -> C {
    return { x -> f(g(x)) }
}
fun <A, B, C> compose(f: (B) -> C, g: (A) -> B): (A) -> C {
    return { x -> f(g(x)) }
}
It returns a composition of two functions passed to it: compose(f, g) = f(g(*)). Now, you can apply it to callable references:


fun length(s: String) = s.length
​
val oddLength = compose(::isOdd, ::length)
val strings = listOf("a", "ab", "abc")
​
println(strings.filter(oddLength))
Target platform: JVMRunning on kotlin v. 1.3.11
Property References
To access properties as first-class objects in Kotlin, we can also use the :: operator:


val x = 1
​
fun main() {
    println(::x.get())
    println(::x.name) 
}
Target platform: JVMRunning on kotlin v. 1.3.11
The expression ::x evaluates to a property object of type KProperty<Int>, which allows us to read its value using get() or retrieve the property name using the name property. For more information, please refer to the docs on the KProperty class.

For a mutable property, e.g. var y = 1, ::y returns a value of type KMutableProperty<Int>, which has a set() method:



    println(y)
}
var y = 1
​
fun main() {
    ::y.set(2)
    println(y)
}
Target platform: JVMRunning on kotlin v. 1.3.11
A property reference can be used where a function with one parameter is expected:


val strs = listOf("a", "bc", "def")
println(strs.map(String::length))
Target platform: JVMRunning on kotlin v. 1.3.11
To access a property that is a member of a class, we qualify it:


class A(val p: Int)
val prop = A::p
println(prop.get(A(1)))
Target platform: JVMRunning on kotlin v. 1.3.11
For an extension property:


val String.lastChar: Char
    get() = this[length - 1]
​
fun main() {
    println(String::lastChar.get("abc"))
}
Target platform: JVMRunning on kotlin v. 1.3.11
Interoperability With Java Reflection
On the Java platform, standard library contains extensions for reflection classes that provide a mapping to and from Java reflection objects (see package kotlin.reflect.jvm). For example, to find a backing field or a Java method that serves as a getter for a Kotlin property, you can say something like this:


import kotlin.reflect.jvm.*

class A(val p: Int)

fun main() {
    println(A::p.javaGetter) // prints "public final int A.getP()"
    println(A::p.javaField)  // prints "private final int A.p"
}
To get the Kotlin class corresponding to a Java class, use the .kotlin extension property:


fun getKClass(o: Any): KClass<Any> = o.javaClass.kotlin
Constructor References
Constructors can be referenced just like methods and properties. They can be used wherever an object of function type is expected that takes the same parameters as the constructor and returns an object of the appropriate type. Constructors are referenced by using the :: operator and adding the class name. Consider the following function that expects a function parameter with no parameters and return type Foo:


class Foo
​
fun function(factory: () -> Foo) {
    val x: Foo = factory()
}
Using ::Foo, the zero-argument constructor of the class Foo, we can simply call it like this:


function(::Foo)`enter code here`