JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-2748

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

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: JRuby 1.1.2
    • Fix Version/s: JRuby 2
    • Component/s: Java Integration
    • Labels:
      None
    • Number of attachments :
      1

      Description

      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
      

        Issue Links

          Activity

            People

            • Assignee:
              Charles Oliver Nutter
              Reporter:
              Michael Bohn
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated: