ByteBuddy 的 ElementMatchers#nameStartsWith 使用什么算法?

What algorithm does ByteBuddy's ElementMatchers#nameStartsWith use?

假设我想将 @Advice.OnMethodEnter 应用于 org.springframework.web.context.support.GenericWebApplicationContext 中声明的方法。为此,我编写了这个最小代理:

public class SequenceAgent {

  public static void premain(final String args,
                             final Instrumentation instrumentation) {
    new AgentBuilder.Default()
        .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
        .type(nameStartsWith(
            "org.springframework.web.context.support.GenericWebApplicationContext"))
        .transform((builder, typeDescription, classLoader, module) -> builder
            .method(any()).intercept(Advice.to(SequenceAdvice.class)))
        .installOn(instrumentation);
  }

  public static class SequenceAdvice {

    @Advice.OnMethodEnter
    static void enter(@Advice.This Object thiz, @Advice.Origin Method method,
                      @Advice.AllArguments Object... args) {
      String className = thiz.getClass().getName();
      String methodName = method.getName();

      System.out.println("Entered: " + className + "#" + methodName);
    }
  }
}

我预计此配置会过滤掉 org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext,因为它与 org.springframework.web.context.support.GenericWebApplicationContext 不匹配,但看起来对此 class 的对象的方法调用也被拦截:

import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;

public class AgentTest {

    public static void main(String[] args) {
        AnnotationConfigServletWebServerApplicationContext context =
                new AnnotationConfigServletWebServerApplicationContext();

        context.containsBean("SomeBean");
    }

}

当附加到代理和 运行 时,它会打印出来(为了便于阅读而换行):

Entered: org.springframework.boot.web.servlet.context.
    AnnotationConfigServletWebServerApplicationContext
    #getResourcePatternResolver
.
.
.
Entered: org.springframework.boot.web.servlet.context.
    AnnotationConfigServletWebServerApplicationContext
    #getResourceCache

Entered: org.springframework.boot.web.servlet.context.
    AnnotationConfigServletWebServerApplicationContext
    #getClassLoader

Entered: org.springframework.boot.web.servlet.context.
    AnnotationConfigServletWebServerApplicationContext
    #containsBean

org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServe.ApplicationContext 的 class 层次结构是:

org.springframework.core.io.DefaultResourceLoader
        ⇧
org.springframework.context.support.AbstractApplicationContext
        ⇧
org.springframework.context.support.GenericApplicationContext
        ⇧
⤏⤏⤏⤏ org.springframework.web.context.support.GenericWebApplicationContext
        ⇧
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext
        ⇧
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServe.ApplicationContext

并且 containsBean 方法在以下位置声明并覆盖:

org.springframework.beans.factory.BeanFactory#containsBean
        ⇧
org.springframework.context.support.AbstractApplicationContext#containsBean

为什么 containsBean@Advice.This Object thiz 解析为 org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext 而不是 org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext

字节好友使用String.startsWith.

您所看到的是由于 Byte Buddy 检测 classes,而不是实例。在某种程度上,想想 Byte Buddy 将通知代码复制到目标方法中。

因此,所有子class都会受到影响。为了做你正在做的事情,你需要在调用期间检查实例 class 的类型,就像你想在 Java 中实现它一样。