use*_*654 6 java bytecode byte-buddy
我知道如何创建BEFORE构造函数拦截器:
return builder.constructor(isDeclaredBy(typeDescription))
.intercept(MethodDelegation.to(constructorInterceptor)
.andThen(SuperMethodCall.INSTANCE));
Run Code Online (Sandbox Code Playgroud)
我知道如何创建一个AFTER构造函数拦截器:
return builder.constructor(isDeclaredBy(typeDescription))
.intercept(SuperMethodCall.INSTANCE
.andThen(MethodDelegation.to(constructorInterceptor)));
Run Code Online (Sandbox Code Playgroud)
使用以下拦截器:
public void intercept(@Origin Constructor<?> constructor) {
System.out.println("intercepted " + constructor.getName());
}
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何创建一个前/后拦截器.这是我尝试的(基于已经为方法工作的天真方法):
return builder.constructor(isDeclaredBy(typeDescription))
.intercept(MethodDelegation.to(constructorInterceptor));
Run Code Online (Sandbox Code Playgroud)
使用此方法委托:
public void intercept(@Origin Constructor<?> constructor, @SuperCall Callable<?> zuper) throws Exception {
System.out.println("before " + constructor.getName());
zuper.call();
System.out.println("after " + constructor.getName());
}
Run Code Online (Sandbox Code Playgroud)
通过这种设置,我得到:
java.lang.ClassFormatError: Bad method name at constant pool index 23 in class file com/flow/agent/test/Foo$auxiliary$syFGNB3u
Run Code Online (Sandbox Code Playgroud)
完整堆栈跟踪:
java.lang.IllegalStateException: Error invoking java.lang.ClassLoader#findClass
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Resolved.loadClass(ClassInjector.java:392)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:201)
at net.bytebuddy.agent.builder.AgentBuilder$InitializationStrategy$SelfInjection$Dispatcher$Split.register(AgentBuilder.java:1017)
at net.bytebuddy.agent.builder.AgentBuilder$Default$Transformation$Simple$Resolution.apply(AgentBuilder.java:2795)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:3081)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
...
Caused by: java.lang.ClassFormatError: Bad method name at constant pool index 23 in class file com/flow/agent/test/Foo$auxiliary$syFGNB3u
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Resolved.loadClass(ClassInjector.java:388)
Run Code Online (Sandbox Code Playgroud)
Java 虚拟机的验证器需要从任何已实现的构造函数中硬编码调用另一个构造函数。因此,@SuperCall不幸的是,用于实现周围建议并不起作用。事实上,@SuperCall注解不能与构造函数一起使用。(理想情况下,Byte Buddy 会捕获此尝试并抛出一个更具可读性的异常,我将在库的下一个版本中修复该问题。)
您可以做的是定义拦截器,如下所示:
public class Interceptor {
public void before(@Origin Constructor<?> constructor) {
System.out.println("before " + constructor.getName());
}
public void after(Origin Constructor<?> constructor) {
System.out.println("after " + constructor.getName());
}
}
Run Code Online (Sandbox Code Playgroud)
使用如下拦截:
MethodDelegation.to(constructorInterceptor).filter(named("before"))
.andThen(SuperMethodCall.INSTANCE
.andThen(MethodDelegation.to(constructorInterceptor))
.filter(named("after")))
Run Code Online (Sandbox Code Playgroud)
这将首先调用该before方法,然后调用超级构造函数,然后调用after拦截器。
当然,您可能希望能够将值从 传输before到after。为此,Byte Buddy 还没有提供很好的做事方式。(我仍然希望能够利用 JVM 本身的增强功能。这种 VM 限制也会影响使用方法句柄的人们,并且经常被使用 VM 的人们提到是一个不幸的缺点。)
目前,您始终可以在拦截器中定义一个字段,在其中存储您在 期间读取的ThreadLocal值。(在单线程环境中,您甚至可以删除并使用一个简单的字段。)我尚未针对此目标的一个原因是大多数构造函数拦截不需要 around-adivce。如果您陈述更详细的用例,我也许可以为您提供进一步的帮助。beforeafterThreadLocal
| 归档时间: |
|
| 查看次数: |
692 次 |
| 最近记录: |