JRuby

Auto java interface coercion for vanilla ruby objects

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: JRuby 1.0.0RC2
  • Fix Version/s: JRuby 1.1b1
  • Component/s: Java Integration
  • Labels:
    None
  • Number of attachments :
    1

Description

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.

Activity

Hide
Nick Sieger added a comment -

Patch generated based on 3727.

Show
Nick Sieger added a comment - Patch generated based on 3727.
Hide
Ricardo Trindade added a comment -

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.

Show
Ricardo Trindade added a comment - 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.
Hide
Nick Sieger added a comment -

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.

Show
Nick Sieger added a comment - 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.
Hide
Ricardo Trindade added a comment -

By the way, the patch no longer applies cleanly. One of the files has conflicts (a test)

Show
Ricardo Trindade added a comment - By the way, the patch no longer applies cleanly. One of the files has conflicts (a test)
Hide
Nick Sieger added a comment -

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 { ... }
Show
Nick Sieger added a comment - 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 { ... }
Hide
Nick Sieger added a comment -
Show
Nick Sieger added a comment - See also...

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: