Mark M. | has entered the room |
Mark M. | turned on guest access |
Mar 6 | 9:20 AM |
Andy | has entered the room |
Andy |
Hey
|
Andy |
how's it goin
|
Mar 6 | 9:25 AM |
Mark M. |
hello, Andy!
|
Mark M. |
sorry for the delay in responding -- I got distracted :-(
|
Mark M. |
how can I help you today?
|
Andy |
no problem mark
|
Andy |
there is a library class with a private field I want to access. I read this can be done with reflection in kotlin. For example, a class GroupAdapter has a private final List<Group> groups
|
Mark M. |
I recommend that you talk to the developers of the library and ask them to add in some sort of accessor -- working with private fields is fragile
|
Mark M. |
using private stuff via reflection is possible in Java and probably is possible in Kotlin, though I haven't had a need to do it in Kotlin yet
|
Andy |
unfortunately I don't have access to them
|
Mark M. |
then... why are you using an unsupported library? don't you have lots of additional risks now?
|
Mar 6 | 9:30 AM |
Andy |
I just want to test a workaround
|
Andy |
I tried to apply this but it didnt work for me https://stackoverflow.com/questions/45131683/ko...
|
Mark M. | |
Mark M. |
that's just what I linked to
|
Mark M. |
what did not work, specifically?
|
Mark M. |
(and note that the question involves an extension function, which you may or may not need)
|
Mark M. |
the first two code snippets in the answer that I linked to should be OK in Kotlin; not sure about the extension function part in the third snippet
|
Andy | |
Mark M. |
then focus on the first two code snippets in the accepted answer there (the one that I linked to) -- IOW, other than Kotlin syntax, there should be no difference than what you would do in plain Java
|
Andy |
View paste
|
Andy |
what is the @let in the accepted answer
|
Mar 6 | 9:35 AM |
Mark M. |
if your problem is the generics, see https://stackoverflow.com/a/42096689/115145
|
Andy |
the error I get is that genericType cant be cast to a MutableList, which makes total sense. I just dont know how to get the value like they did in their Int example. Mine is a List
|
Mark M. |
well, privateList isn't a MutableList<Group> -- it is a java.lang.reflect.Type
|
Mar 6 | 9:40 AM |
Mark M. |
that is what getGenericType() returns, which is what you are calling via the genericType pseudo-property
|
Andy |
yes i know. im trying to see how to get the value. thats why i asked why the @let the SO answer suggested did
|
Mark M. |
return groups.get(yourGroupAdapterInstance) as MutableList<Group> has a better chance at succeeding
|
Mark M. |
get() returns the value of the field, given an instance of the type declaring that field
|
Mark M. |
so, if yourGroupAdapterInstance is an instance of GroupAdapter, groups.get(yourGroupAdapterInstance) returns the actual value of the groups field in youGroupAdapterInstance
|
Mark M. |
that will be an object that implements the java.util.List interface, based on your earlier statement ("class GroupAdapter has a private final List<Group> groups")
|
Mark M. |
one hopes that this at least can be cast to MutableList<Any?> in Kotlin
|
Mark M. |
I don't know enough about how Kotlin handles casts to generics to know whether you can cast to MutableList<Group> successfully, though it is certainly worth trying
|
Mar 6 | 9:55 AM |
Andy | has left the room |
Mark M. | turned off guest access |