https://kotlinlang.org logo
#announcements
Title
# announcements
w

wjur

05/25/2017, 1:16 PM
I have a method that accepts java.lang.reflect.Method and I'm trying to check whether the method is returning a nullable type or not (via 'kotlinFunction.returnType.isMarkedNullable'). However, for data classes, there are the componentX() methods and the corresponding getter methods, getXXX(). Interestingly for getter functions there is no kotlinFunction.
d

diesieben07

05/25/2017, 1:25 PM
wjur: Kotlin does not really see those as functions, they are part of the property. So they are a
KProperty
, not a
KFunction
.
w

wjur

05/25/2017, 1:32 PM
I see. Anyway I need to link a getter with a property 😞
I'm writing a module for jackson.
The
JsonProperty
annotation has a
required
property, which defaults to
false
.
d

diesieben07

05/25/2017, 1:35 PM
Not sure I am following you, what do you mean by "link"?
w

wjur

05/25/2017, 1:36 PM
In Kotlin and data classes, this makes a little sense, because it is
isMarkedNullable
that defines whether the property is required or not.
Consider a sample json model:
Copy code
data class Model(
    @JsonProperty("name")
    val name: String
)
The property says that name is an optional field
while the field is required because is of a non nullable type
d

diesieben07

05/25/2017, 1:39 PM
Ok, so, you want to go from the java.lang.reflect.Method for the getter to the corresponding KProperty?
w

wjur

05/25/2017, 1:40 PM
Yes
d

diesieben07

05/25/2017, 1:42 PM
Since there is only a way to go from Kotlin reflection to Java reflection you have to get all the properties in the class and search through them until
property.getter.javaMethod == yourMethod
w

wjur

05/25/2017, 1:44 PM
It's working!
You saved The World!
Thank you so much!
2 Views