I have a JRuby class that inherits from a java class (java.util.HashMap, for example). 3rd party java code is calling a reflective method like getDeclaredMethods()
on my class's java instance's getClass()
type. I need to push my method(s) defined in my ruby class (HM) into these "declared methods" before the it gets translated to java so they appear to the 3rd party java class. Anyone know a way? Here's my jruby code:
require 'java'
class HM < java.util.HashMap; end
hm = HM.new
puts hm.getClass()
# => org.jruby.proxy.java.util.HashMap$ProxyO
# a third party will make the following call:
puts hm.getClass().getDeclaredMethods().count
# => 2 methods
HM.class_eval do ; def value_at_key(key); return self[key]; end; end
puts hm.getClass().getDeclaredMethods().count
# => still 2 methods
Maybe this bug is currently stopping you? http://jira.codehaus.org/browse/JRUBY-6105
There are two ways to create java classes https://github.com/jruby/jruby/wiki/GeneratingJavaClasses
I've tried both ways and am getting nil as described in the bug.
This question is similar Can I define a Java subclass in ruby, then instantiate it in Java?