1 23 24 28 29 package com.sun.enterprise.tools.common.util; 30 31 import java.lang.reflect.*; 32 import java.util.Vector ; 33 import com.sun.enterprise.tools.common.util.diagnostics.Reporter; 34 35 public class ObjectAnalyzer 36 { 37 public static String getMethods(String className) 38 { 39 try 40 { 41 Class clazz = Class.forName(className); 42 return getMethods(clazz, false); 43 } 44 catch(Exception e) 45 { 46 return "Error loading class: " + e.getMessage(); } 48 } 49 50 52 public static String getMethods(Object obj) 53 { 54 return getMethods(obj, false); 55 } 56 57 59 public static String getMethods(Object obj, boolean settersOnly) 60 { 61 try 62 { 63 Class clazz = safeGetClass(obj); 64 return getMethods(clazz, settersOnly); 65 } 66 catch(Exception e) 67 { 68 return e.getMessage(); 69 } 70 } 71 72 74 public static String getMethods(Class clazz, boolean settersOnly) 75 { 76 String s = new String (); 77 78 Method[] methods = clazz.getMethods(); 79 80 for(int i = 0; i < methods.length; i++) 81 { 82 Method m = methods[i]; 83 boolean isSetter = m.getName().startsWith("set"); 85 if(settersOnly && isSetter == false) 86 continue; 87 88 s = s + m.toString() + '\n'; 89 } 90 return s; 91 } 92 93 95 public static String getSetters(Object obj) 96 { 97 return getMethods(obj, true); 98 } 99 100 102 public static String getSetters(Class clazz) 103 { 104 return getMethods(clazz, true); 105 } 106 107 109 public static String toString(Object obj) 110 { 111 return toString(obj, false); 112 } 113 114 116 public static String toStringWithSuper(Object obj) 117 { 118 return toString(obj, true); 119 } 120 121 123 public static boolean equals(Object a, Object b) 124 { 125 Class cl = a.getClass(); 126 127 if (!cl.equals(b.getClass())) 128 return false; 129 130 Field[] fields = cl.getDeclaredFields(); 131 setAccessible(fields); 132 133 for (int i = 0; i < fields.length; i++) 134 { 135 Field f = fields[i]; 136 try 137 { 138 if (!f.get(a).equals(f.get(b))) 139 return false; 140 } 141 catch(IllegalAccessException e) 142 { 143 return false; 144 } 145 } 146 147 return true; 148 } 149 150 152 public static void useShortClassNames() 153 { 154 useShortClassNames_ = true; 155 } 156 157 159 public static void useLongClassNames() 160 { 161 useShortClassNames_ = false; 162 } 163 164 166 private static String toString(Object obj, boolean doSuperClasses) 167 { 168 try 169 { 170 return getFieldInfo(obj, doSuperClasses).toString(); 171 } 172 catch(ObjectAnalyzerException e) 173 { 174 return e.getMessage(); 175 } 176 } 177 178 180 private static Class safeGetClass(Object obj) throws ObjectAnalyzerException 181 { 182 if(obj == null) 183 throw new ObjectAnalyzerException(fatal + "null Object parameter"); 185 if(! (obj instanceof Object )) 186 throw new ObjectAnalyzerException(fatal + "Object parameter is not really a java.lang.Object"); 188 Class cl = obj.getClass(); 189 190 if(cl == null) 191 throw new ObjectAnalyzerException(fatal + "getClass() on parameter Object returned null"); 193 return cl; 194 } 195 196 198 private static Class safeGetSuperclass(Class cl) throws ObjectAnalyzerException 199 { 200 Class sc = cl.getSuperclass(); 201 202 if(sc == null) 203 throw new ObjectAnalyzerException("getSuperclass() on parameter Object returned null"); 205 return sc; 206 } 207 208 210 private static FieldInfoVector getFieldInfo(Object obj, boolean doSuperClasses) throws ObjectAnalyzerException 211 { 212 FieldInfoVector fiv = new FieldInfoVector(); 213 Class cl = safeGetClass(obj); 214 215 if(doSuperClasses == false) 216 { 217 getFieldInfo(cl, obj, fiv); 218 return fiv; 219 } 220 221 for(Class theClass = cl; theClass != null && !theClass.equals(Object .class); theClass = safeGetSuperclass(theClass)) 222 getFieldInfo(theClass, obj, fiv); 223 224 return fiv; 225 } 226 227 228 230 private static void getFieldInfo(Class cl, Object obj, FieldInfoVector fiv) throws ObjectAnalyzerException 231 { 232 Field[] fields = null; 233 234 try 235 { 236 fields = cl.getDeclaredFields(); 237 } 238 catch(SecurityException e) 239 { 240 throw new ObjectAnalyzerException("got a SecurityException when calling getDeclaredFields() on " + cl.getName()); } 242 243 if(fields == null) 244 throw new ObjectAnalyzerException("calling getDeclaredFields() on " + cl.getName() + " returned null"); 246 setAccessible(fields); 247 248 for(int i = 0; i < fields.length; i++) 249 { 250 Field f = fields[i]; 251 String sval; 252 String modifiers = Modifier.toString(f.getModifiers()); 253 String className = cl.getName(); 254 255 if(useShortClassNames_) 256 className = StringUtils.toShortClassName(className); 257 258 if(modifiers.length() <= 0) 259 modifiers = "(package)"; 261 try 262 { 263 Object val = f.get(obj); 264 265 if(val == null) 266 sval = "<null>"; else 268 sval = val.toString(); 269 } 270 catch (IllegalAccessException e) 271 { 272 sval = "<IllegalAccessException>"; } 274 275 fiv.addElement(new FieldInfo(className, f, sval, modifiers)); 276 } 277 } 278 279 281 private static void setAccessible(Field[] fields) 282 { 283 if(setAccessibleMethod == null) 284 return; 286 try 287 { 288 Boolean b = new Boolean (true); 289 setAccessibleMethod.invoke(null, new Object [] { fields, b } ); 290 } 291 catch(Exception e) 292 { 293 Reporter.warn("Got an exception invoking setAccessible: " + e); } 295 } 296 297 299 private static void setupSetAccessibleMethod() 300 { 301 307 setAccessibleMethod = null; 308 309 Class AO; 310 311 try 312 { 313 AO = Class.forName("java.lang.reflect.AccessibleObject"); } 315 catch(ClassNotFoundException e) 316 { 317 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; 319 } 320 321 Method[] methods = AO.getDeclaredMethods(); 322 323 for(int i = 0; i < methods.length; i++) 324 { 325 Method m = methods[i]; 326 327 if(m.getName().equals("setAccessible") && m.getParameterTypes().length == 2) { 329 Reporter.verbose("Found setAccessible: " + m); setAccessibleMethod = m; 331 break; 332 } 333 } 334 } 335 336 337 339 private static final String fatal = "Fatal Error in ObjectAnalyzer.toString(): "; private static boolean useShortClassNames_ = true; 341 private static Method setAccessibleMethod; 342 343 static 344 { 345 setupSetAccessibleMethod(); 346 } 347 348 350 public static void main(String [] args) 351 { 352 String s = new String ("Hello!"); 354 System.out.println("Regular: \n" + toString(s) + "\n\n"); System.out.println("Super: \n" + toStringWithSuper(s) + "\n\n"); } 357 358 } 359 360 363 class ObjectAnalyzerException extends Exception 364 { 365 ObjectAnalyzerException(String s) 366 { 367 super(s); 368 } 369 370 } 371 372 375 class FieldInfo 376 { 377 Field field; 378 String value; 379 String className; 381 String modifiers; 382 383 FieldInfo(String c, Field f, String v, String m) 384 { 385 className = c; 386 field = f; 387 value = v; 388 modifiers = m; 389 } 390 } 391 392 395 class FieldInfoVector { 397 Vector v = null; 398 399 FieldInfoVector() 400 { 401 v = new Vector (); 402 } 403 404 406 public void addElement(Object o) { 407 v.addElement(o); 408 } 409 410 412 public String toString() 413 { 414 int veclen = v.size(); 415 StringBuffer s = new StringBuffer (); 416 417 setLongestNames(); 418 419 421 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"); 427 for(int i = 0; i < veclen; i++) 428 { 429 FieldInfo fi = fetch(i); 430 431 s.append(StringUtils.padRight(fi.className, classNameLength)); 432 s.append(StringUtils.padRight(fi.modifiers, modifiersLength)); 433 s.append(StringUtils.padRight(fi.field.getName(), fieldNameLength)); 434 s.append(fi.value); 435 s.append('\n'); 436 } 437 438 return s.toString(); 439 } 440 441 443 private FieldInfo fetch(int i) 444 { 445 return (FieldInfo)v.elementAt(i); 446 } 447 448 450 private void setLongestNames() 451 { 452 int veclen = v.size(); 453 454 classNameLength = 5; 455 fieldNameLength = 5; 456 modifiersLength = 5; 457 458 for(int i = 0; i < veclen; i++) 459 { 460 FieldInfo fi = fetch(i); 461 462 int clen = fi.className.length(); 463 int flen = fi.field.getName().length(); 464 int mlen = fi.modifiers.length(); 465 if(clen > classNameLength) classNameLength = clen; 466 if(flen > fieldNameLength) fieldNameLength = flen; 467 if(mlen > modifiersLength) modifiersLength = mlen; 468 } 469 470 classNameLength += 2; 471 fieldNameLength += 2; 472 modifiersLength += 2; 473 } 474 475 477 private int classNameLength = 0; 478 private int fieldNameLength = 0; 479 private int modifiersLength = 0; 480 481 } 482 483 486 490 533 534 535 | Popular Tags |