Created
April 19, 2010 20:11
-
-
Save samueldana/371527 to your computer and use it in GitHub Desktop.
Attempting to use JRuby Objects from Java via Red Bridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.script.*; | |
import java.io.*; | |
import interfaces.*; | |
public class Driver { | |
public static void main(String[] args) throws ScriptException, FileNotFoundException, NoSuchMethodException { | |
ScriptEngineManager manager = new ScriptEngineManager(); | |
ScriptEngine engine = manager.getEngineByName("jruby"); | |
// Import ruby classes | |
String filename = System.getProperty("user.dir") + "/ruby.rb"; | |
FileReader reader = new FileReader(filename); | |
// Retrieve the object returned by the last line of the evaluated script | |
// In this case, a Person object that has a Pet named 'fido' | |
Object receiver = engine.eval(reader); | |
Invocable invocable = (Invocable) engine; | |
// Cast the ruby object to the Person interface | |
//Person person = (Person) invocable.getInterface(receiver, Person.class); | |
Person person = (Person) receiver; | |
// Retrieve the Person's Pet | |
// Two methods for retrieval are shown below, with the second one commented out | |
// The first method fails, the second works | |
// First method: using interface method getPet() | |
Pet pet = (Pet) person.getPet(); // <- Returns an InterfaceImpl | |
// NOTE: uncommenting the line below inexplicably causes first method to work; | |
// Just calling invokeMethod on the getter seems to automagically transform the pet variable | |
//invocable.invokeMethod(person, "pet"); // Can't call getPet() since invoke goes through Ruby | |
// Alternate method: using invokeMethod on person with method "pet" | |
// This method seems to work | |
//Pet pet = (Pet) invocable.invokeMethod(person, "pet"); // <- Returns a Pet | |
// Test the pet variable by passing it to a Ruby function | |
// Using the first method, this will fail... Is there a pet.toRubyObject() or something similar I should be calling? | |
// The second method works, but what's the point of the interface implementation if I can't use any objects | |
// returned by its methods? | |
boolean isPet = person.askIfThisIsAPet(pet); | |
System.out.println("Was the pet a pet?: " + isPet); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interfaces; | |
public interface Person { | |
Pet getPet(); | |
void setPet(Pet newPet); | |
boolean askIfThisIsAPet(Object pet); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interfaces; | |
public interface Pet { | |
String getName(); | |
void setName(String newName); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
class Person | |
include Java::interfaces.Person | |
attr_accessor :pet | |
def initialize(pet); @pet = pet; end | |
def askIfThisIsAPet(pet) | |
puts 'checking...' | |
puts "ancestors: #{pet.class.ancestors.inspect}" | |
pet.is_a?(Pet) | |
end | |
end | |
class Pet | |
include Java::interfaces.Pet | |
attr_accessor :name | |
def initialize(name); @name = name; end | |
end | |
Person.new(Pet.new('fido')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment