-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericReflectionTest.java
More file actions
103 lines (77 loc) · 2.62 KB
/
GenericReflectionTest.java
File metadata and controls
103 lines (77 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package genericPro;
import java.lang.reflect.*;
import java.util.*;
public class GenericReflectionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Class<?> cl=Pair.class;
// printClass(cl);
// for(Method m:cl.getDeclaredMethods())
// printMethod(m);
Type sc=cl.getGenericSuperclass();
System.out.print(sc);
}
public static void printClass(Class<?> cl)
{
System.out.print(cl);
printTypes(cl.getTypeParameters(),"<",",",">",true);
Type sc=cl.getGenericSuperclass();
if(sc!=null){
System.out.print(" extends ");
printType(sc,false);
}
printTypes(cl.getGenericInterfaces()," implements ",", ","",false);
System.out.println();
}
public static void printMethod(Method m){
String name=m.getName();
System.out.print(Modifier.toString(m.getModifiers()));
System.out.print(" ");
printTypes(m.getTypeParameters(),"<",", ","> ",true);
printType(m.getGenericReturnType(),false);
System.out.print(" ");
System.out.print(name);
System.out.print("(");
printTypes(m.getGenericParameterTypes(),"",", ","",false);
System.out.println(")");
}
public static void printTypes(Type[] types,String pre,String sep,String suf,boolean isDefinition){
if(pre.equals(" extends")&&Arrays.equals(types, new Type[]{Object.class})) return;
if(types.length>0) System.out.print(pre);
for(int i=0;i<types.length;i++){
if(i>0) System.out.print(suf);
printType(types[i],isDefinition);
}
if(types.length>0) System.out.print(suf);
}
public static void printType(Type type,boolean isDefinition){
if(type instanceof Class){
Class<?> t=(Class<?>) type;
System.out.print(t.getName());
}else if(type instanceof TypeVariable){
TypeVariable<?> t=(TypeVariable<?>) type;
System.out.print(t.getName());
if(isDefinition)
printTypes(t.getBounds()," extends","& ","",false);
}else if(type instanceof WildcardType){
WildcardType t=(WildcardType) type;
System.out.print("?");
printTypes(t.getUpperBounds()," extends "," & ","",false);
printTypes(t.getLowerBounds()," super "," & ","",false);
}else if(type instanceof ParameterizedType){
ParameterizedType t=(ParameterizedType)type;
Type owner=t.getOwnerType();
if(owner!=null){
printType(owner,false);
System.out.print(".");
}
printType(t.getRawType(),false);
printTypes(t.getActualTypeArguments(),"<",", ",">",false);
}else if(type instanceof GenericArrayType){
GenericArrayType t=(GenericArrayType) type;
System.out.print(" ");
printType(t.getGenericComponentType(),isDefinition);
System.out.print("[]");
}
}
}