United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6500704 : Add language support for converting methods to interfaces

Details
Type:
Enhancement
Submit Date:
2006-12-05
Status:
Closed
Updated Date:
2011-10-31
Project Name:
JDK
Resolved Date:
2011-10-31
Component:
specification
OS:
windows_xp
Sub-Component:
language
CPU:
x86
Priority:
P5
Resolution:
Duplicate
Affected Versions:
5.0
Fixed Versions:

Related Reports
Duplicate:
Relates:
Relates:
Relates:

Sub Tasks

Description
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).

                                    

Comments
EVALUATION

Proposals for new features in the Java programming language are no longer being accepted or maintained in the bug database at http://bugs.sun.com/. This also applies to proposals for new features in the Java Virtual Machine (that is, in the abstract JVM, as opposed to a JVM implementation like HotSpot).

All proposals for new features should be made through the "JDK Enhancement - Proposal & Roadmap Process" described at http://openjdk.java.net/jeps/1.

This specific request to change the Java programming language is related to the work of OpenJDK Project Lambda. It will be considered further by that project, not here.

The bug database continues to accept and maintain submissions about technical errors in the design and specification of the Java programming language and the Java Virtual Machine.
                                     
2006-12-05



Hardware and Software, Engineered to Work Together