Extension of java class throws internal jruby exception: HELP

Ok, there is a bug/feature in jruby such that the # of arguments to a
ruby class ctor must equal the number of args for the java superclass,
assuming one is extending a java super-class. In:

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

it indicates that overriding self.new is a work-around. The ruby side
of this works, however when the interpreter tries to create a java proxy
for the ruby obj it generates a null pointer exception. Is there
something special that needs to be done in the self.new initialization
to make this work? More generally, how can I subclass a java class
where the ruby class has more ctor arguments than the java class?

The stack trace is:

Exception in thread “main” java.lang.NullPointerException
at org.jruby.java.proxies.JavaProxy.getObject(JavaProxy.java:62)
at org.jruby.java.proxies.JavaProxy.toJava(JavaProxy.java:335)
at
org.jruby.javasupport.JavaEmbedUtils.rubyToJava(JavaEmbedUtils.java:283)
at
org.jruby.embed.ScriptingContainer.runUnit(ScriptingContainer.java:458)
at
org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:450)
at test.nonunit.TestRubySubclassing.main(TestRubySubclassing.java:66)

And the code:

package test.nonunit;

import org.jruby.embed.ScriptingContainer;

public class TestRubySubclassing
{
public static class Foo
{
public Foo (double a)
{ _a = a; }

public double f(double a, double b)
{
  return a*b;
}

private double _a;

}

public static void main (String[] argv)
{
final String code1 =
“class A < Java::test.nonunit.TestRubySubclassing::Foo\n” +

    "def initialize(a)\n" +
      "puts \"initializing\", a\n" +
      "@foo = a\n" +
    "end\n\n" +

    "def self.new(*args,&body)\n" +
      "obj = self.allocate\n" +
      "puts \"allocated\",obj\n" +
      "obj.send :initialize, args[0]\n" +
      "obj.instance_variable_set(:@bar,args[1])\n" +
      "obj\n" +
    "end\n" +

    "def f(a,b)\n" +
      "return a*a+b*b\n" +
    "end\n" +
  "end\n\n" +
  "A.new(3,4)";


ScriptingContainer script = new ScriptingContainer();
Foo obj = (Foo)script.runScriptlet(code1);

System.err.println ("value: " + obj.f (3.0, 4.0));

}
}

On Mon, Mar 1, 2010 at 9:43 PM, Jonathan Shore
[email protected] wrote:

Ok, there is a bug/feature in jruby such that the # of arguments to a ruby
class ctor must equal the number of args for the java superclass, assuming
one is extending a java super-class. Â In:
http://jira.codehaus.org/browse/JRUBY-2457
it indicates that overriding self.new is a work-around. Â The ruby side of
this works, however when the interpreter tries to create a java proxy for
the ruby obj it generates a null pointer exception. Â Is there something
special that needs to be done in the self.new initialization to make this
work? Â More generally, how can I subclass a java class where the ruby class
has more ctor arguments than the java class?

I think if you extend the Java class and call “super” with the
appropriate number of arguments for the Java superclass, it should
work. Is that what you tried?

FWIW, the current class-extension code is a really complicated rat’s
nest of logic…if you can write some specs (a la
spec/java_integration in JRuby source repo) for specific extension
cases like this, we can try to fix them now and ensure they work in
the future.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I had read the bug report and thought that one needed to use the
self.new workaround. The most direct approach (your suggestion)
worked, simply explicitly calling super() with the correct number of
arguments. Thanks!

On Mar 2, 2010, at 1:36 PM, Charles Oliver N. wrote:

has more ctor arguments than the java class?

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

We probably should warn or error if you don’t call super for a
superclass constructor that requires arguments (obviously, calling the
default no-arg version for you is fine if it works. Can you file a bug
for that?

On Tue, Mar 2, 2010 at 7:03 PM, Jonathan Shore
[email protected] wrote:

it indicates that overriding self.new is a work-around. Â The ruby side of
FWIW, the current class-extension code is a really complicated rat’s
  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email