Is this a bug with Proxy, or is this me using it incorrectly?

This is really strange behavior that I managed to isolate into its own
test case. I would expect for this code to run without problems;
instead, it throws a ClassCastException. Is this a bug in JRuby, or am
I violating something I am unaware of? Thanks!

package com;

public interface TestInterface {
Integer testMethod(Integer i);

–start jruby_proxy.rb–
require ‘java’
include_class java.lang.reflect.Proxy
include_class java.lang.reflect.InvocationHandler
include_class com.TestInterface

class RubyImpl
include TestInterface
def testMethod(i)
return i + 1

class ProxyHandler
include InvocationHandler
attr_accessor :impl
def initialize
self.impl =

def getProxy(interface_class)


def invoke(arg0, arg1, arg2)
arg1.invoke(impl, arg2)

p =
–end jruby_proxy.rb–

–start console dump–
jruby-1.6.4 :012 > load “jruby_proxy.rb”
NativeException: java.lang.ClassCastException: java.lang.Long cannot
be cast to java.lang.Integer
from $Proxy7:-1:in testMethod' from ./jruby_proxy.rb:33:in(root)’
from org/jruby/ load' from ./jruby_proxy.rb:12:inevaluate’
from org/jruby/ eval' from /Users/yn/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/irb.rb:158:ineval_input’
from /Users/yn/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/irb.rb:271:in
signal_status' from /Users/yn/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/irb.rb:155:ineval_input’
from org/jruby/ loop' from org/jruby/’
from /Users/yn/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/irb.rb:154:in
eval_input' from /Users/yn/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/irb.rb:71:instart’
from org/jruby/ catch' from /Users/yn/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/irb.rb:70:instart’
from /Users/yn/.rvm/rubies/jruby-1.6.4/bin/irb:17:in `(root)’
–end console dump–

I suppose I should describe what my larger objective is, so that it is
clear why I am screwing around with proxies and classloaders.

So, I have a JRuby on Rails application - the front part of it is
fairly standard - RESTful resources, controllers, actions, views, erb
templates, etc. However, there is no ActiveRecord: all ActiveRecord
calls have been replaced with calls on one large interface into an
older system. Let’s call this SI for (SecureInterface)

interface SI {
//30+ methods here

There is a caveat: at the moment, there is no implementation of
SecureInterface written in Java in the older system - it will be
written “next week”. Let’s call that future implementation SIJavaImpl
Until that time, I have been writing a mock implementation of
SecureInterface in ruby. Let’s call this SIRubyImpl:

class SIRubyImpl
include SI
//30+ method implementations here

So, I wrote SIRubyImpl, and I was calling it straight from my
controller code, and everything was peachy. However - since Ruby is
highly dynamic, and I was calling everything directly, I cannot claim
that once someone else writes SIJavaImpl, the front-end
view/action/controller code will properly interact with it. So, I
decided that the right way to proceed would be to write tests and make
sure that when those tests interacted with SIRubyImpl they called it
via a Java proxy that would check at runtime argument and return
types. This would give me some confidence that when SIJavaImpl exists,
if I run my test-suite against it and it fails, the bugs will be on
their side rather than mine.


Thank you, and thanks for maintaning JRuby.

After discovering another issue, I am convinced that this is a bug in
JRuby, so I filed this report: