Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
headius opened this issue Dec 29, 2014 · 1 comment
Closed

Comments

@headius
Copy link
Member

headius commented Dec 29, 2014

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
kares added a commit that referenced this issue Aug 25, 2015
…constructor

this seems non-fixable without introducing some ugly hacks as the constructor's
super is allways the first call to happen and when it's calling an abstract method
that ends up at the Java proxy it will fail with a NPE as the invocation __handler field won't be initialized!

issue has been (previously) reported as #2369
@byteit101
Copy link
Member

Fixed by #6422

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants