Skip to content

Commit e6a09c8

Browse files
author
seal
committed
Release version 1.1.1
Fix refelect overload methods would happen an error
1 parent a821923 commit e6a09c8

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ 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.1.0'
23+
compile 'wu.seal:kotlin-reflect-tools-for-jvm:1.1.1'
2424
```
2525
2626
## APIs
@@ -29,7 +29,7 @@ This is a tool library for Kotlin to use java reflect APIs in Kotlin simply meth
2929
|Any.getPropertyValue(propertyName: String): Any?|get object property value by name|
3030
|Any.changePropertyValue(propertyName: String, newValue: Any?) |change object property value by name|
3131
|Any.changePropertyValueIgnoreItsType(propertyName: String, newValue: Any?)|change object property value by name|
32-
|Any.changePropertyValueByPropertyReference(kProperty: KProperty<R>, newValue: Any?)|change object property value by name|
32+
|Any.changePropertyValueByPropertyReference(kProperty: KProperty<R>, newValue: Any?)|change object property value by property reference|
3333
|Any.invokeMethod(methodName: String, vararg args: Any?): Any?|invoke a method through object by method name|
3434
|<R> KProperty<R>.changeValue(thisObj: Any, newValue: Any?)|change current this property valuev|
3535
|<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|

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,25 @@ fun invokeClassMethodByMethodName(classObj: Any, methodName: String, vararg meth
249249
val containerClass: Class<*> = classObj::class.java
250250

251251
containerClass.declaredMethods.forEach { method ->
252-
if (method.name == methodName) {
252+
if (method.name == methodName && method.parameterTypes.size == methodArgs.size) {
253253
method.isAccessible = true
254254
val modifyFiled = method.javaClass.getDeclaredField("modifiers")
255255
modifyFiled.isAccessible = true
256256
modifyFiled.setInt(method, modifyFiled.getInt(method) and Modifier.FINAL.inv())
257257

258-
if (methodArgs.isNotEmpty()) {
258+
try {
259+
if (methodArgs.isNotEmpty()) {
259260

260-
return method.invoke(classObj, *methodArgs)
261-
} else {
262-
return method.invoke(classObj)
261+
return method.invoke(classObj, *methodArgs)
262+
} else {
263+
return method.invoke(classObj)
264+
}
265+
} catch(e: Exception) {
266+
return@forEach
263267
}
264268
}
265269
}
266-
throw IllegalArgumentException("Can't find the method named :$methodName in the classObj : $classObj")
270+
throw IllegalArgumentException("Can't find the method named :$methodName with args ${methodArgs.toList().toString()} in the classObj : $classObj")
267271
}
268272

269273
/**
@@ -278,19 +282,23 @@ fun invokeTopMethodByMethodName(otherCallableReference: CallableReference, metho
278282
throw IllegalArgumentException("No such property 'jClass'")
279283
}
280284
containerClass.declaredMethods.forEach { method ->
281-
if (method.name == methodName) {
285+
if (method.name == methodName && method.parameterTypes.size == methodArgs.size) {
282286
method.isAccessible = true
283287
val modifyFiled = method.javaClass.getDeclaredField("modifiers")
284288
modifyFiled.isAccessible = true
285289
modifyFiled.setInt(method, modifyFiled.getInt(method) and Modifier.FINAL.inv())
286290

287-
if (methodArgs.isNotEmpty()) {
288-
return method.invoke(null, *methodArgs)
289-
} else {
290-
return method.invoke(null)
291+
try {
292+
if (methodArgs.isNotEmpty()) {
293+
return method.invoke(null, *methodArgs)
294+
} else {
295+
return method.invoke(null)
296+
}
297+
} catch(e: Exception) {
298+
return@forEach
291299
}
292300
}
293301
}
294-
throw IllegalArgumentException("Can't find the method named :$methodName in the same file with ${otherCallableReference.name}")
302+
throw IllegalArgumentException("Can't find the method named :$methodName with args ${methodArgs.toList().toString()} in the same file with ${otherCallableReference.name}")
295303

296304
}

src/test/kotlin/wu/seal/jvm/kotlinreflecttools/ReflectToolsKtTest.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package wu.seal.jvm.kotlinreflecttools
22

3+
import com.winterbe.expekt.should
34
import org.junit.Assert.*
5+
import org.junit.Test
46
import kotlin.jvm.internal.CallableReference
57

68
/**
@@ -147,5 +149,49 @@ class ReflectToolsKtTest {
147149
}
148150

149151

152+
@Test
153+
fun invokeMethodByMethodNameWithDifferentArguments() {
154+
val demoObj = TestDemo()
155+
val expectedObjMethodValue = false
156+
val getMethodValue = invokeClassMethodByMethodName(demoObj, "isMan", expectedObjMethodValue)
157+
getMethodValue.should.be.equal(expectedObjMethodValue)
158+
159+
val args = 0.1
160+
val getOtherTypeArgMethodValue = invokeClassMethodByMethodName(demoObj, "isMan", args) as Boolean
161+
getOtherTypeArgMethodValue.should.be.`false`
162+
163+
}
164+
165+
@Test
166+
fun invokeMethodByMethodNameWithWrongArguments() {
167+
val demoObj = TestDemo()
168+
val expectedObjMethodValue = false
169+
170+
val returnValue = try {
171+
invokeClassMethodByMethodName(demoObj, "isMan", "") as Boolean?
172+
} catch (e: Exception) {
173+
false
174+
}
175+
expectedObjMethodValue.should.be.equal(returnValue)
176+
177+
}
178+
179+
@Test
180+
fun invokeTopMethodByMethodNameWithDifferentArguments() {
181+
var result = invokeTopMethodByMethodName(::topName as CallableReference, "gotIt")
182+
result.should.be.equal(true)
183+
184+
result = invokeTopMethodByMethodName(::topName as CallableReference, "gotIt", false)
185+
result.should.be.equal(false)
186+
187+
result= try {
188+
invokeTopMethodByMethodName(::topName as CallableReference, "gotIt", "Wrong")
189+
} catch (e: Exception) {
190+
"Wrong"
191+
}
192+
result.should.be.equal("Wrong")
193+
}
194+
195+
150196
}
151197

src/test/kotlin/wu/seal/jvm/kotlinreflecttools/TestThings.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ private val topAge = 666
1111
private val topAgeName = "666"
1212

1313
private fun gotIt() = true
14+
private fun gotIt(boolean: Boolean) = boolean
1415
private fun preTopAge(): Int {
1516
return funPropertyReduceAge(topAge)
1617
}
@@ -57,6 +58,14 @@ class TestDemo {
5758
return true
5859
}
5960

61+
private fun isMan(boolean: Boolean) :Boolean{
62+
return boolean
63+
}
64+
65+
private fun isMan(double: Double) :Boolean{
66+
return double==0.0
67+
}
68+
6069
fun nextAge(): Int {
6170
return age + 1
6271
}

0 commit comments

Comments
 (0)