1 9 package javolution.lang; 10 11 import javolution.JavolutionError; 12 import javolution.util.FastMap; 13 import j2me.lang.CharSequence; 14 15 50 public final class Reflection { 51 52 55 private Reflection() { 56 } 57 58 68 public static Class getClass(CharSequence name) 69 throws ClassNotFoundException { 70 Class cls = (Class ) _NameToClass.get(name); 71 return (cls != null) ? cls : searchClass(name.toString()); 72 } 73 74 private static Class searchClass(String name) throws ClassNotFoundException { 75 Class cls = null; 76 try { 77 cls = Class.forName(name); } catch (ClassNotFoundException e0) { 88 if (cls == null) 89 throw new ClassNotFoundException ("Cannot found class " + name); 90 } 91 synchronized (_NameToClass) { 92 _NameToClass.put(name, cls); 93 } 94 return cls; 95 } 96 97 private static final FastMap _NameToClass = new FastMap(); 98 99 102 public static Class getClass(String name) throws ClassNotFoundException { 103 Class cls = (Class ) _NameToClass.get(name); 104 return (cls != null) ? cls : searchClass(name); 105 } 106 107 114 public static Constructor getConstructor(String signature) { 115 int argStart = signature.indexOf('(') + 1; 116 if (argStart < 0) { 117 throw new IllegalArgumentException ("Parenthesis '(' not found"); 118 } 119 int argEnd = signature.indexOf(')'); 120 if (argEnd < 0) { 121 throw new IllegalArgumentException ("Parenthesis ')' not found"); 122 } 123 String className = signature.substring(0, argStart - 1); 124 Class theClass; 125 try { 126 theClass = Reflection.getClass(className); 127 } catch (ClassNotFoundException e) { 128 return null; 129 } 130 String args = signature.substring(argStart, argEnd); 131 if (args.length() == 0) 132 return new DefaultConstructor(theClass); 133 146 return null; 147 } 148 149 private static class DefaultConstructor extends Constructor { 150 final Class _class; 151 152 DefaultConstructor(Class cl) { 153 _class = cl; 154 } 155 156 public Object allocate(Object [] args) { 157 try { 158 return _class.newInstance(); 159 } catch (InstantiationException e) { 160 throw new JavolutionError("Instantiation error for " 161 + _class.getName() + " default constructor", e); 162 } catch (IllegalAccessException e) { 163 throw new JavolutionError("Illegal access error for " 164 + _class.getName() + " constructor", e); 165 } 166 } 167 168 public String toString() { 169 return _class + " default constructor"; 170 } 171 } 172 173 209 210 217 public static Method getMethod(String signature) { 218 250 return null; 251 } 252 253 285 286 383 384 399 public static abstract class Constructor { 400 401 404 public Constructor() { 405 } 406 407 414 protected abstract Object allocate(Object [] args); 415 416 421 public final Object newInstance() { 422 return allocate(NO_ARG); 423 } 424 425 private static final Object [] NO_ARG = new Object [0]; 426 427 433 public final Object newInstance(Object arg0) { 434 synchronized (this) { 435 array1[0] = arg0; 436 return allocate(array1); 437 } 438 } 439 440 private final Object [] array1 = new Object [1]; 441 442 449 public final Object newInstance(Object arg0, Object arg1) { 450 synchronized (this) { 451 array2[0] = arg0; 452 array2[1] = arg1; 453 return allocate(array2); 454 } 455 } 456 457 private final Object [] array2 = new Object [2]; 458 459 467 public final Object newInstance(Object arg0, Object arg1, Object arg2) { 468 synchronized (this) { 469 array3[0] = arg0; 470 array3[1] = arg1; 471 array3[2] = arg2; 472 return allocate(array3); 473 } 474 } 475 476 private final Object [] array3 = new Object [3]; 477 478 487 public final Object newInstance(Object arg0, Object arg1, Object arg2, 488 Object arg3) { 489 synchronized (this) { 490 array4[0] = arg0; 491 array4[1] = arg1; 492 array4[2] = arg2; 493 array4[3] = arg3; 494 return allocate(array4); 495 } 496 } 497 498 private final Object [] array4 = new Object [4]; 499 } 500 501 516 public static abstract class Method { 517 518 521 public Method() { 522 } 523 524 532 protected abstract Object execute(Object thisObject, Object [] args); 533 534 542 public final Object invoke(Object thisObject) { 543 return execute(thisObject, NO_ARG); 544 } 545 546 private static final Object [] NO_ARG = new Object [0]; 547 548 558 public final Object invoke(Object thisObject, Object arg0) { 559 synchronized (this) { 560 array1[0] = arg0; 561 return execute(thisObject, array1); 562 } 563 } 564 565 private final Object [] array1 = new Object [1]; 566 567 580 public final Object invoke(Object thisObject, Object arg0, Object arg1) { 581 synchronized (this) { 582 array2[0] = arg0; 583 array2[1] = arg1; 584 return execute(thisObject, array2); 585 } 586 } 587 588 private final Object [] array2 = new Object [2]; 589 590 602 public final Object invoke(Object thisObject, Object arg0, Object arg1, 603 Object arg2) { 604 synchronized (this) { 605 array3[0] = arg0; 606 array3[1] = arg1; 607 array3[2] = arg2; 608 return execute(thisObject, array3); 609 } 610 } 611 612 private final Object [] array3 = new Object [3]; 613 614 627 public final Object invoke(Object thisObject, Object arg0, Object arg1, 628 Object arg2, Object arg3) { 629 synchronized (this) { 630 array4[0] = arg0; 631 array4[1] = arg1; 632 array4[2] = arg2; 633 array4[3] = arg3; 634 return execute(thisObject, array4); 635 } 636 } 637 638 private final Object [] array4 = new Object [4]; 639 } 640 641 } | Popular Tags |