Module: JRSplenda::MethodHelper

Defined in:
lib/jrsplenda/jrsplenda_method_helper.rb

Instance Method Summary collapse

Instance Method Details

#wrap_java_methods(object) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jrsplenda/jrsplenda_method_helper.rb', line 7

def wrap_java_methods(object)
  if object.is_a?(Class)
    klass = object
    message = :java_class
    method_type = :declared_class_methods
  else 
    klass = object.class
    message = :java_object
    method_type = :declared_instance_methods
  end
  uniq_method_names = klass.java_class.send(method_type).collect(&:name)
  uniq_method_names.each do |method_name|
    method = klass.java_class.declared_method_smart(method_name) 
    next if method.modifiers & (Modifier::PUBLIC | Modifier::FINAL) != 0
    ruby_method_name = "#{method.name.underscore}"
    object.singleton_class.send :define_method, ruby_method_name do |*args|
      method.accessible = true
      if method.modifiers & Modifier::STATIC != 0
        method.invoke_static(*args)
      else
        method.invoke(send(message), *args)
      end
      method.accessible = false
    end
    object.singleton_class.send(:alias_method, method_name, ruby_method_name)
  end
end