-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
P4
-
Affects Version/s: 21.0.9
-
Component/s: core-libs
-
Fix Understood
-
generic
A DESCRIPTION OF THE PROBLEM :
Assume we compare two maps by `equals`, where the first one is `ConcurrentHashMap`. It is important that these maps have different types of keys.
`ConcurrentHashMap` calls `get` on the second map during comparison, but it is not prepared to `ClassCastException` that can be rightfully thrown from `Map#get`.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just run the snippet.
---------- BEGIN SOURCE ----------
```
public class Main {
public static void main(String[] args) {
var map = new ConcurrentHashMap<String, String>();
map.put("key", "value");
var map2 = new MyMap<String>();
map2.put(1, "value");
System.out.println(map.equals(map2));
}
static class MyMap<V> extends ConcurrentHashMap<Integer,V>{
@Override
public V get(Object key) {
Integer newKey = (Integer)key;
return super.get(newKey);
}
}
}
```
---------- END SOURCE ----------
FREQUENCY :
ALWAYS
Assume we compare two maps by `equals`, where the first one is `ConcurrentHashMap`. It is important that these maps have different types of keys.
`ConcurrentHashMap` calls `get` on the second map during comparison, but it is not prepared to `ClassCastException` that can be rightfully thrown from `Map#get`.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just run the snippet.
---------- BEGIN SOURCE ----------
```
public class Main {
public static void main(String[] args) {
var map = new ConcurrentHashMap<String, String>();
map.put("key", "value");
var map2 = new MyMap<String>();
map2.put(1, "value");
System.out.println(map.equals(map2));
}
static class MyMap<V> extends ConcurrentHashMap<Integer,V>{
@Override
public V get(Object key) {
Integer newKey = (Integer)key;
return super.get(newKey);
}
}
}
```
---------- END SOURCE ----------
FREQUENCY :
ALWAYS
- links to
-
Review(master)
openjdk/jdk/28453