Redefine a specific instance with ByteBuddy

I'm quite new to ByteBuddy and may lack some points here.

I want to redefine a specific instance or more precise, instances of a Type that are instantiated in a specific class with ByteBuddy.

So if I have a class A which declares a variable of Type C and a class B which also declares a variable C, I want just to redefine the instance created in A and leave the original implementation in B. (e.g. just redefine objects which passes an equals check / object == object)

I tried the following, which does not work because the equals check goes against a TypeDescription not against the object in the ElementMatcher.is() method:

Object myObject = new Object();
    new AgentBuilder.Default()
                    .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
                    .with(AgentBuilder.Listener.StreamWriting.toSystemError().withTransformationsOnly())
                    .disableClassFormatChanges()
                    .type(ElementMatchers.is(Object.class))
                    .and(ElementMatchers.is(myObject))
                    .transform((builder, typeDescription, classLoader, module) ->
                                       builder.visit(
                                               //Bind all necessary attributes here                                                
                    .installOn(instrumentation);

Is there any way to achieve that? I didn't find anything regarding to ElementMatchers.



Read more here: https://stackoverflow.com/questions/67383039/redefine-a-specific-instance-with-bytebuddy

Content Attribution

This content was originally published by David Witte at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: