Skip to content

Commit 34aff0f

Browse files
author
seal
committed
Release version 1.1.0
Add new API for easy use in kotlin
1 parent 3f55d8e commit 34aff0f

File tree

11 files changed

+297
-79
lines changed

11 files changed

+297
-79
lines changed

.idea/libraries/Gradle__com_winterbe_expekt_0_5_0.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/kotlin-reflect-tools-for-jvm.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/kotlin-reflect-tools-for-jvm_main.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/kotlin-reflect-tools-for-jvm_test.iml

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,28 @@ This is a tool library for Kotlin to use java reflect APIs in Kotlin simply meth
2020
* Apply library in dependency config:
2121
2222
```groovy
23-
compile 'wu.seal:kotlin-reflect-tools-for-jvm:1.0.1'
23+
compile 'wu.seal:kotlin-reflect-tools-for-jvm:1.1.0'
2424
```
2525
2626
## APIs
27-
27+
### API since 1.1
28+
|Method | Describe |
29+
|:------------- |:-------------|
30+
|Any.getPropertyValue(propertyName: String): Any?|get object property value by name|
31+
|Any.changePropertyValue(propertyName: String, newValue: Any?) |change object property value by name|
32+
|Any.changePropertyValueIgnoreItsType(propertyName: String, newValue: Any?)|change object property value by name|
33+
|Any.changePropertyValueByPropertyReference(kProperty: KProperty<R>, newValue: Any?)|change object property value by name|
34+
|Any.invokeMethod(methodName: String, vararg args: Any?): Any?|invoke a method through object by method name|
35+
|<R> KProperty<R>.changeValue(thisObj: Any, newValue: Any?)|change current this property valuev|
36+
|<R> KProperty<R>.packageLevelGetPropertyValueByName(otherPropertyName: String): Any? |get other package level property value by other package level property name which is in the same kotlin file|
37+
|<R> KFunction<R>.packageLevelGetPropertyValueByName(otherPropertyName: String): Any?|get other package level property value by other package level property name which is in the same kotlin file|
38+
|<R> KProperty<R>.packageLevelChangePropertyValue(newValue: Any?)|change package level property value|
39+
|<R> KProperty<R>.packageLevelChangeOtherPropertyValueByName(otherPropertyName: String, newValue: Any?)|change other package level property value by other package level property name which is in the same kotlin file|
40+
|<R> KFunction<R>.packageLevelChangeOtherPropertyValueByName(otherPropertyName: String, newValue: Any?)|change other package level property value by other package level property name which is in the same kotlin file|
41+
|<R> KProperty<R>.packageLevelInvokeMethodByName(methodName: String, vararg args: Any?): Any? |invoke package level method by name which is in the same kotlin file|
42+
|<R> KFunction<R>.packageLevelInvokeMethodByName(methodName: String, vararg args: Any?): Any?|invoke package level method by name which is in the same kotlin file|
43+
44+
### API since 1.0
2845
|Method | Describe |
2946
| ------------- |:-------------|
3047
|changeTopPropertyValue | change the top level property value |
@@ -42,19 +59,12 @@ All method don't care what the property or method visibility it is
4259
## Demo
4360
For example a Kotlin file like this:
4461
```kotlin
62+
4563
val topName = "topSeal"
64+
val topNameWu = "topSealWu"
4665
private val topAge = 666
47-
private val topAgeName = "666"
48-
private fun preTopAge(): Int {
49-
return funPropertyReduceAge(topAge)
50-
}
51-
private fun nextTopAge(): Int {
52-
return funPropertyPlusAge(topAge)
53-
}
54-
55-
val funPropertyPlusAge: (Int) -> Int = { age -> age + 1 }
5666
57-
val funPropertyReduceAge: (Int) -> Int = { age -> age - 1 }
67+
private fun gotIt() = true
5868
5969
fun funDoubleAge(age: Int): Int {
6070
return age * 2
@@ -67,69 +77,45 @@ class TestDemo {
6777
private fun isMan(): Boolean {
6878
return true
6979
}
70-
71-
fun nextAge(): Int {
72-
return age + 1
73-
}
7480
}
7581
```
7682
Then we could do these :
7783
```kotlin
78-
@org.junit.Test
79-
fun changeTopPropertyValue() {
80-
val targetName = "fashionSeal"
81-
assertNotEquals(targetName, topName)
82-
changeTopPropertyValue(::topName, targetName)
83-
assertEquals(targetName, topName)
84+
85+
@Test
86+
fun getPropertyValue() {
87+
val demo = TestDemo()
88+
val nameValue = demo.getPropertyValue("name")
89+
nameValue.should.be.equal("seal")
8490
}
85-
86-
@org.junit.Test
87-
fun changeClassPropertyValue() {
88-
val demoObj = TestDemo()
89-
val preAge = demoObj.age
90-
changeClassPropertyValue(demoObj, demoObj::age, preAge + 1)
91-
assertNotEquals(preAge, demoObj.age)
92-
}
93-
94-
@org.junit.Test
95-
fun changeClassPropertyValueByName() {
96-
val demoObj = TestDemo()
97-
val preAge = demoObj.age
98-
changeClassPropertyValueByName(demoObj, "age", preAge + 1)
99-
assertNotEquals(preAge, demoObj.age)
100-
101-
val newValue = "newSeal"
102-
changeClassPropertyValueByName(demoObj, "name", newValue)
103-
assertEquals(newValue, getClassPropertyValueByName(demoObj, "name"))
91+
92+
@Test
93+
fun changePropertyValue() {
94+
val demo = TestDemo()
95+
val originValue = demo.age
96+
demo.changePropertyValue("age", 100)
97+
val nowValue = demo.age
98+
originValue.should.not.equal(nowValue)
99+
nowValue.should.be.equal(100)
104100
}
105101

106-
@org.junit.Test
107-
fun changeTopPropertyValueByName() {
108-
109-
val targetName = "fashionSeal"
110-
assertNotEquals(targetName, topName)
111-
changeTopPropertyValueByName(::topName as CallableReference, "topName", targetName)
112-
assertEquals(targetName, topName)
113-
114-
val targetAgeName = "newName"
115-
assertNotEquals(targetAgeName, getTopPropertyValueByName(::topName as CallableReference, "topAgeName"))
116-
changeTopPropertyValueByName(::topName as CallableReference, "topAgeName", targetAgeName)
117-
assertEquals(targetAgeName, getTopPropertyValueByName(::topName as CallableReference, "topAgeName"))
118-
119-
val targetAge = 18
120-
assertNotEquals(targetAge, getTopPropertyValueByName(::topName as CallableReference, "topAge"))
121-
changeTopPropertyValueByName(::topName as CallableReference, "topAge", targetAge)
122-
assertEquals(targetAge, getTopPropertyValueByName(::topName as CallableReference, "topAge"))
102+
@Test
103+
fun changeValue() {
104+
val demo = TestDemo()
105+
demo::age.changeValue(demo, 100)
106+
demo.age.should.be.equal(100)
123107
}
124-
125-
@org.junit.Test
126-
fun invokeMethodByMethodName() {
127-
val demoObj = TestDemo()
128-
val expectedObjMethodValue = true
129-
val getMethodValue = invokeClassMethodByMethodName(demoObj, "isMan")
130108

131-
assertEquals(expectedObjMethodValue, getMethodValue)
109+
@Test
110+
fun packageLevelGetPropertyValueByName() {
111+
val topAge = ::topNameWu.packageLevelGetPropertyValueByName("topAge")
112+
topAge.should.be.equal(666)
113+
}
132114

115+
@Test
116+
fun packageLevelInvokeMethodByName() {
117+
val methodResult = ::topName.packageLevelInvokeMethodByName("gotIt") as Boolean
118+
methodResult.should.be.`true`
133119
}
134120

135121
```

build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ apply plugin: 'maven-publish'
1919
sourceCompatibility = 1.8
2020

2121
group 'wu.seal'
22-
version '1.0.1'
22+
version '1.1.0'
2323
repositories {
2424
jcenter()
2525
mavenCentral()
@@ -54,10 +54,10 @@ bintray {
5454
licenses = ['Apache-2.0']
5555
vcsUrl = 'https://github.com/wuseal/Kotlin-Reflect-Tools-For-JVM'
5656
version {
57-
name = '1.0.1'
57+
name = '1.1.0'
5858
desc = 'First version commit'
5959
released = new Date()
60-
vcsTag = '1.0.1'
60+
vcsTag = '1.1.0'
6161
}
6262
}
6363
}
@@ -66,4 +66,6 @@ dependencies {
6666
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
6767
testCompile group: 'junit', name: 'junit', version: '4.12'
6868
compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.1.51'
69+
testCompile "com.winterbe:expekt:0.5.0"
70+
6971
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package wu.seal.jvm.kotlinreflecttools
2+
3+
import kotlin.jvm.internal.CallableReference
4+
import kotlin.reflect.KFunction
5+
import kotlin.reflect.KProperty
6+
7+
/**
8+
* API extension model for easy use
9+
* Created by Seal.Wu on 2018/1/9.
10+
*/
11+
12+
/**
13+
* get object property value by name
14+
*/
15+
fun Any.getPropertyValue(propertyName: String): Any? {
16+
return getClassPropertyValueByName(this, propertyName)
17+
}
18+
19+
/**
20+
* change object property value by name
21+
*/
22+
fun Any.changePropertyValue(propertyName: String, newValue: Any?) {
23+
changeClassPropertyValueByName(this, propertyName, newValue)
24+
}
25+
26+
/**
27+
* change object property value by name
28+
*/
29+
fun Any.changePropertyValueIgnoreItsType(propertyName: String, newValue: Any?) {
30+
changeClassPropertyValueByNameIgnoreType(this, propertyName, newValue)
31+
}
32+
33+
/**
34+
* change object property value by name
35+
*/
36+
fun <R> Any.changePropertyValueByPropertyReference(kProperty: KProperty<R>, newValue: Any?) {
37+
changeClassPropertyValue(this, kProperty, newValue)
38+
}
39+
40+
/**
41+
* invoke a method through object by method name
42+
*
43+
*/
44+
fun Any.invokeMethod(methodName: String, vararg args: Any?): Any? {
45+
return invokeClassMethodByMethodName(this, methodName, *args)
46+
}
47+
48+
/**
49+
* change current this property value
50+
*/
51+
fun <R> KProperty<R>.changeValue(thisObj: Any, newValue: Any?) {
52+
changeClassPropertyValue(thisObj, this, newValue)
53+
}
54+
55+
/**
56+
* get other package level property value by other package level property name which is in the same kotlin file
57+
*/
58+
fun <R> KProperty<R>.packageLevelGetPropertyValueByName(otherPropertyName: String): Any? {
59+
return getTopPropertyValueByName(this as CallableReference, otherPropertyName)
60+
}
61+
62+
/**
63+
* get other package level property value by other package level property name which is in the same kotlin file
64+
*/
65+
fun <R> KFunction<R>.packageLevelGetPropertyValueByName(otherPropertyName: String): Any? {
66+
return getTopPropertyValueByName(this as CallableReference, otherPropertyName)
67+
}
68+
69+
/**
70+
* change package level property value
71+
*/
72+
fun <R> KProperty<R>.packageLevelChangePropertyValue(newValue: Any?) {
73+
changeTopPropertyValue(this, newValue)
74+
}
75+
76+
/**
77+
* change other package level property value by other package level property name which is in the same kotlin file
78+
*/
79+
fun <R> KProperty<R>.packageLevelChangeOtherPropertyValueByName(otherPropertyName: String, newValue: Any?) {
80+
changeTopPropertyValueByName(this as CallableReference, otherPropertyName, newValue)
81+
}
82+
83+
/**
84+
* change other package level property value by other package level property name which is in the same kotlin file
85+
*/
86+
fun <R> KFunction<R>.packageLevelChangeOtherPropertyValueByName(otherPropertyName: String, newValue: Any?) {
87+
changeTopPropertyValueByName(this as CallableReference, otherPropertyName, newValue)
88+
}
89+
90+
91+
/**
92+
* invoke package level method by name which is in the same kotlin file
93+
*/
94+
fun <R> KProperty<R>.packageLevelInvokeMethodByName(methodName: String, vararg args: Any?): Any? {
95+
return invokeTopMethodByMethodName(this as CallableReference, methodName, *args)
96+
}
97+
98+
/**
99+
* invoke package level method by name which is in the same kotlin file
100+
*/
101+
fun <R> KFunction<R>.packageLevelInvokeMethodByName(methodName: String, vararg args: Any?): Any? {
102+
return invokeTopMethodByMethodName(this as CallableReference, methodName, *args)
103+
}
104+
105+

src/main/kotlin/wu/seal/jvm/kotlinreflecttools/ReflectTools.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fun <R> changeClassPropertyValueByName(classObj: Any, propertyName: String, newV
7171
/**
7272
* change the property value with new value int the special Property name in package level property ,not the property in class
7373
*/
74-
fun changeTopPropertyValueByName(otherCallableReference: CallableReference, propertyName: String, newValue: Any) {
74+
fun changeTopPropertyValueByName(otherCallableReference: CallableReference, propertyName: String, newValue: Any?) {
7575

7676
val owner = otherCallableReference.owner
7777
val containerClass: Class<*>

0 commit comments

Comments
 (0)