Module: Jtor::StdLib::Base::ClassMethods
- Defined in:
- lib/jtor-stdlib/base.rb
Instance Method Summary collapse
- #_constructor_calls ⇒ Object
- #_initializers ⇒ Object
- #_lookup_table ⇒ Object
- #_lookup_table_static ⇒ Object
- #add_java_constructor(param_types, constructor_call = nil, &block) ⇒ Object
-
#add_java_initializer(&block) ⇒ Object
For field initializers.
-
#add_java_method(name, param_types, &block) ⇒ Object
Method overloading is not a ruby thing, so we implement a pseudo-lookup mechanism for method based on the type/count of their args.
- #add_method_to_lookup(lookup_table, name, param_types, &block) ⇒ Object
- #add_static_java_method(name, param_types, &block) ⇒ Object
- #lookup(name, *args) ⇒ Object
- #params_match(types, values) ⇒ Object
Instance Method Details
#_constructor_calls ⇒ Object
38 39 40 |
# File 'lib/jtor-stdlib/base.rb', line 38 def _constructor_calls @_constructor_calls ||= {} end |
#_initializers ⇒ Object
42 43 44 |
# File 'lib/jtor-stdlib/base.rb', line 42 def _initializers @initializers ||= [] end |
#_lookup_table ⇒ Object
30 31 32 |
# File 'lib/jtor-stdlib/base.rb', line 30 def _lookup_table @_lookup_table ||= {} end |
#_lookup_table_static ⇒ Object
34 35 36 |
# File 'lib/jtor-stdlib/base.rb', line 34 def _lookup_table_static @_lookup_table_static ||= {} end |
#add_java_constructor(param_types, constructor_call = nil, &block) ⇒ Object
71 72 73 74 |
# File 'lib/jtor-stdlib/base.rb', line 71 def add_java_constructor(param_types, constructor_call = nil, &block) add_method_to_lookup(_lookup_table, :initialize, param_types, &block) _constructor_calls[param_types] = constructor_call end |
#add_java_initializer(&block) ⇒ Object
For field initializers
47 48 49 |
# File 'lib/jtor-stdlib/base.rb', line 47 def add_java_initializer(&block) _initializers << block end |
#add_java_method(name, param_types, &block) ⇒ Object
Method overloading is not a ruby thing, so we implement a pseudo-lookup mechanism for method based on the type/count of their args
53 54 55 56 57 58 59 60 |
# File 'lib/jtor-stdlib/base.rb', line 53 def add_java_method(name, param_types, &block) previously_defined = add_method_to_lookup(_lookup_table, name, param_types, &block) return if previously_defined define_method(name) do |*args| instance_exec(*args, &self.class.lookup(name, *args)) end end |
#add_method_to_lookup(lookup_table, name, param_types, &block) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/jtor-stdlib/base.rb', line 103 def add_method_to_lookup(lookup_table, name, param_types, &block) previously_defined = lookup_table[name] lookup_table[name] ||= {} lookup_table[name][param_types] = block previously_defined end |
#add_static_java_method(name, param_types, &block) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/jtor-stdlib/base.rb', line 62 def add_static_java_method(name, param_types, &block) previously_defined = add_method_to_lookup(_lookup_table_static, name, param_types, &block) return if previously_defined define_singleton_method(name) do |*args| instance_exec(*args, &lookup(name, *args)) end end |
#lookup(name, *args) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/jtor-stdlib/base.rb', line 76 def lookup(name, *args) if _lookup_table[name] _lookup_table[name].each do |param_types, method| # NOTE: This is an oversimplification of the way Java determines # which method to invoke. I may work further into this later (see # http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12) next unless params_match(param_types, args) return (if (constructor_call = _constructor_calls[param_types]) [method, constructor_call] else method end) end end method_missing(name, *args) end |
#params_match(types, values) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/jtor-stdlib/base.rb', line 93 def params_match(types, values) # TODO: Handle var args (...) return false if types.size != values.size types.each_with_index do |type, index| value = values[index] return false unless value.is_a?(type) || value.to_java.is_a?(type) end true end |