Class: Rjb::JavaObjectWrapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ivy/java/java_object_wrapper.rb

Instance Attribute Summary collapse

Class Method 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.



18
19
20
# File 'lib/ivy/java/java_object_wrapper.rb', line 18

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.



35
36
37
38
39
40
41
42
43
44
# File 'lib/ivy/java/java_object_wrapper.rb', line 35

def method_missing(m, *args)
  begin
    JavaObjectWrapper.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.



13
14
15
# File 'lib/ivy/java/java_object_wrapper.rb', line 13

def java_object
  @java_object
end

Class Method Details

.wrap_java_object(object) ⇒ Object

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.



79
80
81
82
83
84
85
86
87
88
# File 'lib/ivy/java/java_object_wrapper.rb', line 79

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
    find_converter(object) || JavaObjectWrapper.new(object)
  else
    object
  end
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.



24
25
26
27
28
29
30
31
# File 'lib/ivy/java/java_object_wrapper.rb', line 24

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.



52
53
54
# File 'lib/ivy/java/java_object_wrapper.rb', line 52

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

#respond_to?(sym) ⇒ Boolean

Checks if underlying java object responds to method prior using standard respond_to? method.

Returns:

  • (Boolean)


47
48
49
# File 'lib/ivy/java/java_object_wrapper.rb', line 47

def respond_to?(sym)
  java = @java_object.getClass.getMethods.any? {|m| m.getName == sym.to_s} || super.respond_to?(sym)
end

#to_sObject

Use the underlying Java object’s stringification.



57
58
59
# File 'lib/ivy/java/java_object_wrapper.rb', line 57

def to_s
  toString
end