Details
-
Bug
-
Resolution: Fixed
-
P4
-
19
-
b20
-
generic
-
generic
Description
A DESCRIPTION OF THE PROBLEM :
A for each on an intersection type with an Iterable whose iterator() method returns a subclass (e.g. MyIterator iterable()) generates a call to a non existing method of the class java.lang.Iterable (iterator() returning MyIterator).
The error at runtime is:
Exception in thread "main" java.lang.NoSuchMethodError: 'Main$MyIterable$MyIterator java.lang.Iterable.iterator()'
at Main.main(Main.java:7
The full offending program is below:
public class Main {
public static void main(String... args) {
for (Object s : (MyIterable & Serializable) null) {}
)}
static class MyIterable implements Iterable<Object>, Serializable {
class MyIterator implements Iterator<Object> {
public boolean hasNext() {
return false;
}
public Object next() {
return null;
}
public void remove() {}
}
public MyIterator iterator() {
return new MyIterator();
}
}
}
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
---- Main.java
public class Main {
public static void main(String... args) {
for (Object s : (MyIterable & Serializable) null) {}
)}
static class MyIterable implements Iterable<Object>, Serializable {
class MyIterator implements Iterator<Object> {
public boolean hasNext() {
return false;
}
public Object next() {
return null;
}
public void remove() {}
}
public MyIterator iterator() {
return new MyIterator();
}
}
}
----
javac Main.java
java Main
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
no error
ACTUAL -
Exception in thread "main" java.lang.NoSuchMethodError: 'Main$MyIterable$MyIterator java.lang.Iterable.iterator()'
at Main.main(Main.java:7)
FREQUENCY : always
A for each on an intersection type with an Iterable whose iterator() method returns a subclass (e.g. MyIterator iterable()) generates a call to a non existing method of the class java.lang.Iterable (iterator() returning MyIterator).
The error at runtime is:
Exception in thread "main" java.lang.NoSuchMethodError: 'Main$MyIterable$MyIterator java.lang.Iterable.iterator()'
at Main.main(Main.java:7
The full offending program is below:
public class Main {
public static void main(String... args) {
for (Object s : (MyIterable & Serializable) null) {}
)}
static class MyIterable implements Iterable<Object>, Serializable {
class MyIterator implements Iterator<Object> {
public boolean hasNext() {
return false;
}
public Object next() {
return null;
}
public void remove() {}
}
public MyIterator iterator() {
return new MyIterator();
}
}
}
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
---- Main.java
public class Main {
public static void main(String... args) {
for (Object s : (MyIterable & Serializable) null) {}
)}
static class MyIterable implements Iterable<Object>, Serializable {
class MyIterator implements Iterator<Object> {
public boolean hasNext() {
return false;
}
public Object next() {
return null;
}
public void remove() {}
}
public MyIterator iterator() {
return new MyIterator();
}
}
}
----
javac Main.java
java Main
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
no error
ACTUAL -
Exception in thread "main" java.lang.NoSuchMethodError: 'Main$MyIterable$MyIterator java.lang.Iterable.iterator()'
at Main.main(Main.java:7)
FREQUENCY : always
Attachments
Issue Links
- duplicates
-
JDK-8295022 javac emits incorrect code for for each on an intersection type.
-
- Closed
-