1 3 package jodd.introspector; 4 5 import jodd.util.ReflectUtil; 6 7 import java.lang.reflect.Field ; 8 import java.lang.reflect.Method ; 9 import java.lang.reflect.Modifier ; 10 import java.lang.reflect.Constructor ; 11 import java.util.Map ; 12 import java.util.List ; 13 14 32 public class ClassDescriptor { 33 34 private final Class type; 35 private int usage; 36 37 protected ClassDescriptor(Class type) { 38 this.type = type; 39 usage = 1; 40 isMap = ReflectUtil.isSubclass(type, Map .class); 41 isList = ReflectUtil.isSubclass(type, List .class); 42 } 43 44 47 public Class getType() { 48 return type; 49 } 50 51 54 protected void increaseUsage() { 55 usage++; 56 } 57 58 61 public int getUsage() { 62 return usage; 63 } 64 65 67 private boolean isMap; 68 71 public boolean isMap() { 72 return isMap; 73 } 74 75 private boolean isList; 76 79 public boolean isList() { 80 return isList; 81 } 82 83 85 protected Fields publicFields; 86 protected Fields allFields; 87 88 93 protected void inspectFields() { 94 if (allFields != null) { 95 return; 96 } 97 Fields publicFields = new Fields(); 98 Fields allFields = new Fields(); 99 100 Field [] fields = ReflectUtil.getAccessibleFields(type); 101 for (int i = 0; i < fields.length; i++) { 102 Field field = fields[i]; 103 String fName = field.getName(); 104 if (ReflectUtil.isPublic(field)) { 105 publicFields.addField(fName, field); 106 } 107 ReflectUtil.forceAccess(field); 108 allFields.addField(fName, field); 109 } 110 publicFields.lock(); 111 allFields.lock(); 112 this.publicFields = publicFields; 113 this.allFields = allFields; 114 } 115 116 122 public Field getField(String name, boolean suppressSecurity) { 123 inspectFields(); 124 if (suppressSecurity == true) { 125 return allFields.getField(name); 126 } else { 127 return publicFields.getField(name); 128 } 129 } 130 131 134 public Field getField(String name) { 135 inspectFields(); 136 return publicFields.getField(name); 137 } 138 139 142 public int getFieldCount(boolean suppressSecurity) { 143 inspectFields(); 144 if (suppressSecurity == true) { 145 return allFields.getCount(); 146 } else { 147 return publicFields.getCount(); 148 } 149 } 150 151 154 public int getFieldCount() { 155 inspectFields(); 156 return publicFields.getCount(); 157 } 158 159 162 public Field [] getAllFields(boolean suppressSecurity) { 163 inspectFields(); 164 if (suppressSecurity == true) { 165 return allFields.getAllFields(); 166 } else { 167 return publicFields.getAllFields(); 168 } 169 } 170 173 public Field [] getAllFields() { 174 inspectFields(); 175 return publicFields.getAllFields(); 176 } 177 178 179 181 protected Methods publicMethods; 182 protected Methods allMethods; 183 184 185 190 protected void inspectMethods() { 191 if (allMethods != null) { 192 return; 193 } 194 Methods publicMethods = new Methods(); 195 Methods allMethods = new Methods(); 196 197 Method [] methods = ReflectUtil.getAccessibleMethods(type); 198 for (int i = 0; i < methods.length; i++) { 199 Method method = methods[i]; 200 String methodName = method.getName(); 201 if (ReflectUtil.isPublic(method)) { 202 publicMethods.addMethod(methodName, method); 203 } 204 ReflectUtil.forceAccess(method); 205 allMethods.addMethod(methodName, method); 206 } 207 allMethods.lock(); 208 publicMethods.lock(); 209 this.allMethods = allMethods; 210 this.publicMethods = publicMethods; 211 } 212 213 219 public Method getMethod(String name, boolean suppressSecurity) { 220 inspectMethods(); 221 if (suppressSecurity == true) { 222 return allMethods.getMethod(name); 223 } else { 224 return publicMethods.getMethod(name); 225 } 226 } 227 228 231 public Method getMethod(String name) { 232 inspectMethods(); 233 return publicMethods.getMethod(name); 234 } 235 236 239 public Method getMethod(String name, Class [] params, boolean suppressSecurity) { 240 inspectMethods(); 241 if (suppressSecurity == true) { 242 return allMethods.getMethod(name, params); 243 } else { 244 return publicMethods.getMethod(name, params); 245 } 246 } 247 250 public Method getMethod(String name, Class [] params) { 251 inspectMethods(); 252 return publicMethods.getMethod(name, params); 253 } 254 255 258 public Method [] getAllMethods(String name, boolean supressSecurity) { 259 inspectMethods(); 260 if (supressSecurity == true) { 261 return allMethods.getAllMethods(name); 262 } else { 263 return publicMethods.getAllMethods(name); 264 } 265 } 266 269 public Method [] getAllMethods(String name) { 270 inspectMethods(); 271 return publicMethods.getAllMethods(name); 272 } 273 274 277 public Method [] getAllMethods(boolean supressSecurity) { 278 inspectMethods(); 279 if (supressSecurity == true) { 280 return allMethods.getAllMethods(); 281 } else { 282 return publicMethods.getAllMethods(); 283 } 284 } 285 286 289 public Method [] getAllMethods() { 290 inspectMethods(); 291 return publicMethods.getAllMethods(); 292 } 293 294 295 297 protected Properties publicProperties; 298 protected Properties allProperties; 299 300 303 protected void inspectProperties() { 304 if (publicProperties != null) { 305 return; 306 } 307 Properties publicProperties = new Properties(); 308 Properties allProperties = new Properties(); 309 310 Method [] methods = ReflectUtil.getAccessibleMethods(type); 311 for (int i = 0; i < methods.length; i++) { 312 Method method = methods[i]; 313 if (Modifier.isStatic(method.getModifiers())) { 314 continue; } 316 String methodName = method.getName(); 317 Class [] paramTypes = method.getParameterTypes(); 318 Class returnType = method.getReturnType(); 319 boolean add = false; 320 321 if (methodName.startsWith("get") && methodName.equals("getClass") == false) { if ((returnType != null) && (paramTypes.length == 0)) { methodName = '-' + methodName.substring(3, 4).toLowerCase() + methodName.substring(4); 324 add = true; 325 } 326 } else if (methodName.startsWith("is")) { if ((returnType != null) && (paramTypes.length == 0)) { methodName = '-' + methodName.substring(2, 3).toLowerCase() + methodName.substring(3); 329 add = true; 330 } 331 } else if (methodName.startsWith("set")) { if (paramTypes.length == 1) { methodName = '+' + methodName.substring(3, 4).toLowerCase() + methodName.substring(4); 334 add = true; 335 } 336 } 337 338 if (add == true) { 339 if (ReflectUtil.isPublic(method)) { 340 publicProperties.addMethod(methodName, method); 341 } 342 ReflectUtil.forceAccess(method); 343 allProperties.addMethod(methodName, method); 344 } 345 } 346 allProperties.lock(); 347 publicProperties.lock(); 348 this.allProperties = allProperties; 349 this.publicProperties = publicProperties; 350 } 351 352 355 public Method getBeanSetter(String name, boolean suppressSecurity) { 356 inspectProperties(); 357 if (suppressSecurity == true) { 358 return allProperties.setters.getMethod(name); 359 } else { 360 return publicProperties.setters.getMethod(name); 361 } 362 } 363 366 public Method getBeanSetter(String name) { 367 inspectProperties(); 368 return publicProperties.setters.getMethod(name); 369 } 370 373 public Method [] getAllBeanSetters(boolean suppressSecurity) { 374 inspectProperties(); 375 if (suppressSecurity == true) { 376 return allProperties.setters.getAllMethods(); 377 } else { 378 return publicProperties.setters.getAllMethods(); 379 } 380 } 381 384 public Method [] getAllBeanSetters() { 385 inspectProperties(); 386 return publicProperties.setters.getAllMethods(); 387 } 388 389 392 public String [] getAllBeanSetterNames(boolean suppressSecurity) { 393 inspectProperties(); 394 if (suppressSecurity == true) { 395 return allProperties.setterNames; 396 } else { 397 return publicProperties.setterNames; 398 } 399 } 400 403 public String [] getAllBeanSetterNames() { 404 inspectProperties(); 405 return publicProperties.setterNames; 406 } 407 408 411 public Method getBeanGetter(String name, boolean suppressSecurity) { 412 inspectProperties(); 413 if (suppressSecurity == true) { 414 return allProperties.getters.getMethod(name); 415 } else { 416 return publicProperties.getters.getMethod(name); 417 } 418 } 419 420 423 public Method getBeanGetter(String name) { 424 inspectProperties(); 425 return publicProperties.getters.getMethod(name); 426 } 427 430 public Method [] getAllBeanGetters(boolean suppressSecurity) { 431 inspectProperties(); 432 if (suppressSecurity == true) { 433 return allProperties.getters.getAllMethods(); 434 } else { 435 return publicProperties.getters.getAllMethods(); 436 } 437 } 438 441 public Method [] getAllBeanGetters() { 442 inspectProperties(); 443 return publicProperties.getters.getAllMethods(); 444 } 445 448 public String [] getAllBeanGetterNames(boolean suppressSecurity) { 449 inspectProperties(); 450 if (suppressSecurity == true) { 451 return allProperties.getterNames; 452 } else { 453 return publicProperties.getterNames; 454 } 455 } 456 459 public String [] getAllBeanGetterNames() { 460 inspectProperties(); 461 return publicProperties.getterNames; 462 } 463 464 465 466 468 protected Ctors publicCtors; 469 protected Ctors allCtors; 470 471 476 protected void inspectCtors() { 477 if (allCtors != null) { 478 return; 479 } 480 Ctors publicCtors = new Ctors(); 481 Ctors allCtors = new Ctors(); 482 483 484 publicCtors.addCtors(type.getConstructors()); 485 allCtors.addCtors(type.getDeclaredConstructors()); 486 Constructor [] ctors = allCtors.getAllCtors(); 487 for (int i = 0; i < ctors.length; i++) { 488 Constructor ctor = ctors[i]; 489 if (ReflectUtil.isPublic(ctor) == false) { 490 ReflectUtil.forceAccess(ctor); 491 } 492 } 493 publicCtors.lock(); 494 allCtors.lock(); 495 this.publicCtors = publicCtors; 496 this.allCtors = allCtors; 497 } 498 499 502 public Constructor getDefaultCtor(boolean suppressSecurity) { 503 inspectCtors(); 504 if (suppressSecurity == true) { 505 return allCtors.getDefaultCtor(); 506 } else { 507 return publicCtors.getDefaultCtor(); 508 } 509 } 510 511 517 public Constructor getCtor(Class [] args, boolean suppressSecurity) { 518 inspectCtors(); 519 if (suppressSecurity == true) { 520 return allCtors.getCtor(args); 521 } else { 522 return publicCtors.getCtor(args); 523 } 524 } 525 526 529 public Constructor getCtor(Class [] args) { 530 inspectCtors(); 531 return publicCtors.getCtor(args); 532 } 533 534 537 public Constructor getDefaultCtor() { 538 inspectCtors(); 539 return publicCtors.getDefaultCtor(); 540 } 541 542 543 544 547 public int getCtorCount(boolean suppressSecurity) { 548 inspectCtors(); 549 if (suppressSecurity == true) { 550 return allCtors.getCount(); 551 } else { 552 return publicCtors.getCount(); 553 } 554 } 555 556 557 560 public int getCtorCount() { 561 inspectCtors(); 562 return publicCtors.getCount(); 563 } 564 565 568 569 public Constructor [] getAllCtors(boolean suppressSecurity) { 570 inspectCtors(); 571 if (suppressSecurity == true) { 572 return allCtors.getAllCtors(); 573 } else { 574 return publicCtors.getAllCtors(); 575 } 576 } 577 578 581 public Constructor [] getAllCtors() { 582 inspectCtors(); 583 return publicCtors.getAllCtors(); 584 } 585 586 } | Popular Tags |