• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

wuseal/Kotlin-Reflect-Tools-For-JVM: Kotlin reflect tools for JVM

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

wuseal/Kotlin-Reflect-Tools-For-JVM

开源软件地址(OpenSource Url):

https://github.com/wuseal/Kotlin-Reflect-Tools-For-JVM

开源编程语言(OpenSource Language):

Kotlin 100.0%

开源软件介绍(OpenSource Introduction):

Bintray GitHub stars license

Kotlin-Reflect-Tools-For-JVM

Related Project: Kotlin-Reflect-Tools-For-Android

OverView

This is a tool library for Kotlin to use java reflect APIs in Kotlin simply method.It can modify or read the top level private visible property value in Kotlin way.

Usage

  • Add jcenter repository in your moduel build gradle:

     repositories {
          jcenter()
     }
  • Apply library in dependency config:

       compile 'wu.seal:kotlin-reflect-tools-for-jvm: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.changePropertyValueIgnoreItsType(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:

val topName = "topSeal"
val topNameWu = "topSealWu"
private val topAge = 666

private fun gotIt() = true

fun funDoubleAge(age: Int): Int {
    return age * 2
}

class TestDemo {
    private val name = "seal"
    val age = 28

    private fun isMan(): Boolean {
        return true
    }
}

Then we could do these :

   
    @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 test case in project.

Others

  • Welcome to raise any issue.
  • Welcome to push a pull request

Find me useful ? ❤️

  • Support me by clicking the button on the upper right of this page. ✌️



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap