1 23 24 29 30 package com.sun.appserv.management.util.misc; 31 32 import java.lang.reflect.Array ; 33 import java.lang.reflect.Field ; 34 import java.lang.reflect.Constructor ; 35 import java.lang.reflect.Method ; 36 import java.lang.ClassLoader ; 37 38 import java.util.Set ; 39 import java.util.HashSet ; 40 41 44 final class ClassToClassMapping 45 { 46 final Class mSrc; 47 final Class mDest; 48 49 public 50 ClassToClassMapping( Class src, Class dest ) 51 { 52 mSrc = src; 53 mDest = dest; 54 } 55 } 56 57 58 62 public final class ClassUtil 63 { 64 private 65 ClassUtil( ) 66 { 67 } 69 70 public static boolean 71 classIsAccessible( String name ) 72 { 73 boolean accessible = false; 74 75 try 76 { 77 accessible = getClassFromName( name ) != null; 78 } 79 catch( ClassNotFoundException e ) 80 { 81 } 82 return( accessible ); 83 } 84 85 86 public static boolean 87 sigsEqual( 88 final Class [] sig1, 89 final Class [] sig2 ) 90 { 91 boolean equal = sig1.length == sig2.length; 92 93 if ( equal ) 94 { 95 for( int i = 0; i < sig1.length; ++i ) 96 { 97 if ( sig1[ i ] != sig2[ i ] ) 98 { 99 equal = false; 100 break; 101 } 102 } 103 } 104 105 return( equal ); 106 } 107 113 public static boolean 114 objectIsArray( Object o ) 115 { 116 return( classIsArray( o.getClass() ) ); 117 } 118 119 125 public static boolean 126 classIsArray( Class theClass ) 127 { 128 return( classnameIsArray( theClass.getName() ) ); 129 } 130 131 137 public static boolean 138 objectIsPrimitiveArray( Object o ) 139 { 140 return( getPrimitiveArrayTypeCode( o.getClass() ) != 0 ); 141 } 142 143 149 public static boolean 150 classnameIsArray( String classname ) 151 { 152 return( classname.startsWith( "[" ) ); 153 } 154 155 161 public static String 162 stripPackageName( String classname ) 163 { 164 final int lastDot = classname.lastIndexOf( "." ); 165 if ( lastDot < 0 ) 166 { 167 return( classname ); 168 } 169 170 return( classname.substring( lastDot + 1, classname.length() ) ); 171 } 172 173 174 180 public static boolean 181 classnameIsPrimitiveArray( String classname ) 182 { 183 return( getPrimitiveArrayTypeCode( classname ) != 0 ); 184 } 185 186 193 public static char 194 getPrimitiveArrayTypeCode( Class theClass ) 195 { 196 char typeCode = 0; 197 198 if ( classIsArray( theClass ) ) 199 { 200 typeCode = getPrimitiveArrayTypeCode( theClass.getName() ); 201 } 202 203 return( typeCode ); 204 } 205 206 212 public static char 213 getPrimitiveArrayTypeCode( String classname ) 214 { 215 char typeCode = 0; 216 217 final int length = classname.length(); 218 219 if ( classnameIsArray( classname ) && 220 classname.charAt( length - 2 ) == '[' ) 221 { 222 typeCode = classname.charAt( length - 1 ); 223 224 switch( typeCode ) 225 { 226 default: typeCode = 0; break; 227 228 case 'Z': 229 case 'B': 230 case 'C': 231 case 'S': 232 case 'I': 233 case 'J': 234 case 'F': 235 case 'D': 236 break; 237 } 238 } 239 240 return( typeCode ); 241 } 242 243 244 250 public static String 251 getArrayMemberClassName( String classname ) 252 { 253 String result = null; 254 255 if ( ! classnameIsArray( classname ) ) 256 { 257 throw new IllegalArgumentException ( "not an array" ); 258 } 259 260 final int classnameLength = classname.length(); 261 262 263 if ( classnameIsPrimitiveArray( classname ) ) 264 { 265 final char lastChar = classname.charAt(classnameLength -1 ); 266 267 switch( lastChar ) 268 { 269 default: throw new RuntimeException ( "illegal primitive" ); 270 271 case 'Z': result = "boolean"; break; 273 case 'B': result = "byte"; break; 274 case 'C': result = "char"; break; 275 case 'S': result = "short"; break; 276 case 'I': result = "int"; break; 277 case 'J': result = "long"; break; 278 case 'F': result = "float"; break; 279 case 'D': result = "double"; break; 280 } 281 } 282 else 283 { 284 result = classname.substring( 2, classnameLength - 1 ); 286 } 287 288 return( result ); 289 } 290 291 292 293 294 297 final static class ClassNameToClassMapping 298 { 299 String mName; 300 Class mClass; 301 302 ClassNameToClassMapping( String name, Class theClass ) 303 { 304 mName = name; 305 mClass = theClass; 306 } 307 } 308 309 private static final ClassNameToClassMapping [] sPrimitiveNameToObjectClass = 310 new ClassNameToClassMapping [] 311 { 312 new ClassNameToClassMapping( "int", int.class ), 313 new ClassNameToClassMapping( "long", long.class ), 314 new ClassNameToClassMapping( "short", short.class ), 315 new ClassNameToClassMapping( "byte", byte.class ), 316 new ClassNameToClassMapping( "boolean", boolean.class ), 317 new ClassNameToClassMapping( "float", float.class ), 318 new ClassNameToClassMapping( "double", double.class ), 319 new ClassNameToClassMapping( "char", char.class ), 320 new ClassNameToClassMapping( "void", void.class ), 321 }; 322 323 324 public static Class <?> 325 classForName( String name ) 326 throws ClassNotFoundException 327 { 328 Class <?> c = null; 329 330 try 331 { 332 c = Class.forName( name ); 333 } 334 catch ( ClassNotFoundException e ) 335 { 336 c = Class.forName( name, true, Thread.currentThread().getContextClassLoader() ); 337 } 338 catch ( NoClassDefFoundError e ) 339 { 340 c = Class.forName( name, true, Thread.currentThread().getContextClassLoader() ); 341 } 342 343 return( c ); 344 } 345 346 353 public static Class 354 getClassFromName( final String classname ) 355 throws ClassNotFoundException 356 { 357 Class theClass = null; 358 359 if ( classname.startsWith( "[L" )) 360 { 361 theClass = classForName( classname ); 363 } 364 else 365 { 366 final int numMappings = Array.getLength( sPrimitiveNameToObjectClass ); 367 for( int i = 0; i < numMappings; ++i ) 368 { 369 if ( sPrimitiveNameToObjectClass[ i ].mName.equals( classname ) ) 370 { 371 theClass = sPrimitiveNameToObjectClass[ i ].mClass; 372 break; 373 } 374 } 375 376 if ( theClass == null ) 377 { 378 theClass = classForName( classname ); 379 } 380 } 381 return( theClass ); 382 } 383 384 385 private static final ClassToClassMapping [] sPrimitiveClassToObjectClass = 386 new ClassToClassMapping [] 387 { 388 new ClassToClassMapping( boolean.class, Boolean .class), 389 new ClassToClassMapping( byte.class, Byte .class ), 390 new ClassToClassMapping( char.class, Character .class ), 391 new ClassToClassMapping( short.class, Short .class ), 392 new ClassToClassMapping( int.class, Integer .class ), 393 new ClassToClassMapping( long.class, Long .class ), 394 new ClassToClassMapping( float.class, Float .class ), 395 new ClassToClassMapping( double.class, Double .class ), 396 }; 397 403 public static Class 404 PrimitiveClassToObjectClass( final Class theClass ) 405 { 406 Class result = theClass; 407 408 final int numMappings = Array.getLength( sPrimitiveClassToObjectClass ); 409 for( int i = 0; i < numMappings; ++i ) 410 { 411 final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[ i ]; 412 413 if ( mapping.mSrc == theClass ) 414 { 415 result = mapping.mDest; 416 break; 417 } 418 } 419 420 return( result ); 421 } 422 423 429 public static Class 430 ObjectClassToPrimitiveClass( final Class theClass ) 431 { 432 Class result = theClass; 433 434 final int numMappings = Array.getLength( sPrimitiveClassToObjectClass ); 435 for( int i = 0; i < numMappings; ++i ) 436 { 437 final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[ i ]; 438 439 if ( mapping.mDest == theClass ) 440 { 441 result = mapping.mSrc; 442 break; 443 } 444 } 445 446 return( result ); 447 } 448 449 455 public static boolean 456 IsPrimitiveClass( final Class theClass ) 457 { 458 boolean isSimple = false; 459 460 final int numMappings = Array.getLength( sPrimitiveClassToObjectClass ); 461 for( int i = 0; i < numMappings; ++i ) 462 { 463 final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[ i ]; 464 465 if ( mapping.mSrc.equals( theClass ) ) 466 { 467 isSimple = true; 468 break; 469 } 470 } 471 472 return( isSimple ); 473 } 474 475 476 482 public static String 483 PrimitiveLetterToClassName( final char primitive) 484 { 485 String result = "" + primitive; 486 487 switch( primitive ) 489 { 490 case 'B': result = "byte"; break; 491 case 'C': result = "char"; break; 492 case 'D': result = "double"; break; 493 case 'F': result = "float"; break; 494 case 'I': result = "int"; break; 495 case 'J': result = "long"; break; 496 case 'S': result = "short"; break; 497 case 'Z': result = "boolean";break; 498 } 499 500 return( result ); 501 } 502 503 504 512 public static String [] 513 getTypes( final Object [] args ) 514 { 515 if ( args == null ) 516 return( null ); 517 518 final int numArgs = Array.getLength( args ); 519 520 final String [] types = new String [ numArgs ]; 521 522 for( int i = 0; i < numArgs; ++i ) 523 { 524 if ( args[ i ] == null ) 525 { 526 types[ i ] = null; 527 } 528 else 529 { 530 types[ i ] = args[ i ].getClass().getName(); 531 } 532 } 533 534 return( types ); 535 } 536 537 545 public static Class [] 546 signatureFromClassnames( String [] classnames ) 547 throws ClassNotFoundException 548 { 549 if ( classnames == null ) 550 { 551 return( null ); 552 } 553 554 final Class [] signature = new Class [ classnames.length ]; 555 556 for ( int i = 0; i < signature.length; ++i ) 557 { 558 signature[ i ] = getClassFromName( classnames[ i ] ); 559 } 560 561 return( signature ); 562 } 563 564 565 573 public static String [] 574 classnamesFromSignature( Class [] classes ) 575 { 576 final String [] classnames = new String [ classes.length ]; 577 578 for ( int i = 0; i < classnames.length; ++i ) 579 { 580 classnames[ i ] = classes[ i ].getName(); 581 } 582 583 return( classnames ); 584 } 585 586 587 588 596 public static String 597 getFriendlyClassname( Class theClass ) 598 { 599 return( getFriendlyClassname( theClass.getName() ) ); 600 } 601 602 616 final static String javaLang = "java.lang."; 617 public static String 618 getFriendlyClassname( String type ) 619 { 620 String result = type; 621 622 if ( type.startsWith( "[" ) ) 623 { 624 int depth = 0; 626 while ( type.charAt( depth ) == (int)'[' ) 627 { 628 ++depth; 629 } 630 631 result = type.substring( depth, type.length() ); 633 634 if ( result.startsWith( "L" ) && result.endsWith( ";" ) ) 635 { 636 result = result.substring( 1, result.length() - 1 ); 637 } 638 else if ( result.length() == 1 ) 639 { 640 switch( result.charAt( 0 ) ) 642 { 643 case 'Z': result = "boolean"; break; 644 case 'B': result = "byte"; break; 645 case 'C': result = "char"; break; 646 case 'S': result = "short"; break; 647 case 'I': result = "int"; break; 648 case 'J': result = "long"; break; 649 case 'F': result = "float"; break; 650 case 'D': result = "double"; break; 651 } 652 } 653 654 for( int i = 0; i < depth; ++i ) 655 { 656 result = result + "[]"; 657 } 658 } 659 660 if ( result.startsWith( javaLang ) ) 661 { 662 result = result.substring( javaLang.length(), result.length() ); 663 } 664 665 return( result ); 666 } 667 668 669 671 public static int 672 getArrayDimensions( final Class theClass ) 673 { 674 final String classname = theClass.getName(); 675 676 int dim = 0; 677 while ( classname.charAt( dim ) == '[' ) 678 { 679 ++dim; 680 } 681 682 return( dim ); 683 } 684 685 687 public static Class 688 getArrayElementClass( final Class arrayClass ) 689 { 690 final String arrayClassName = arrayClass.getName(); 691 692 if ( ! classnameIsArray( arrayClassName ) ) 693 { 694 throw new IllegalArgumentException ( "not an array" ); 695 } 696 697 String name = arrayClassName; 698 699 name = name.substring( 1, name.length() ); 701 702 if ( ! name.startsWith( "[" ) ) 703 { 704 706 if ( name.startsWith( "L" ) ) 707 { 708 name = name.substring( 1, name.length() - 1); 710 } 711 else if ( name.length() == 1 ) 712 { 713 name = PrimitiveLetterToClassName( name.charAt( 0 ) ); 715 } 716 } 717 else 718 { 719 } 721 722 Class theClass = null; 723 try 724 { 725 theClass = getClassFromName( name ); 726 } 727 catch( ClassNotFoundException e ) 728 { 729 assert( false ); 730 } 731 732 return( theClass ); 733 } 734 735 public static Class 736 getInnerArrayElementClass( final Class arrayClass ) 737 throws ClassNotFoundException 738 { 739 Class elementClass = arrayClass; 740 741 do 742 { 743 elementClass = getArrayElementClass( elementClass ); 744 } 745 while ( classIsArray( elementClass ) ); 746 747 return( elementClass ); 748 } 749 750 751 752 753 private static Object 754 InstantiateObject( final String theString ) 755 throws Exception 756 { 757 Object result = null; 758 759 try 760 { 761 result = InstantiateNumber( theString ); 762 } 763 catch( NumberFormatException e ) 764 { 765 result = theString; 766 } 767 768 return( result ); 769 } 770 771 777 public static boolean 778 signaturesAreCompatible( final Class <?> [] callee, final Class <?> [] argsSignature ) 779 { 780 boolean compatible = false; 781 782 if ( callee.length == argsSignature.length ) 783 { 784 compatible = true; 785 786 for( int i = 0; i < callee.length; ++i ) 787 { 788 if ( ! callee[ i ].isAssignableFrom( argsSignature[ i ] ) ) 789 { 790 compatible = false; 791 break; 792 } 793 } 794 } 795 796 return( compatible ); 797 } 798 799 802 public static final Method 803 findMethod( 804 final Class theClass, 805 final String methodName, 806 final Class [] sig ) 807 { 808 Method m = null; 809 try 810 { 811 m = theClass.getMethod( methodName, sig ); 812 } 813 catch( NoSuchMethodException e ) 814 { 815 } 817 818 return( m ); 819 } 820 821 824 public static final Set <Method > 825 findMethods( 826 final Method [] candidates, 827 final String methodName ) 828 { 829 final Set <Method > s = new HashSet <Method >(); 830 831 for( int methodIdx = 0; methodIdx < candidates.length; ++methodIdx ) 832 { 833 final Method method = candidates[ methodIdx ]; 834 if ( method.getName().equals( methodName ) ) 835 { 836 s.add( method ); 837 } 838 } 839 840 return( s ); 841 } 842 843 844 851 public static Object 852 InstantiateObject( final Class theClass, final Object [] args ) 853 throws Exception 854 { 855 final Class [] signature = new Class [ args.length ]; 856 857 for( int i = 0; i < signature.length; ++i ) 858 { 859 signature[ i ] = args[ i ].getClass(); 860 } 861 862 Constructor constructor = null; 863 try 864 { 865 constructor = theClass.getConstructor( signature ); 868 } 869 catch( NoSuchMethodException e ) 870 { 871 final Constructor [] constructors = theClass.getConstructors(); 872 873 int numMatches = 0; 874 for( int i = 0; i < constructors.length; ++i ) 875 { 876 final Constructor tempConstructor = constructors[ i ]; 877 878 final Class [] tempSignature = tempConstructor.getParameterTypes(); 879 880 if ( signaturesAreCompatible( tempSignature, signature ) ) 881 { 882 ++numMatches; 883 constructor = tempConstructor; 884 } 886 } 887 888 if ( numMatches != 1 ) 890 { 891 throw e; 892 } 893 } 894 895 Object result = null; 896 try 897 { 898 result = constructor.newInstance( args ); 899 } 900 catch( java.lang.reflect.InvocationTargetException e ) 901 { 902 final Throwable cause = e.getCause(); 904 905 if ( cause instanceof Exception ) 906 { 907 throw (Exception )cause; 908 } 909 else 910 { 911 throw e; 913 } 914 } 915 916 return( result ); 917 } 918 919 920 927 public static Object 928 InstantiateObject( final Class theClass, final String theString ) 929 throws Exception 930 { 931 final Class [] signature = new Class [] { String .class }; 932 final Constructor constructor = theClass.getConstructor( signature ); 933 934 Object result = null; 935 try 936 { 937 result = constructor.newInstance( new Object [] { theString } ); 938 } 939 catch( java.lang.reflect.InvocationTargetException e ) 940 { 941 Throwable cause = e.getCause(); 943 944 if ( cause instanceof Exception ) 945 { 946 throw (Exception )cause; 947 } 948 else 949 { 950 throw e; 952 } 953 } 954 955 return( result ); 956 } 957 958 968 private static Object 969 InstantiateNumber( final String theString ) 970 throws Exception 971 { 972 Object result = null; 973 974 if ( theString.indexOf( '.' ) >= 0 ) 975 { 976 result = InstantiateObject( Double .class, theString ); 977 } 978 else 979 { 980 try 981 { 982 result = InstantiateObject( Integer .class, theString ); 983 } 984 catch( NumberFormatException e ) 985 { 986 result = InstantiateObject( Long .class, theString ); 988 } 989 } 990 return( result ); 991 } 992 993 994 1002 public static Object 1003 InstantiateFromString( final Class theClass, final String theString ) 1004 throws Exception 1005 { 1006 Object result = null; 1007 1008 if ( theClass == Object .class ) 1010 { 1011 result = InstantiateObject( theString ); 1013 } 1014 else if ( theClass == Number .class ) 1015 { 1016 result = InstantiateNumber( theString ); 1018 } 1019 else if ( theClass == Character .class || theClass == char.class) 1020 { 1021 if ( theString.length() != 1 ) 1022 { 1023 throw new IllegalArgumentException ( "not a character: " + theString ); 1024 } 1025 1026 result = new Character ( theString.charAt( 0 ) ); 1027 } 1028 else 1029 { 1030 1031 final Class objectClass = PrimitiveClassToObjectClass( theClass ); 1032 1033 result = InstantiateObject( objectClass, theString ); 1034 } 1035 1036 return( result ); 1037 } 1038 1039 1040 1048 public static Object 1049 InstantiateDefault( final Class inClass ) 1050 throws Exception 1051 { 1052 Object result = null; 1053 1054 final Class objectClass = PrimitiveClassToObjectClass( inClass ); 1055 1056 if ( Number .class.isAssignableFrom( objectClass ) ) 1057 { 1058 result = InstantiateFromString( objectClass, "0" ); 1059 } 1060 else if ( objectClass == Boolean .class) 1061 { 1062 result = new Boolean ( "true" ); 1063 } 1064 else if ( objectClass == Character .class) 1065 { 1066 result = new Character ( 'X' ); 1067 } 1068 else if ( classIsArray( objectClass ) ) 1069 { 1070 result = Array.newInstance( objectClass, 0 ); 1071 } 1072 else if ( objectClass == Object .class ) 1073 { 1074 result = new String ( "anyObject" ); 1075 } 1076 else if ( objectClass == String .class ) 1077 { 1078 result = new String ( "" ); 1079 } 1080 else if ( objectClass == java.net.URL .class ) 1081 { 1082 result = new java.net.URL ( "http://www.sun.com" ); 1083 } 1084 else if ( objectClass == java.net.URI .class ) 1085 { 1086 result = new java.net.URI ( "http://www.sun.com" ); 1087 } 1088 else if ( classIsArray( inClass ) ) 1089 { 1090 final int dimensions = 3; 1091 result = Array.newInstance( getInnerArrayElementClass( inClass ), dimensions ); 1092 } 1093 else 1094 { 1095 result = objectClass.newInstance(); 1096 } 1098 return( result ); 1099 } 1100 1101 1102 final static String [] sJavaLangTypes = 1103 { "Character", "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double", "String", "Object"}; 1104 final static int sNumBaseTypes = Array.getLength( sJavaLangTypes ); 1105 1106 1111 public static String 1112 ExpandClassName( final String name ) 1113 { 1114 String fullName = name; 1115 1116 final int numTypes = sNumBaseTypes; 1117 for( int i = 0; i < numTypes; ++i ) 1118 { 1119 if ( name.equals( sJavaLangTypes[ i ] ) ) 1120 { 1121 fullName = "java.lang." + name; 1122 break; 1123 } 1124 } 1125 1126 if ( fullName == name ) { 1128 if ( name.equals( "Number" ) ) 1129 { 1130 fullName = "java.lang." + name; 1131 } 1132 else if ( name.equals( "BigDecimal" ) || name.equals( "BigInteger" ) ) 1133 { 1134 fullName = "java.math." + name; 1135 } 1136 else if ( name.equals( "URL" ) || name.equals( "URI" ) ) 1137 { 1138 fullName = "java.net." + name; 1139 } 1140 else if ( name.equals( "Date" ) ) 1141 { 1142 fullName = "java.util." + name; 1143 } 1144 else if ( name.equals( "ObjectName" ) ) 1145 { 1146 fullName = "javax.management." + name; 1147 } 1148 1149 } 1150 1151 return( fullName ); 1152 } 1153 1154 1155 1156 1164 public static Class 1165 convertArrayClass( final Class arrayClass, final Class newInnerType ) 1166 throws ClassNotFoundException 1167 { 1168 final String arrayClassname = arrayClass.getName(); 1169 if ( ! arrayClassname.endsWith( ";" ) ) 1170 { 1171 throw new IllegalArgumentException ( "not an array of Object" ); 1172 } 1173 1174 final int innerNameBegin = 1 + arrayClassname.indexOf( "L" ); 1175 1176 final String newClassName = arrayClassname.substring( 0, innerNameBegin ) + newInnerType.getName() + ";"; 1177 1178 final Class newClass = getClassFromName( newClassName ); 1179 1180 return( newClass ); 1181 } 1182 1183 1184 private static class MyClassLoader extends ClassLoader 1185 { 1186 MyClassLoader( ) 1187 { 1188 this( Thread.currentThread().getContextClassLoader() ); 1189 } 1190 1191 MyClassLoader( ClassLoader cl ) 1192 { 1193 super( cl ); 1194 } 1195 1196 public Package [] 1197 getPackages() 1198 { 1199 return( super.getPackages() ); 1200 } 1201 } 1202 1203 public static Package [] 1204 getPackages() 1205 { 1206 return( new MyClassLoader().getPackages() ); 1207 } 1208 1209 public static Package [] 1210 getPackages( ClassLoader cl ) 1211 { 1212 return( new MyClassLoader( cl ).getPackages( ) ); 1213 } 1214 1215 1216 1218 public static Object 1219 getFieldValue( 1220 final Class theInterface, 1221 final String name ) 1222 { 1223 Object value = null; 1224 1225 try 1226 { 1227 final Field field = theInterface.getField( name ); 1228 value = field.get( theInterface ); 1229 } 1230 catch( Exception e ) 1231 { 1232 value = null; 1233 } 1234 1235 return( value ); 1236 } 1237 1238 public static String 1239 stripPackagePrefix( final String classname ) 1240 { 1241 final int index = classname.lastIndexOf( "." ); 1242 1243 String result = classname; 1244 if ( index > 0 ) 1245 { 1246 result = classname.substring( index + 1, classname.length() ); 1247 } 1248 1249 return( result ); 1250 }; 1251 1252 1253 public static String 1254 getPackagePrefix( final String classname ) 1255 { 1256 final int index = classname.lastIndexOf( "." ); 1257 1258 String result = classname; 1259 if ( index > 0 ) 1260 { 1261 result = classname.substring( 0, index ); 1262 } 1263 1264 return( result ); 1265 }; 1266 1267} 1268 1269 1270 1271 1272 1273 1274 1275 | Popular Tags |