Office Hours — Today, March 6

Wednesday, February 27

Mark M.
has entered the room
Mark M.
turned on guest access
Mar 6
9:20 AM
Andy
has entered the room
Andy
Hey
how's it goin
9:25 AM
Mark M.
hello, Andy!
sorry for the delay in responding -- I got distracted :-(
how can I help you today?
Andy
no problem mark
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
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?
9:30 AM
Andy
I just want to test a workaround
I tried to apply this but it didnt work for me https://stackoverflow.com/questions/45131683/ko...
Mark M.
that's just what I linked to
what did not work, specifically?
(and note that the question involves an extension function, which you may or may not need)
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
View paste
I'm not using an extension function
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
val groups = GroupAdapter::class.java.getDeclaredField("groups")
groups.isAccessible = true
val privateList = groups.genericType
return privateList as MutableList<Group>
what is the @let in the accepted answer
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
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
get() returns the value of the field, given an instance of the type declaring that field
so, if yourGroupAdapterInstance is an instance of GroupAdapter, groups.get(yourGroupAdapterInstance) returns the actual value of the groups field in youGroupAdapterInstance
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")
one hopes that this at least can be cast to MutableList<Any?> in Kotlin
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
9:55 AM
Andy
has left the room
Mark M.
turned off guest access

Wednesday, February 27

 

Office Hours

People in this transcript

  • Andy
  • Mark Murphy