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!

–start TestInterface.java–
package com;

public interface TestInterface {
Integer testMethod(Integer i);
}
–end TestInterface.java–

–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
end
end

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

def getProxy(interface_class)
Proxy.new_proxy_instance(interface_class.java_class.class_loader,

[interface_class.java_class].to_java(java.lang.Class),
self)
end

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

p = ProxyHandler.new.getProxy(com.TestInterface)
p.testMethod(0)
–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/RubyKernel.java:1063:in load' from ./jruby_proxy.rb:12:inevaluate’
from org/jruby/RubyKernel.java:1088:in 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/RubyKernel.java:1419:in loop' from org/jruby/RubyKernel.java:1191:incatch’
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/RubyKernel.java:1191:in 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:

include_class
class SIRubyImpl
include SI
//30+ method implementations here
end

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.

Ideas?

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:

http://jira.codehaus.org/browse/JRUBY-6158

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs