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?
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.