Skip to content

Commit f013383

Browse files
author
seal
committed
Release version 1.1.0
add easy use API base kotlin syntax
1 parent c1b5c23 commit f013383

File tree

8 files changed

+268
-98
lines changed

8 files changed

+268
-98
lines changed

README.md

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,33 @@ 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-android:1.0.0'
23+
compile 'wu.seal:kotlin-reflect-tools-for-android:1.1.0'
2424
```
2525
2626
## APIs
27+
### API since 1.1
28+
29+
|Method | Describe |
30+
|:------------- |:-------------|
31+
|Any.getPropertyValue(propertyName: String): Any?|get object property value by name|
32+
|Any.changePropertyValue(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|
2743
28-
|Method |Describe |
29-
|:------------- |:-------------|
44+
45+
## APIs
46+
### API since 1.1
47+
48+
|Method | Describe |
49+
| ------------- |:-------------|
3050
|changeTopPropertyValue | change the top level property value |
3151
| changeTopPropertyValueByName | change the top leve property name by porpery name |
3252
| getTopPropertyValueByName | get the top level property value by property name |
@@ -41,19 +61,12 @@ All method don't care what the property or method visibility it is
4161
## Demo
4262
For example a Kotlin file like this:
4363
```kotlin
64+
4465
val topName = "topSeal"
66+
val topNameWu = "topSealWu"
4567
private val topAge = 666
46-
private val topAgeName = "666"
47-
private fun preTopAge(): Int {
48-
return funPropertyReduceAge(topAge)
49-
}
50-
private fun nextTopAge(): Int {
51-
return funPropertyPlusAge(topAge)
52-
}
5368
54-
val funPropertyPlusAge: (Int) -> Int = { age -> age + 1 }
55-
56-
val funPropertyReduceAge: (Int) -> Int = { age -> age - 1 }
69+
private fun gotIt() = true
5770
5871
fun funDoubleAge(age: Int): Int {
5972
return age * 2
@@ -66,74 +79,50 @@ class TestDemo {
6679
private fun isMan(): Boolean {
6780
return true
6881
}
69-
70-
fun nextAge(): Int {
71-
return age + 1
72-
}
7382
}
7483
```
7584
Then we could do these :
7685
```kotlin
77-
@org.junit.Test
78-
fun changeTopPropertyValue() {
79-
val targetName = "fashionSeal"
80-
assertNotEquals(targetName, topName)
81-
changeTopPropertyValue(::topName, targetName)
82-
assertEquals(targetName, topName)
83-
}
84-
85-
@org.junit.Test
86-
fun changeClassPropertyValue() {
87-
val demoObj = TestDemo()
88-
val preAge = demoObj.age
89-
changeClassPropertyValue(demoObj, demoObj::age, preAge + 1)
90-
assertNotEquals(preAge, demoObj.age)
86+
87+
@Test
88+
fun getPropertyValue() {
89+
val demo = TestDemo()
90+
val nameValue = demo.getPropertyValue("name")
91+
nameValue.should.be.equal("seal")
9192
}
92-
93-
@org.junit.Test
94-
fun changeClassPropertyValueByName() {
95-
val demoObj = TestDemo()
96-
val preAge = demoObj.age
97-
changeClassPropertyValueByName(demoObj, "age", preAge + 1)
98-
assertNotEquals(preAge, demoObj.age)
99-
100-
val newValue = "newSeal"
101-
changeClassPropertyValueByName(demoObj, "name", newValue)
102-
assertEquals(newValue, getClassPropertyValueByName(demoObj, "name"))
93+
94+
@Test
95+
fun changePropertyValue() {
96+
val demo = TestDemo()
97+
val originValue = demo.age
98+
demo.changePropertyValue("age", 100)
99+
val nowValue = demo.age
100+
originValue.should.not.equal(nowValue)
101+
nowValue.should.be.equal(100)
103102
}
104103

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

130-
assertEquals(expectedObjMethodValue, getMethodValue)
111+
@Test
112+
fun packageLevelGetPropertyValueByName() {
113+
val topAge = ::topNameWu.packageLevelGetPropertyValueByName("topAge")
114+
topAge.should.be.equal(666)
115+
}
131116

117+
@Test
118+
fun packageLevelInvokeMethodByName() {
119+
val methodResult = ::topName.packageLevelInvokeMethodByName("gotIt") as Boolean
120+
methodResult.should.be.`true`
132121
}
133122

134123
```
135124

136-
To see more usage cases ,you can have a look at the test case in project.
125+
To see more usage cases ,you can have a look at the AndroidTest case in project.
137126

138127
## Others
139128
* Welcome to raise any issue.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
jcenter()
99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.0.0'
11+
classpath 'com.android.tools.build:gradle:3.0.1'
1212
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1313
classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:${dokka_version}"
1414

library/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ apply plugin: 'org.jetbrains.dokka-android'
99

1010

1111
group = 'wu.seal'
12-
version = '1.0.1'
12+
version = '1.1.0'
1313
android {
1414
compileSdkVersion 26
1515
defaultConfig {
@@ -62,10 +62,10 @@ bintray {
6262
licenses = ['Apache-2.0']
6363
vcsUrl = 'https://github.com/wuseal/Kotlin-Reflect-Tools-For-Android'
6464
version {
65-
name = '1.0.1'
65+
name = '1.1.0'
6666
desc = 'First version commit'
6767
released = new Date()
68-
vcsTag = '1.0.1'
68+
vcsTag = '1.1.0'
6969
}
7070
}
7171
}
@@ -78,6 +78,8 @@ dependencies {
7878
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
7979
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
8080
compile "org.jetbrains.kotlin:kotlin-reflect:1.1.51"
81+
androidTestImplementation "com.winterbe:expekt:0.5.0"
82+
8183
}
8284
repositories {
8385
mavenCentral()
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package wu.seal.android.kotlinreflecttools
2+
3+
import android.support.test.runner.AndroidJUnit4
4+
import com.winterbe.expekt.should
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
8+
/** API Extension API test
9+
* Created by Seal.Wu on 2018/1/10.
10+
*/
11+
@RunWith(AndroidJUnit4::class)
12+
class APIExtensionKtTest {
13+
14+
@Test
15+
fun getPropertyValue() {
16+
val demo = TestDemo()
17+
val nameValue = demo.getPropertyValue("name")
18+
nameValue.should.be.equal("seal")
19+
}
20+
21+
@Test
22+
fun changePropertyValue() {
23+
val demo = TestDemo()
24+
val originValue = demo.age
25+
demo.changePropertyValue("age", 100)
26+
val nowValue = demo.age
27+
originValue.should.not.equal(nowValue)
28+
nowValue.should.be.equal(100)
29+
}
30+
31+
@Test
32+
fun changePropertyValueByPropertyReference() {
33+
val demo = TestDemo()
34+
val originValue = demo.age
35+
demo.changePropertyValueByPropertyReference(demo::age, 100)
36+
val nowValue = demo.age
37+
originValue.should.not.equal(nowValue)
38+
nowValue.should.be.equal(100)
39+
}
40+
41+
@Test
42+
fun invokeMethod() {
43+
val demo = TestDemo()
44+
val methodResult = demo.invokeMethod("isMan") as Boolean
45+
methodResult.should.be.`true`
46+
}
47+
48+
@Test
49+
fun changeValue() {
50+
val demo = TestDemo()
51+
demo::age.changeValue(demo, 100)
52+
demo.age.should.be.equal(100)
53+
}
54+
55+
@Test
56+
fun packageLevelGetPropertyValueByName() {
57+
val topAge = ::topNameWu.packageLevelGetPropertyValueByName("topAge")
58+
topAge.should.be.equal(666)
59+
}
60+
61+
62+
@Test
63+
fun packageLevelGetPropertyValueByName1() {
64+
val topAge = ::funDoubleAge.packageLevelGetPropertyValueByName("topAge")
65+
topAge.should.be.equal(666)
66+
}
67+
68+
@Test
69+
fun packageLevelChangePropertyValue() {
70+
::topName.packageLevelChangePropertyValue("sealwu")
71+
topName.should.be.equal("sealwu")
72+
}
73+
74+
@Test
75+
fun packageLevelChangeOtherPropertyValueByName() {
76+
::topName.packageLevelChangeOtherPropertyValueByName("topNameWu", "seal")
77+
::topNameWu.packageLevelGetPropertyValueByName("topNameWu").should.be.equal("seal")
78+
}
79+
80+
@Test
81+
fun packageLevelChangeOtherPropertyValueByName1() {
82+
::funDoubleAge.packageLevelChangeOtherPropertyValueByName("topNameWu", "seal")
83+
::topNameWu.packageLevelGetPropertyValueByName("topNameWu").should.be.equal("seal")
84+
}
85+
86+
@Test
87+
fun packageLevelInvokeMethodByName() {
88+
val methodResult = ::topName.packageLevelInvokeMethodByName("gotIt") as Boolean
89+
methodResult.should.be.`true`
90+
}
91+
92+
@Test
93+
fun packageLevelInvokeMethodByName1() {
94+
val methodResult = ::funDoubleAge.packageLevelInvokeMethodByName("gotIt") as Boolean
95+
methodResult.should.be.`true`
96+
}
97+
98+
}

library/src/androidTest/java/wu/seal/android/kotlinreflecttools/TestThins.kt renamed to library/src/androidTest/java/wu/seal/android/kotlinreflecttools/TestThings.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package wu.seal.android.kotlinreflecttools
22

3-
43
/**
5-
Created By: Seal.Wu
6-
Date: 2017/11/14
7-
Time: 18:31
4+
* Created by Seal.Wu on 2017/10/27.
85
*/
96

107

118
val topName = "topSeal"
9+
val topNameWu = "topSealWu"
1210
private val topAge = 666
1311
private val topAgeName = "666"
12+
13+
private fun gotIt() = true
1414
private fun preTopAge(): Int {
1515
return funPropertyReduceAge(topAge)
1616
}
@@ -20,23 +20,23 @@ private fun nextTopAge(): Int {
2020
}
2121

2222

23-
private fun doubleAgeAndPrint(age: Int):Int {
24-
val doubleAge = age*2
23+
private fun doubleAgeAndPrint(age: Int): Int {
24+
val doubleAge = age * 2
2525
println("double age = $doubleAge")
2626
return doubleAge
2727
}
2828

29-
private fun plusTwoAge(age1: Int, age2: Int):Int {
29+
private fun plusTwoAge(age1: Int, age2: Int): Int {
3030
val plusAge = age1 + age2
3131
println("plus age = $plusAge")
3232

3333
return plusAge
3434
}
3535

3636

37-
private fun plusNameAndAge(name: String, age2: Int):String {
37+
private fun plusNameAndAge(name: String, age2: Int): String {
3838
val plusNameAndAge = name + age2
39-
println("plusNameAndAge = $plusNameAndAge")
39+
println("wu.seal.jvm.kotlinreflecttools.plusNameAndAge = $plusNameAndAge")
4040

4141
return plusNameAndAge
4242
}

0 commit comments

Comments
 (0)