Any way to make this code look nicer with Kotlin? ...
# reflect
a
Any way to make this code look nicer with Kotlin?
Copy code
try {
        val field = javaClass.getDeclaredField("mShiftingMode")
        field.isAccessible = true
        field.set(this, enable)
    } catch (e: NoSuchFieldException) {
        e.printStackTrace()
    } catch (e: IllegalAccessException) {
        e.printStackTrace()
    }
I looked into kotlin-reflect, but can’t find a way to make private property public
r
Maybe:
Copy code
try {
        with( javaClass.getDeclaredField("mShiftingMode")){
           isAccessible = true
           set(this, enable)
       }
    } catch (e: NoSuchFieldException) {
        e.printStackTrace()
    } catch (e: IllegalAccessException) {
        e.printStackTrace()
    }
And if you want to hide those try catch blocks, maybe send those to another function; and pass a lambda with your field logic.
exactly as Andrey mentioned, because I don’t think Kotlin supports multicatch at the moment