1 8 14 package jfun.yan; 15 16 import java.lang.reflect.Constructor ; 17 import java.lang.reflect.Field ; 18 import java.lang.reflect.Method ; 19 import java.lang.reflect.Modifier ; 20 21 import jfun.util.beans.Bean; 22 import jfun.util.beans.BeanType; 23 import jfun.yan.function.Function; 24 import jfun.yan.util.ReflectionUtil; 25 26 27 49 public final class Functions { 50 56 public static <T> Function<T> ctor(Constructor <T> ctor){ 57 return new ConstructorFunction<T>(ctor); 58 } 59 67 public static Function method(Object obj, Method mtd){ 68 return new MethodFunction(canonicalize(obj, mtd), mtd); 69 } 70 private static final Object canonicalize(Object obj, Method mtd){ 71 if(java.lang.reflect.Modifier.isStatic(mtd.getModifiers())){ 75 return null; 76 } 77 else return obj; 78 } 79 private static final Object canonicalize(Object obj, Field fld){ 80 if(java.lang.reflect.Modifier.isStatic(fld.getModifiers())){ 84 return null; 85 } 86 else return obj; 87 } 88 101 public static <T> Function<T> ctor(Class <T> c) 102 throws IllegalArgumentException { 103 return ctor(c, false); 104 } 105 119 public static <T> Function<T> ctor(Class <T> c, boolean suppress_security) 120 throws IllegalArgumentException { 121 if(c.isArray()){ 122 return new ArrayConstructorFunction<T>(c); 123 } 124 else{ 125 final Constructor <T> ctor = ReflectionUtil.getConstructor(c, suppress_security); 126 return ctor(ctor); 127 } 128 } 129 143 public static <T> Function<T> ctor(Class <T> c, Class [] param_types){ 144 return ctor(c, param_types, false); 145 } 146 161 public static <T> Function<T> ctor(Class <T> c, Class [] param_types, boolean suppress_security) 162 throws IllegalArgumentException { 163 if(c.isArray() && param_types!=null && 164 param_types.length==1 && int.class.equals(param_types[0])){ 165 return new ArrayConstructorFunction<T>(c); 166 } 167 else{ 168 final Constructor <T> ctor = ReflectionUtil.getConstructor(c, param_types, 169 suppress_security); 170 return ctor(ctor); 171 } 172 } 173 187 public static Function static_method(Class c, String name){ 188 return static_method(c, name, false); 189 } 190 205 public static Function static_method(Class c, final String name, boolean suppress_security){ 206 final Method mtd = ReflectionUtil.getStaticMethod(c, name, suppress_security); 207 return method(null, mtd); 208 } 209 225 public static Function static_method(Class c, String name, 226 Class [] param_types){ 227 return static_method(c, name, param_types, false); 228 } 229 246 public static Function static_method(Class c, String name, 247 Class [] param_types, boolean suppress_security) 248 throws IllegalArgumentException { 249 final Method mtd = ReflectionUtil.getMethod(c, name, param_types, suppress_security); 250 return static_method(mtd); 251 } 252 261 public static Function static_method(Method mtd){ 262 if(!Modifier.isStatic(mtd.getModifiers())){ 263 throw new IllegalArgumentException (""+mtd 264 + " is not static"); 265 } 266 return method(null, mtd); 267 } 268 285 public static Function instance_method(Class c, String name){ 286 return instance_method(c, name, false); 287 } 288 306 public static Function instance_method(Class c, String name, 307 boolean suppress_security) 308 throws IllegalArgumentException { 309 final Method mtd = ReflectionUtil.getInstanceMethod(c, name, 310 suppress_security); 311 return instance_method(c, mtd); 312 } 313 331 public static Function instance_method(Class c, String name, 332 Class [] param_types){ 333 return instance_method(c, name, param_types, false); 334 } 335 355 public static Function instance_method(Class c, String name, 356 Class [] param_types, boolean suppress_security) 357 throws IllegalArgumentException { 358 final Method mtd = ReflectionUtil.getMethod(c, name, param_types, 359 suppress_security); 360 return instance_method(c, mtd); 361 } 362 373 public static Function instance_method(Class c, Method mtd){ 374 if(Modifier.isStatic(mtd.getModifiers())){ 375 throw new IllegalArgumentException (""+mtd 376 +" is static"); 377 } 378 return new FloatingMethodFunction(c, mtd); 379 } 380 381 382 395 public static Function method(Object obj, String name){ 396 return method(obj, name, false); 397 } 398 412 public static Function method(Object obj, final String name, 413 boolean suppress_security){ 414 return method(obj.getClass(), obj, name, suppress_security); 415 } 416 432 public static Function method(Class type, Object obj, String name){ 433 return method(type, obj, name, false); 434 } 435 452 public static Function method(Class type, Object obj, final String name, 453 boolean suppress_security){ 454 final Method mtd = ReflectionUtil.getMethod(type, name, 455 suppress_security); 456 return method(obj, mtd); 457 } 458 472 public static Function method(Object obj, String name, Class [] param_types){ 473 return method(obj, name, param_types, false); 474 } 475 490 public static Function method(Object obj, 491 String name, Class [] param_types, boolean suppress_security){ 492 return method(obj.getClass(), obj, name, param_types, 493 suppress_security); 494 } 495 512 public static Function method(Class type, Object obj, 513 String name, Class [] param_types){ 514 return method(type, obj, name, param_types, false); 515 } 516 534 public static Function method(Class type, Object obj, 535 String name, Class [] param_types, boolean suppress_security){ 536 final Method mtd = ReflectionUtil.getMethod( 537 type, name, param_types, suppress_security); 538 return method(obj, mtd); 539 } 540 541 547 public static Function getter(Bean bean, String name){ 548 if(bean.getBeanType().getReader(name) == null) 549 throw new IllegalArgumentException ("property getter for "+ 550 bean.getBeanType().getType().getName()+"."+name+" not found."); 551 return new PropertyReader(bean, name); 552 } 553 560 public static Function indexed_getter(Bean bean, String name, int ind){ 561 if(bean.getBeanType().getIndexedReader(name) == null) 562 throw new IllegalArgumentException ("indexed property getter for " 563 +bean.getBeanType().getType().getName()+"."+name+" not found."); 564 return new IndexedPropertyReader(bean, name, ind); 565 } 566 572 public static Function setter(Bean bean, String name){ 573 final BeanType btype = bean.getBeanType(); 574 if(btype.getWriter(name) == null 575 && btype.getIndexedWriter(name)==null){ 576 throw new IllegalArgumentException ("property setter for "+ 577 bean.getBeanType().getType().getName()+"."+name+" not found."); 578 } 579 else 580 return new PropertyWriter(bean, name); 581 } 582 589 public static Function indexed_setter(Bean bean, String name, int ind){ 590 if(bean.getBeanType().getIndexedWriter(name) == null) 591 throw new IllegalArgumentException ("indexed property setter for "+ 592 bean.getBeanType().getType().getName()+"."+name+" not found."); 593 return new IndexedPropertyWriter(bean, name, ind); 594 } 595 596 597 608 public static Function static_field(Class c, String name){ 609 return static_field(c, name, false); 610 } 611 621 public static Function static_field(Class c, final String name, 622 boolean suppress_security){ 623 final Field fld = ReflectionUtil.getStaticField(c, name, 624 suppress_security); 625 return field(null, fld); 626 } 627 628 637 public static Function static_field(Field fld){ 638 if(!Modifier.isStatic(fld.getModifiers())){ 639 throw new IllegalArgumentException (""+fld 640 + " is not static"); 641 } 642 return field(null, fld); 643 } 644 658 public static Function instance_field(Class c, String name){ 659 return instance_field(c, name, false); 660 } 661 675 public static Function instance_field(Class c, String name, 676 boolean suppress_security) 677 throws IllegalArgumentException { 678 final Field fld = ReflectionUtil.getInstanceField(c, name, 679 suppress_security); 680 return instance_field(c, fld); 681 } 682 683 694 public static Function instance_field(Class c, Field fld){ 695 if(Modifier.isStatic(fld.getModifiers())){ 696 throw new IllegalArgumentException (""+fld 697 +" is static"); 698 } 699 return new FloatingFieldFunction(c, fld); 700 } 701 702 703 713 public static Function field(Object obj, String name){ 714 return field(obj, name, false); 715 } 716 726 public static Function field(Object obj, final String name, 727 boolean suppress_security){ 728 return field(obj.getClass(), obj, name, suppress_security); 729 } 730 743 public static Function field(Class type, Object obj, String name){ 744 return field(type, obj, name, false); 745 } 746 759 public static Function field(Class type, Object obj, final String name, 760 boolean suppress_security){ 761 final Field fld = ReflectionUtil.getField(type, name, suppress_security); 762 return field(obj, fld); 763 } 764 772 public static Function field(Object obj, Field fld){ 773 return new FieldFunction(canonicalize(obj, fld), fld); 774 } 775 776 } 777 | Popular Tags |