Class: Contrast::Agent::Rewriter Deprecated

Inherits:
Object
  • Object
show all
Includes:
Components::Interface
Defined in:
lib/contrast/agent/rewriter.rb

Overview

Deprecated.

Changes to this class are discouraged as this approach is being phased out with support for those language versions.

Used for Ruby 2.5 to allow us to rewrite those methods which have interpolation in them.

TODO: RUBY-714 remove w/ EOL of 2.5

Constant Summary collapse

SELF_DEFINITION =
'def self.'
DEFINITION =
'def '

Class Method Summary collapse

Methods included from Components::Interface

included

Class Method Details

.rewrite_class(module_data, redo_rewrite = false) ⇒ Object



27
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
# File 'lib/contrast/agent/rewriter.rb', line 27

def rewrite_class module_data, redo_rewrite = false
  mod = module_data.mod
  return unless mod

  status = Contrast::Agent::Patching::Policy::PatchStatus.get_status(mod)
  return if (status.rewritten? || status.rewriting?) && !redo_rewrite

  status.rewriting!

  # default-initialize to nil within top-level method scope
  # so that it's available w/in the `ensure` block
  opener = nil
  with_contrast_scope do
    unless should_rewrite?(module_data)
      status.no_rewrite!
      return
    end

    opener = Contrast::Agent::ClassReopener.new(module_data)
    if opener.nil? || (instance_methods.empty? && singleton_methods.empty?)
      status.no_rewrite!
      return
    end

    rewrite_all_methods(opener, mod, mod.public_instance_methods(false), :PUBLIC_INSTANCE)
    rewrite_all_methods(opener, mod, mod.protected_instance_methods(false), :PROTECTED_INSTANCE)
    rewrite_all_methods(opener, mod, mod.private_instance_methods(false), :PRIVATE_INSTANCE)
    rewrite_all_methods(opener, mod, mod.singleton_methods(false), :SINGLETON)
    status.rewritten!
  end
rescue SyntaxError, StandardError => e
  opener = nil
  mod ||= module_data.mod
  logger.debug('Reopening threw a handled exception - skipping rewriting', e, module: module_data.name)
  status ||= Contrast::Agent::Patching::Policy::PatchStatus.get_status(mod)
  status.failed_rewrite!
ensure
  opener&.commit_patches
  logger.trace('Rewriting complete',
               module: module_data.name,
               result: Contrast::Agent::Patching::Policy::PatchStatus.get_status(
                   module_data.mod).rewrite_status)
end