-
Bug
-
Resolution: Unresolved
-
P4
-
7
-
x86
-
windows_vista
FULL PRODUCT VERSION :
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b59)
Java HotSpot(TM) Client VM (build 16.0-b03, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [version 6.0.6000]
A DESCRIPTION OF THE PROBLEM :
I was trying to retrofit a program written in Java 1.4 to use generics.
It's currently impossible without adding some lines of code
because Collections.list() was not correctly generified in 1.5.
It should take a wildcard i.e:
public static <T> ArrayList<T> list(Enumeration<T> e)
should be
public static <T> ArrayList<T> list(Enumeration<? extends T> e)
In the program, the returned ArrayList is used in Collections.sort
with a hadhoc comparator, so the
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try to compile the source code.
This code compiles and safely works with a pre-1.5 compiler
if you remove type arguments.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should compile.
ACTUAL -
The compiler emits an error because
Collections.list(file.entries())
returns an ArrayList<? extends ZipEntry>.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
output of javac:
Bug.java:14: incompatible types
ArrayList<ZipEntry> list = Collections.list(file.entries());
^
required: java.util.ArrayList<java.util.zip.ZipEntry>
found: java.util.ArrayList<capture#1 of ? extends java.util.zip.ZipEntry>
1 error
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Bug {
public static void main(String[] args) throws IOException {
ZipFile file=new ZipFile(args[0]);
ArrayList<ZipEntry> list = Collections.list(file.entries());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
duplicate the ArrayList.
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b59)
Java HotSpot(TM) Client VM (build 16.0-b03, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [version 6.0.6000]
A DESCRIPTION OF THE PROBLEM :
I was trying to retrofit a program written in Java 1.4 to use generics.
It's currently impossible without adding some lines of code
because Collections.list() was not correctly generified in 1.5.
It should take a wildcard i.e:
public static <T> ArrayList<T> list(Enumeration<T> e)
should be
public static <T> ArrayList<T> list(Enumeration<? extends T> e)
In the program, the returned ArrayList is used in Collections.sort
with a hadhoc comparator, so the
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try to compile the source code.
This code compiles and safely works with a pre-1.5 compiler
if you remove type arguments.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should compile.
ACTUAL -
The compiler emits an error because
Collections.list(file.entries())
returns an ArrayList<? extends ZipEntry>.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
output of javac:
Bug.java:14: incompatible types
ArrayList<ZipEntry> list = Collections.list(file.entries());
^
required: java.util.ArrayList<java.util.zip.ZipEntry>
found: java.util.ArrayList<capture#1 of ? extends java.util.zip.ZipEntry>
1 error
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Bug {
public static void main(String[] args) throws IOException {
ZipFile file=new ZipFile(args[0]);
ArrayList<ZipEntry> list = Collections.list(file.entries());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
duplicate the ArrayList.