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