Bytebuddy:根据名称而不是 class 在接口上应用转换

Bytebuddy: apply a transform on an iterface based on name rather than class

我想在实现某个接口的 classes 上应用转换,但由于 class 加载问题,我想按名称而不是提供 class .有办法吗?

我的意思是,而不是:

new AgentBuilder.Default()
        .disableClassFormatChanges()
        .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
        .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
        .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
        .type(isSubTypeOf(myClass.class).and(not(isAbstract())).and(not(isInterface())))
        .transform(new AgentBuilder.Transformer.ForAdvice()
                .advice((ElementMatchers.named("method")),
                        "adviceClass"))
        .installOn(inst);

我想做这样的事情(注意下面的 isSubTypeOf()):

new AgentBuilder.Default()
        .disableClassFormatChanges()
        .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
        .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
        .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
        .type(isSubTypeOf(**TypeDescription.ForLoadedType.ofName(className)**).and(not(isAbstract())).and(not(isInterface())))
        .transform(new AgentBuilder.Transformer.ForAdvice()
                .advice((ElementMatchers.named("method")),
                        "adviceClass"))
        .installOn(inst);

有办法吗?

有一个匹配器:hasSuperType(named(className))。或者,您可以实现自己的匹配器并仅在类型层次结构中导航。这可能会稍微更有效率,例如,如果您知道所讨论的类型不是接口。