Last active
September 3, 2015 20:27
-
-
Save h3xstream/0b02944e0017dcb50f54 to your computer and use it in GitHub Desktop.
Type erasure
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
> javac *.java | |
> javap -c StringBuilderProcessor | |
Compiled from "StringBuilderProcessor.java" | |
public class StringBuilderProcessor implements GenericProcessor<java.lang.StringBuilder> { | |
public StringBuilderProcessor(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1 // Method java/lang/Object."<init>":()V | |
4: return | |
public void modify(java.lang.StringBuilder); | |
Code: | |
0: aload_1 | |
1: ldc #2 // String test | |
3: invokevirtual #3 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
6: pop | |
7: return | |
public void modify(java.lang.Object); | |
Code: | |
0: aload_0 | |
1: aload_1 | |
2: checkcast #4 // class java/lang/StringBuilder | |
5: invokevirtual #5 // Method modify:(Ljava/lang/StringBuilder;)V | |
8: return | |
} | |
> javap -c MainTest | |
Compiled from "MainTest.java" | |
public class MainTest { | |
public MainTest(); | |
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 StringBuilderProcessor | |
3: dup | |
4: invokespecial #3 // Method StringBuilderProcessor."<init>":()V | |
7: astore_1 | |
8: aload_1 | |
9: new #4 // class java/lang/StringBuilder | |
12: dup | |
13: invokespecial #5 // Method java/lang/StringBuilder."<init>":()V | |
16: invokeinterface #6, 2 // InterfaceMethod GenericProcessor.modify:(Ljava/lang/Object;)V | |
21: return | |
} |
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
public interface GenericProcessor<T> { | |
void modify(T test); | |
} |
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
public class MainTest { | |
public static void main(String[] args) { | |
GenericProcessor p = new StringBuilderProcessor(); | |
p.modify(new StringBuilder()); | |
} | |
} |
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
public class StringBuilderProcessor implements GenericProcessor<StringBuilder> { | |
@Override | |
public void modify(StringBuilder test) { | |
test.append("test"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment