Skip to content

Instantly share code, notes, and snippets.

@nathansgreen
Last active August 29, 2015 14:03
Show Gist options
  • Save nathansgreen/4cc5392683708bad679a to your computer and use it in GitHub Desktop.
Save nathansgreen/4cc5392683708bad679a to your computer and use it in GitHub Desktop.
Java 8 ConcurrentHashMap.keySet() bytecode incompatibility
// In Java 8, ConcurrentHashMap backward compatibility was broken by changing to a covariant return type
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String...);
Code:
0: new #2 // class java/util/TreeSet
3: dup
4: invokespecial #3 // Method java/util/TreeSet."<init>":()V
7: new #4 // class java/util/concurrent/ConcurrentHashMap
10: dup
11: invokespecial #5 // Method java/util/concurrent/ConcurrentHashMap."<init>":()V
14: invokevirtual #6 // Method java/util/concurrent/ConcurrentHashMap.keySet:()Ljava/util/concurrent/ConcurrentHashMap$KeySetView;
17: invokevirtual #7 // Method java/util/TreeSet.addAll:(Ljava/util/Collection;)Z
20: pop
21: return
@nathansgreen
Copy link
Author

While Java 8 typically does a good job of creating bytecode that is compatible with Java 7, this is an example of where it does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment