【问题标题】:Kotlin: Get method parameter valuesKotlin:获取方法参数值
【发布时间】:2018-10-16 00:54:39
【问题描述】:

我正在尝试将参数值传递给我正在穿越的方法。 我现在得到它的方式是使用连接点参数。

代码:

@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
}

我想知道是否有办法使用方法反射来获取它(参数值)?

我尝试以这种方式获取值,但没有成功,最终使用 joinPoint 来获取它。

【问题讨论】:

  • 反射基本上是一种从 .class 文件中读取数据的方式。它不能用于访问仅在运行时存在的信息,例如单个方法调用的参数。
  • 哦...是的...谢谢 :) 现在我觉得问这个很愚蠢 :( ...你能写这个作为答案吗?对于未来的智慧寻求者?

标签: reflection kotlin aop


【解决方案1】:

反射基本上是一种从 .class 文件中读取数据的方法。它不能用于访问仅在运行时存在的信息,例如单个方法调用的参数。

【讨论】:

    猜你喜欢
    • 2015-12-10
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2022-07-24
    • 2022-01-17
    相关资源
    最近更新 更多