Class: Rjb::JavaObjectWrapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/diarize.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, *args) ⇒ JavaObjectWrapper

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.



42
43
44
45
# File 'lib/diarize.rb', line 42

def initialize(obj, *args)
  @java_object = obj.class == String ?
  Rjb::import(obj).send(:new, *args) : obj
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Reflect unhandled method calls to the underlying Java object.



59
60
61
# File 'lib/diarize.rb', line 59

def method_missing(m, *args)
  wrap_java_object(@java_object.send(m, *args))
end

Instance Attribute Details

#java_objectObject (readonly)

The underlying Java object.



37
38
39
# File 'lib/diarize.rb', line 37

def java_object
  @java_object
end

Instance Method Details

#eachObject

Enumerate all the items in the object using its iterator. If the object has no iterator, this function yields nothing.



49
50
51
52
53
54
55
56
# File 'lib/diarize.rb', line 49

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

#inspectObject

Show the classname of the underlying Java object.



112
113
114
# File 'lib/diarize.rb', line 112

def inspect
  "<#{@java_object._classname}>"
end

#to_sObject

Use the underlying Java object’s stringification.



117
118
119
# File 'lib/diarize.rb', line 117

def to_s
  toString
end