Created
September 17, 2011 20:24
-
-
Save frsyuki/1224328 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.lang.reflect.Method; | |
public class GenericsOverload { | |
public static void main(String[] args) throws Exception { | |
GenericsOverload go = new GenericsOverload(); | |
go.read(String.class); //=> Class<String> | |
go.read(Long.class); //=> Class<Long> | |
go.read(Integer.class); //=> Class<T> T=class java.lang.Integer | |
go.read(byte[].class); //=> Class<T> T=class [B | |
Method m = GenericsOverload.class.getMethod("read", Class.class); | |
m.invoke(go, Void.class); //=> Class<String> | |
} | |
public <T> T read(Class<T> klass) { | |
System.out.println("Class<T> T="+klass); | |
return null; | |
} | |
public String read(Class<String> klass) { | |
System.out.println("Class<String>"); | |
return null; | |
} | |
public Long read(Class<Long> klass) { | |
System.out.println("Class<Long>"); | |
return null; | |
} | |
} |
This file contains hidden or 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 GenericsOverload | |
Compiled from "GenericsOverload.java" | |
public class GenericsOverload extends java.lang.Object{ | |
public GenericsOverload(); | |
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 GenericsOverload | |
3: dup | |
4: invokespecial #3; //Method "<init>":()V | |
7: astore_1 | |
8: aload_1 | |
9: ldc_w #4; //class java/lang/String | |
12: invokevirtual #5; //Method read:(Ljava/lang/Class;)Ljava/lang/String; | |
15: pop | |
16: aload_1 | |
17: ldc_w #6; //class java/lang/Long | |
20: invokevirtual #7; //Method read:(Ljava/lang/Class;)Ljava/lang/Long; | |
23: pop | |
24: aload_1 | |
25: ldc_w #8; //class java/lang/Integer | |
28: invokevirtual #9; //Method read:(Ljava/lang/Class;)Ljava/lang/Object; | |
31: pop | |
32: aload_1 | |
33: ldc_w #10; //class "[B" | |
36: invokevirtual #9; //Method read:(Ljava/lang/Class;)Ljava/lang/Object; | |
39: pop | |
40: return | |
public java.lang.Object read(java.lang.Class); | |
Code: | |
0: getstatic #11; //Field java/lang/System.out:Ljava/io/PrintStream; | |
3: new #12; //class java/lang/StringBuilder | |
6: dup | |
7: invokespecial #13; //Method java/lang/StringBuilder."<init>":()V | |
10: ldc #14; //String Class<T> T= | |
12: invokevirtual #15; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
15: aload_1 | |
16: invokevirtual #16; //Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder; | |
19: invokevirtual #17; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; | |
22: invokevirtual #18; //Method java/io/PrintStream.println:(Ljava/lang/String;)V | |
25: aconst_null | |
26: areturn | |
public java.lang.String read(java.lang.Class); | |
Code: | |
0: getstatic #11; //Field java/lang/System.out:Ljava/io/PrintStream; | |
3: ldc #19; //String Class<String> | |
5: invokevirtual #18; //Method java/io/PrintStream.println:(Ljava/lang/String;)V | |
8: aconst_null | |
9: areturn | |
public java.lang.Long read(java.lang.Class); | |
Code: | |
0: getstatic #11; //Field java/lang/System.out:Ljava/io/PrintStream; | |
3: ldc #20; //String Class<Long> | |
5: invokevirtual #18; //Method java/io/PrintStream.println:(Ljava/lang/String;)V | |
8: aconst_null | |
9: areturn | |
} |
This file contains hidden or 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
Compiled from "GenericsOverload.java" | |
public class GenericsOverload extends java.lang.Object{ | |
public GenericsOverload(); | |
public static void main(java.lang.String[]); | |
public java.lang.Object read(java.lang.Class); | |
public java.lang.String read(java.lang.Class); | |
public java.lang.Long read(java.lang.Class); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment