adding to a jruby class's getDeclaredMethods()

混江龙づ霸主 提交于 2019-12-23 04:25:09

问题


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

回答1:


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?



来源:https://stackoverflow.com/questions/18368726/adding-to-a-jruby-classs-getdeclaredmethods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!