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