Created
March 25, 2018 20:39
-
-
Save v6ak/794a6f81d5d06349dcc6eb8d741042b7 to your computer and use it in GitHub Desktop.
Rough edge of Java 10 type inference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.List; | |
import java.util.ArrayList; | |
import java.io.Serializable; | |
public class Main { | |
private static void trick(List list){ | |
// WARNING: black magic with non-generic code on a generic List… | |
list.clear(); | |
list.add(new Serializable(){}); // Serializable but not CharSequence | |
list.add(new javax.swing.text.Segment()); // CharSequence but not Serializable | |
} | |
public static void main(String[] args) { | |
var list = new ArrayList<>(List.of("", new StringBuilder())); | |
trick(list); | |
for (int i=0; i<list.size(); i++){ | |
try { | |
System.out.println("Looking at item #"+i); | |
var item = list.get(i); // fails here with the second item (new javax.swing.text.Segment()) | |
System.out.println("Got the item."); | |
System.out.println(item.charAt(0)); // fails here with the first item (new Serializable(){}) | |
} catch(Throwable e){ | |
e.printStackTrace(); | |
// continue experiment | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% javap -c test.Main | |
Compiled from "Main.java" | |
public class test.Main { | |
public test.Main(); | |
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 #8 // class java/util/ArrayList | |
3: dup | |
4: ldc #9 // String | |
6: new #10 // class java/lang/StringBuilder | |
9: dup | |
10: invokespecial #11 // Method java/lang/StringBuilder."<init>":()V | |
13: invokestatic #12 // InterfaceMethod java/util/List.of:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/util/List; | |
16: invokespecial #13 // Method java/util/ArrayList."<init>":(Ljava/util/Collection;)V | |
19: astore_1 | |
20: aload_1 | |
21: invokestatic #14 // Method trick:(Ljava/util/List;)V | |
24: iconst_0 | |
25: istore_2 | |
26: iload_2 | |
27: aload_1 | |
28: invokevirtual #15 // Method java/util/ArrayList.size:()I | |
31: if_icmpge 93 | |
34: getstatic #16 // Field java/lang/System.out:Ljava/io/PrintStream; | |
37: iload_2 | |
38: invokedynamic #17, 0 // InvokeDynamic #0:makeConcatWithConstants:(I)Ljava/lang/String; | |
43: invokevirtual #18 // Method java/io/PrintStream.println:(Ljava/lang/String;)V | |
46: aload_1 | |
47: iload_2 | |
48: invokevirtual #19 // Method java/util/ArrayList.get:(I)Ljava/lang/Object; | |
51: checkcast #20 // class java/io/Serializable | |
54: astore_3 | |
55: getstatic #16 // Field java/lang/System.out:Ljava/io/PrintStream; | |
58: ldc #21 // String Got the item. | |
60: invokevirtual #18 // Method java/io/PrintStream.println:(Ljava/lang/String;)V | |
63: getstatic #16 // Field java/lang/System.out:Ljava/io/PrintStream; | |
66: aload_3 | |
67: checkcast #22 // class java/lang/CharSequence | |
70: iconst_0 | |
71: invokeinterface #23, 2 // InterfaceMethod java/lang/CharSequence.charAt:(I)C | |
76: invokevirtual #24 // Method java/io/PrintStream.println:(C)V | |
79: goto 87 | |
82: astore_3 | |
83: aload_3 | |
84: invokevirtual #26 // Method java/lang/Throwable.printStackTrace:()V | |
87: iinc 2, 1 | |
90: goto 26 | |
93: return | |
Exception table: | |
from to target type | |
34 79 82 Class java/lang/Throwable | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% java test/Main | |
Looking at item #0 | |
Got the item. | |
java.lang.ClassCastException: test.Main$1 cannot be cast to java.base/java.lang.CharSequence | |
at test.Main.main(Main.java:24) | |
Looking at item #1 | |
java.lang.ClassCastException: java.desktop/javax.swing.text.Segment cannot be cast to java.base/java.io.Serializable | |
at test.Main.main(Main.java:22) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment