1 8 14 package jfun.yan.util; 15 16 17 import java.lang.reflect.Constructor ; 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Modifier ; 21 import jfun.util.Misc; 22 import jfun.util.typing.TypeUtils; 23 24 25 26 48 public final class ReflectionUtil { 49 55 public static Class toObjectType(Class c){ 56 return TypeUtils.toObjectType(c); 57 } 58 68 public static boolean isAssignableFrom(Class t1, Class t2){ 69 if(void.class.equals(t2)){ 70 return !t1.isPrimitive(); 71 } 72 return toObjectType(t1).isAssignableFrom(toObjectType(t2)); 73 } 74 80 public static boolean isCompatible(Class a, Class b){ 81 return isAssignableFrom(a,b) || isAssignableFrom(b, a); 82 } 83 93 public static boolean isInstance(Class t, Object obj){ 94 if(void.class.equals(t)){ 95 return true; 97 } 98 if(obj == null){ 99 return !t.isPrimitive(); 100 } 101 else return toObjectType(t).isInstance(obj); 102 } 103 104 105 private static final Introspector introspector = new Introspector(); 106 116 public static <T> Constructor <T> getConstructor(Class <T> c, int param_count) 117 throws AmbiguityException{ 118 return getConstructor(c, param_count, false); 119 } 120 130 public static <T> Constructor <T> getConstructor(Class <T> c, 131 int param_count, boolean suppress_security){ 132 final Constructor <T> ctor = introspector.getClassDescriptor(c) 133 .getConstructor(param_count, suppress_security); 134 if(ctor==null) 135 throw new IllegalArgumentException ("constructor not found for type " 136 + Misc.getTypeName(c)); 137 return ctor; 138 } 139 147 public static <T> Constructor <T> getConstructor(Class <T> c) 148 throws AmbiguityException{ 149 return getConstructor(c, false); 150 } 151 159 public static <T> Constructor <T> getConstructor(Class <T> c, boolean suppress_security){ 160 final Constructor <T> ctor = introspector.getClassDescriptor(c) 161 .getConstructor(suppress_security); 162 if(ctor==null) 163 throw new IllegalArgumentException ("constructor not found for type " 164 + Misc.getTypeName(c)); 165 return ctor; 166 182 } 183 191 public static <T> Constructor <T> getConstructor(Class <T> c, Class [] param_types){ 192 return getConstructor(c, param_types, false); 193 } 194 202 public static <T> Constructor <T> getConstructor(Class <T> c, Class [] param_types, 203 boolean suppress_security){ 204 217 final Constructor <T> ctor = introspector.getClassDescriptor(c) 218 .getConstructor(param_types, suppress_security); 219 if(ctor == null){ 220 throw new IllegalArgumentException ("constructor not found for: " 221 + Misc.getTypeName(c) + jfun.util.StringUtils.listString("(",",",")", 222 param_types) 223 ); 224 } 225 return ctor; 226 } 227 238 public static Method getStaticMethod(Class c, String name) 239 throws AmbiguityException{ 240 return getStaticMethod(c, name, false); 241 } 242 252 public static Method getStaticMethod(Class c, final String name, 253 boolean suppress_security) 254 throws AmbiguityException{ 255 return filterMethod(c, name, new MethodPredicate(){ 256 public boolean isMethod(Method mtd){ 257 return java.lang.reflect.Modifier.isStatic(mtd.getModifiers()) 258 ; 260 } 261 public String toString(){ 262 return "static method named "+name; 263 } 264 }, suppress_security); 265 } 266 267 280 public static Method getStaticMethod(Class c, String name, int param_count) 281 throws AmbiguityException{ 282 return getStaticMethod(c, name, param_count, false); 283 } 284 296 public static Method getStaticMethod(Class c, final String name, 297 final int param_count, boolean suppress_security) 298 throws AmbiguityException{ 299 return filterMethod(c, name, new MethodPredicate(){ 300 public boolean isMethod(Method mtd){ 301 return java.lang.reflect.Modifier.isStatic(mtd.getModifiers()) 302 && mtd.getParameterTypes().length==param_count; 303 } 305 public String toString(){ 306 return "static " + toMethodString(name, param_count); 307 } 308 }, suppress_security); 309 } 310 311 private static String getQualified(Class c, String name){ 312 return Misc.getTypeName(c)+"."+name; 313 } 314 321 public static Field getField(Class c, final String name){ 322 return getField(c, name, false); 323 } 324 332 public static Field getField(Class c, final String name, 333 boolean suppress_security){ 334 final Field fld = introspector.getClassDescriptor(c) 335 .getField(name,suppress_security); 336 if(fld==null) 337 throw new IllegalArgumentException (getQualified(c,name)+" not found."); 338 return fld; 339 348 } 349 356 public static Field getStaticField(Class c, final String name){ 357 return getStaticField(c, name, false); 358 } 359 367 public static Field getStaticField(Class c, final String name, 368 boolean suppress_security){ 369 final Field fld = getField(c, name, suppress_security); 370 if(!Modifier.isStatic(fld.getModifiers())){ 371 throw new IllegalArgumentException (getQualified(c,name)+" is not a static field"); 372 } 373 return fld; 374 381 } 382 389 public static Field getInstanceField(Class c, final String name){ 390 return getInstanceField(c, name, false); 391 } 392 400 public static Field getInstanceField(Class c, final String name, 401 boolean suppress_security){ 402 final Field fld = getField(c, name, suppress_security); 403 if(Modifier.isStatic(fld.getModifiers())){ 404 throw new IllegalArgumentException (getQualified(c,name)+" is a static field"); 405 } 406 return fld; 408 } 409 419 public static Method getInstanceMethod(Class c, final String name) 420 throws AmbiguityException{ 421 return getInstanceMethod(c, name, false); 422 } 423 434 public static Method getInstanceMethod(Class c, final String name, 435 boolean suppress_security){ 436 return filterMethod(c, name, new MethodPredicate(){ 437 public boolean isMethod(Method mtd){ 438 return !java.lang.reflect.Modifier.isStatic(mtd.getModifiers()) 439 ; 441 } 442 public String toString(){ 443 return "instance method named "+name; 444 } 445 }, suppress_security); 446 } 447 459 public static Method getInstanceMethod(Class c, 460 int param_count, final String name) 461 throws AmbiguityException{ 462 return getInstanceMethod(c, name, param_count, false); 463 } 464 477 public static Method getInstanceMethod(Class c, final String name, 478 final int param_count, boolean suppress_security){ 479 return filterMethod(c, name, new MethodPredicate(){ 480 public boolean isMethod(Method mtd){ 481 return !java.lang.reflect.Modifier.isStatic(mtd.getModifiers()) 482 && mtd.getParameterTypes().length==param_count; 483 } 484 public String toString(){ 485 return "instance method "+ toMethodString(name, param_count); 486 } 487 }, suppress_security); 488 } 489 490 500 public static Method getMethod(Class c, final String name){ 501 return getMethod(c, name, false); 502 } 503 512 public static Method getMethod(Class c, final String name, 513 boolean suppress_security){ 514 final Method mtd = introspector.getClassDescriptor(c) 515 .getMethod(name, suppress_security); 516 if(mtd == null){ 517 throw new IllegalArgumentException ( 518 "method " + Misc.getTypeName(c)+ '.' + name 519 + " not found"); 520 } 521 return mtd; 522 523 533 } 534 546 public static Method getMethod(Class c, final String name, int param_count){ 547 return getMethod(c, name, param_count, false); 548 } 549 561 public static Method getMethod(Class c, final String name, 562 final int param_count, boolean suppress_security){ 563 final Method mtd = introspector.getClassDescriptor(c) 564 .filterMethod(name, getParamCountPredicate(name, param_count), suppress_security); 565 if(mtd == null){ 566 throw new IllegalArgumentException ( 567 "method " + Misc.getTypeName(c)+ '.' + name 568 + " not found"); 569 } 570 return mtd; 571 } 572 573 581 public static Method getMethod(Class c, String name, Class [] param_types){ 582 return getMethod(c, name, param_types, false); 583 } 584 593 public static Method getMethod(Class c, String name, Class [] param_types, 594 boolean suppress_security){ 595 final Method mtd = introspector.getClassDescriptor(c) 597 .getMethod(name, checkTypes(param_types), suppress_security); 598 if(mtd==null){ 599 throw new IllegalArgumentException ("method " 600 + Misc.getTypeName(c)+'.'+name+ 601 Utils.toString(param_types) 602 +" not found."); 603 } 604 return mtd; 605 } 606 628 629 private static Method filterMethod(Class c, String name, 630 final MethodPredicate pred, 631 boolean suppress_security) 632 throws AmbiguityException, IllegalArgumentException { 633 final Method ret = introspector.getClassDescriptor(c) 634 .filterMethod(name, pred, suppress_security); 635 if(ret==null){ 636 throw new IllegalArgumentException ( 637 pred.toString()+" cannot be found in " 638 + Misc.getTypeName(c) 639 ); 640 } 641 return ret; 642 643 666 } 667 private static MethodPredicate getParamCountPredicate( 668 final String name, final int param_count){ 669 return new MethodPredicate(){ 670 public boolean isMethod(Method mtd) { 671 return mtd.getParameterTypes().length==param_count; 672 } 673 public String toString(){ 674 return toMethodString(name, param_count); 675 } 676 }; 677 } 678 685 public static String toMethodString(String name, int param_count){ 686 StringBuffer buf = new StringBuffer (); 687 buf.append(name); 688 buf.append('('); 689 for(int i=0; i<param_count; i++){ 690 if(i>0) buf.append(','); 691 buf.append('_'); 692 } 693 buf.append(')'); 694 return buf.toString(); 695 } 696 private static Class [] checkTypes(Class [] ptypes){ 697 return ptypes==null?no_params:ptypes; 698 } 699 static final Class [] no_params = new Class [0]; 700 701 } 702 | Popular Tags |