Proxy gotchas in JRuby
I’ve been playing around with JRuby recently, and thought it would be fun to get it working with our Java middleware. Once I got my IDE support up and running (which is a story in itself!), everything seemed to pretty much “just work”. However, I did have one issue which took me a minute or two to get to the bottom of…
Our middleware makes a lot of use of the Java Proxy class - basically, all remote access is done through a proxy. I got a bit confused when the middleware started showing errors on the hashCode method. With a bit of debugging, it turned out that org.jruby.javasupport.JavaSupport.getJavaObjectFromCache was using hashCode and equals to load the Java object (the proxy) from the instance cache. These methods (which weren’t explicitly implemented by the proxy), were sent to the invoke proxy method, which then tried to send them over the wire to the middleware!
If you wish to continue to use an explicit proxy, then find your invoke method :
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {}
and simply add the lines :
if (method.getName().equals("hashCode")) {
return hashCode();
}
if (method.getName().equals("equals")) {
return equals(args[0]);
}
if (method.getName().equals("toString")) {
return toString();
}
to the start.


October 30th, 2006 at 4:25 pm
That’s weird all right. We should perhaps be smarter about that. If I’m reading this right, the issue is that Proxy instances don’t implement those two object methods unless you make them, yes? Is there something we could do to handle this better? Perhaps we could use an IdentityHash for the instance cache instead?We’d be very appreciative if you’d file a JIRA issue for this too, with a simple test case. We’ll see if there’s a good way to fix it…if it’s appropriate to fix. Thanks for finding some JRuby weirdness :)
October 31st, 2006 at 11:35 am
Thanks for your comment!I’ve created JRUBY-244 for this issue.
Alex.