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