|
1 | 1 | module MethodSource |
2 | 2 | module SourceLocation |
3 | 3 | module MethodExtensions |
| 4 | + if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/ |
| 5 | + require 'java' |
4 | 6 |
|
5 | | - def trace_func(event, file, line, id, binding, classname) |
6 | | - return unless event == 'call' |
7 | | - set_trace_func nil |
| 7 | + # JRuby version source_location hack |
| 8 | + # @return [Array] A two element array containing the source location of the method |
| 9 | + def source_location |
| 10 | + to_java.source_location(Thread.current.to_java.getContext()) |
| 11 | + end |
| 12 | + else |
8 | 13 |
|
9 | | - @file, @line = file, line |
10 | | - raise :found |
11 | | - end |
12 | 14 |
|
13 | | - private :trace_func |
| 15 | + def trace_func(event, file, line, id, binding, classname) |
| 16 | + return unless event == 'call' |
| 17 | + set_trace_func nil |
| 18 | + |
| 19 | + @file, @line = file, line |
| 20 | + raise :found |
| 21 | + end |
14 | 22 |
|
15 | | - # Return the source location of a method for Ruby 1.8. |
16 | | - # @return [Array] A two element array. First element is the |
17 | | - # file, second element is the line in the file where the |
18 | | - # method definition is found. |
19 | | - def source_location |
20 | | - if @file.nil? |
21 | | - args =[*(1..(arity<-1 ? -arity-1 : arity ))] |
| 23 | + private :trace_func |
22 | 24 |
|
23 | | - set_trace_func method(:trace_func).to_proc |
24 | | - call(*args) rescue nil |
25 | | - set_trace_func nil |
26 | | - @file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file)) |
| 25 | + # Return the source location of a method for Ruby 1.8. |
| 26 | + # @return [Array] A two element array. First element is the |
| 27 | + # file, second element is the line in the file where the |
| 28 | + # method definition is found. |
| 29 | + def source_location |
| 30 | + if @file.nil? |
| 31 | + args =[*(1..(arity<-1 ? -arity-1 : arity ))] |
| 32 | + |
| 33 | + set_trace_func method(:trace_func).to_proc |
| 34 | + call(*args) rescue nil |
| 35 | + set_trace_func nil |
| 36 | + @file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file)) |
| 37 | + end |
| 38 | + return [@file, @line] if File.exist?(@file.to_s) |
27 | 39 | end |
28 | | - return [@file, @line] if File.exist?(@file.to_s) |
29 | 40 | end |
30 | 41 | end |
31 | 42 |
|
@@ -56,43 +67,54 @@ def source_location |
56 | 67 | end |
57 | 68 |
|
58 | 69 | module UnboundMethodExtensions |
| 70 | + if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/ |
| 71 | + require 'java' |
59 | 72 |
|
60 | | - # Return the source location of an instance method for Ruby 1.8. |
61 | | - # @return [Array] A two element array. First element is the |
62 | | - # file, second element is the line in the file where the |
63 | | - # method definition is found. |
64 | | - def source_location |
65 | | - klass = case owner |
66 | | - when Class |
67 | | - owner |
68 | | - when Module |
69 | | - method_owner = owner |
70 | | - Class.new { include(method_owner) } |
71 | | - end |
72 | | - |
73 | | - # deal with immediate values |
74 | | - case |
75 | | - when klass == Symbol |
76 | | - return :a.method(name).source_location |
77 | | - when klass == Fixnum |
78 | | - return 0.method(name).source_location |
79 | | - when klass == TrueClass |
80 | | - return true.method(name).source_location |
81 | | - when klass == FalseClass |
82 | | - return false.method(name).source_location |
83 | | - when klass == NilClass |
84 | | - return nil.method(name).source_location |
| 73 | + # JRuby version source_location hack |
| 74 | + # @return [Array] A two element array containing the source location of the method |
| 75 | + def source_location |
| 76 | + to_java.source_location(Thread.current.to_java.getContext()) |
85 | 77 | end |
| 78 | + else |
| 79 | + |
| 80 | + |
| 81 | + # Return the source location of an instance method for Ruby 1.8. |
| 82 | + # @return [Array] A two element array. First element is the |
| 83 | + # file, second element is the line in the file where the |
| 84 | + # method definition is found. |
| 85 | + def source_location |
| 86 | + klass = case owner |
| 87 | + when Class |
| 88 | + owner |
| 89 | + when Module |
| 90 | + method_owner = owner |
| 91 | + Class.new { include(method_owner) } |
| 92 | + end |
| 93 | + |
| 94 | + # deal with immediate values |
| 95 | + case |
| 96 | + when klass == Symbol |
| 97 | + return :a.method(name).source_location |
| 98 | + when klass == Fixnum |
| 99 | + return 0.method(name).source_location |
| 100 | + when klass == TrueClass |
| 101 | + return true.method(name).source_location |
| 102 | + when klass == FalseClass |
| 103 | + return false.method(name).source_location |
| 104 | + when klass == NilClass |
| 105 | + return nil.method(name).source_location |
| 106 | + end |
86 | 107 |
|
87 | | - begin |
88 | | - klass.allocate.method(name).source_location |
89 | | - rescue TypeError |
| 108 | + begin |
| 109 | + klass.allocate.method(name).source_location |
| 110 | + rescue TypeError |
90 | 111 |
|
91 | | - # Assume we are dealing with a Singleton Class: |
92 | | - # 1. Get the instance object |
93 | | - # 2. Forward the source_location lookup to the instance |
94 | | - instance ||= ObjectSpace.each_object(owner).first |
95 | | - instance.method(name).source_location |
| 112 | + # Assume we are dealing with a Singleton Class: |
| 113 | + # 1. Get the instance object |
| 114 | + # 2. Forward the source_location lookup to the instance |
| 115 | + instance ||= ObjectSpace.each_object(owner).first |
| 116 | + instance.method(name).source_location |
| 117 | + end |
96 | 118 | end |
97 | 119 | end |
98 | 120 | end |
|
0 commit comments