logo       

[jruby-user] JRuby class arguments: msg#00157

lang.jruby.user

Subject: [jruby-user] JRuby class arguments

I'm trying to use JXPath and JRuby. To do so I need to provide JXPath
with a Java class that is the superclass of all JRuby instances. For
example in the Jython code below this is the Java class called
PyInstance.

I also need to provide JXPath with a class defined in JRuby that
implements a Java interface. Note, I do not need to pass to Java an
instance of this class. Rather I need to pass Java the class itself.
See the Python code below where the register method is given the
arguments PyInstance and JythonPropertyHandler.

Further down is the equivalent JRuby code with a couple of question
marks for filling in these missing arguments. Can this be done in
JRuby yet?

* JYTHON EXAMPLE *

class JythonPropertyHandler (DynamicPropertyHandler):
def getPropertyNames (self, py_object):
keys = py_object.__dict__.keys()
return jarray.array(keys, String)
def getProperty(self, py_object, property):
if py_object.__dict__.has_key(property):
return py_object.__dict__[property]
else:
return None
def setProperty(self, py_object, property, value):
py_object.__dict__[property] = value

def registerJythonPropertyHandler():
JXPathIntrospector.registerDynamicClass(PyInstance, JythonPropertyHandler)

*****************
* JRUBY EXAMPLE *

class JRubyPropertyHandler
include org.apache.commons.jxpath.DynamicPropertyHandler

def getPropertyNames(robject)
return robject.instance_variables.to_java
end

def getProperty(robject, property)
if robject.instance_variables.include?(property)
robject.instance_variable_get(property)
else
nil
end
end

def setProperty(robject, property, value)
robject.instance_variable_set(property, value)
end

def JRubyPropertyHandler.register()
introspector = org.apache.commons.jxpath.JXPathIntrospector
introspector.registerDynamicClass(???, ???)
end
end

---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise