有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java前后构造函数拦截器

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

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

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

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) 个答案

  1. # 1 楼答案

    Java虚拟机的验证器需要从任何已实现的构造函数中调用另一个构造函数。因此,不幸的是,使用@SuperCall来实现around建议是行不通的。事实上,@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字段,在该字段中存储在after期间读取的before中的值。(在单线程环境中,您甚至可以删除ThreadLocal并使用一个简单字段。)我还没有针对这一点的一个原因是,大多数构造函数拦截不需要adivce。如果您陈述一个更详细的用例,我可能会进一步帮助您