| Class | Rjb::JavaObjectWrapper |
| In: |
lib/java_object.rb
lib/stanfordparser.rb |
| Parent: | Object |
A generic wrapper for a Java object loaded via the Ruby-Java Bridge. The wrapper class handles intialization and stringification, and passes other method calls down to the underlying Java object. Objects returned by the underlying Java object are converted to the appropriate Ruby object.
Other modules may extend the list of Java objects that are converted by adding their own converter functions. See wrap_java_object for details.
This object is enumerable, yielding items in the order defined by the underlying Java object‘s iterator.
| java_object | [R] | The underlying Java object. |
Initialize with a Java object obj. If obj is a String, treat it as a Java class name and instantiate it. Otherwise, treat obj as an instance of a Java object.
# File lib/java_object.rb, line 38 def initialize(obj, *args) @java_object = obj.class == String ? Rjb::import(obj).send(:new, *args) : obj end
Enumerate all the items in the object using its iterator. If the object has no iterator, this function yields nothing.
# File lib/java_object.rb, line 45 def each if @java_object.getClass.getMethods.any? {|m| m.getName == "iterator"} i = @java_object.iterator while i.hasNext yield wrap_java_object(i.next) end end end
Show the classname of the underlying Java object.
# File lib/java_object.rb, line 116 def inspect "<#{@java_object._classname}>" end
Reflect unhandled method calls to the underlying Java object and wrap the return value in the appropriate Ruby object.
# File lib/java_object.rb, line 56 def method_missing(m, *args) begin wrap_java_object(@java_object.send(m, *args)) rescue RuntimeError => e # The instance method failed. See if this is a static method. if not e.message.match(/^Fail: unknown method name/).nil? getClass.send(m, *args) end end end
Use the underlying Java object‘s stringification.
# File lib/java_object.rb, line 121 def to_s toString end
Alias for wrap_edu_stanford_nlp_trees_Tree
Alias for wrap_edu_stanford_nlp_trees_Tree
FeatureLabel objects go inside a FeatureLabel wrapper.
# File lib/stanfordparser.rb, line 102 def wrap_edu_stanford_nlp_ling_FeatureLabel(object) StanfordParser::FeatureLabel.new(object) end
Tree objects go inside a Tree wrapper. Various tree types are aliased to this function.
# File lib/stanfordparser.rb, line 108 def wrap_edu_stanford_nlp_trees_Tree(object) Tree.new(object) end
Convert a value returned by a call to the underlying Java object to the appropriate Ruby object.
If the value is a JavaObjectWrapper, convert it using a protected function with the name wrap_ followed by the underlying object‘s classname with the Java path delimiters converted to underscores. For example, a java.util.ArrayList would be converted by a function called wrap_java_util_ArrayList.
If the value lacks the appropriate converter function, wrap it in a generic JavaObjectWrapper.
If the value is not a JavaObjectWrapper, return it unchanged.
This function is called recursively for every element in an Array.
# File lib/java_object.rb, line 82 def wrap_java_object(object) if object.kind_of?(Array) object.collect {|item| wrap_java_object(item)} elsif object.respond_to?(:_classname) # Ruby-Java Bridge Java objects all have a _classname member which # tells the name of their Java class. Convert this to the # corresponding wrapper function name. wrapper_name = ("wrap_" + object._classname.gsub(/\./, "_")).to_sym respond_to?(wrapper_name) ? send(wrapper_name, object) : JavaObjectWrapper.new(object) else object end end
Convert java.util.ArrayList objects to Ruby Array objects.
# File lib/java_object.rb, line 97 def wrap_java_util_ArrayList(object) array_list = [] object.size.times do |i| array_list << wrap_java_object(object.get(i)) end array_list end