I have two solutions.  The first will fix it the way you are doing it
and the second is using Java reflection itself.  More discussion below
examples:

require 'java'

include_class 'Person'
include_class('java.lang.String') {|p,n|  "J" + n }

p = Person.new
p.java_class.declared_fields.collect { |field| field.name }

name = p.java_class.declared_fields[0]

name.accessible=true
name.set_value(p.java_object, JString.new('mike').java_object)
p name.value(p.java_object)

Pure Java Reflection Method:

require 'java'

include_class 'Person'
include_class('java.lang.Class') { |p, n| "J" + n }

p = Person.new
JPerson = JClass.forName('Person')
name = JPerson.getDeclaredField('name')
name.accessible = true

name.set(p.java_object, 'mike')

p name.get(p.java_object)

  In most regards, I think using Java reflection will be a better way
of performing reflective activities.  We are still figuring the best
way of doing this stuff and using Java reflection API's directly
will always work regardless of how we change our Java integration
libraries.  As you can see the code is about the same size too.

  One question people may be thinking is why do we have classes
like JavaClass, JavaObject, JavaMethod, and JavaField?  At some point
long long ago, someone decided to implement much of Java integration
in Ruby itself.  Cool.  However, in order to do that they needed some
simple building blocks to be able to support that.  Those building
blocks are known as low-level Java support and the classes I mentioned
above represent that (plus a few others).

  What most people use when they write stuff in JRuby that needs Java
integration is in src/builtin/javasupport.rb (and a few Java classes).
This support largely makes the Ruby/Java language barrier seem fairly
transparent with a few exceptions.  Inevitably, someone wants to do
some introspective and reflective stuff with Java objects in Ruby and
they notice the low-level java support facilities.  I can hardly blame
them since they have the right name and what appears to be a slightly 
more Ruby-friendly syntax.  By design, those classes do not take the
transparent syntax of high-level integration since these classes are
what implemented high-level integration.

  One challenge we could solve is to provide decent documentation so
people do not go down this path as much.  Another challenge would be
to change this in some fundamental way so that this stuff is not
visible or in existence anymore.  Even the concept of implementing
high-level Java support in Ruby may be costing us quite a bit right
now since the amount of interpretation to do in Ruby->Java dispatching
is high.

  I rambled a bit off of user support...I recommend using Java reflection
directly :)

-Tom


On Mon, 03 Jul 2006, [EMAIL PROTECTED] defenestrated me:
> 
>    Sorry Nick, I've been receiving emails to this list in digest mode, so
>    cannot reply directly to your message, so I pasted it below for
>    context.
>    I am using 0.8.3.
>    The line you suggested also fails, as follows:
>    irb(main):008:0> name.set_value(p.java_object, JString.new('mike'))
>    NoMethodError: undefined method `java_object' for "java.lang":String
>            from (irb):1:in `method_missing'
>            from (irb):1
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:148:in
>    `eval_input'
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:70:in
>    `signal_status'
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:187:in
>    `eval_input'
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:70:in
>    `each_top_level_statement'
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:188:in `loop'
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:188:in `catch'
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:188:in
>    `eval_input'
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:70:in `start'
>            from c:\tools\jruby-0.8.3\bin\jirb:12:in `catch'
>            from c:/tools/jruby-0.8.3/lib/ruby/1.8/irb.rb:71:in `start'
>            from c:\tools\jruby-0.8.3\bin\jirb:12
>    irb(main):009:0>
>    Any more ideas?
>    Thanks very much,
>    Mike.
>    On 6/29/06, [EMAIL PROTECTED]
>    <[EMAIL PROTECTED]> wrote:
>    >
>    > irb(main):015:0> name.set_value(p.java_object, 'mike')
>    > TypeError: not a java object:mike
>    >         from (irb):1:in `set_value'
>    >         from (irb):1
>    This seems like a bug to me.  Until it's fixed, an unfortunate
>    workaround would be to
>    include_class('java.lang.String') {|p,c| "JString"}
>    ...
>    name.set_value(p.java_object, JString.new('mike'))
>    What version of JRuby are you using?  0.8.3 or from CVS?
>    /Nick
> 
> This message and any attachments (the "message") is 
> intended solely for the addressees and is confidential. 
> If you receive this message in error, please delete it and
> immediately notify the sender. Any use not in accord with
> its purpose, any dissemination or disclosure, either whole
> or partial, is prohibited except formal approval. The internet
> can not guarantee the integrity of this message.
> BNP PARIBAS (and its subsidiaries) shall (will) not
> therefore be liable for the message if modified. 
> 
> *******************************************************************************
> ***************
> 
> BNP Paribas Private Bank London Branch is authorised
> by CECEI & AMF and is regulated by the Financial Services
> Authority for the conduct of its investment business in
> the United Kingdom.
> 
> BNP Paribas Securities Services London Branch is authorised 
> by CECEI & AMF and is regulated by the Financial Services 
> Authority for the conduct of its investment business in 
> the United Kingdom.
>   
> BNP Paribas Fund Services UK Limited is authorised and 
> regulated by the Financial Services Authority

> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

> _______________________________________________
> Jruby-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/jruby-user


-- 
+ http://www.tc.umn.edu/~enebo +---- mailto:[EMAIL PROTECTED] ----+
| Thomas E Enebo, Protagonist  | "Luck favors the prepared    |
|                              |  mind." -Louis Pasteur       |

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jruby-devel mailing list
Jruby-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jruby-devel

Reply via email to