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
https://gist.github.com/boberetezeke/ba919d3e45fca0f18a2d.

Thanks for any help.

Steve


“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)
end

def pre
puts “before draw”
parent.background(100)
end

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

end

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): Test of JRuby/Java reflection · GitHub

“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
cmp_cc.sh…

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

–rt

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!