History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: JRUBY-991
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Nick Sieger
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
JRuby

Auto java interface coercion for vanilla ruby objects

Created: 24/May/07 12:56 PM   Updated: 23/Apr/08 10:04 AM
Component/s: Java Integration
Affects Version/s: JRuby 1.0.0RC2
Fix Version/s: JRuby 1.1b1

Time Tracking:
Not Specified

File Attachments: 1. Text File JRUBY-991.patch (5 kb)



 Description  « Hide
This patch will allow plain Ruby objects to be passed to Java method arguments that are interface types, and the Ruby object will automatically be wrapped in an interface proxy, as originally proposed here.

Another example:

include Java

filter = Object.new
def filter.accept(file, str)
  !!(str =~ /jar/)
end  

file = java.io.File.new("lib")
file.list(filter).each {|f| puts f}

I still think this approach has a place alongside the rest of the recently revamped java integration. It's somewhat of an edge case in that the code will only be activated when you pass a vanilla Ruby object to Java; I think the chance for unexpected surprises is low.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Nick Sieger - 24/May/07 01:30 PM
Patch generated based on 3727.

Ricardo Trindade - 21/Jun/07 04:54 AM
This patch works very well, and we're finding it extremely useful, the only issue we found with it is that it doesn't work for constructors.

Nick Sieger - 21/Jun/07 11:37 AM
Yes, it would be nice to get it working for constructors. I'll try to do it sometime soon, in the meantime if it's obvious to you what needs to be changed feel free to add to or update the patch.

Ricardo Trindade - 27/Jun/07 02:45 PM
By the way, the patch no longer applies cleanly. One of the files has conflicts (a test)

Nick Sieger - 01/Jul/07 01:32 AM
Committed, with some additional enhancements.
  1. Constructors should coerce ruby objects to interface proxies now
  2. Procs, when coerced to an interface, will be additionally extended with a custom method_missing that will allow the proc to be called whenever an interface method is invoked. This will allow for things like:
    button = JButton.new
    button.add_action_listener(proc { ... })
  3. Blocks, when passed to Java methods, are coerced to a proc and added as a parameter to the method, thus following semantics for procs. This will allow for:
    button = JButton.new
    button.add_action_listener { ... }

Nick Sieger - 01/Jul/07 02:02 AM