« Return to Thread: Java integration: Swing string problem

Re: Java integration: Swing string problem

by Bill Dortch :: Rate this Message:

Reply to Author | View in Thread

This fails in your original case because the Java String is converted to a Ruby String when the constant is set.  Ruby string values are stored as ByteLists (wrapping a byte array), and are divorced from the original Java string.  The second case works because JavaField#static_value just wraps the original Java string in a JavaObject (low-level wrapper).

JavaObject is likely to go away (or at least be re-purposed) post-1.1, so we'll need to be mindful of this use case.  If we do the refactoring right, it should be possible to have an alternative String implementation for strings returned from Java that retains the original value (converting to ByteList only if necessary), which will make sense because in many (most?) cases, strings returned from Java are just going to be passed back in without further manipulation.

-Bill

On 1/5/08, Hugh Winkler <hughw@...> wrote:
Hi,

I'm mainly just recording some information here in a place where it
might be useful. I can't drill deeper at the moment. There's no bug
here, but there may be an opportunity to improve Java integration.

I encountered a case where Swing compares a passed in string, against
a static final field, using ==. IOW, they're using it like an enum. In
Java SE 6 it's in AbstractButton.actionPropertyChanged.

So my JRuby program calls AbstractAction putValue(String key, object
value). The action object notifies listeners, including JMenuItems
which inherit from AbstractButton.

This is how how you change menu text in Swing, so the undo menu reads
"Undo Typing" or whatever.

My call in jruby looked like: my_action.put_value(Action::NAME, "Undo Typing")

The AbstractButton code, however, fails the comparison of this key
against Action.NAME. I guess JRuby is passing a copy of the string
around. That's why it fails ==.

My workaround is:
ActionNameKey =
javax.swing.Action.java_class.declared_field('NAME').static_value
my_action.put_value(ActionNameKey, "Undo Typing")

If I had the time right now I'd dig into whether Jruby could make
cases like this succeed by always calling String.intern before passing
the string around. 1. I don't know that jruby doesn't do this, 2. I
don't know that it would fix the problem. But it would be nice to
reduce the friction if possible.


Hugh

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: Java integration: Swing string problem