2

Recently I've cobbled together a small project to play with JRuby and its interactions with Java. Here's the Github gist.

LogicProcessor.java:

package me.artsolopov.jrp;

import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.text.JTextComponent;

public interface LogicProcessor {
    void actionTrig(String inst, JTextComponent anno);
    void actionClose();
    void actionAddRow();
    void setTableFilter(String filter);

    TableModel getTableModel();
}

Parts from logic_impl.rb:

require 'java'

java_import javax.swing.table.AbstractTableModel

class LProc
  java_import Java::MeArtsolopovJrp::LogicProcessor
  include LogicProcessor

  class TableModel < AbstractTableModel
    COLUMN_NAMES = {
      q: 'Q',
      w: 'Win',
      x: 'Cross'
    }.freeze

    def initialize(data)
      super()
      @data = data
    end

    def data=(new_data)
      @data = new_data
      fire_table_data_changed
    end

    def getColumnName(col)
      COLUMN_NAMES.values[col]
    end

    def getColumnCount
      COLUMN_NAMES.count
    end

    def getRowCount
      @data.count
    end

    def getValueAt(row, col)
      col_key = COLUMN_NAMES.keys[col]
      @data[row][col_key] || 0
    end

    def isCellEditable(_r, _c)
      true
    end

    def setValueAt(value, row, col)
      col_key = COLUMN_NAMES.keys[col]
      @data[row][col_key] = Integer(value)
    end
  end

  def initialize(frame)
    @frame = frame
    @table = [
      { q: 1, w: 2, x: 3 }, { q: 2, w: 4, x: 3 },
      { q: -1, w: 5, x: 4 }, { q: 3, w: 2, x: 1 },
      { q: -2, w: 2, x: 6 }
    ]
    @slice = @table
    @table_model = TableModel.new(@slice)
  end

  attr_reader :table_model

  def action_trig(inst, anno)
    anno.text = <<~DOC
      Inputted text: #{inst}
      data: #{@table}
    DOC
  end

  def action_close
    @frame.dispose
  end

  def action_add_row
    @table << {}
    @table_model.fire_table_rows_inserted(@table.length - 1, @table.length - 1)
  end

  def set_table_filter(filter)
    data = case filter
           when 'qpos' then @table.select { |row| row[:q].positive? }
           else @table
           end
    @table_model.data = data
  end
end

The code above (and in the gist) works. It produces a form, injects my LProc instance into the form, and my JRuby class implements a sort of business logic.

However, if I try to define methods in LProc::TableModel in snake_case (for example, column_name or get_column_name instead of getColumnName), I get errors because I haven't implemented abstract methods.

Why does JRuby behave that way?

art-solopov
  • 4,289
  • 3
  • 25
  • 44
  • Do you mean that you do not get any error if you just defined your methods as, i.e. `def actionAddRow`? – user1934428 Aug 05 '20 at 07:23
  • If I define methods in `LProc` the same way they appear in the interface, I don't get any errors, it all works. – art-solopov Aug 05 '20 at 09:25
  • 1
    Just a guess: Since JRuby has no built-in equivalence of camel case and snake case identifiers, I guess that the implementation goes like this, that internally, the spelling of the method name is always like in Java. If you invoke a snake_case method in a class derived from a Java object, `method_missing` is triggered (which is likely defined in all those Java-based classes) and tries to call the corresponding camel case method instead. This would at least explain the behaviour you have observed. – user1934428 Aug 05 '20 at 09:30
  • 2
    You could do in your class an `alias_method`, to define the camel-case version of your snake_case methods. – user1934428 Aug 05 '20 at 09:31

1 Answers1

4

The simple answer is "we haven't made class extension work that way yet." The logic for extending a class is significantly more complicated than the logic for implementing an interface, and as a result we have been reluctant to make significant changes to it for many years. It was written originally before we started to make snake_case pervasive, and long before we improved interface implementation to support snake_case methods from Ruby implementing their equivalent camelCase interface methods.

In short, it's not a conscious decision... it's just a complicated subsystem that has not yet been aligned with the rest of JRuby's Java integration.

  • Thank you! This is really interesting, I've never thought that interface implementation would be so different to subclassing. – art-solopov Aug 06 '20 at 16:51