简体   繁体   English

添加到jruby类的getDeclaredMethods()

[英]adding to a jruby class's getDeclaredMethods()

I have a JRuby class that inherits from a java class (java.util.HashMap, for example). 我有一个从Java类继承的JRuby类(例如,java.util.HashMap)。 3rd party java code is calling a reflective method like getDeclaredMethods() on my class's java instance's getClass() type. 第三方Java代码正在类的Java实例的getClass()类型上调用类似getDeclaredMethods()的反射方法。 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. 我需要将我在ruby类(HM)中定义的方法推送到这些“声明的方法”中,然后将其转换为Java,以便它们出现在第三方Java类中。 Anyone know a way? 有人知道吗? Here's my jruby code: 这是我的jruby代码:

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 http://jira.codehaus.org/browse/JRUBY-6105

There are two ways to create java classes https://github.com/jruby/jruby/wiki/GeneratingJavaClasses 有两种创建Java类的方法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? 这个问题类似。我可以在ruby中定义Java子类,然后在Java中实例化它吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM