Skip to content

Commit 3e4199f

Browse files
author
seal
committed
add support for object class method invoke with overload
1 parent e8f29a2 commit 3e4199f

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

library/build.gradle

Lines changed: 4 additions & 4 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.1.0'
12+
version = '1.1.1'
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.1.0'
66-
desc = 'First version commit'
65+
name = '1.1.1'
66+
desc = 'Fix bug about use reflect API when in overload function condidtion'
6767
released = new Date()
68-
vcsTag = '1.1.0'
68+
vcsTag = '1.1.1'
6969
}
7070
}
7171
}

library/src/androidTest/java/wu/seal/android/kotlinreflecttools/ReflectToolTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ Date: 2017/11/14
77
Time: 18:31
88
*/
99
import android.support.test.runner.AndroidJUnit4
10+
import com.winterbe.expekt.should
1011
import org.junit.Assert.assertEquals
1112
import org.junit.Assert.assertNotEquals
13+
import org.junit.Test
1214
import org.junit.runner.RunWith
1315
import kotlin.jvm.internal.CallableReference
1416

@@ -143,5 +145,30 @@ class ReflectToolsKtTest {
143145
assertEquals(testName + testAg3, invokeMethodPlusNameAge)
144146
}
145147

148+
@Test
149+
fun invokeMethodByMethodNameWithDifferentArguments() {
150+
val demoObj = TestDemo()
151+
val expectedObjMethodValue = false
152+
val getMethodValue = invokeClassMethodByMethodName(demoObj, "isMan", expectedObjMethodValue)
153+
getMethodValue.should.be.equal(expectedObjMethodValue)
154+
155+
val args = 0.1
156+
val getOtherTypeArgMethodValue = invokeClassMethodByMethodName(demoObj, "isMan", args) as Boolean
157+
getOtherTypeArgMethodValue.should.be.`false`
158+
159+
}
146160

161+
@Test
162+
fun invokeMethodByMethodNameWithWrongArguments() {
163+
val demoObj = TestDemo()
164+
val expectedObjMethodValue = false
165+
166+
val returnValue = try {
167+
invokeClassMethodByMethodName(demoObj, "isMan", "") as Boolean?
168+
} catch (e: Exception) {
169+
false
170+
}
171+
expectedObjMethodValue.should.be.equal(returnValue)
172+
173+
}
147174
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class TestDemo {
5757
return true
5858
}
5959

60+
private fun isMan(boolean: Boolean) :Boolean{
61+
return boolean
62+
}
63+
private fun isMan(double: Double) :Boolean{
64+
return double==0.0
65+
}
66+
6067
fun nextAge(): Int {
6168
return age + 1
6269
}

library/src/main/java/wu/seal/android/kotlinreflecttools/KotlinRefelectForAndroid.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,21 @@ fun invokeClassMethodByMethodName(classObj: Any, methodName: String, vararg meth
144144
val containerClass: Class<*> = classObj::class.java
145145

146146
containerClass.declaredMethods.forEach { method ->
147-
if (method.name == methodName) {
147+
if (method.name == methodName && method.parameterTypes.size == methodArgs.size) {
148148
method.isAccessible = true
149-
if (methodArgs.isNotEmpty()) {
150-
151-
return method.invoke(classObj, *methodArgs)
152-
} else {
153-
return method.invoke(classObj)
149+
try {
150+
if (methodArgs.isNotEmpty()) {
151+
152+
return method.invoke(classObj, *methodArgs)
153+
} else {
154+
return method.invoke(classObj)
155+
}
156+
} catch (e: Exception) {
157+
return@forEach
154158
}
155159
}
156160
}
157-
throw IllegalArgumentException("Can't find the method named :$methodName in the classObj : $classObj")
161+
throw IllegalArgumentException("Can't find the method named :$methodName with args ${methodArgs.toList().toString()} in the classObj : $classObj")
158162
}
159163

160164
/**
@@ -179,6 +183,6 @@ fun invokeTopMethodByMethodName(otherCallableReference: CallableReference, metho
179183
}
180184
}
181185
}
182-
throw IllegalArgumentException("Can't find the method named :$methodName in the same file with ${otherCallableReference.name}")
186+
throw IllegalArgumentException("Can't find the method named :$methodName with args $methodArgs in the same file with ${otherCallableReference.name}")
183187

184188
}

0 commit comments

Comments
 (0)