Closed kberezin-nshl closed 6 days 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.
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:
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:but I have no clue how can I do something like this:
I know I can call
defineMethod
,defineField
, etc, but I don't want to do it manually for every single method inChild
because those are really complicated.Thanks!