module T::Private::Methods

def self._on_method_added(hook_mod, method_name, is_singleton_method: false)

Only public because it needs to get called below inside the replace_method blocks below.
def self._on_method_added(hook_mod, method_name, is_singleton_method: false)
  if T::Private::DeclState.current.skip_on_method_added
    return
  end
  current_declaration = T::Private::DeclState.current.active_declaration
  mod = is_singleton_method ? hook_mod.singleton_class : hook_mod
  if T::Private::Final.final_module?(mod) && (current_declaration.nil? || !current_declaration.final)
    raise "#{mod} was declared as final but its method `#{method_name}` was not declared as final"
  end
  _check_final_ancestors(mod, mod.ancestors, [method_name])
  if current_declaration.nil?
    return
  end
  T::Private::DeclState.current.reset!
  if method_name == :method_added || method_name == :singleton_method_added
    raise(
      "Putting a `sig` on `#{method_name}` is not supported" +
      " (sorbet-runtime uses this method internally to perform `sig` validation logic)"
    )
  end
  original_method = mod.instance_method(method_name)
  sig_block = lambda do
    T::Private::Methods.run_sig(hook_mod, method_name, original_method, current_declaration)
  end
  # Always replace the original method with this wrapper,
  # which is called only on the *first* invocation.
  # This wrapper is very slow, so it will subsequently re-wrap with a much faster wrapper
  # (or unwrap back to the original method).
  new_method = nil
  T::Private::ClassUtils.replace_method(mod, method_name) do |*args, &blk|
    if !T::Private::Methods.has_sig_block_for_method(new_method)
      # This should only happen if the user used alias_method to grab a handle
      # to the original pre-unwound `sig` method. I guess we'll just proxy the
      # call forever since we don't know who is holding onto this handle to
      # replace it.
      new_new_method = mod.instance_method(method_name)
      if new_method == new_new_method
        raise "`sig` not present for method `#{method_name}` but you're trying to run it anyways. " \
        "This should only be executed if you used `alias_method` to grab a handle to a method after `sig`ing it, but that clearly isn't what you are doing. " \
        "Maybe look to see if an exception was thrown in your `sig` lambda or somehow else your `sig` wasn't actually applied to the method. " \
        "Contact #dev-productivity if you're really stuck."
      end
      return new_new_method.bind(self).call(*args, &blk)
    end
    method_sig = T::Private::Methods.run_sig_block_for_method(new_method)
    # Should be the same logic as CallValidation.wrap_method_if_needed but we
    # don't want that extra layer of indirection in the callstack
    if method_sig.mode == T::Private::Methods::Modes.abstract
      # We're in an interface method, keep going up the chain
      if defined?(super)
        super(*args, &blk)
      else
        raise NotImplementedError.new("The method `#{method_sig.method_name}` on #{mod} is declared as `abstract`. It does not have an implementation.")
      end
    # Note, this logic is duplicated (intentionally, for micro-perf) at `CallValidation.wrap_method_if_needed`,
    # make sure to keep changes in sync.
    elsif method_sig.check_level == :always || (method_sig.check_level == :tests && T::Private::RuntimeLevels.check_tests?)
      CallValidation.validate_call(self, original_method, method_sig, args, blk)
    else
      original_method.bind(self).call(*args, &blk)
    end
  end
  new_method = mod.instance_method(method_name)
  key = method_to_key(new_method)
  @sig_wrappers[key] = sig_block
  if current_declaration.final
    add_final_method(key)
    # use hook_mod, not mod, because for example, we want class C to be marked as having final if we def C.foo as
    # final. change this to mod to see some final_method tests fail.
    add_module_with_final(hook_mod)
  end
end