JDK-8068901 : Surprising behavior with more than one functional interface on a class
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2015-01-13
  • Updated: 2016-01-14
  • Resolved: 2015-09-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u72Fixed 9 b81Fixed
Related Reports
Relates :  
Description
Currently we link invocations of @FunctionalInterface methods with an "instanceof <declaring interface>" guard. This seems like a nice stable linkage but can produce weird behavior with classes that implement multiple functional interfaces. Currently, the functional method of the first functional interface of the latest-linked object will dominate earlier-linked ones in that call site. To illustrate:

var fc = new (Java.extend(java.util.function.Function, java.util.function.Consumer))({
    apply:  function(x) { print("fc invoked as a function") },
    accept: function(x) { print("fc invoked as a consumer") }
});
var c = new java.util.function.Consumer(function(x) { print("c invoked as a consumer") });
for each(x in [fc, c, fc]) { x(null); }

prints:

fc invoked as a function
c invoked as a consumer
fc invoked as a consumer


Comments
Confirmed for current 9-dev. So the expected behaviour would be to have fc invoked as a function in the third call as well, because Function takes precedence over Consumer in fc's definition?
28-08-2015

It'd probably make sense to fix this together with JDK-8068903 by using DynamicMethod and rely on exact class guards instead.
13-01-2015