SUGGESTED FIX
When type erasure occurs in TransTypes, constant value associated with a Type (in this case reference Type String) is erased. For primitive types, constant value is preserved.
Hence the following code compiles fine:
public class Test {
public void func(int i) {
switch (i) { case (int)33: break }
}
}
But, the constant value associated with String type is erased and so example in "Description" results in NPE. The following diff fixes the issue by restoring constant value after type erasure. Note that we restore type only when needed (to avoid having to use anon-class instance for type - see Type.constType).
diff -r 1d2db0e5eabc src/share/classes/com/sun/tools/javac/code/Types.java
--- a/src/share/classes/com/sun/tools/javac/code/Types.java Fri Aug 10 10:14:48 2012 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/Types.java Tue Aug 14 10:38:35 2012 +0530
@@ -1589,7 +1589,13 @@
* type parameters in t are deleted.
*/
public Type erasure(Type t) {
- return erasure(t, false);
+ Object constVal = t.constValue();
+ Type erasedType = erasure(t, false);
+ // Type erasure should not erase constant value
+ if (constVal != null && erasedType.constValue() == null) {
+ erasedType = erasedType.constType(constVal);
+ }
+ return erasedType;
}
//where
private Type erasure(Type t, boolean recurse) {
|