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