Fixing another bug: The java.lang.StringBuilder.append(char c) needs to consider...
[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           StringBuilder sb = new StringBuilder(0);
60           sb.append('[');
61           sb.append(']');
62           System.out.println(sb.toString());
63            
64           /* TODO: Enumerate all methods in Class.class
65       Method[] methods = Collection.class.getMethods();
66       for(Method mth : methods) {
67           System.out.println("===========================");
68           //System.out.println("Method: " + mth.getName());
69                   Type[] parameters = mth.getGenericParameterTypes();
70                   for (int i = 0; i < parameters.length; i++) {
71                      System.out.println(parameters[i]);
72                   }
73                   System.out.println();
74                   Type returnType = mth.getGenericReturnType();
75                   System.out.println(returnType + "\n");
76       }*/
77
78       /*Method[] methods = Collection.class.getMethods();
79       //  Method[] methods = Class.class.getMethods();
80         Method method = null;
81         for(Method meth : methods) {
82                         System.out.println("===========================");
83                         //System.out.println("Method: " + meth.toString());
84                         Type[] parameters = meth.getGenericParameterTypes();
85                         for (int i = 0; i < parameters.length; i++) {
86                                 System.out.println(parameters[i]);
87                         }
88                         Type returnType = meth.getGenericReturnType();
89                         System.out.println(returnType);
90                         System.out.println("===========================");
91         }*/
92           
93         /* TODO: This is an excerpt of the BigInteger library
94                 int radix = 10;
95         String val = "-1";
96         int signum = 0;
97         final int[] mag;
98
99         int cursor = 0, numDigits;
100         final int len = val.length();
101
102         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
103             throw new NumberFormatException("Radix out of range");
104         if (len == 0)
105             throw new NumberFormatException("Zero length BigInteger");
106
107         // Check for at most one leading sign
108         int sign = 1;
109         int index1 = val.lastIndexOf('-');
110         int index2 = val.lastIndexOf('+');
111         if (index1 >= 0) {
112             if (index1 != 0 || index2 >= 0) {
113                 throw new NumberFormatException("Illegal embedded sign character");
114             }
115             sign = -1;
116             cursor = 1;
117         } else if (index2 >= 0) {
118             if (index2 != 0) {
119                 throw new NumberFormatException("Illegal embedded sign character");
120             }
121             cursor = 1;
122         }
123                 System.out.println(cursor);
124         if (cursor == len)
125             throw new NumberFormatException("Zero length BigInteger");
126
127         // Skip leading zeros and compute number of digits in magnitude
128         while (cursor < len &&
129                 Character.digit(val.charAt(cursor), radix) == 0) {
130             cursor++;
131         }
132
133         if (cursor == len) {
134             signum = 0;
135             //mag = ZERO.mag;
136             //mag = null;
137             return;
138         }
139
140         numDigits = len - cursor;
141         signum = sign;
142
143         long numBits = ((numDigits * bitsPerDigit[radix]) >>> 10) + 1;
144         if (numBits + 31 >= (1L << 32)) {
145             System.out.println("Overflow!");
146         }
147         int numWords = (int) (numBits + 31) >>> 5;
148         int[] magnitude = new int[numWords];
149
150         // Process first (potentially short) digit group
151         int firstGroupLen = numDigits % digitsPerInt[radix];
152         if (firstGroupLen == 0)
153             firstGroupLen = digitsPerInt[radix];
154                 int cursor2 = cursor + firstGroupLen;
155         String group = val.substring(cursor, cursor2);
156         magnitude[numWords - 1] = Integer.parseInt(group, radix);
157         if (magnitude[numWords - 1] < 0)
158             throw new NumberFormatException("Illegal digit");*/
159           
160       /*Type superCls = Generic.class.getGenericSuperclass();
161       //Type superCls = String.class.getGenericSuperclass();
162       System.out.println(superCls);
163         System.out.println();
164         Type[] interfaces = Generic.class.getGenericInterfaces();
165         for (int i = 0; i < interfaces.length; i++) {
166             System.out.println(interfaces[i]);
167         }*/
168       
169       
170           /*Method[] methods = Map.class.getMethods();
171           Method method = null;
172       for(Method mth : methods) {
173         if (mth.getName().equals("putAll")) {
174         //if (mth.getName().equals("isAssignableFrom")) {
175         //if (mth.getName().equals("getSuperclass")) {
176            method = mth;
177                    break;
178         }
179       }
180       Type[] parameters = method.getGenericParameterTypes();
181       //Type[] parameters = methods[0].getGenericParameterTypes();
182       for (int i = 0; i < parameters.length; i++) {
183          System.out.println(parameters[i]);
184       }
185       System.out.println();
186       Type returnType = method.getGenericReturnType();
187       System.out.println(returnType);*/
188
189       /*Class[] parameterTypes = methods[0].getParameterTypes();
190       for(Class parameterType: parameterTypes){
191          System.out.println(parameterType.getName());   
192  
193       }
194       System.out.println();*/
195       /*TypeVariable[] typeParameters = Generic.class.getTypeParameters();
196       //TypeVariable[] typeParameters = SampleClass.class.getTypeParameters();
197       for(TypeVariable typeVar: typeParameters){
198          System.out.println(typeVar);   
199  
200       }
201       System.out.println();
202
203       Type[] bounds = typeParameters[0].getBounds();
204       for (Type bound : bounds) {
205           System.out.println(bound);
206       }
207       System.out.println();*/
208           
209           //ProtectionDomain pd = Class.class.getProtectionDomain();
210       //System.out.println(pd);
211    }
212 }
213