@@ -76,6 +76,11 @@ class OkReflect {
7676 */
7777 private var withOuterInstance = false
7878
79+ /* *
80+ * The class of parameters in the method.
81+ */
82+ private var parameterTypes: Array <Class <* >>? = null
83+
7984 /* *
8085 * @param className: The name of the class that you want to create.
8186 *
@@ -154,13 +159,22 @@ class OkReflect {
154159 }
155160
156161 /* *
157- * @param methodName: the name of the method that you want to call.
162+ * @param methodName: The name of the method that you want to call.
163+ * @param classes: The class of the parameters.
158164 * @param args: The parameters of the method that you wan to call.
159165 *
160166 * Call the method that you want to call.
161167 * The method will be called when [get] method called.
162168 * The method will be call with the instance.
163169 */
170+ fun call (methodName : String , classes : Array <Class <* >>, vararg args : Any ): OkReflect {
171+ parameterTypes = classes
172+ return realCall(methodName, true , * args)
173+ }
174+
175+ /* *
176+ * @See [call]
177+ */
164178 fun call (methodName : String , vararg args : Any ): OkReflect {
165179 return realCall(methodName, true , * args)
166180 }
@@ -202,6 +216,7 @@ class OkReflect {
202216 * you need to pass the instance in this method.
203217 */
204218 fun with (instance : Any ): OkReflect {
219+ withOuterInstance = true
205220 this .instance = instance
206221 return this
207222 }
@@ -218,6 +233,7 @@ class OkReflect {
218233 return this
219234 }
220235
236+
221237 /* *
222238 * @param fieldName: The name of the field.
223239 * @param arg: The value that you want to set to the field.
@@ -241,7 +257,7 @@ class OkReflect {
241257 */
242258 private fun setFieldOfInstance (fieldName : String , arg : Any ) {
243259 val osName = System .getProperty(" os.name" )
244- var field = instance !! .javaClass .getDeclaredField(fieldName)
260+ var field = clazz !! .getDeclaredField(fieldName)
245261 field = accessible(field)
246262 if (osName != " Linux" ) {
247263 removeFinalModifier(field)
@@ -263,14 +279,12 @@ class OkReflect {
263279 */
264280 private fun invoke (methodCall : MethodCall ) {
265281 val args = methodCall.args
266- val methods = instance!! .javaClass.methods
267- val method = getMethod(clazz, methodCall.methodName, args)
268- val withInstance = methodCall.callWithInstance
282+ val method = getMethod(clazz, methodCall.methodName, args, parameterTypes)
269283 val returnType = method!! .returnType.toString()
270284 if (returnType == " void" ) {
271285 method.invoke(instance, * args)
272286 } else {
273- result = if (withInstance ) {
287+ result = if (methodCall.callWithInstance ) {
274288 method.invoke(instance, * args)
275289 } else {
276290 verifyResult()
@@ -376,25 +390,32 @@ class OkReflect {
376390 }
377391 }
378392
379- /* *
380- * Get the result value from last method if it has a return value,
381- * or else you will get the instance.
382- */
383- fun <T > get (): T ? {
384- return getByFlag(RETURN_FLAG_RESULT )
385- }
386-
387393 /* *
388394 * @param fieldName: The name of the field that you want to get.
389395 *
390- * Get the result value from last method if it has a return value,
391- * or else you will get the instance.
396+ * Get instance when return value from last method is null.
392397 */
393398 fun <T > get (fieldName : String ): T ? {
394399 targetFieldName = fieldName
395400 return getByFlag<T >(RETURN_FLAG_FIELD )
396401 }
397402
403+ /* *
404+ * @see [get]
405+ */
406+ fun <T > get (): T ? {
407+ return getByFlag(RETURN_FLAG_RESULT_OR_INSTANCE )
408+ }
409+
410+ /* *
411+ * OkReflect will return instance when the result is null,
412+ * when you trying to get return value no matter result is null,
413+ * then you can use this method.
414+ */
415+ fun <T > getResult ():T ? {
416+ return getByFlag(RETURN_FLAG_RESULT )
417+ }
418+
398419 /* *
399420 * Get the class.
400421 */
@@ -427,14 +448,11 @@ class OkReflect {
427448 */
428449 private fun <T > realGet (returnFlag : Int ): T ? {
429450 if (! withOuterInstance) {
430- val needInstance = returnFlag == RETURN_FLAG_INSTANCE || returnFlag == RETURN_FLAG_RESULT
431- if (needInstance) {
451+ if (needInstance(returnFlag)) {
432452 verifyClassInfo()
433453 verifyConstructorArgs()
434454 }
435- if (clazz == null ) {
436- this .clazz = Class .forName(className!! )
437- }
455+ initClazz()
438456 if (createCalled) {
439457 initInstance()
440458 invokeMethods()
@@ -447,6 +465,16 @@ class OkReflect {
447465 return getByResult<T >(returnFlag)
448466 }
449467
468+ private fun initClazz () {
469+ if (clazz == null ) {
470+ this .clazz = Class .forName(className!! )
471+ }
472+ }
473+
474+ private fun needInstance (returnFlag : Int ): Boolean {
475+ return returnFlag == RETURN_FLAG_INSTANCE || returnFlag == RETURN_FLAG_RESULT_OR_INSTANCE
476+ }
477+
450478 /* *
451479 * If there is no constructor parameters for the , there will throw an exception
452480 */
@@ -495,7 +523,8 @@ class OkReflect {
495523 RETURN_FLAG_FIELD -> {
496524 targetFieldValue as T
497525 }
498- RETURN_FLAG_RESULT -> {
526+ RETURN_FLAG_RESULT -> result as T
527+ RETURN_FLAG_RESULT_OR_INSTANCE -> {
499528 if (result != null ) {
500529 result as T
501530 } else {
@@ -526,13 +555,18 @@ class OkReflect {
526555 /* *
527556 * Return the return value from the method that you invoked.
528557 */
529- private const val RETURN_FLAG_RESULT = 3
558+ private const val RETURN_FLAG_RESULT_OR_INSTANCE = 3
530559
531560 /* *
532561 * Return the field.
533562 */
534563 private const val RETURN_FLAG_FIELD = 4
535564
565+ /* *
566+ * Return the return value from the invoked method.
567+ */
568+ private const val RETURN_FLAG_RESULT = 5
569+
536570 /* *
537571 * Set the class name of the instance/
538572 */
0 commit comments