Skip to content

Commit

Permalink
Release version 1.1.1
Browse files Browse the repository at this point in the history
Fix refelect overload methods would happen an error
  • Loading branch information
seal committed Feb 27, 2018
1 parent a821923 commit e6a09c8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This is a tool library for Kotlin to use java reflect APIs in Kotlin simply meth
* Apply library in dependency config:
```groovy
compile 'wu.seal:kotlin-reflect-tools-for-jvm:1.1.0'
compile 'wu.seal:kotlin-reflect-tools-for-jvm:1.1.1'
```
## APIs
Expand All @@ -29,7 +29,7 @@ This is a tool library for Kotlin to use java reflect APIs in Kotlin simply meth
|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<R>, newValue: Any?)|change object property value by name|
|Any.changePropertyValueByPropertyReference(kProperty: KProperty<R>, 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|
|<R> KProperty<R>.changeValue(thisObj: Any, newValue: Any?)|change current this property valuev|
|<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|
Expand Down
32 changes: 20 additions & 12 deletions src/main/kotlin/wu/seal/jvm/kotlinreflecttools/ReflectTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,25 @@ fun invokeClassMethodByMethodName(classObj: Any, methodName: String, vararg meth
val containerClass: Class<*> = classObj::class.java

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

if (methodArgs.isNotEmpty()) {
try {
if (methodArgs.isNotEmpty()) {

return method.invoke(classObj, *methodArgs)
} else {
return method.invoke(classObj)
return method.invoke(classObj, *methodArgs)
} else {
return method.invoke(classObj)
}
} catch(e: Exception) {
return@forEach
}
}
}
throw IllegalArgumentException("Can't find the method named :$methodName in the classObj : $classObj")
throw IllegalArgumentException("Can't find the method named :$methodName with args ${methodArgs.toList().toString()} in the classObj : $classObj")
}

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

if (methodArgs.isNotEmpty()) {
return method.invoke(null, *methodArgs)
} else {
return method.invoke(null)
try {
if (methodArgs.isNotEmpty()) {
return method.invoke(null, *methodArgs)
} else {
return method.invoke(null)
}
} catch(e: Exception) {
return@forEach
}
}
}
throw IllegalArgumentException("Can't find the method named :$methodName in the same file with ${otherCallableReference.name}")
throw IllegalArgumentException("Can't find the method named :$methodName with args ${methodArgs.toList().toString()} in the same file with ${otherCallableReference.name}")

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wu.seal.jvm.kotlinreflecttools

import com.winterbe.expekt.should
import org.junit.Assert.*
import org.junit.Test
import kotlin.jvm.internal.CallableReference

/**
Expand Down Expand Up @@ -147,5 +149,49 @@ class ReflectToolsKtTest {
}


@Test
fun invokeMethodByMethodNameWithDifferentArguments() {
val demoObj = TestDemo()
val expectedObjMethodValue = false
val getMethodValue = invokeClassMethodByMethodName(demoObj, "isMan", expectedObjMethodValue)
getMethodValue.should.be.equal(expectedObjMethodValue)

val args = 0.1
val getOtherTypeArgMethodValue = invokeClassMethodByMethodName(demoObj, "isMan", args) as Boolean
getOtherTypeArgMethodValue.should.be.`false`

}

@Test
fun invokeMethodByMethodNameWithWrongArguments() {
val demoObj = TestDemo()
val expectedObjMethodValue = false

val returnValue = try {
invokeClassMethodByMethodName(demoObj, "isMan", "") as Boolean?
} catch (e: Exception) {
false
}
expectedObjMethodValue.should.be.equal(returnValue)

}

@Test
fun invokeTopMethodByMethodNameWithDifferentArguments() {
var result = invokeTopMethodByMethodName(::topName as CallableReference, "gotIt")
result.should.be.equal(true)

result = invokeTopMethodByMethodName(::topName as CallableReference, "gotIt", false)
result.should.be.equal(false)

result= try {
invokeTopMethodByMethodName(::topName as CallableReference, "gotIt", "Wrong")
} catch (e: Exception) {
"Wrong"
}
result.should.be.equal("Wrong")
}


}

9 changes: 9 additions & 0 deletions src/test/kotlin/wu/seal/jvm/kotlinreflecttools/TestThings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ private val topAge = 666
private val topAgeName = "666"

private fun gotIt() = true
private fun gotIt(boolean: Boolean) = boolean
private fun preTopAge(): Int {
return funPropertyReduceAge(topAge)
}
Expand Down Expand Up @@ -57,6 +58,14 @@ class TestDemo {
return true
}

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

private fun isMan(double: Double) :Boolean{
return double==0.0
}

fun nextAge(): Int {
return age + 1
}
Expand Down

0 comments on commit e6a09c8

Please sign in to comment.