Last active
August 14, 2019 21:42
-
-
Save yongjhih/6d1cee717a000113abb68dd107d7786e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> Class<Any>.invoke(methodName: String , vararg args: Pair<Class<*>, *>): T? { | |
return try { | |
getMethod(methodName, *(args.map { it.first }.toTypedArray())) | |
?.invoke(this, *(args.map { it.second }.toTypedArray())) as? T | |
} catch (e: Throwable) { | |
e.printStackTrace() | |
null | |
} | |
} | |
fun <T> Class<Any>.invokes(methodName: String , vararg values: Any): T? { | |
return invoke(methodName, *(values.map { | |
when (it.javaClass) { | |
Integer::class.java -> Int::class.java to it | |
else -> it.javaClass to it | |
} | |
}.toTypedArray())) as? T | |
} | |
fun <T> classOf(name: String): Class<T>? { | |
return try { | |
Class.forName(name) as? Class<T> | |
} catch (e: Throwable) { | |
e.printStackTrace() | |
null | |
} | |
} | |
fun getSystemPropertiesInt(key: String, defaultValue: Int = 0): Int? { | |
return classOf<Any>("android.os.SystemProperties") | |
?.invokes<Int>("getInt", key, defaultValue) | |
} | |
fun getSystemPropertiesString(key: String, defaultValue: String? = null): String? { | |
return classOf<Any>("android.os.SystemProperties") | |
?.invoke<String>("get", String::class.java to key, String::class.java to defaultValue) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment