构造函数拦截器之前和之后

时间:2015-12-23 10:21:20

标签: java bytecode byte-buddy

我知道如何创建BEFORE构造函数拦截器:

return builder.constructor(isDeclaredBy(typeDescription))
   .intercept(MethodDelegation.to(constructorInterceptor)
   .andThen(SuperMethodCall.INSTANCE));

我知道如何创建一个AFTER构造函数拦截器:

return builder.constructor(isDeclaredBy(typeDescription))
   .intercept(SuperMethodCall.INSTANCE
   .andThen(MethodDelegation.to(constructorInterceptor)));

使用以下拦截器:

public void intercept(@Origin Constructor<?> constructor) {
    System.out.println("intercepted " + constructor.getName());
}

但是我不知道如何创建一个前/后拦截器。这是我尝试的(基于已经为方法工作的天真方法):

return builder.constructor(isDeclaredBy(typeDescription))
    .intercept(MethodDelegation.to(constructorInterceptor));

使用此方法委托:

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());
}

通过此设置,我得到:

java.lang.ClassFormatError: Bad method name at constant pool index 23 in class file com/flow/agent/test/Foo$auxiliary$syFGNB3u

完整堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:3)

Java虚拟机的验证程序需要从任何实现的构造函数中对另一个构造函数进行硬编码调用。因此,遗憾的是,使用@SuperCall来实现around-advice不起作用。事实上,@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());
  }
}

使用拦截:

MethodDelegation.to(constructorInterceptor).filter(named("before"))
                .andThen(SuperMethodCall.INSTANCE
                .andThen(MethodDelegation.to(constructorInterceptor))
                                         .filter(named("after")))

这将首先调用before方法,然后调用超级构造函数,然后调用after拦截器。

当然,您可能希望能够将值从before传输到after。为此,Byte Buddy还没有提供一种很好的做事方式。 (我仍然希望JVM本身可以利用这一优势。这种VM限制也会让人们使用方法句柄,并且经常被使用VM的人视为不幸的不利因素。)

目前,您始终可以在拦截器中定义ThreadLocal字段,用于存储before期间您读取的值after。 (在单线程环境中,您甚至可以删除ThreadLocal并使用一个简单的字段。)我还没有针对这一点的一个原因是大多数构造函数拦截都不需要求助于它。如果您说明更详细的用例,我可以帮助您。