JavaProxy should be an interface to make Java integration easier
----------------------------------------------------------------
Key: JRUBY-4314
URL: http://jira.codehaus.org/browse/JRUBY-4314
Project: JRuby
Issue Type: Improvement
Components: Java Integration
Affects Versions: JRuby 1.5
Reporter: Yoko Harada
Attachments: ji_improvement.patch, ji_improvement.zip
This is a proposal of Java integration improvement. The purpose of this
proposal is to make the implementation of Java proxy objects much easier.
Currently, most instances passed from Java to Ruby are just Java objects in
Ruby program and don't have Ruby methods such as "each." For example, when
people put a HashMap type object in a method argument to invoke Ruby method,
the object is still a Java object in Ruby code, not a RubyHash object. This is
a filed issue, JRUBY-4223.
The answer to solve the issue is to write a proxy class of
org.jruby.java.proxies packages. For example, MapJavaProxy class is. However,
it is not easy and not efficient because of the class inheritance design. When
we look at current hierarchy of ArrayJavaProxy, it is:
RubyBasicObject <- RubyObject <- JavaProxy <- ArrayJavaProxy
ArrayJavaProxy doesn't have RubyArray in its ancestors. Thus, ArrayJavaProxy
needs to implement Ruby's Array methods by itself. Redefining all Ruby methods
in the proxy class is not easy. Also, it is weird since multiple kinds of
implementation of one Ruby method will be scattered around. If we can reuse
RubyHash, RubyArray or other matured classes for proxies, we can wrap Java
object up more easily.
The reason that ArrayJavaProxy couldn't inherit RubyArray is probably JavaProxy
is a concrete class. My proposal is to change JavaProxy to an interface. Then,
in my implementation, instance methods are in JavaProxyProvier, and class
methods are in JavaProxyProviderUtils since static methods can't be defined in
the interface and implemented directly. Although the patch includes changes of
JavaProxy, I attached complete code of these three to clarify what I did. (see
the zipped files)
If JavaProxy is the interface, new proxies will be:
public class MapJavaProxy extends RubyHash implements JavaProxy {}
public class ListJavaProxy extends RubyArray implements JavaProxy {}
.....
I attached MapJavaProxy in the zip file to show how it was implemented. The
class is a mock based on ArrayJavaProxy, but perhaps is enough to show what the
implementation will be.
The influence to other classes seems not to be so big. Attached patch includes
whole change along with the new design of JavaProxy. After I made this change,
JRuby was compiled successfully and worked as was before though I might have
needed to test more and various cases.
To make Java integration easier, I'd like to submit this idea.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email