Forum: JRuby Understanding Java/JRuby reflection

2f2aae23dc89cd45f9da28f2b9497288?d=identicon&s=25 Steve (Gmail) (Guest)
on 2014-06-10 19:44
(Received via mailing list)
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/ba919d3e45fca....

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
712effd8673463d44a730bac4cd29311?d=identicon&s=25 Martin Prout (monkstone)
on 2014-06-11 14:50
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.
2f2aae23dc89cd45f9da28f2b9497288?d=identicon&s=25 Steve (Gmail) (Guest)
on 2014-06-11 18:23
(Received via mailing list)
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
712effd8673463d44a730bac4cd29311?d=identicon&s=25 Martin Prout (monkstone)
on 2014-06-11 20:06
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!
2f2aae23dc89cd45f9da28f2b9497288?d=identicon&s=25 Steve Tuckner (Guest)
on 2014-06-11 20:40
(Received via mailing list)
To what file/line are you referring to?
479e3575dd768767dcc6cebab579239a?d=identicon&s=25 Robert Tomb (Guest)
on 2014-06-23 03:08
(Received via mailing list)
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...

https://github.com/bikeonastick/steve_callback

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 Tuckner <stevetuckner@gmail.com>
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.