1 23 24 28 29 package com.sun.enterprise.config.backup.util; 30 31 import java.lang.reflect.*; 32 import java.util.Vector ; 33 34 public class ObjectAnalyzer 35 { 36 39 public static String getMethods(String className) 40 { 41 try 42 { 43 Class clazz = Class.forName(className); 44 return getMethods(clazz, false); 45 } 46 catch(Exception e) 47 { 48 return "Error loading class: " + e.getMessage(); } 50 } 51 52 54 57 public static String getMethods(Object obj) 58 { 59 return getMethods(obj, false); 60 } 61 62 64 68 public static String getMethods(Object obj, boolean settersOnly) 69 { 70 try 71 { 72 Class clazz = safeGetClass(obj); 73 return getMethods(clazz, settersOnly); 74 } 75 catch(Exception e) 76 { 77 return e.getMessage(); 78 } 79 } 80 81 83 87 public static String getMethods(Class clazz, boolean settersOnly) 88 { 89 String s = new String (); 90 91 Method[] methods = clazz.getMethods(); 92 93 for(int i = 0; i < methods.length; i++) 94 { 95 Method m = methods[i]; 96 boolean isSetter = m.getName().startsWith("set"); 98 if(settersOnly && isSetter == false) 99 continue; 100 101 s = s + m.toString() + '\n'; 102 } 103 return s; 104 } 105 106 108 111 public static String getSetters(Object obj) 112 { 113 return getMethods(obj, true); 114 } 115 116 118 121 public static String getSetters(Class clazz) 122 { 123 return getMethods(clazz, true); 124 } 125 126 128 131 public static String toString(Object obj) 132 { 133 return toString(obj, false); 134 } 135 136 138 141 public static String toStringWithSuper(Object obj) 142 { 143 return toString(obj, true); 144 } 145 146 148 152 public static boolean equals(Object a, Object b) 153 { 154 Class cl = a.getClass(); 155 156 if (!cl.equals(b.getClass())) 157 return false; 158 159 Field[] fields = cl.getDeclaredFields(); 160 setAccessible(fields); 161 162 for (int i = 0; i < fields.length; i++) 163 { 164 Field f = fields[i]; 165 try 166 { 167 if (!f.get(a).equals(f.get(b))) 168 return false; 169 } 170 catch(IllegalAccessException e) 171 { 172 return false; 173 } 174 } 175 176 return true; 177 } 178 179 181 public static void useShortClassNames() 182 { 183 useShortClassNames_ = true; 184 } 185 186 188 public static void useLongClassNames() 189 { 190 useShortClassNames_ = false; 191 } 192 193 195 private static String toString(Object obj, boolean doSuperClasses) 196 { 197 try 198 { 199 return getFieldInfo(obj, doSuperClasses).toString(); 200 } 201 catch(ObjectAnalyzerException e) 202 { 203 return e.getMessage(); 204 } 205 } 206 207 209 private static Class safeGetClass(Object obj) throws ObjectAnalyzerException 210 { 211 if(obj == null) 212 throw new ObjectAnalyzerException(fatal + "null Object parameter"); 214 if(! (obj instanceof Object )) 215 throw new ObjectAnalyzerException(fatal + "Object parameter is not really a java.lang.Object"); 217 Class cl = obj.getClass(); 218 219 if(cl == null) 220 throw new ObjectAnalyzerException(fatal + "getClass() on parameter Object returned null"); 222 return cl; 223 } 224 225 227 private static Class safeGetSuperclass(Class cl) throws ObjectAnalyzerException 228 { 229 Class sc = cl.getSuperclass(); 230 231 if(sc == null) 232 throw new ObjectAnalyzerException("getSuperclass() on parameter Object returned null"); 234 return sc; 235 } 236 237 239 private static FieldInfoVector getFieldInfo(Object obj, boolean doSuperClasses) throws ObjectAnalyzerException 240 { 241 FieldInfoVector fiv = new FieldInfoVector(); 242 Class cl = safeGetClass(obj); 243 244 if(doSuperClasses == false) 245 { 246 getFieldInfo(cl, obj, fiv); 247 return fiv; 248 } 249 250 for(Class theClass = cl; theClass != null && !theClass.equals(Object .class); theClass = safeGetSuperclass(theClass)) 251 getFieldInfo(theClass, obj, fiv); 252 253 return fiv; 254 } 255 256 257 259 private static void getFieldInfo(Class cl, Object obj, FieldInfoVector fiv) throws ObjectAnalyzerException 260 { 261 Field[] fields = null; 262 263 try 264 { 265 fields = cl.getDeclaredFields(); 266 } 267 catch(SecurityException e) 268 { 269 throw new ObjectAnalyzerException("got a SecurityException when calling getDeclaredFields() on " + cl.getName()); } 271 272 if(fields == null) 273 throw new ObjectAnalyzerException("calling getDeclaredFields() on " + cl.getName() + " returned null"); 275 setAccessible(fields); 276 277 for(int i = 0; i < fields.length; i++) 278 { 279 Field f = fields[i]; 280 String sval; 281 String modifiers = Modifier.toString(f.getModifiers()); 282 String className = cl.getName(); 283 284 if(useShortClassNames_) 285 className = StringUtils.toShortClassName(className); 286 287 if(modifiers.length() <= 0) 288 modifiers = "(package)"; 290 try 291 { 292 Object val = f.get(obj); 293 294 if(val == null) 295 sval = "<null>"; else 297 sval = val.toString(); 298 } 299 catch (IllegalAccessException e) 300 { 301 sval = "<IllegalAccessException>"; } 303 304 fiv.addElement(new FieldInfo(className, f, sval, modifiers)); 305 } 306 } 307 308 310 private static void setAccessible(Field[] fields) 311 { 312 if(setAccessibleMethod == null) 313 return; 315 try 316 { 317 Boolean b = new Boolean (true); 318 setAccessibleMethod.invoke(null, new Object [] { fields, b } ); 319 } 320 catch(Exception e) 321 { 322 } 323 } 324 325 327 private static void setupSetAccessibleMethod() 328 { 329 335 setAccessibleMethod = null; 336 337 Class AO; 338 339 try 340 { 341 AO = Class.forName("java.lang.reflect.AccessibleObject"); } 343 catch(ClassNotFoundException e) 344 { 345 return; 346 } 347 348 Method[] methods = AO.getDeclaredMethods(); 349 350 for(int i = 0; i < methods.length; i++) 351 { 352 Method m = methods[i]; 353 354 if(m.getName().equals("setAccessible") && m.getParameterTypes().length == 2) { 356 setAccessibleMethod = m; 357 break; 358 } 359 } 360 } 361 362 363 365 private static final String fatal = "Fatal Error in ObjectAnalyzer.toString(): "; private static boolean useShortClassNames_ = true; 367 private static Method setAccessibleMethod; 368 369 static 370 { 371 setupSetAccessibleMethod(); 372 } 373 374 376 378 public static void main(String [] args) 379 { 380 String s = new String ("Hello!"); 382 System.out.println("Regular: \n" + toString(s) + "\n\n"); System.out.println("Super: \n" + toStringWithSuper(s) + "\n\n"); } 385 386 } 387 388 391 class ObjectAnalyzerException extends Exception 392 { 393 ObjectAnalyzerException(String s) 394 { 395 super(s); 396 } 397 398 } 399 400 403 class FieldInfo 404 { 405 Field field; 406 String value; 407 String className; 409 String modifiers; 410 411 FieldInfo(String c, Field f, String v, String m) 412 { 413 className = c; 414 field = f; 415 value = v; 416 modifiers = m; 417 } 418 } 419 420 423 class FieldInfoVector { 425 Vector v = null; 426 427 FieldInfoVector() 428 { 429 v = new Vector (); 430 } 431 432 434 436 public void addElement(Object o) { 437 v.addElement(o); 438 } 439 440 442 444 public String toString() 445 { 446 int veclen = v.size(); 447 StringBuffer s = new StringBuffer (); 448 449 setLongestNames(); 450 451 453 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"); 459 for(int i = 0; i < veclen; i++) 460 { 461 FieldInfo fi = fetch(i); 462 463 s.append(StringUtils.padRight(fi.className, classNameLength)); 464 s.append(StringUtils.padRight(fi.modifiers, modifiersLength)); 465 s.append(StringUtils.padRight(fi.field.getName(), fieldNameLength)); 466 s.append(fi.value); 467 s.append('\n'); 468 } 469 470 return s.toString(); 471 } 472 473 475 private FieldInfo fetch(int i) 476 { 477 return (FieldInfo)v.elementAt(i); 478 } 479 480 482 private void setLongestNames() 483 { 484 int veclen = v.size(); 485 486 classNameLength = 5; 487 fieldNameLength = 5; 488 modifiersLength = 5; 489 490 for(int i = 0; i < veclen; i++) 491 { 492 FieldInfo fi = fetch(i); 493 494 int clen = fi.className.length(); 495 int flen = fi.field.getName().length(); 496 int mlen = fi.modifiers.length(); 497 if(clen > classNameLength) classNameLength = clen; 498 if(flen > fieldNameLength) fieldNameLength = flen; 499 if(mlen > modifiersLength) modifiersLength = mlen; 500 } 501 502 classNameLength += 2; 503 fieldNameLength += 2; 504 modifiersLength += 2; 505 } 506 507 509 private int classNameLength = 0; 510 private int fieldNameLength = 0; 511 private int modifiersLength = 0; 512 } 513 | Popular Tags |