Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kberezin-nshl opened this issue Feb 8, 2024 · 1 comment
Open
Assignees
Labels
Milestone

Comments

@kberezin-nshl
Copy link

kberezin-nshl commented Feb 8, 2024

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
Copy link

dogourd commented Feb 23, 2024

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.

@raphw raphw self-assigned this Feb 24, 2024
@raphw raphw added the question label Feb 24, 2024
@raphw raphw added this to the 1.14.12 milestone Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants