1

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 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that might be it. I used the link you posted and tried the same solution posed there and ran into weird problems - in this case it didn't seem to recognize the VBox proxy (from javafx) as a valid argument. I will try to see if I can get that working or see if I can't re-open the array returned of declared methods (complete hack, but i'm still open to suggestions...). class Controller < Java::javafx.scene.layout.VBox; end: #<NameError: no method 'rubyToJava' for arguments (org.jruby.proxy.javafx.scene.layout.VBox$Proxy2) on Java::OrgJrubyJavasupport::JavaEmbedUtils>
I added some clarifications to the question based on feedback from the similar post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.