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:in
evaluate’
from org/jruby/RubyKernel.java:1088:in eval' from /Users/yn/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/irb.rb:158:in
eval_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:in
eval_input’
from org/jruby/RubyKernel.java:1419:in loop' from org/jruby/RubyKernel.java:1191:in
catch’
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:in
start’
from org/jruby/RubyKernel.java:1191:in catch' from /Users/yn/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/irb.rb:70:in
start’
from /Users/yn/.rvm/rubies/jruby-1.6.4/bin/irb:17:in `(root)’
–end console dump–