>> click to see github project
>> click to see main class ReflectExt.kt
kotlinx-reflect
Some functions to make reflective calls.
simple example
Demo.01 simple example
import com.runningmessage.kotlinx.reflect.*
lateinit var context: Context
val Builder = "android.support.v7.app.AlertDialog${'$'}Builder"
val OnClickListener = "android.content.DialogInterface${'$'}OnClickListener"
Builder(context) // Like AlertDialog.Builder(context)
.calls("setTitle")("Hello World") // Call matched specific named function
.calls("setPositiveButton")("OK",
OnClickListener.createInners{ // Like object: DialogInterface.OnClickListener
/***
* Like:
* override fun onClick(dialog: Any, which: Int){
*
* }
*/
override<Any, Int>("onClick"){ dialog, which ->
}
})
.calls("create").calls("show")() // Like builder.create().show()
Demo.02 Call function by reflect
private lateinit var activity: Any
activity.runInActivity()
private fun Any.runInActivity() {// In the code below , [this] is Activity Object
/***
* setContentView(R.layout.activity_main)
* ***/
calls("setContentView")(R.layout.activity_main)
/***
* val npRecyclerView = findViewById<ViewGroup>(R.id.npRecyclerView)
* ***/
val npRecyclerView = call<ViewGroup>("findViewById")(R.id.npRecyclerView)
val makeText = "android.widget.Toast.makeText"
callsStatic(makeText)(applicationContext, "Hello World", 0)
.calls("show")()
... ...
}
Demo.03 Create instance by reflect
/***************************** Import to reflect *****************************/
/** The string below can be used just same as declared class in the code **/
private const val MyLoadMoreRecyclerAdapter = "com.runningmessage.kotlinx.demo.MyLoadMoreRecyclerAdapter"
private const val StaggeredGridLayoutManager = "android.support.v7.widget.StaggeredGridLayoutManager"
private fun Any.runInActivity() {// In the code below , [this] is Activity Object
... ..
// set the load more recycler adapter
val applicationContext = call<Context>("getApplicationContext")()
/***
* the declared string [MyLoadMoreRecyclerAdapter] can be used same as class which is imported
*
* val adapter = com.runningmessage.kotlinx.demo.MyLoadMoreRecyclerAdapter(applicationContext)
* ***/
val adapter: Any? = MyLoadMoreRecyclerAdapter(applicationContext)
/***
* the declared string [StaggeredGridLayoutManager] can be used same as class which is imported
*
* npRecyclerView.setLayoutManager(StaggeredGridLayoutManager(2, 1))
* ***/
npRecyclerView.calls("setLayoutManager")(StaggeredGridLayoutManager(2, 1))
... ...
}
Demo.04 Call property by reflect
private fun Any.runInActivity() {// In the code below , [this] is Activity Object
... ..
/***
* adapter.isAutoLoadMore = false
***/
adapter.propertys("isAutoLoadMore").value = false
/***
* val count = adapter.dataCount
***/
val count = adapter.property<Int>("dataCount").value ?: 0
val makeText = "android.widget.Toast.makeText"
val LENGTH_LONG = "android.widget.Toast.LENGTH_LONG"
callsStatic(makeText)(applicationContext, "Hello World", property<Int>(LENGTH_LONG)?: 0)
.calls("show")()
... ...
}
Demo.05 Create anonymous inner class instance by reflect
val TextWatcher = "android.text.TextWatcher"
val watcher = TextWatcher.createInners{ // Like val watcher = object: TextWatcher{ ... ... }
/**
* override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
*
* }
**/
override<CharSequence, Int, Int, Int>("beforeTextChanged"){
s, start, before, count ->
}
/**
* override fun onTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
*
* }
**/
override("onTextChanged"){
s: CharSequence?, start: Int?, before: Int?, count: Int? ->
}
/**
* override fun afterTextChanged(s: Editable?) {
*
* }
**/
override("afterTextChanged", CallHandlerFunction { args: Array<out Any?>? ->
val s = args?.get(0) as? Editable
})
}
Demo.06 Call instanceOf/is by reflect
val AppCompatActivity = "android.support.v7.app.AppCompatActivity"
lateinit var activity: Any
if(activity iss AppCompatActivity){
// do something
}
activity.ifIs(AppCompatActivity) { appCompatActivity: Any ->
appCompatActivity.runInActivity()
}
// another sample
val context: Context? = activity.ifIs(AppCompatActivity) { appCompatActivity: Any ->
return@ifIs appCompatActivity.call<Context>("getApplicationContext")()
}