d5041d2b238817afa5d786a831ffc706748d05dd
[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                         Class<? extends List> list, Class<? super Map> map, Class<?> clazz2, Class<VWXZ> clazz3,
29                         List<String> listString, Map<Integer,String> mapString, 
30                         Generic<Integer,String,Double,Short,Float> test, 
31                         String sampleField, int one, short two, double three, Object obj) {
32             
33                         this.sampleField = sampleField;
34             return clazz;
35         }
36                  
37            
38            /*public String getSampleField() {
39                   return sampleField;
40            }*/
41            
42            /*public void setSampleField(String sampleField) {
43               this.sampleField = sampleField;
44            }
45            
46            public List<String> setSampleField(List<String> listString) {
47                   return listString;
48            }*/
49         }
50
51    public static void main(String[] args) {
52
53       Method[] methods = SampleClass.class.getMethods();
54       //  Method[] methods = Class.class.getMethods();
55         Method method = null;
56         for(Method meth : methods) {
57             if (meth.getName().equals("setSampleField")) {
58                 method = meth;
59             }
60         }
61         Type[] parameters = method.getGenericParameterTypes();
62       //Type[] parameters = methods[0].getGenericParameterTypes();
63       for (int i = 0; i < parameters.length; i++) {
64          System.out.println(parameters[i]);
65       }
66       System.out.println();
67       Type returnType = method.getGenericReturnType();
68       System.out.println(returnType);
69       
70       /*Type superCls = Generic.class.getGenericSuperclass();
71       //Type superCls = String.class.getGenericSuperclass();
72       System.out.println(superCls);
73         System.out.println();
74         Type[] interfaces = Generic.class.getGenericInterfaces();
75         for (int i = 0; i < interfaces.length; i++) {
76             System.out.println(interfaces[i]);
77         }*/
78       
79       
80           /*Method[] methods = Class.class.getMethods();
81           Method method = null;
82       for(Method mth : methods) {
83         if (mth.getName().equals("getConstructor")) {
84         //if (mth.getName().equals("isAssignableFrom")) {
85         //if (mth.getName().equals("getSuperclass")) {
86            method = mth;
87         }
88       }
89       Type[] parameters = method.getGenericParameterTypes();
90       //Type[] parameters = methods[0].getGenericParameterTypes();
91       for (int i = 0; i < parameters.length; i++) {
92          System.out.println(parameters[i]);
93       }
94       System.out.println();
95       Type returnType = method.getGenericReturnType();
96       System.out.println(returnType);*/
97       
98       /* TODO: Enumerate all methods in Class.class
99       Method[] methods = Class.class.getMethods();
100       for(Method mth : methods) {
101           System.out.println("===========================");
102           System.out.println("Method: " + mth.getName());
103                   Type[] parameters = mth.getGenericParameterTypes();
104                   for (int i = 0; i < parameters.length; i++) {
105                      System.out.println(parameters[i]);
106                   }
107                   System.out.println();
108                   Type returnType = mth.getGenericReturnType();
109                   System.out.println(returnType + "\n");
110       }*/
111
112       /*Class[] parameterTypes = methods[0].getParameterTypes();
113       for(Class parameterType: parameterTypes){
114          System.out.println(parameterType.getName());   
115  
116       }
117       System.out.println();*/
118       /*TypeVariable[] typeParameters = Generic.class.getTypeParameters();
119       //TypeVariable[] typeParameters = SampleClass.class.getTypeParameters();
120       for(TypeVariable typeVar: typeParameters){
121          System.out.println(typeVar);   
122  
123       }
124       System.out.println();
125
126       Type[] bounds = typeParameters[0].getBounds();
127       for (Type bound : bounds) {
128           System.out.println(bound);
129       }
130       System.out.println();*/
131   
132    }
133 }
134