Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wuseal/kotlin-reflect-tools-for-android
Kotlin reflect tools for Android
https://github.com/wuseal/kotlin-reflect-tools-for-android
android kotlin kotlin-reflect reflect
Last synced: 11 days ago
JSON representation
Kotlin reflect tools for Android
- Host: GitHub
- URL: https://github.com/wuseal/kotlin-reflect-tools-for-android
- Owner: wuseal
- License: apache-2.0
- Created: 2017-11-15T09:49:58.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-03T06:49:18.000Z (over 6 years ago)
- Last Synced: 2024-10-16T00:36:18.455Z (23 days ago)
- Topics: android, kotlin, kotlin-reflect, reflect
- Language: Kotlin
- Size: 150 KB
- Stars: 85
- Watchers: 4
- Forks: 26
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Bintray](https://img.shields.io/bintray/v/wusealking/maven/wu.seal.android.kotlinreflecttools.svg)](https://bintray.com/wusealking/maven/wu.seal.android.kotlinreflecttools#)
[![GitHub stars](https://img.shields.io/github/stars/wuseal/Kotlin-Reflect-Tools-For-Android.svg?style=social&label=Stars&style=plastic)](https://github.com/wuseal/Kotlin-Reflect-Tools-For-Android/stargazers)
[![license](https://img.shields.io/github/license/wuseal/Kotlin-Reflect-Tools-For-Android.svg)](https://github.com/wuseal/Kotlin-Reflect-Tools-For-Android/blob/master/LICENSE)
# Kotlin-Reflect-Tools-For-Android
Kotlin reflect tools for AndroidRelated Project: [Kotlin-Reflect-Tools-For-JVM](https://github.com/wuseal/Kotlin-Reflect-Tools-For-JVM)
## OverView
This is a tool library for Kotlin to use java reflect APIs in Kotlin simply method on android platform. It can modify or read the top level private visible property value in Kotlin way.
## Usage
* Add jcenter repository in your moduel build gradle:
```groovy
repositories {
jcenter()
}
```
* Apply library in dependency config:
```groovy
compile 'wu.seal:kotlin-reflect-tools-for-android:1.1.2'
```
## APIs|Method | Describe |
|:------------- |:-------------|
|Any.getPropertyValue(propertyName: String): Any?|get object property value by name|
|Any.changePropertyValue(propertyName: String, newValue: Any?) |change object property value by name|
|Any.changePropertyValueByPropertyReference(kProperty: KProperty, newValue: Any?)|change object property value by property reference|
|Any.invokeMethod(methodName: String, vararg args: Any?): Any?|invoke a method through object by method name|
| KProperty.changeValue(thisObj: Any, newValue: Any?)|change current this property valuev|
| KProperty.packageLevelGetPropertyValueByName(otherPropertyName: String): Any? |get other package level property value by other package level property name which is in the same kotlin file|
| KFunction.packageLevelGetPropertyValueByName(otherPropertyName: String): Any?|get other package level property value by other package level property name which is in the same kotlin file|
| KProperty.packageLevelChangePropertyValue(newValue: Any?)|change package level property value|
| KProperty.packageLevelChangeOtherPropertyValueByName(otherPropertyName: String, newValue: Any?)|change other package level property value by other package level property name which is in the same kotlin file|
| KFunction.packageLevelChangeOtherPropertyValueByName(otherPropertyName: String, newValue: Any?)|change other package level property value by other package level property name which is in the same kotlin file|
| KProperty.packageLevelInvokeMethodByName(methodName: String, vararg args: Any?): Any? |invoke package level method by name which is in the same kotlin file|
| KFunction.packageLevelInvokeMethodByName(methodName: String, vararg args: Any?): Any?|invoke package level method by name which is in the same kotlin file|
All method don't care what the property or method visibility it is## Demo
For example a Kotlin file like this:
```kotlinval topName = "topSeal"
val topNameWu = "topSealWu"
private val topAge = 666private fun gotIt() = true
fun funDoubleAge(age: Int): Int {
return age * 2
}class TestDemo {
private val name = "seal"
val age = 28private fun isMan(): Boolean {
return true
}
}
```
Then we could do these :
```kotlin
@Test
fun getPropertyValue() {
val demo = TestDemo()
val nameValue = demo.getPropertyValue("name")
nameValue.should.be.equal("seal")
}@Test
fun changePropertyValue() {
val demo = TestDemo()
val originValue = demo.age
demo.changePropertyValue("age", 100)
val nowValue = demo.age
originValue.should.not.equal(nowValue)
nowValue.should.be.equal(100)
}
@Test
fun changeValue() {
val demo = TestDemo()
demo::age.changeValue(demo, 100)
demo.age.should.be.equal(100)
}@Test
fun packageLevelGetPropertyValueByName() {
val topAge = ::topNameWu.packageLevelGetPropertyValueByName("topAge")
topAge.should.be.equal(666)
}@Test
fun packageLevelInvokeMethodByName() {
val methodResult = ::topName.packageLevelInvokeMethodByName("gotIt") as Boolean
methodResult.should.be.`true`
}```
To see more usage cases ,you can have a look at the AndroidTest case in project.
## Others
* Welcome to raise any issue.
* Welcome to push a pull request## Find me useful ? :heart:
* Support me by clicking the :star: button on the upper right of this page. :v: