001 package other;
002
003 //import java.beans.BeanInfo;
004 //import java.beans.IntrospectionException;
005 //import java.beans.Introspector;
006 //import java.beans.PropertyDescriptor;
007 import java.util.HashMap;
008
009 public class Debug
010 {
011
012 private static boolean debug = true;
013 private static boolean printMethodName = false;
014 private static HashMap<String, Integer> counters = new HashMap<String, Integer>();
015
016 public static void log(Object o)
017 {
018 print(o);
019 // print("\tin: " + Debug.getCallingMethod());
020 }
021
022 public static void exception(Exception e)
023 {
024 print(e);
025 }
026
027 public static void print(Object o)
028 {
029 if (debug)
030 {
031 if (o != null && o.getClass().getName().startsWith("["))
032 o = arrayToString(o);
033 String stringToPrint = String.valueOf(o);
034 stringToPrint = addBeforeEachLine(stringToPrint, "~");
035 System.out.println(stringToPrint);
036 System.out.flush();
037 }
038 if (printMethodName)
039 (new Exception("This is just to get the method name"))
040 .printStackTrace();
041 }
042
043 public static void print(int i)
044 {
045 print(Integer.toString(i));
046 }
047
048 public static void print(Object o, int i)
049 {
050 if (i == 1)
051 print(o);
052 }
053
054 public static void println(Object o)
055 {
056 print(o + "\n");
057 }
058
059 public static void addCounter(String name)
060 {
061 if (counters.containsKey(name))
062 print("Resetting counter: \"" + name + "\" with value: "
063 + counters.get(name));
064 counters.put(name, new Integer(0));
065 }
066
067 public static void printCounter(String name)
068 {
069 String s = "`Counter: \"" + name + "\" = " + counters.get(name) + "\n";
070 System.out.print(s);
071 }
072
073 public static void incrementCounter(String name)
074 {
075 counters.put(name, new Integer(counters.get(name).intValue() + 1));
076 }
077
078 public static void incrementAndPrintCounter(String name)
079 {
080 printCounter(name);
081 incrementCounter(name);
082 }
083
084 public static void count(String name)
085 {
086 if (counters.containsKey(name))
087 incrementCounter(name);
088 else
089 addCounter(name);
090 printCounter(name);
091 }
092
093 public static String addBeforeEachLine(String s, String prefix)
094 {
095 return prefix + s;
096 }
097
098 public static String arrayToString(Object o)
099 {
100 return arrayToString(o, ", ", ",\n");
101 }
102
103 public static String arrayToStringln(Object o)
104 {
105 return arrayToString(o, ",\n ", ",\n");
106 }
107
108 public static String arrayToString(Object o, String elementSperator,
109 String arraySeperator)
110 {
111 if (o == null)
112 return "null";
113 String cl = o.getClass().getName();
114 if (!cl.startsWith("["))
115 return o.toString();
116 StringBuffer sb = new StringBuffer("[");
117 if (o instanceof byte[])
118 {
119 byte ba[] = (byte[]) o;
120 for (int i = 0; i < ba.length; i++)
121 {
122 if (i > 0)
123 sb.append(elementSperator);
124 sb.append("(byte)");
125 sb.append(ba[i]);
126 }
127
128 } else if (o instanceof char[])
129 {
130 char ca[] = (char[]) o;
131 for (int i = 0; i < ca.length; i++)
132 {
133 if (i > 0)
134 sb.append(elementSperator);
135 sb.append("'");
136 sb.append(ca[i]);
137 sb.append("'");
138 }
139
140 } else if (o instanceof short[])
141 {
142 short sa[] = (short[]) o;
143 for (int i = 0; i < sa.length; i++)
144 {
145 if (i > 0)
146 sb.append(elementSperator);
147 sb.append("(short)");
148 sb.append(sa[i]);
149 }
150
151 } else if (o instanceof int[])
152 {
153 int ia[] = (int[]) o;
154 for (int i = 0; i < ia.length; i++)
155 {
156 if (i > 0)
157 sb.append(elementSperator);
158 sb.append(ia[i]);
159 }
160
161 } else if (o instanceof long[])
162 {
163 long la[] = (long[]) o;
164 for (int i = 0; i < la.length; i++)
165 {
166 if (i > 0)
167 sb.append(elementSperator);
168 sb.append(la[i]);
169 sb.append("L");
170 }
171
172 } else if (o instanceof float[])
173 {
174 float fa[] = (float[]) o;
175 for (int i = 0; i < fa.length; i++)
176 {
177 if (i > 0)
178 sb.append(elementSperator);
179 sb.append(fa[i]);
180 sb.append("F");
181 }
182
183 } else if (o instanceof double[])
184 {
185 double da[] = (double[]) o;
186 for (int i = 0; i < da.length; i++)
187 {
188 if (i > 0)
189 sb.append(elementSperator);
190 sb.append(da[i]);
191 sb.append("D");
192 }
193
194 } else if (o instanceof String)
195 {
196 String sa[] = (String[]) o;
197 for (int i = 0; i < sa.length; i++)
198 {
199 if (i > 0)
200 sb.append(elementSperator);
201 sb.append("\"");
202 sb.append(sa[i]);
203 sb.append("\"");
204 }
205
206 } else if (cl.startsWith("[L"))
207 {
208 Object oa[] = (Object[]) o;
209 for (int i = 0; i < oa.length; i++)
210 {
211 if (i > 0)
212 sb.append(elementSperator);
213 sb.append(i + "-");
214 sb.append(oa[i]);
215 }
216
217 } else if (cl.startsWith("[["))
218 {
219 Object aa[] = (Object[]) o;
220 for (int i = 0; i < aa.length; i++)
221 {
222 if (i > 0)
223 sb.append(arraySeperator);
224 sb
225 .append(arrayToString(aa[i], elementSperator,
226 arraySeperator));
227 }
228
229 } else
230 {
231 sb.append("(unknown array type)");
232 }
233 sb.append("]");
234 return sb.toString();
235 }
236
237 // public static void initLoggerThread()
238 // {
239 // try
240 // {
241 //
242 // }
243 // catch (FileNotFoundException e)
244 // {
245 // e.printStackTrace();
246 // }
247 // }
248 //
249 // public static String getBeanStuff(Object bean)
250 // {
251 // String s= "";
252 // BeanInfo beanInfo = null;
253 // try
254 // {
255 // beanInfo = Introspector.getBeanInfo(bean.getClass());
256 // }
257 // catch (IntrospectionException e1)
258 // {
259 // e1.printStackTrace();
260 // }
261 // PropertyDescriptor property[] = beanInfo.getPropertyDescriptors();
262 // for (int i = 0; i < property.length; i++)
263 // {
264 // String propertyName = property[i].getName();
265 // try
266 // {
267 // Object propertyValue = property[i].getReadMethod()
268 // .invoke(bean, null);
269 // s += propertyName + " = " + propertyValue + "\n";
270 // }
271 // catch (IllegalArgumentException e)
272 // {
273 // e.printStackTrace();
274 // }
275 // catch (IllegalAccessException e)
276 // {
277 // e.printStackTrace();
278 // }
279 // catch (InvocationTargetException e)
280 // {
281 // e.printStackTrace();
282 // }
283 // }
284 // return s;
285 // }
286 //
287 // public static void inspectBean(Object bean)
288 // {
289 // print(getBeanStuff(bean));
290 // }
291
292 public static String getCallingMethod()
293 {
294 StackTraceElement[] stackElements = new Exception().getStackTrace();
295
296 StackTraceElement callingElement = null;
297 for (int i = 0; i < stackElements.length; i++)
298 if (!stackElements[i].getClassName().equals(Debug.class.getName()))
299 callingElement = stackElements[i];
300
301 return callingElement == null ? null : callingElement.toString();
302 // String info = null;
303 // if(callingElement != null)
304 // {
305 // info = callingElement.getClassName();
306 //
307 // if (info != null)
308 // info += "." + callingElement.getMethodName();
309 // }
310 }
311 }