Understanding Java/JRuby reflection

Hello all,

I am trying to use a Java library/engine called Activiti. It can
callback into the library that uses it by supplying a class name and
implementing an interface. I have not been successful in doing that so I
thought I would try to do some simple java tests to see how all this
works and I am confused. I am a java novice, so please point out how
this should be done (if it is possible).

I have a gist that has a top level ruby file (caller_test.rb) that
requires some java code that calls back into either java or jruby. The
four java files (Caller.java, A.java, B.java and C.java) allow me to
test java reflection and then I try to get the classes defined in JRuby.
What is most curious is that if I compile a ruby file to java with
jrubyc and the --java flag, and then use javac with the jruby.jar file
to compile that generated java class, then the reflection sees it. If I
go straight to a class file with jrubyc, then it doesn’t work.

Here is the gist

Thanks for any help.


“You must be the change you want to see in the world.” Mahatma Gandhi

blog: http://steve.stewdle.com/blog
twitter: @boberetezeke

Your link to gist does not work for me, gives 404 (where you editing it
when you posted?). It is not always necessary to pre-compile to java to
use reflection (and supply a class) you can use ‘to_java’ as following:-

in ruby-processing using reflection

require ‘jruby/core_ext’

class TestRegister
attr_reader :parent
def initialize parent
@parent = parent
register = parent.java_method :registerMethod,
[Java::JavaLang::String, java.lang.Object]
register.call(:draw, self)
register.call(:pre, self)

def pre
puts “before draw”

def draw
puts “at begin draw”
parent.fill(200, 100)
parent.ellipse(100, 100, 60, 60)


cls = TestRegister.become_java!

in this case we are sending the self object which needs to be a
java.lang.Object for reflection to work, the to_java does the compile
for you at runtime. The methods pre and draw are registered (by
reflection) on java side at runtime.

Here is the gist link again (it works for me - when not logged into
github): https://gist.github.com/boberetezeke/ba919d3e45fca0f18a2d

“You must be the change you want to see in the world.” Mahatma Gandhi

blog: http://steve.stewdle.com/blog
twitter: @boberetezeke

To what file/line are you referring to?

OK, I had some time to look at your problems here. It seems like you may
need to run jrubyc with an additional flag.

I created this repo to show you my changes. Look at the shell script


Most of your other classes stayed the same other than changing some log
output messages.


On Wed, Jun 11, 2014 at 1:38 PM, Steve T. [email protected]

Aha looks like you are sending a String when the class object is
required! Not much point converting it to a java object if you are
sending it as String no!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs