简体   繁体   English

是否可以使用ByteBuddy在Java代理中检索调用方“对象”?

[英]Is there a way to retrieve the caller “object” in a Java agent using ByteBuddy?

To retrieve the class of the caller, we can use the StackWalker : 要检索调用者的类,我们可以使用StackWalker

@Advice.OnMethodEnter
static void enter(@Advice.This Object thiz, 
                  @Advice.Origin Method method, 
                  @Advice.AllArguments Object... args) {
    var walker =  StackWalker.getInstance(RETAIN_CLASS_REFERENCE);
    var callerClass = walker.getCallerClass();
    ...
}

but is there a way to get a reference to the caller object, if it has not been garbage collected already? 但是如果还没有被垃圾回收,有没有办法获得对调用者对象的引用?

In particular I am interested in the identity hash code of the caller object. 我特别对调用者对象的身份哈希代码感兴趣。

No, this is not possible, neither in a Java agent, nor without it. 不,在Java代理中或没有它,这都是不可能的。 The only way to get the reference would be by instrumenting the caller of a method to provide its own instance reference. 获取引用的唯一方法是通过对方法的调用程序进行检测以提供其自己的实例引用。

Conceptually, I would however not recommend to go for this solution as it is very vulnerable to refactoring and would also yield undefined behavior if reflection, method handles or calls from static methods would occur. 从概念上讲,我不建议您使用此解决方案,因为它非常容易重构,并且如果发生反射,方法处理或从静态方法进行调用,也会产生未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM