Class: AppMap::Hook::Method

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook_class, hook_method) ⇒ Method

Returns a new instance of Method.



19
20
21
22
23
24
25
26
# File 'lib/appmap/hook/method.rb', line 19

def initialize(hook_class, hook_method)
  @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.



4
5
6
# File 'lib/appmap/hook/method.rb', line 4

def hook_class
  @hook_class
end

#hook_methodObject (readonly)

Returns the value of attribute hook_method.



4
5
6
# File 'lib/appmap/hook/method.rb', line 4

def hook_method
  @hook_method
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).



9
10
11
# File 'lib/appmap/hook/method.rb', line 9

def method_display_name
  @method_display_name
end

Instance Method Details

#activateObject



28
29
30
31
32
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
# File 'lib/appmap/hook/method.rb', line 28

def activate
  if Hook::LOG
    msg = if method_display_name
            "#{method_display_name}"
          else
            "#{hook_method.name} (class resolution deferrred)"
          end
    warn "AppMap: Hooking " + msg
  end

  defined_class = @defined_class
  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_class.define_method hook_method.name do |*args, &block|
    instance_method = hook_method.bind(self).to_proc

    # 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

    hook_disabled = Thread.current[HOOK_DISABLE_KEY]
    enabled = true if !hook_disabled && AppMap.tracing.enabled?
    return instance_method.call(*args, &block) unless enabled

    call_event, start_time = with_disabled_hook.() do
      before_hook.(self, defined_class, args)
    end
    return_value = nil
    exception = nil
    begin
      return_value = instance_method.(*args, &block)
    rescue
      exception = $ERROR_INFO
      raise
    ensure
      with_disabled_hook.() do
        after_hook.(call_event, start_time, return_value, exception)
      end
    end
  end
end