我正在尝试使用字节伙伴库在Java代理中构建调用树。要向树中添加元素,我想使用方法委托。但是,要确保谁是任何叶子的父级,我需要知道谁调用了该方法。
我不想使用:
sun.reflect.Reflection#getCallerClass(int)
因为它已弃用并且在Java 8+中不可用。 另外,我尝试过:
public class ThreadUtil {
public static StackTraceElement getCaller() {
Instant now = Instant.now();
StackTraceElement ste = Thread.currentThread().getStackTrace()[3];
String callerClass = ste.getClassName();
String callerMethod = ste.getMethodName();
Instant now2= Instant.now();
System.out.println(Duration.between(now, now2));
return ste;
}
}
但是,它非常慢(〜1毫秒-如果我有成千上万的电话就太多了)。
在这一点上,是否有一种方法可以有效地吸引呼叫者(也许是一些字节好友的把戏)?
P.S。 我的经纪人:
private static void instrument(String agentOps, Instrumentation inst) {
System.out.println("Agent");
new AgentBuilder.Default().with(new Eager())
.ignore(ElementMatchers.nameContains("com.dvelopp.agenttest"))
.type((ElementMatchers.any()))
.transform((builder, typeDescription, classLoader, module) -> builder.method(ElementMatchers.any())
.intercept(MethodDelegation.to(Interceptor.class))).installOn(inst);
}
public static class Interceptor {
@RuntimeType
public static Object intercept(@SuperCall Callable<?> zuper, @Origin Method method,
@AllArguments Object[] args, @This(optional = true) Object me) throws Exception {
System.out.println("CURRENT: " + method.getDeclaringClass().getName());
System.out.println("CALLER: " + ThreadUtil.getCaller().getClassName());
return zuper.call();
}
}
ENV:Java 8
答案 0 :(得分:2)
字节码检测仅允许您生成还可以编写自己的代码。对于您的情况,您需要创建一个我不建议使用的相当侵入性的工具:
Class
的新参数。更好的解决方案肯定是Holger在评论中提出的建议。使用StackWalker
,如果不可用,请使用sun.reflect.Reflection
(我所知道的所有JVM中都存在)。