support bootstrap method coping when using code coping by likey3 · Pull Request #480 · jboss-javassist/javassist (original) (raw)

Hello, I'm using javassist recently, and I really like the function it provides to copy methods, this makes it very convenient for me to copy the specified method to the target method without having to worry about bytecode writing.

However, during use, it was found that if the method contains lambda expressions or the simple string concatenation(after jdk>=9,after JDK 9, because string concatenation uses BootstrapMethods), it will cause compilation errors or runtime errors.
I looked through the relevant information but not much. I also saw someone mentioning issue(#388 ) related to this.

Cause:

I looked at the source code myself and found that when copying the invoke dynamic operator, only the data of the constant pool was copied, and the bootstrap method was not copied.This will cause errors if there is no corresponding bootstrap method in the target class.

Solution:

When copying the invoke dynamic operator, find the bootstrap method based on the bootstrap method index in the source class and overwrite or add the bootstrap method to the new bootstrap method index in the destination class. In order to prevent the loss of static methods generated by some lambdas, after copying the bootstrap method, the static methods referenced in the source class are also copied to the target class.

I have personally evaluated the new code and it should not affect other operations, and it provides great convenience when using the code copy function. but I am not sure whether it violates the design intention of javassist. Thanks for the discussion and review.