Skip to content

Instantly share code, notes, and snippets.

@lichengwu
Created September 14, 2012 05:20
Show Gist options
  • Save lichengwu/3719952 to your computer and use it in GitHub Desktop.
Save lichengwu/3719952 to your computer and use it in GitHub Desktop.
when will generic type be erased
when you declare generic type, you can get it. when you use generic type, it will be erased. eg:
public class Generics {
public <T> T m1(T object){
return object;
}
public void m2(){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
}
}
decompilation: m1() deckare generic type, you can get it from class file. m2() use generic type,erased.
public <T extends java/lang/Object> T m1(T);
flags: ACC_PUBLIC
Code:
stack=1, locals=2, args_size=2
0: aload_1
1: areturn
LineNumberTable:
line 20: 0
Signature: #15 // <T:Ljava/lang/Object;>(TT;)TT;
public void m2();
flags: ACC_PUBLIC
Code:
stack=2, locals=2, args_size=1
0: new #2 // class java/util/ArrayList
3: dup
4: invokespecial #3 // Method java/util/ArrayList."<init>":()V
7: astore_1
8: aload_1
9: iconst_1
10: invokestatic #4 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
13: invokeinterface #5, 2 // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
18: pop
19: return
LineNumberTable:
line 24: 0
line 25: 8
line 26: 19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment