class Rjb::JavaObjectWrapper
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.
This object is enumerable, yielding items in the order defined by the Java object's iterator.
Attributes
The underlying Java object.
Public Class Methods
Initialize with a Java object obj. If obj is a String, assume it is a Java class name and instantiate it. Otherwise, treat obj as an instance of a Java object.
# File lib/rjb/java_object_wrapper.rb, line 20 def initialize(obj, *args) @java_object = obj.class == String ? Rjb::import(obj).send(:new, *args) : obj end
Public Instance Methods
Enumerate all the items in the object using its iterator. If the object has no iterator, this function yields nothing.
# File lib/rjb/java_object_wrapper.rb, line 27 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/rjb/java_object_wrapper.rb, line 90 def inspect "<#{@java_object._classname}>" end
Reflect unhandled method calls to the underlying Java object.
# File lib/rjb/java_object_wrapper.rb, line 37 def method_missing(m, *args) wrap_java_object(@java_object.send(m, *args)) end
Use the underlying Java object's stringification.
# File lib/rjb/java_object_wrapper.rb, line 95 def to_s toString end
Protected Instance Methods
Convert a value returned by a call to the underlying Java object to the appropriate Ruby object as follows:
-
RJB objects are placed inside a generic
JavaObjectWrapper
wrapper. -
java.util.ArrayList
objects are converted to Ruby Arrays. -
java.util.HashSet
objects are converted to Ruby Sets -
Other objects are left unchanged.
This function is applied recursively to items in collection objects such as set and arrays.
# File lib/rjb/java_object_wrapper.rb, line 50 def wrap_java_object(object) if object.kind_of?(Array) object.collect {|item| wrap_java_object(item)} # Ruby-Java Bridge Java objects all have a _classname member which tells # the name of their Java class. elsif object.respond_to?(:_classname) case object._classname when /java\.util\.ArrayList/ # Convert java.util.ArrayList objects to Ruby arrays. array_list = [] object.size.times do |i| array_list << wrap_java_object(object.get(i)) end array_list when /java\.util\.HashSet/ # Convert java.util.HashSet objects to Ruby sets. set = Set.new i = object.iterator while i.hasNext set << wrap_java_object(i.next) end set else # Pass other RJB objects off to a handler. wrap_rjb_object(object) end # case else # Return non-RJB objects unchanged. object end # if end
By default, all RJB classes other than java.util.ArrayList
and java.util.HashSet
go in a generic wrapper. Derived classes may change this behavior.
# File lib/rjb/java_object_wrapper.rb, line 85 def wrap_rjb_object(object) JavaObjectWrapper.new(object) end