0

I want to apply a transform on classes implementing a certain interface, but due to class loading issues I want to do it by name and not by providing the class. Is there a way to do that?

What I mean is that instead of:

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

I want to do something like this (note the isSubTypeOf() below):

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

Is there a way to do that?

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
Amirrrrr
  • 63
  • 4
  • Don't you need to use reflection for that ? But if you are going to use reflection, and runtime code generation, you might as well use a language which is at its heart built for that. – nicolas Jan 09 '19 at 09:17
  • If providing the class would require a compile time dependency, reflection would solve that, but require a run-time dependency which i'm trying to avoid. Since bytebuddy is going over all the classes being loaded (or classes already loaded) I would have liked it to match the name instead of the type... – Amirrrrr Jan 09 '19 at 09:25

1 Answers1

0

There is a matcher for that: hasSuperType(named(className)). Alternatively, you can implement your own matcher and just navigate the type hierarchy. This can be slightly more efficient, for example if you know that the type in question is not an interface.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192