raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.23k stars 804 forks source link

Question: copy class hierarchy in an environment without Java Agent #1590

Closed kberezin-nshl closed 6 days ago

kberezin-nshl commented 7 months ago

Hi, I think I am missing something very obvious but I couldn't figure it out myself. The thing I want to do is the following:

public class Parent {
   // method I want to override, but I can't, so I have to redefine the whole class because of it
   final String importantMethod(String s) {
      return s + "foo";
   }

   public void complicatedFunction() {
      // pretty complicated method that I don't want to copy-paste with 100 lines of code or something, and then
      var importantResult = importantMethod(someString);
      // another 100 lines of code or something
   }
}

public class Child extends Parent {
    // pretty complicated subclass that I don't want to copy-paste
} 

I don't have an option to use Java agent so I can't replace classes, however I would like to copy them with Parent.importantMethod to be changed. Here's how I can do what I need with parent:

var parentClass =
        new ByteBuddy()
            .redefine(Parent.class)
            .suffix("_hacked")
            .method(
                ElementMatchers.named("importantMethod")
                    .and(ElementMatchers.isFinal())
                    .and(ElementMatchers.isDeclaredBy(Parent.class))
                    .and(ElementMatchers.returns(String.class)))
            .intercept(FixedValue.value("The value I really want to use!"))
            .make()
            .load(someClassLoader)
            .getLoaded();

but I have no clue how can I do something like this:

var childClass = 
    new ByteBuddy()
        .subclass(parentClass)
        .copyImplementationFrom(Child.class)
        ...

I know I can call defineMethod, defineField, etc, but I don't want to do it manually for every single method in Child because those are really complicated.

Thanks!

dogourd commented 7 months ago

ByteBuddy does not directly provide an API for this operation. You need to leverage the visitors provided by ASM to achieve this. You can refer to the suggested approach here for specifics. How to copy method body of a particular method to another method in different class using ASM Similarly, you may encounter the side effects mentioned therein.

If you're only dealing with the specific requirement described in the issue, you could consider achieving it through redefine + visit, for example:

new ByteBuddy()
   .redefine(Child.class)
   .name("child2")
   .visit(new AsmVisitorWrapper.AbstractBase() {
                    @Override
                    public ClassVisitor wrap(TypeDescription instrumentedType, ClassVisitor classVisitor, Implementation.Context implementationContext, TypePool typePool, FieldList<FieldDescription.InDefinedShape> fields, MethodList<?> methods, int writerFlags, int readerFlags) {
                        return new ClassVisitor(OpenedClassReader.ASM_API, classVisitor) {
                            @Override
                            public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
                                super.visit(version, access, name, signature, Type.getInternalName(parentClass), interfaces);
                            }
                        };
                    }
                });

However, it still cannot avoid the potential errors mentioned in the link above, so you must be clear about what you are doing and have the ability to handle those issues on your own.