-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
22
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
java 22.0.1 2024-04-16
Java(TM) SE Runtime Environment (build 22.0.1+8-16)
Java HotSpot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
Double brace initializer produces runtime type of the enclosing class.
If you are currently writing a class named A and are using the double brace initializer on some collection, the runtime instance type of the collection will be of type A, instead of the collection type.
This really screws up reflection and serialization.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) initialize a new collection with brace initializer
2) print the collection class name
3) prints the enclosing class
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
HashMap$1
HashSet$1
ACTUAL -
Main$1
Main$2
---------- BEGIN SOURCE ----------
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>(){{
put("key1", "value1");
put("key2", "value2");
}};
System.out.println(map.getClass().getName());
Set<String> set = new HashSet<>(){{
add("value1");
add("value2");
}};
System.out.println(set.getClass().getName());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
simply don't use double brace initializer
FREQUENCY : always
java 22.0.1 2024-04-16
Java(TM) SE Runtime Environment (build 22.0.1+8-16)
Java HotSpot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
Double brace initializer produces runtime type of the enclosing class.
If you are currently writing a class named A and are using the double brace initializer on some collection, the runtime instance type of the collection will be of type A, instead of the collection type.
This really screws up reflection and serialization.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) initialize a new collection with brace initializer
2) print the collection class name
3) prints the enclosing class
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
HashMap$1
HashSet$1
ACTUAL -
Main$1
Main$2
---------- BEGIN SOURCE ----------
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>(){{
put("key1", "value1");
put("key2", "value2");
}};
System.out.println(map.getClass().getName());
Set<String> set = new HashSet<>(){{
add("value1");
add("value2");
}};
System.out.println(set.getClass().getName());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
simply don't use double brace initializer
FREQUENCY : always