Simplified syntax to create instances of anonymous classes for Java event 
handlers/adapters
-------------------------------------------------------------------------------------------

                 Key: JRUBY-3543
                 URL: http://jira.codehaus.org/browse/JRUBY-3543
             Project: JRuby
          Issue Type: Improvement
          Components: Java Integration
            Reporter: Steve Riegel
            Priority: Minor


This would be handy for creating instances of anonymous classes for java event 
handlers/adapters:

  foo = JavaInterfaceOrClass.new

  class << foo
    def method1(x)
      # override method1
    end
    def method2(y, z)
      # override method2
    end
    # etc...
 end 

 some_other_method(a, b, foo, c)

It turns out that this works for a java Interface, but not for a java Class.

For the java Class case, I have to extend (subclass) the class to define the 
overrides.  Unfortunately, that code ends up lexically remote from the call in 
which it is used (can't define the subclass just before the some_other_method() 
call), even though it is often highly related/specific to that call and other 
code around it.

I happen to have a case where I actually need instances of each (a java 
Interface and a java Class) to pass to some_other_method().  Yup ... two event 
handlers where one is derived from a java Interface, and the other from a java 
Class.  Each handler also happens to require more than one method, so I can't 
use the "closure conversion" (as described in "Calling Java from JRuby").

If I understand correctly, normal Ruby implements this "Object-Specific Class" 
concept by creating a singleton class and inserting it into the inheritance 
hierarchy (between the object and its original superclass).  My guess is that 
JRuby happens to wrap the correct object+class in the Interface case (because 
it is actually creating the Interface object+class at the same time  
(JRUBY-169)), but does not in the Class case.  It appears to wrap only the 
original object+superclass in the Class case.  It would be neat if JRuby could 
"re-wrap" to the object+singleton in the Class case (picking up the overrides).

To me, the key advantage of having this (or similar) syntax work is that it 
permits highly related code to be visually colocated.

-- 
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


Reply via email to