Skip to content

Can't subclass a Java class that calls an abstract function in its constructor #2369

@headius

Description

@headius

From http://jira.codehaus.org/browse/JRUBY-2748

Original report

I have found that while subclassing a Java class that calls an abstract function in its constructor will cause the initialization of the subclass in JRuby to throw an ArgumentError with message "Constructor invocation failed: null."

Here is my Java base class:

package com.allstontrading.logalyzer;
import java.util.List;

public abstract class AbstractTest {
     protected String obj;

     public AbstractTest() {
          this("dog");
          this.run();
     }
     public AbstractTest(String obj) {
          this.obj = obj;
     }

     protected abstract List<String> myAbstractFunc();
     public void run() {
          if (this.myAbstractFunc() != null)
               for (String string : this.myAbstractFunc())
                    System.out.println("Big " + this.obj + "s Eat: " + string);
          else
               System.out.println("OMG");
     }
}

Here is my example JRuby script:

include Java

import 'com.allstontrading.logalyzer.AbstractTest'
import 'java.util.ArrayList'

class RubyTest < com.allstontrading.logalyzer.AbstractTest
    def initialize()
        super("Pink Elephant")
    end

    def myAbstractFunc()
        list = ArrayList.new
        list << "Cancerous Rocks"
        list << "Candy Apples"
        list << "Moldy Bread"
        return list
    end
end

puts "This test does NOT call an abstract function in the constructor."
test = RubyTest.new
test.run

class AnotherRubyTest < com.allstontrading.logalyzer.AbstractTest
    def initialize()
        super
    end

    def myAbstractFunc()
        list = ArrayList.new
        list << "Cancerous Rocks"
        list << "Candy Apples"
        list << "Moldy Bread"
        return list
    end 
end     

puts ""
puts ""
puts "This test does call an abstract function in the constructor."
anotherTest = AnotherRubyTest.new

Here is the output:

This test does NOT call an abstract function in the constructor.
Big Pink Elephants Eat: Cancerous Rocks
Big Pink Elephants Eat: Candy Apples
Big Pink Elephants Eat: Moldy Bread

This test does call an abstract function in the constructor.
/home/mbohn/jruby-1.1.2/lib/ruby/site_ruby/1.8/builtin/javasupport/utilities/base.rb:26:in `new_instance2': Constructor invocation failed: null (ArgumentError)
        from /home/mbohn/jruby-1.1.2/lib/ruby/site_ruby/1.8/builtin/javasupport/utilities/base.rb:26:in `__jcreate!'
        from /home/mbohn/jruby-1.1.2/lib/ruby/site_ruby/1.8/builtin/javasupport/proxy/concrete.rb:23:in `initialize'
        from rubyTestAbstract.rb:26:in `initialize'
        from /home/mbohn/jruby-1.1.2/lib/ruby/site_ruby/1.8/builtin/javasupport/proxy/concrete.rb:6:in `new'
        from /home/mbohn/jruby-1.1.2/lib/ruby/site_ruby/1.8/builtin/javasupport/proxy/concrete.rb:6:in `new'
        from rubyTestAbstract.rb:41

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions