I have a class which looks like below
public class HelloWorld{
public void sayHelloWorld(){
System.out.println("Hello World");
}
}
Now I would like to add another method to the HelloWorld class using bytebuddy and add a call to the new method in sayHelloWorld. So hypothetically the class would look like this after bytebuddy does it's magic. (I know that bytebuddy works with bytecode and not java source files. The below code is just for illustration purpose.)
public class HelloWorld{
public void sayHelloWorld(){
System.out.println("Hello World");
sayHelloAgain()
}
public void sayHelloAgain(){
System.out.println("Hello Again")
}
}
- Firstly, is this possible with bytebuddy?
- Secondly, if it is possible, how can I do it? I have understood that bytebuddy can be used to redefine methods, but not modify the method body. Is this true?
It would be great if someone could shed some light on this. TIA!