Fixing another bug for getGenericReturnType so that it recognizes Class<?> as Wildcar...
[jpf-core.git] / examples / Reflection.java
1 import java.lang.reflect.Method;
2 import java.lang.reflect.Type;
3 import java.lang.reflect.TypeVariable;
4 import java.io.Serializable;
5
6 import java.util.List;
7 import java.util.Map;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10
11 public class Reflection {
12
13     interface GenericSuperShort<XYZ> {
14
15     }
16
17     class GenericShort<TUVW,ABCD> {
18     }
19
20     class Generic<TUVW,ABCD,KLM,NOP,XYZ> extends GenericShort<TUVW,ABCD> implements GenericSuperShort<XYZ>, Serializable {
21
22     }
23
24     class SampleClass<VWXZ> {
25         private String sampleField;
26
27         public Class<?> setSampleField(Class<?> clazz, 
28                         List<String> listString, Map<Integer,String> mapString, 
29                         Generic<Integer,String,Double,Short,Float> test, 
30                         String sampleField, int one, short two, double three, Object obj) {
31             
32                         this.sampleField = sampleField;
33             return clazz;
34         }
35                  
36            
37            /*public String getSampleField() {
38                   return sampleField;
39            }*/
40            
41            /*public void setSampleField(String sampleField) {
42               this.sampleField = sampleField;
43            }
44            
45            public List<String> setSampleField(List<String> listString) {
46                   return listString;
47            }*/
48         }
49
50    public static void main(String[] args) {
51
52       /*Method[] methods = SampleClass.class.getMethods();
53       //  Method[] methods = Class.class.getMethods();
54         Method method = null;
55         for(Method meth : methods) {
56             if (meth.getName().equals("setSampleField")) {
57                 method = meth;
58             }
59         }
60         Type[] parameters = method.getGenericParameterTypes();
61       //Type[] parameters = methods[0].getGenericParameterTypes();
62       for (int i = 0; i < parameters.length; i++) {
63          System.out.println(parameters[i]);
64       }
65       System.out.println();*/
66       /*Type superCls = Generic.class.getGenericSuperclass();
67       //Type superCls = String.class.getGenericSuperclass();
68       System.out.println(superCls);
69         System.out.println();
70         Type[] interfaces = Generic.class.getGenericInterfaces();
71         for (int i = 0; i < interfaces.length; i++) {
72             System.out.println(interfaces[i]);
73         }*/
74       
75       Method[] methods = SampleClass.class.getMethods();
76         Method method = null;
77         for(Method mth : methods) {
78             if (mth.getName().equals("setSampleField")) {
79                 method = mth;
80             }
81         }
82       Type[] parameters = method.getGenericParameterTypes();
83       //Type[] parameters = methods[0].getGenericParameterTypes();
84       for (int i = 0; i < parameters.length; i++) {
85          System.out.println(parameters[i]);
86       }
87       System.out.println();
88       /*Class[] parameterTypes = methods[0].getParameterTypes();
89       for(Class parameterType: parameterTypes){
90          System.out.println(parameterType.getName());   
91  
92       }
93       System.out.println();*/
94       /*TypeVariable[] typeParameters = Generic.class.getTypeParameters();
95       //TypeVariable[] typeParameters = SampleClass.class.getTypeParameters();
96       for(TypeVariable typeVar: typeParameters){
97          System.out.println(typeVar);   
98  
99       }
100       System.out.println();
101
102       Type[] bounds = typeParameters[0].getBounds();
103       for (Type bound : bounds) {
104           System.out.println(bound);
105       }
106       System.out.println();*/
107       Type returnType = method.getGenericReturnType();
108       System.out.println(returnType);
109           
110    }
111 }
112