0

I am using ByteBuddy to substitute a field reference by another one into methods of a class. In another question I was suggested to use the class net.bytebuddy.asm.MemberSubstitution. I have been looking for examples about how to use this in mi java agent but a could not find any.

I want to use the following code:

MemberSubstitution.relaxed()
  .field(ElementMatchers.named("oneField"))
  .onRead()
  .replaceWith(otherField);

My agent is:

public class MyAgent {

    public static void premain(String arg, Instrumentation inst)
            throws Exception {
        File temp = Files.createTempDirectory("tmp").toFile();
        ClassInjector.UsingInstrumentation.of(temp,
                ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, inst)
                .inject(Collections.singletonMap(
                        new TypeDescription.ForLoadedType(MyInterceptor.class),
                        ClassFileLocator.ForClassLoader.read(MyInterceptor.class).resolve()));

        new AgentBuilder.Default()
                .enableBootstrapInjection(inst, temp)
                .type(isAnnotatedWith(CriptoObject.class))
                .transform(new AgentBuilder.Transformer() {
                    @Override
                    public DynamicType.Builder<?> transform(
                            DynamicType.Builder<?> builder,
                            TypeDescription typeDescription,
                            ClassLoader classLoader,
                            JavaModule module) {
                        return builder.method(any())
                                .intercept(MethodDelegation.to(MyInterceptor.class)
                                .andThen(SuperMethodCall.INSTANCE));
                    }
                }).installOn(inst);
    }
}

The interceptor code is

public class MyInterceptor {

    public static void intercept(@Origin Method m, @This Object o)
            throws Exception {

        System.out.println("Intercepted!" + o.getClass().getField("b").get(o));
    }
}

I don't know where to place the MemberSubstitution code to transform a specific method.

Please, can someone help me with this?

Thank in advance

  • [Questions asking us to recommend or find a book, tool, software library, **tutorial or other off-site resource are off-topic** for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.](http://meta.stackoverflow.com/questions/254393) –  Aug 01 '18 at 16:02

1 Answers1

2

Its a visitor that transforms existing code without adding anything where you register it via:

builder = builder.visit(MemberSubstitution.relaxed()
  .field(ElementMatchers.named("oneField"))
  .onRead()
  .replaceWith(otherField)
  .on(method(any()));
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • Hello Rafael. I'm sorry I do not understand. If I try to implement that in my code, **builder.method(any())** returns an instance of **ImplementationDefinition ** and does not have the method **visit(MethodVisitorWrapper visitor)**. Can you tell me if there's something I'm missing? – Luis O. Mangioni Aug 10 '18 at 16:12
  • Sorry, missed that. I finally updated the code. My bad. – Rafael Winterhalter Oct 30 '19 at 10:49