lundi 15 octobre 2018

Kotlin: Get method parameter values

I am trying to get parameter values passed to the method I am crossing. The way I am getting it right now is using join point args.

Code:

@Around("@annotation(Log)")
fun log(joinPoint: ProceedingJoinPoint): Any {

    val signature = joinPoint.signature as MethodSignature
    val methodName = signature.method.name
    val parameterTypes = signature.method.parameterTypes
    var paramValues = ""

    // TODO: make this a bit more useful
    logger.info("begin execution of $methodName")

    val startTime = System.currentTimeMillis()
    val result = joinPoint.proceed()
    joinPoint.args.iterator().forEach {x -> paramValues += x.toString()}

    logger.info("complete execution of $methodName($paramValues) took " +
            (System.currentTimeMillis() - startTime)/1000.0 + "s")

    return result
}

I want to know if there is a way of getting it(param values) using method reflection?

I tried getting the values that way, with no success and ended up using joinPoint to get it.





Aucun commentaire:

Enregistrer un commentaire