-
Enhancement
-
Resolution: Fixed
-
P4
-
None
-
6
-
x86
-
windows_xp
FULL PRODUCT VERSION :
Tested on both Java5 and Java6
$ java -version
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 10.0-b19, mixed mode)
$ java -version
java version "1.5.0_15"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_15-b04, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 5.2.3790]
(running in a Cygwin shell)
A DESCRIPTION OF THE PROBLEM :
When using the ternary operator (conditional assignment) generics are not
properly handled. This code gives an error
import java.util.*;
public class TestGenerics {
public static void main(String[] args) throws Exception {
boolean b = true;
List<String> list = b
? Collections.emptyList()
: Collections.emptyList();
}
}
$ javac TestGenerics.java
TestGenerics.java:6: incompatible types
found : java.util.List<java.lang.Object>
required: java.util.List<java.lang.String>
? Collections.emptyList()
On the other hand this works
List<String> list = null;
if (b)
list = Collections.emptyList();
else
list = Collections.emptyList();
I don't see why these should be different.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the above code with javac from JDK 1.6.0
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Code should be valid
ACTUAL -
$ javac TestGenerics.java
TestGenerics.java:6: incompatible types
found : java.util.List<java.lang.Object>
required: java.util.List<java.lang.String>
? Collections.emptyList()
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.*;
public class TestGenerics {
public static void main(String[] args) throws Exception {
boolean b = true;
List<String> list = b
? Collections.emptyList()
: Collections.emptyList();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Use if/else
Tested on both Java5 and Java6
$ java -version
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 10.0-b19, mixed mode)
$ java -version
java version "1.5.0_15"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_15-b04, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 5.2.3790]
(running in a Cygwin shell)
A DESCRIPTION OF THE PROBLEM :
When using the ternary operator (conditional assignment) generics are not
properly handled. This code gives an error
import java.util.*;
public class TestGenerics {
public static void main(String[] args) throws Exception {
boolean b = true;
List<String> list = b
? Collections.emptyList()
: Collections.emptyList();
}
}
$ javac TestGenerics.java
TestGenerics.java:6: incompatible types
found : java.util.List<java.lang.Object>
required: java.util.List<java.lang.String>
? Collections.emptyList()
On the other hand this works
List<String> list = null;
if (b)
list = Collections.emptyList();
else
list = Collections.emptyList();
I don't see why these should be different.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the above code with javac from JDK 1.6.0
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Code should be valid
ACTUAL -
$ javac TestGenerics.java
TestGenerics.java:6: incompatible types
found : java.util.List<java.lang.Object>
required: java.util.List<java.lang.String>
? Collections.emptyList()
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.*;
public class TestGenerics {
public static void main(String[] args) throws Exception {
boolean b = true;
List<String> list = b
? Collections.emptyList()
: Collections.emptyList();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Use if/else
- relates to
-
JDK-7177387 Add target-typing support in method context
-
- Closed
-
-
JDK-6785112 Umbrella: issues with generic method type-inference
-
- Closed
-
-
JDK-6721146 type-inference from return-type not applied in conditional expressions
-
- Closed
-