tags:

views:

28

answers:

0

I'm trying out an experiment where I have an interface in java and implementing (including) it in an ActiveRecord or DataMapper model so that I can use AR or DM as a java ORM.

However, both these libraries uses #allocate instead of #new to create instances. This prevents the instance returned from the Ruby script be casted to the interface in java. Example:

MyBean.java

interface MyBean {
    String getName();
}

my_bean.rb

class A
    include Java::MyBean

    def name
      "Bosse"
    end
end

A.allocate # or A.new

Test.java

EmbedEvalUnit embedEvalUnit = container.parse(new FileInputStream(
    new File(url.toURI())), "my_bean.rb");

IRubyObject rubyObject = embedEvalUnit.run();
MyBean myBean = (MyBean) rubyObject.toJava(MyBean.class);

If I use A.allocate I get a ClassCastException: Exception in thread "main" java.lang.ClassCastException: org.jruby.RubyObject cannot be cast to MyBean. However, if I use A.new it works fine.

Is there any way to cast to a java object if the ruby object is instantiated using #allocate?