1 3 package jodd.util; 4 5 import java.lang.reflect.InvocationTargetException ; 6 import java.lang.reflect.Method ; 7 import java.lang.reflect.Modifier ; 8 import java.lang.reflect.AccessibleObject ; 9 import java.lang.reflect.Member ; 10 import java.lang.reflect.Constructor ; 11 import java.lang.reflect.Field ; 12 import java.lang.reflect.Array ; 13 import java.util.*; 14 15 18 public class ReflectUtil { 19 20 21 23 private static Method _getMethod0 = null; 24 static { 25 try { 26 _getMethod0 = Class .class.getDeclaredMethod("getMethod0", new Class [] {String .class, Class [].class}); 27 _getMethod0.setAccessible(true); 28 } catch (Exception e) { 29 try { 30 _getMethod0 = Class .class.getMethod("getMethod", new Class [] {String .class, Class [].class}); 31 } catch (Exception ex) { 32 _getMethod0 = null; 33 } 34 } 35 } 36 37 46 public static Method getMethod0(Class c, String name, Class [] parameterTypes) { 47 try { 48 return (Method ) _getMethod0.invoke(c, new Object []{name, parameterTypes}); 49 } catch (Exception ex) { 50 return null; 51 } 52 } 53 54 63 public static Method getMethod0(Object o, String name, Class [] parameterTypes) { 64 try { 65 return (Method ) _getMethod0.invoke(o.getClass(), new Object []{name, parameterTypes}); 66 } catch (Exception ex) { 67 return null; 68 } 69 } 70 71 73 83 public static Method findMethod(Class c, String methodName) { 84 return findDeclaredMethod(c, methodName, true); 85 } 86 87 public static Method findDeclaredMethod(Class c, String methodName) { 88 return findDeclaredMethod(c, methodName, false); 89 } 90 91 92 private static Method findDeclaredMethod(Class c, String methodName, boolean publicOnly) { 93 if ((methodName == null) || (c == null)) { 94 return null; 95 } 96 Method [] ms = publicOnly ? c.getMethods() : c.getDeclaredMethods(); 97 for (int i = 0; i < ms.length; i++) { 98 Method m = ms[i]; 99 if (m.getName().equals(methodName)) { 100 return m; 101 } 102 } 103 return null; 104 105 } 106 107 108 110 113 public static Class [] getClasses(Object objects[]) { 114 if (objects == null) { 115 return null; 116 } 117 Class [] classes = new Class [objects.length]; 118 for (int i = 0; i < objects.length; i++) { 119 if (objects[i] != null) { 120 classes[i] = objects[i].getClass(); 121 } 122 } 123 return classes; 124 } 125 126 127 129 130 139 public static Object invoke(Class c, Object obj, String method, Class [] paramClasses, Object [] params) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 140 Method m = c.getMethod(method, paramClasses); 141 return m.invoke(obj, params); 142 } 143 144 145 153 public static Object invoke(Object obj, String method, Class [] paramClasses, Object [] params) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 154 Method m = obj.getClass().getMethod(method, paramClasses); 155 return m.invoke(obj, params); 156 } 157 158 164 public static Object invoke(Object obj, String method, Object [] params) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 165 Class [] paramClass = getClasses(params); 166 return invoke(obj, method, paramClass, params); 167 } 168 169 public static Object invoke(Class c, Object obj, String method, Object [] params) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 170 Class [] paramClass = getClasses(params); 171 return invoke(c, obj, method, paramClass, params); 172 } 173 174 175 177 178 187 public static Object invokeDeclared(Class c, Object obj, String method, Class [] paramClasses, Object [] params) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 188 Method m = c.getDeclaredMethod(method, paramClasses); 189 m.setAccessible(true); 190 return m.invoke(obj, params); 191 } 192 193 201 public static Object invokeDeclared(Object obj, String method, Class [] paramClasses, Object [] params) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 202 Method m = obj.getClass().getDeclaredMethod(method, paramClasses); 203 m.setAccessible(true); 204 return m.invoke(obj, params); 205 } 206 207 public static Object invokeDeclared(Object obj, String method, Object [] params) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 208 Class [] paramClass = getClasses(params); 209 return invokeDeclared(obj, method, paramClass, params); 210 } 211 212 public static Object invokeDeclared(Class c, Object obj, String method, Object [] params) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException { 213 Class [] paramClass = getClasses(params); 214 return invokeDeclared(c, obj, method, paramClass, params); 215 } 216 217 218 220 225 public static boolean isSubclass(Class thisClass, Class target) { 226 if (thisClass == target) { 227 return true; 228 } 229 if ((thisClass == null) || (target == null)){ 230 return false; 231 } 232 for (Class x = thisClass; x != null; x = x.getSuperclass()) { 233 if (x == target) { 234 return true; 235 } 236 if (target.isInterface() == true) { 237 Class [] interfaces = x.getInterfaces(); 238 for (int i = 0; i < interfaces.length; i++) { 239 if (isSubclass(interfaces[i], target)) { 240 return true; 241 } 242 } 243 } 244 } 245 return false; 246 } 247 248 256 public static boolean isInstanceOf(Object o, Class target) { 257 return isSubclass(o.getClass(), target); 258 } 259 260 261 263 264 268 public static Method [] getAccessibleMethods(Class clazz) { 269 return getAccessibleMethods(clazz, Object .class); 270 } 271 272 276 public static Method [] getAccessibleMethods(Class clazz, Class limit) { 277 Package topPackage = clazz.getPackage(); 278 List methodList = new ArrayList(); 279 int topPackageHash = topPackage == null ? 0 : topPackage.hashCode(); 280 boolean top = true; 281 do { 282 if (clazz == null) { 283 break; 284 } 285 Method [] declaredMethods = clazz.getDeclaredMethods(); 286 for (int i = 0; i < declaredMethods.length; i++) { 287 Method method = declaredMethods[i]; 288 if (top == true) { methodList.add(method); 290 continue; 291 } 292 int modifier = method.getModifiers(); 293 if (Modifier.isPrivate(modifier) == true) { 294 continue; } 296 if (Modifier.isAbstract(modifier) == true) { continue; 298 } 299 if (Modifier.isPublic(modifier) == true) { 300 addMethodIfNotExist(methodList, method); continue; 302 } 303 if (Modifier.isProtected(modifier) == true) { 304 addMethodIfNotExist(methodList, method); continue; 306 } 307 Package pckg = method.getDeclaringClass().getPackage(); 309 int pckgHash = pckg == null ? 0 : pckg.hashCode(); 310 if (pckgHash == topPackageHash) { 311 addMethodIfNotExist(methodList, method); 312 } 313 } 314 top = false; 315 } while ((clazz = clazz.getSuperclass()) != limit); 316 317 Method [] methods = new Method [methodList.size()]; 318 for (int i = 0; i < methods.length; i++) { 319 methods[i] = (Method ) methodList.get(i); 320 } 321 return methods; 322 } 323 324 private static void addMethodIfNotExist(List allMethods, Method newMethod) { 325 for (int i = 0; i < allMethods.size(); i++) { 326 Method m = (Method ) allMethods.get(i); 327 if (compareSignatures(m, newMethod) == true) { 328 return; 329 } 330 } 331 allMethods.add(newMethod); 332 } 333 334 336 337 public static Field [] getAccessibleFields(Class clazz) { 338 return getAccessibleFields(clazz, Object .class); 339 } 340 341 public static Field [] getAccessibleFields(Class clazz, Class limit) { 342 Package topPackage = clazz.getPackage(); 343 List fieldList = new ArrayList(); 344 int topPackageHash = topPackage == null ? 0 : topPackage.hashCode(); 345 boolean top = true; 346 do { 347 if (clazz == null) { 348 break; 349 } 350 Field [] declaredFields = clazz.getDeclaredFields(); 351 for (int i = 0; i < declaredFields.length; i++) { 352 Field field = declaredFields[i]; 353 if (top == true) { fieldList.add(field); 355 continue; 356 } 357 int modifier = field.getModifiers(); 358 if (Modifier.isPrivate(modifier) == true) { 359 continue; } 361 if (Modifier.isPublic(modifier) == true) { 362 addFieldIfNotExist(fieldList, field); continue; 364 } 365 if (Modifier.isProtected(modifier) == true) { 366 addFieldIfNotExist(fieldList, field); continue; 368 } 369 Package pckg = field.getDeclaringClass().getPackage(); 371 int pckgHash = pckg == null ? 0 : pckg.hashCode(); 372 if (pckgHash == topPackageHash) { 373 addFieldIfNotExist(fieldList, field); 374 } 375 } 376 top = false; 377 } while ((clazz = clazz.getSuperclass()) != limit); 378 379 Field [] fields = new Field [fieldList.size()]; 380 for (int i = 0; i < fields.length; i++) { 381 fields[i] = (Field ) fieldList.get(i); 382 } 383 return fields; 384 } 385 386 private static void addFieldIfNotExist(List allFields, Field newField) { 387 for (int i = 0; i < allFields.size(); i++) { 388 Field f = (Field ) allFields.get(i); 389 if (compareSignatures(f, newField) == true) { 390 return; 391 } 392 } 393 allFields.add(newField); 394 } 395 396 397 399 400 public static Method [] getSupportedMethods(Class clazz) { 401 return getSupportedMethods(clazz, Object .class); 402 } 403 404 411 public static Method [] getSupportedMethods(Class clazz, Class limit) { 412 ArrayList supportedMethods = new ArrayList(); 413 for (Class c = clazz; c != limit; c = c.getSuperclass()) { 414 Method [] methods = c.getDeclaredMethods(); 415 for (int i = 0; i < methods.length; i++) { 416 boolean found = false; 417 for (int j = 0; j < supportedMethods.size(); j++) { 418 if (compareSignatures(methods[i], (Method ) supportedMethods.get(j))) { 419 found = true; 420 break; 421 } 422 } 423 if (found == false) { 424 supportedMethods.add(methods[i]); 425 } 426 } 427 } 428 Method [] mArray = new Method [supportedMethods.size()]; 429 for (int k = 0; k < mArray.length; k++) { 430 mArray[k] = (Method ) supportedMethods.get(k); 431 } 432 return mArray; 433 } 434 435 436 public static Field [] getSupportedFields(Class clazz) { 437 return getSupportedFields(clazz, Object .class); 438 } 439 440 public static Field [] getSupportedFields(Class clazz, Class limit) { 441 ArrayList supportedFields = new ArrayList(); 442 for (Class c = clazz; c != limit; c = c.getSuperclass()) { 443 Field [] fields = c.getDeclaredFields(); 444 for (int i = 0; i < fields.length; i++) { 445 boolean found = false; 446 for (int j = 0; j < supportedFields.size(); j++) { 447 if (compareSignatures(fields[i], (Field ) supportedFields.get(j))) { 448 found = true; 449 break; 450 } 451 } 452 if (found == false) { 453 supportedFields.add(fields[i]); 454 } 455 } 456 } 457 Field [] mArray = new Field [supportedFields.size()]; 458 for (int k = 0; k < mArray.length; k++) { 459 mArray[k] = (Field ) supportedFields.get(k); 460 } 461 return mArray; 462 } 463 464 465 467 470 public static boolean compareDeclarations(Method first, Method second) { 471 if (first.getReturnType() != second.getReturnType()) { 472 return false; 473 } 474 return compareSignatures(first, second); 475 } 476 477 480 public static boolean compareSignatures(Method first, Method second) { 481 if (first.getName().equals(second.getName()) == false) { 482 return false; 483 } 484 return compareParameteres(first.getParameterTypes(), second.getParameterTypes()); 485 } 486 487 490 public static boolean compareSignatures(Constructor first, Constructor second) { 491 if (first.getName().equals(second.getName()) == false) { 492 return false; 493 } 494 return compareParameteres(first.getParameterTypes(), second.getParameterTypes()); 495 } 496 497 public static boolean compareSignatures(Field first, Field second) { 498 return first.getName().equals(second.getName()); 499 } 500 501 502 503 506 public static boolean compareParameteres(Class [] first, Class [] second) { 507 if (first.length != second.length) { 508 return false; 509 } 510 for (int i = 0; i < first.length; i++) { 511 if (first[i] != second[i]) { 512 return false; 513 } 514 } 515 return true; 516 } 517 518 519 520 522 525 public static void forceAccess(AccessibleObject accObject){ 526 try { 527 accObject.setAccessible(true); 528 } catch (SecurityException sex) { 529 } 531 } 532 533 535 538 public static boolean isPublic(Member member) { 539 return Modifier.isPublic(member.getModifiers()); 540 } 541 542 545 public static boolean isPublicPublic(Member member) { 546 if (Modifier.isPublic(member.getModifiers()) == true) { 547 if (Modifier.isPublic(member.getDeclaringClass().getModifiers())) { 548 return true; 549 } 550 } 551 return false; 552 } 553 554 557 public static boolean isPublic(Class c) { 558 return Modifier.isPublic(c.getModifiers()); 559 } 560 561 562 564 private static final Object [] cachedInstances = { 565 new Integer (0), 566 "", 567 new Long (0), 568 new Float (0), 569 new Double (0), 570 new Byte ((byte) 0), 571 new Short ((short) 0), 572 new Character ((char) 0), 573 }; 574 575 581 public static Object newInstance(Class type) throws IllegalAccessException , InstantiationException { 582 if (type == Integer .class) { 583 return cachedInstances[0]; 584 } 585 if (type == String .class) { 586 return cachedInstances[1]; 587 } 588 if (type == Long .class) { 589 return cachedInstances[2]; 590 } 591 if (type == Float .class) { 592 return cachedInstances[3]; 593 } 594 if (type == Double .class) { 595 return cachedInstances[4]; 596 } 597 598 if (type == Map.class) { 599 return new HashMap(); 600 } 601 if (type == List.class) { 602 return new ArrayList(); 603 } 604 if (type == Set.class) { 605 return new HashSet(); 606 } 607 608 if (type == Byte .class) { 609 return cachedInstances[5]; 610 } 611 if (type == Short .class) { 612 return cachedInstances[6]; 613 } 614 if (type == Character .class) { 615 return cachedInstances[7]; 616 } 617 618 if (type.isArray() == true) { 619 return Array.newInstance(type.getComponentType(), 0); 620 } 621 622 return type.newInstance(); 623 } 624 625 626 628 public static boolean isAssignableFrom(Member member1, Member member2) { 629 return member1.getDeclaringClass().isAssignableFrom(member2.getDeclaringClass()); 630 } 631 632 635 public static Class [] getSuperclasses(Class type) { 636 int i = 0; 637 for (Class x = type.getSuperclass(); x != null; x = x.getSuperclass()) { 638 i++; 639 } 640 Class [] result = new Class [i]; 641 i = 0; 642 for (Class x = type.getSuperclass(); x != null; x = x.getSuperclass()) { 643 result[i] = x; 644 i++; 645 } 646 return result; 647 } 648 649 652 public static boolean isUserDefinedMethod(final Method method) { 653 return method.getDeclaringClass() != Object .class; 654 } 655 656 657 660 public static boolean isBeanProperty(Method method) { 661 String methodName = method.getName(); 662 Class returnType = method.getReturnType(); 663 Class [] paramTypes = method.getParameterTypes(); 664 if (methodName.startsWith("get") && methodName.equals("getClass") == false) { if ((returnType != null) && (paramTypes.length == 0)) { return true; 667 } 668 } else if (methodName.startsWith("is")) { if ((returnType != null) && (paramTypes.length == 0)) { return true; 671 } 672 } else if (methodName.startsWith("set")) { if (paramTypes.length == 1) { return true; 675 } 676 } 677 return false; 678 } 679 680 } | Popular Tags |