Class: AppMap::Hook::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/appmap/hook/method.rb

Constant Summary collapse

ARRAY_OF_EMPTY_HASH =
[{}.freeze].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook_package, hook_class, hook_method) ⇒ Method

Returns a new instance of Method.



23
24
25
26
27
28
29
30
31
# File 'lib/appmap/hook/method.rb', line 23

def initialize(hook_package, hook_class, hook_method)
  @hook_package = hook_package
  @hook_class = hook_class
  @hook_method = hook_method

  # Get the class for the method, if it's known.
  @defined_class, method_symbol = Hook.qualify_method_name(@hook_method)
  @method_display_name = [@defined_class, method_symbol, @hook_method.name].join if @defined_class
end

Instance Attribute Details

#hook_classObject (readonly)

Returns the value of attribute hook_class.



6
7
8
# File 'lib/appmap/hook/method.rb', line 6

def hook_class
  @hook_class
end

#hook_methodObject (readonly)

Returns the value of attribute hook_method.



6
7
8
# File 'lib/appmap/hook/method.rb', line 6

def hook_method
  @hook_method
end

#hook_packageObject (readonly)

Returns the value of attribute hook_package.



6
7
8
# File 'lib/appmap/hook/method.rb', line 6

def hook_package
  @hook_package
end

#method_display_nameObject (readonly)

method_display_name may be nil if name resolution gets deferred until runtime (e.g. for a singleton method on an embedded Struct).



11
12
13
# File 'lib/appmap/hook/method.rb', line 11

def method_display_name
  @method_display_name
end

Instance Method Details

#activateObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/appmap/hook/method.rb', line 33

def activate
  if Hook::LOG
    msg = if method_display_name
            "#{method_display_name}"
          else
            "#{hook_method.name} (class resolution deferred)"
          end
    warn "AppMap: Hooking #{msg} at line #{(hook_method.source_location || []).join(':')}"
  end

  defined_class = @defined_class
  hook_package = self.hook_package
  hook_method = self.hook_method
  before_hook = self.method(:before_hook)
  after_hook = self.method(:after_hook)
  with_disabled_hook = self.method(:with_disabled_hook)

  hook_method_def = Proc.new do |*args, &block|
    instance_method = hook_method.bind(self).to_proc
    call_instance_method = -> {
      # https://github.com/applandinc/appmap-ruby/issues/153
      if Util.ruby_minor_version >= 2.7 && args == ARRAY_OF_EMPTY_HASH && hook_method.arity == 1
        instance_method.call({}, &block)
      else
        instance_method.call(*args, &block)
      end
    }

    # We may not have gotten the class for the method during
    # initialization (e.g. for a singleton method on an embedded
    # struct), so make sure we have it now.
    defined_class, = Hook.qualify_method_name(hook_method) unless defined_class

    reentrant = Thread.current[HOOK_DISABLE_KEY]
    disabled_by_shallow_flag = \
      -> { hook_package&.shallow? && AppMap.tracing.last_package_for_current_thread == hook_package }

    enabled = true if AppMap.tracing.enabled? && !reentrant && !disabled_by_shallow_flag.call

    return call_instance_method.call unless enabled

    call_event, start_time = with_disabled_hook.call do
      before_hook.call(self, defined_class, args)
    end
    return_value = nil
    exception = nil
    begin
      return_value = call_instance_method.call
    rescue
      exception = $ERROR_INFO
      raise
    ensure
      with_disabled_hook.call do
        after_hook.call(self, call_event, start_time, return_value, exception) if call_event
      end
    end
  end
  hook_method_def = hook_method_def.ruby2_keywords if hook_method_def.respond_to?(:ruby2_keywords)

  hook_class.define_method_with_arity(hook_method.name, hook_method.arity, hook_method_def)
end