Fixing a new bug: Considering parameters with Type and Type array, e.g., T and T[].
[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 import java.util.Collection;
11
12 import java.math.BigInteger;
13 import java.security.ProtectionDomain;
14
15 public class Reflection {
16
17     interface GenericSuperShort<XYZ> {
18
19     }
20
21     class GenericShort<TUVW,ABCD> {
22     }
23
24     class Generic<TUVW,ABCD,KLM,NOP,XYZ> extends GenericShort<TUVW,ABCD> implements GenericSuperShort<XYZ>, Serializable {
25
26     }
27
28     class SampleClass<VWXZ> {
29         private String sampleField;
30
31         public Class<?> setSampleField(Class<?> clazz,
32                         Class<? extends List> list, Class<? super Map> map, Class<?> clazz2, Class<VWXZ> clazz3,
33                         List<String> listString, Map<Integer,String> mapString, 
34                         Generic<Integer,String,Double,Short,Float> test, 
35                         String sampleField, int one, short two, double three, Object obj) {
36             
37                         this.sampleField = sampleField;
38             return clazz;
39         }
40                  
41            
42            /*public String getSampleField() {
43                   return sampleField;
44            }*/
45            
46            /*public void setSampleField(String sampleField) {
47               this.sampleField = sampleField;
48            }
49            
50            public List<String> setSampleField(List<String> listString) {
51                   return listString;
52            }*/
53         }
54
55    public static void main(String[] args) {
56            
57           BigInteger bi = new BigInteger("-1");
58           System.out.println(bi);
59            
60           /* TODO: Enumerate all methods in Class.class 
61       Method[] methods = Class.class.getMethods();
62       for(Method mth : methods) {
63           //System.out.println("===========================");
64           //System.out.println("Method: " + mth.getName());
65                   Type[] parameters = mth.getGenericParameterTypes();
66                   for (int i = 0; i < parameters.length; i++) {
67                      System.out.println(parameters[i]);
68                   }
69                   System.out.println();
70                   Type returnType = mth.getGenericReturnType();
71                   System.out.println(returnType + "\n");
72       }*/
73
74       Method[] methods = Collection.class.getMethods();
75       //  Method[] methods = Class.class.getMethods();
76         Method method = null;
77         for(Method meth : methods) {
78                                 System.out.println("===========================");
79                                 //System.out.println("Method: " + meth.toString());
80                                 Type[] parameters = meth.getGenericParameterTypes();
81                                 for (int i = 0; i < parameters.length; i++) {
82                                         System.out.println(parameters[i]);
83                                 }
84                                 Type returnType = meth.getGenericReturnType();
85                                 System.out.println(returnType);
86                                 System.out.println("===========================");
87         }
88         Type[] parameters = method.getGenericParameterTypes();
89       //Type[] parameters = methods[0].getGenericParameterTypes();
90       for (int i = 0; i < parameters.length; i++) {
91          System.out.println(parameters[i]);
92       }
93       System.out.println();
94       Type returnType = method.getGenericReturnType();
95       System.out.println(returnType);
96           
97         /* TODO: This is an excerpt of the BigInteger library
98                 int radix = 10;
99         String val = "-1";
100         int signum = 0;
101         final int[] mag;
102
103         int cursor = 0, numDigits;
104         final int len = val.length();
105
106         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
107             throw new NumberFormatException("Radix out of range");
108         if (len == 0)
109             throw new NumberFormatException("Zero length BigInteger");
110
111         // Check for at most one leading sign
112         int sign = 1;
113         int index1 = val.lastIndexOf('-');
114         int index2 = val.lastIndexOf('+');
115         if (index1 >= 0) {
116             if (index1 != 0 || index2 >= 0) {
117                 throw new NumberFormatException("Illegal embedded sign character");
118             }
119             sign = -1;
120             cursor = 1;
121         } else if (index2 >= 0) {
122             if (index2 != 0) {
123                 throw new NumberFormatException("Illegal embedded sign character");
124             }
125             cursor = 1;
126         }
127                 System.out.println(cursor);
128         if (cursor == len)
129             throw new NumberFormatException("Zero length BigInteger");
130
131         // Skip leading zeros and compute number of digits in magnitude
132         while (cursor < len &&
133                 Character.digit(val.charAt(cursor), radix) == 0) {
134             cursor++;
135         }
136
137         if (cursor == len) {
138             signum = 0;
139             //mag = ZERO.mag;
140             //mag = null;
141             return;
142         }
143
144         numDigits = len - cursor;
145         signum = sign;
146
147         long numBits = ((numDigits * bitsPerDigit[radix]) >>> 10) + 1;
148         if (numBits + 31 >= (1L << 32)) {
149             System.out.println("Overflow!");
150         }
151         int numWords = (int) (numBits + 31) >>> 5;
152         int[] magnitude = new int[numWords];
153
154         // Process first (potentially short) digit group
155         int firstGroupLen = numDigits % digitsPerInt[radix];
156         if (firstGroupLen == 0)
157             firstGroupLen = digitsPerInt[radix];
158                 int cursor2 = cursor + firstGroupLen;
159         String group = val.substring(cursor, cursor2);
160         magnitude[numWords - 1] = Integer.parseInt(group, radix);
161         if (magnitude[numWords - 1] < 0)
162             throw new NumberFormatException("Illegal digit");*/
163           
164       /*Type superCls = Generic.class.getGenericSuperclass();
165       //Type superCls = String.class.getGenericSuperclass();
166       System.out.println(superCls);
167         System.out.println();
168         Type[] interfaces = Generic.class.getGenericInterfaces();
169         for (int i = 0; i < interfaces.length; i++) {
170             System.out.println(interfaces[i]);
171         }*/
172       
173       /*
174           Method[] methods = Collection.class.getMethods();
175           Method method = null;
176       for(Method mth : methods) {
177         if (mth.getName().equals("toArray")) {
178         //if (mth.getName().equals("isAssignableFrom")) {
179         //if (mth.getName().equals("getSuperclass")) {
180            method = mth;
181                    break;
182         }
183       }
184       Type[] parameters = method.getGenericParameterTypes();
185       //Type[] parameters = methods[0].getGenericParameterTypes();
186       for (int i = 0; i < parameters.length; i++) {
187          System.out.println(parameters[i]);
188       }
189       System.out.println();
190       Type returnType = method.getGenericReturnType();
191       System.out.println(returnType);*/
192
193       /*Class[] parameterTypes = methods[0].getParameterTypes();
194       for(Class parameterType: parameterTypes){
195          System.out.println(parameterType.getName());   
196  
197       }
198       System.out.println();*/
199       /*TypeVariable[] typeParameters = Generic.class.getTypeParameters();
200       //TypeVariable[] typeParameters = SampleClass.class.getTypeParameters();
201       for(TypeVariable typeVar: typeParameters){
202          System.out.println(typeVar);   
203  
204       }
205       System.out.println();
206
207       Type[] bounds = typeParameters[0].getBounds();
208       for (Type bound : bounds) {
209           System.out.println(bound);
210       }
211       System.out.println();*/
212           
213           //ProtectionDomain pd = Class.class.getProtectionDomain();
214       //System.out.println(pd);
215    }
216 }
217