Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6500704

Add language support for converting methods to interfaces

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 5.0
    • specification
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      A method with the same signature (except the name) as the sole method in an interface could be converted, either automatically (coercion) or explicitly, to that interface.

      This could be considered syntactic sugar for inner classes that simply wrap a specific function, and it could also be implemented like that (but more efficient implementations may be possible).

      JUSTIFICATION :
      The goal would be to provide a type-safe and simple to use mechanism for event handling similar to "procedure of object" pointers in Object Pascal / Delphi or (to a lesser degree) signals and slots in Qt or the boost C++ libraries.

      This solution will work with many existing interfaces and event source implementations. Only the code for binding listeners will be simplified, which is both the most commonly used and (in my option) the most cumbersome part today.

      Like (anonymous) inner classes, it does not publicly expose the interface being implemented.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Example:

      -- PositionChangeHandler.java:

      public interface PositionChangeHandler {
         public void handlePositionChange(Event e, int x, int y);
      }

      -- EventSender.java:

      public class EventSender {
         public void addPositionChangeListener(PositionChangeHandler handler) {
            [...]
         }
         [...]
      }

      -- MyEventReceiver.java:

      public class MyEventReceiver {
         private EventSender sender1;
         private EventSender sender2;

         private void posChanged1(Event e, int x, int y) {
            [...]
         }

         private void posChanged2(Event e, int x, int y) {
            [...]
         }

         private void connectEvents() {
            /* The following has the same effect as:
            sender1.addPostionChangeListener(new PositionChangeHandler() {
               public void handlePositionChange(Event e, int x, int y) {
                  posChanged1(e, x, y);
               });
            sender2.addPostionChangeListener(new PositionChangeHandler() {
               public void handlePositionChange(Event e, int x, int y) {
                  posChanged2(e, x, y);
               });
            */
            sender1.addPositionChangeListener(posChanged1);
            sender2.addPositionChangeListener(posChanged2);
         }

         [...]
      }

      Alternative solution:

      Maybe the conversion from method to interface should be made explicit instead of automatic. It could also be useful to have a way to bind several methods for interfaces defining more than one than one function interface.

      An example (incomplete, but you get the point):

      interface MyInterface {
        void method1(int arg);
        void method2();
      }

      class MyClass {
        private void handler1(int arg) {}
        private void handler2() {}
        private void attachEvents {
          // a better syntax can probably be found
          anEventSource.addListener(MyInterface(method1 = handler1, method2 = handler2));
        }
      }


      CUSTOMER SUBMITTED WORKAROUND :
      Use anonymous inner classes (cumbersome) or reflection (no compile-time type checking).

            abuckley Alex Buckley
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: