logo       

Re: java.lang.reflect.Array considered harmful?: msg#00032

lang.jruby.user

Subject: Re: java.lang.reflect.Array considered harmful?

On Tue, 14 Feb 2006, Werner Schuster (murphee) defenestrated me:
>
> Well this reprodcues the error message:
> include_class "java.lang.reflect.Array"
> foo=[]
> foo << 1
>
> Which is clear enough (the reflect.Array seems to replace Rubys Array).

Very strange.

Reflect's Array cannot replace Ruby's Array in the sense that Rubys
Array is already an assigned constant. However, it is possible
our include_class code is changing methods on the builtin Array class
since all class definitions are always open in Ruby.

I suspect we should be throwing some type of Error during include_class
to halt this problem in its tracks (and make sure we are internally not
mutilating the original class we had a conflict with by adding proxy methods
to it).

I will look at this after work today. The above is an educated
guess, but it looks like a pretty good explanation.

In any case, you should use the expanded form of include_class:

include_class('java.lang.reflect.Array') {|p,n| "J" . n }

> The problematic JMX Code I'm trying to use is this ('mb' is an instance
> of com.sun.jmx.mbeanserver.JmxMBeanServer):
> mb.invoke(mem.objectName, "getThreadUserTime", [], [])
>
> The first [] must be an Object[], the second one must be a String[].
> The Object[] doesn't seem to be a problem; but no matter what I do,
> the second parameter is always turned into a RubyArray instance.
>
> I even tried hacks like calling
> x = StringBuffer.new("")
> "".toString.split("XXX"),
> which return an empty String array (or one containing an empty string),
> but that returns a RubyArray as well.

This will probably work (I am not close to a jruby imp right now):

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

string_array = JString[].new(1)
string_array[0] = "HEH"

I have recently ran into how unwieldy it is to deal with java
arrays and would like to consider other mechanisms for this. Even
if it is just a convenience method.

If I remember right we have no way of making an array of primitives.
So this area needs some work anyways...

-Tom

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


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise