Class MethodGrafter


  • public final class MethodGrafter
    extends Object
    Copies a method from one ClassFile into another, re-resolving its constant-pool references into the target pool.
    • Method Detail

      • graftMethod

        public static MethodEntry graftMethod​(ClassFile source,
                                              MethodEntry method,
                                              ClassFile target)
        Grafts method from source into target as a brand-new method, returning the new method entry on the target.
        Parameters:
        source - the class file the method currently lives in
        method - the method to copy
        target - the class file to copy it into
        Returns:
        the newly created method on target
      • replaceMethodBody

        public static void replaceMethodBody​(ClassFile source,
                                             MethodEntry sourceMethod,
                                             ClassFile target,
                                             MethodEntry targetMethod)
        Replaces targetMethod's body in place with sourceMethod's, remapping every constant-pool reference into the target.
        Parameters:
        source - the class file sourceMethod lives in
        sourceMethod - the method whose body to copy
        target - the class file targetMethod lives in
        targetMethod - the method on target to overwrite (typically same name and descriptor)