Reputation: 79
I see usage of reflection with
private fun invokeMethod(parameterTypes: Array<Class<*>>?, parameters: Array<Any>?, methodName: String?): Card? {
try {
//val method = javaClass.getDeclaredMethod(methodName, parameterTypes)
for (x in parameterTypes!!) println("Parameter Types: $x")
if (parameters != null) {
for (x in parameters) print("Parameters: $x")
}
val method = javaClass.getDeclaredMethod(methodName, *parameterTypes)
return method.invoke(this, parameters) as Card
} catch (e: Exception) {
println("Class Error ${e.message}")
}
return null
}
That is kotlin by the way, Here is
val method = javaClass.getDeclaredMethod(methodName, *parameterTypes)
the method calling from
javaClass
I know we can call the method like this
Test obj = new Test();
Class cls = obj.getClass();
cls.getDeclaredMethod("method2", int.class)
So my question is which class does javaClass refer to?
Upvotes: 0
Views: 263
Reputation: 170899
It's an extension property on any non-nullable type. Here it's applied to this
, which can be omitted as usual. So it will return the Class
object for the instance it's called on, equivalent to this::class.java
(which may be the same as the class the method defined in, or its subclass).
Upvotes: 2