JDK-8167966 : MethodHandles.iteratedLoop fails with IAE in the case of correct arguments
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Affected Version: 9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2016-10-13
  • Updated: 2017-07-13
  • Resolved: 2016-11-02
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 9
9 b143Fixed
Related Reports
Relates :  
Description
Please, observe MethodHandles.iteratedLoop(...) constraints for iterator, init and body.

According to these constraints, three MethodHandle with following signatures should be accepted in abovementioned method :
iterator=()Iterator
init=()I
body=(I,Object,I)I

Let's look on their types. First I is locals state, Object is T, and second I  is single loop argument. Init's parameter list should be effectively identical to loop parameter list (it is 'I'). The same requirements for iterator handle. However, following exception occurs:
>> Exception in thread "main" java.lang.IllegalArgumentException: inferred first loop argument must be an array or inherit from Iterable: int

Observing last constraint you can find  constraint (first argument must be Iterable or array) in the case iterator=null. Here iterator is not null, so it is inapplicable.
You can find complete code in Example0.java


Was found on jdk9b140
Comments
I think there is a bug in MethodHandles.iteratedLoopChecks: Class<?> iterableType = outerList.isEmpty() ? null : outerList.get(0); if (iterableType != null && !Iterable.class.isAssignableFrom(iterableType) && !iterableType.isArray()) { throw newIllegalArgumentException( "inferred first loop argument must be an array or inherit from Iterable: " + iterableType); } return iterableType; // help the caller a bit It probably requires: if (iterator == null && iterableType != null ... )
29-10-2016

Testsuite: JCK9/b47 OS: Solaris 12 x64 Tested JDK: JDK 9 140 RULE "api/java_lang/invoke/MethodHandles/index_IteratedLoopTests" Exception java.lang.ClassCastException: Cannot cast [Ljava.lang.String; to java.lang.Iterable RULE "api/java_lang/invoke/MethodHandles/index_IteratedLoopTests" Exception java.lang.IllegalArgumentException: inferred first loop argument must be an array or inherit from Iterable: class java.lang.Integer RULE "api/java_lang/invoke/MethodHandles/index_IteratedLoopTests" Exception java.lang.IllegalArgumentException: inferred first loop argument must be an array or inherit from Iterable: int RULE "api/java_lang/invoke/MethodHandles/index_IteratedLoopTests" TestCase [testIteratorDefinesExternalParameterList] test cases: 54; passed: 9; failed: 45 Matches: RULE "api/java_lang/invoke/MethodHandles/index_IteratedLoopTests" TestCase [testResultIsLastLocalState] test cases: 24; passed: 18; failed: 6
20-10-2016

Corresponding tests are excluded: api/java_lang/invoke/MethodHandles/index.html#IteratedLoopTests[testBodyDefinesExternalParameterList] api/java_lang/invoke/MethodHandles/index.html#IteratedLoopTests[testHandlesConformingEachOther] api/java_lang/invoke/MethodHandles/index.html#IteratedLoopTests[testHandlesReturnType] api/java_lang/invoke/MethodHandles/index.html#IteratedLoopTests[testIteratorDefinesExternalParameterList]
13-10-2016