Class: Rjb::JavaObjectWrapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/java_object.rb,
lib/stanfordparser.rb

Overview

– The documentation below is for the original Rjb::JavaObjectWrapper object. It is reproduced here because rdoc only takes the last document block defined. If Rjb is moved into its own gem, this documentation should go with it, and the following should be written as documentation for this class:

Extension of the generic Ruby-Java Bridge wrapper object for the StanfordParser module. ++ 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.

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, treat it as a Java class name and instantiate it. Otherwise, treat obj as an instance of a Java object.



38
39
40
41
# File 'lib/java_object.rb', line 38

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 and wrap the return value in the appropriate Ruby object.



56
57
58
59
60
61
62
63
64
65
# 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

Instance Attribute Details

#java_objectObject (readonly)

The underlying Java object.



33
34
35
# File 'lib/java_object.rb', line 33

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.



45
46
47
48
49
50
51
52
# 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

#inspectObject

Show the classname of the underlying Java object.



116
117
118
# File 'lib/java_object.rb', line 116

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

#to_sObject

Use the underlying Java object’s stringification.



121
122
123
# File 'lib/java_object.rb', line 121

def to_s
  toString
end