0

Saw the exiting question, but the accepted answer with 22 votes does not seem to compile. The answer is

val hello = Hello()
val f = Hello::class.memberProperties.find { it.name == "world" }
f?.let {
    it.isAccessible = true
    val w = it.get(hello) as World
    println(w.foo())
}

, but class has no memberProperties. The closest thing I could find is members, but it seems that it does not return the correct type. How can I get the private field "mEditor"?

    val f = TextView::class.members.find {it.name == "mEditor"};
    f?.let{
        it.isAccessible=true;
        val w = it.get(text2)
    }

Unresolved reference: isAccessible

TextView.java

@RemoteView
public class TextView extends View implements ViewTreeObserver.OnPreDrawListener {

....

/**
 * {@link EditText} specific data, created on demand when one of the Editor fields is used.
 * See {@link #createEditorIfNeeded()}.
 */
@UnsupportedAppUsage
private Editor mEditor;
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

1

You can use getDeclaredField just like java.

    val f = TextView::class.java.getDeclaredField("mEditor")
    f?.let {
        it.isAccessible = true
        Log.d("TYPE:",f.genericType.toString())
    }

Log Output:

TYPE:: class android.widget.Editor
Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41