1 23 24 28 29 package com.sun.enterprise.util.diagnostics; 30 31 import java.lang.reflect.*; 32 import java.util.Vector ; 33 import com.sun.enterprise.util.diagnostics.Reporter; 34 import com.sun.enterprise.util.StringUtils; 35 36 public class ObjectAnalyzer 37 { 38 41 public static String getMethods(String className) 42 { 43 try 44 { 45 Class clazz = Class.forName(className); 46 return getMethods(clazz, false); 47 } 48 catch(Exception e) 49 { 50 return "Error loading class: " + e.getMessage(); } 52 } 53 54 56 59 public static String getMethods(Object obj) 60 { 61 return getMethods(obj, false); 62 } 63 64 66 70 public static String getMethods(Object obj, boolean settersOnly) 71 { 72 try 73 { 74 Class clazz = safeGetClass(obj); 75 return getMethods(clazz, settersOnly); 76 } 77 catch(Exception e) 78 { 79 return e.getMessage(); 80 } 81 } 82 83 85 89 public static String getMethods(Class clazz, boolean settersOnly) 90 { 91 String s = new String (); 92 93 Method[] methods = clazz.getMethods(); 94 95 for(int i = 0; i < methods.length; i++) 96 { 97 Method m = methods[i]; 98 boolean isSetter = m.getName().startsWith("set"); 100 if(settersOnly && isSetter == false) 101 continue; 102 103 s = s + m.toString() + '\n'; 104 } 105 return s; 106 } 107 108 110 113 public static String getSetters(Object obj) 114 { 115 return getMethods(obj, true); 116 } 117 118 120 123 public static String getSetters(Class clazz) 124 { 125 return getMethods(clazz, true); 126 } 127 128 130 133 public static String toString(Object obj) 134 { 135 return toString(obj, false); 136 } 137 138 140 143 public static String toStringWithSuper(Object obj) 144 { 145 return toString(obj, true); 146 } 147 148 150 154 public static boolean equals(Object a, Object b) 155 { 156 Class cl = a.getClass(); 157 158 if (!cl.equals(b.getClass())) 159 return false; 160 161 Field[] fields = cl.getDeclaredFields(); 162 setAccessible(fields); 163 164 for (int i = 0; i < fields.length; i++) 165 { 166 Field f = fields[i]; 167 try 168 { 169 if (!f.get(a).equals(f.get(b))) 170 return false; 171 } 172 catch(IllegalAccessException e) 173 { 174 return false; 175 } 176 } 177 178 return true; 179 } 180 181 183 public static void useShortClassNames() 184 { 185 useShortClassNames_ = true; 186 } 187 188 190 public static void useLongClassNames() 191 { 192 useShortClassNames_ = false; 193 } 194 195 197 private static String toString(Object obj, boolean doSuperClasses) 198 { 199 try 200 { 201 return getFieldInfo(obj, doSuperClasses).toString(); 202 } 203 catch(ObjectAnalyzerException e) 204 { 205 return e.getMessage(); 206 } 207 } 208 209 211 private static Class safeGetClass(Object obj) throws ObjectAnalyzerException 212 { 213 if(obj == null) 214 throw new ObjectAnalyzerException(fatal + "null Object parameter"); 216 if(! (obj instanceof Object )) 217 throw new ObjectAnalyzerException(fatal + "Object parameter is not really a java.lang.Object"); 219 Class cl = obj.getClass(); 220 221 if(cl == null) 222 throw new ObjectAnalyzerException(fatal + "getClass() on parameter Object returned null"); 224 return cl; 225 } 226 227 229 private static Class safeGetSuperclass(Class cl) throws ObjectAnalyzerException 230 { 231 Class sc = cl.getSuperclass(); 232 233 if(sc == null) 234 throw new ObjectAnalyzerException("getSuperclass() on parameter Object returned null"); 236 return sc; 237 } 238 239 241 private static FieldInfoVector getFieldInfo(Object obj, boolean doSuperClasses) throws ObjectAnalyzerException 242 { 243 FieldInfoVector fiv = new FieldInfoVector(); 244 Class cl = safeGetClass(obj); 245 246 if(doSuperClasses == false) 247 { 248 getFieldInfo(cl, obj, fiv); 249 return fiv; 250 } 251 252 for(Class theClass = cl; theClass != null && !theClass.equals(Object .class); theClass = safeGetSuperclass(theClass)) 253 getFieldInfo(theClass, obj, fiv); 254 255 return fiv; 256 } 257 258 259 261 private static void getFieldInfo(Class cl, Object obj, FieldInfoVector fiv) throws ObjectAnalyzerException 262 { 263 Field[] fields = null; 264 265 try 266 { 267 fields = cl.getDeclaredFields(); 268 } 269 catch(SecurityException e) 270 { 271 throw new ObjectAnalyzerException("got a SecurityException when calling getDeclaredFields() on " + cl.getName()); } 273 274 if(fields == null) 275 throw new ObjectAnalyzerException("calling getDeclaredFields() on " + cl.getName() + " returned null"); 277 setAccessible(fields); 278 279 for(int i = 0; i < fields.length; i++) 280 { 281 Field f = fields[i]; 282 String sval; 283 String modifiers = Modifier.toString(f.getModifiers()); 284 String className = cl.getName(); 285 286 if(useShortClassNames_) 287 className = StringUtils.toShortClassName(className); 288 289 if(modifiers.length() <= 0) 290 modifiers = "(package)"; 292 try 293 { 294 Object val = f.get(obj); 295 296 if(val == null) 297 sval = "<null>"; else 299 sval = val.toString(); 300 } 301 catch (IllegalAccessException e) 302 { 303 sval = "<IllegalAccessException>"; } 305 306 fiv.addElement(new FieldInfo(className, f, sval, modifiers)); 307 } 308 } 309 310 312 private static void setAccessible(Field[] fields) 313 { 314 if(setAccessibleMethod == null) 315 return; 317 try 318 { 319 Boolean b = new Boolean (true); 320 setAccessibleMethod.invoke(null, new Object [] { fields, b } ); 321 } 322 catch(Exception e) 323 { 324 Reporter.warn("Got an exception invoking setAccessible: " + e); } 326 } 327 328 330 private static void setupSetAccessibleMethod() 331 { 332 338 setAccessibleMethod = null; 339 340 Class AO; 341 342 try 343 { 344 AO = Class.forName("java.lang.reflect.AccessibleObject"); } 346 catch(ClassNotFoundException e) 347 { 348 Reporter.info("Can't find java.lang.reflect.AccessibleObject -- thus I can't show any private or protected variable values. This must be pre JDK 1.2.x"); return; 350 } 351 352 Method[] methods = AO.getDeclaredMethods(); 353 354 for(int i = 0; i < methods.length; i++) 355 { 356 Method m = methods[i]; 357 358 if(m.getName().equals("setAccessible") && m.getParameterTypes().length == 2) { 360 Reporter.verbose("Found setAccessible: " + m); setAccessibleMethod = m; 362 break; 363 } 364 } 365 } 366 367 368 370 private static final String fatal = "Fatal Error in ObjectAnalyzer.toString(): "; private static boolean useShortClassNames_ = true; 372 private static Method setAccessibleMethod; 373 374 static 375 { 376 setupSetAccessibleMethod(); 377 } 378 379 381 383 public static void main(String [] args) 384 { 385 String s = new String ("Hello!"); 387 System.out.println("Regular: \n" + toString(s) + "\n\n"); System.out.println("Super: \n" + toStringWithSuper(s) + "\n\n"); } 390 391 } 392 393 396 class ObjectAnalyzerException extends Exception 397 { 398 ObjectAnalyzerException(String s) 399 { 400 super(s); 401 } 402 403 } 404 405 408 class FieldInfo 409 { 410 Field field; 411 String value; 412 String className; 414 String modifiers; 415 416 FieldInfo(String c, Field f, String v, String m) 417 { 418 className = c; 419 field = f; 420 value = v; 421 modifiers = m; 422 } 423 } 424 425 428 class FieldInfoVector { 430 Vector v = null; 431 432 FieldInfoVector() 433 { 434 v = new Vector (); 435 } 436 437 439 441 public void addElement(Object o) { 442 v.addElement(o); 443 } 444 445 447 449 public String toString() 450 { 451 int veclen = v.size(); 452 StringBuffer s = new StringBuffer (); 453 454 setLongestNames(); 455 456 458 s.append(StringUtils.padRight("Class", classNameLength)); s.append(StringUtils.padRight("Modifiers", modifiersLength)); s.append(StringUtils.padRight("Field", fieldNameLength)); s.append("Value"); s.append("\n\n"); 464 for(int i = 0; i < veclen; i++) 465 { 466 FieldInfo fi = fetch(i); 467 468 s.append(StringUtils.padRight(fi.className, classNameLength)); 469 s.append(StringUtils.padRight(fi.modifiers, modifiersLength)); 470 s.append(StringUtils.padRight(fi.field.getName(), fieldNameLength)); 471 s.append(fi.value); 472 s.append('\n'); 473 } 474 475 return s.toString(); 476 } 477 478 480 private FieldInfo fetch(int i) 481 { 482 return (FieldInfo)v.elementAt(i); 483 } 484 485 487 private void setLongestNames() 488 { 489 int veclen = v.size(); 490 491 classNameLength = 5; 492 fieldNameLength = 5; 493 modifiersLength = 5; 494 495 for(int i = 0; i < veclen; i++) 496 { 497 FieldInfo fi = fetch(i); 498 499 int clen = fi.className.length(); 500 int flen = fi.field.getName().length(); 501 int mlen = fi.modifiers.length(); 502 if(clen > classNameLength) classNameLength = clen; 503 if(flen > fieldNameLength) fieldNameLength = flen; 504 if(mlen > modifiersLength) modifiersLength = mlen; 505 } 506 507 classNameLength += 2; 508 fieldNameLength += 2; 509 modifiersLength += 2; 510 } 511 512 514 private int classNameLength = 0; 515 private int fieldNameLength = 0; 516 private int modifiersLength = 0; 517 } 518 | Popular Tags |