1 23 24 28 29 34 35 package com.sun.enterprise.admin.util; 36 37 import java.lang.reflect.Array ; 38 import java.lang.reflect.Constructor ; 39 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 61 public final class ClassUtil 62 { 63 private 64 ClassUtil( ) 65 { 66 } 68 69 static private void 70 p( Object o ) 71 { 72 System.out.println( o.toString() ); 73 } 74 75 81 public static boolean 82 objectIsArray( Object o ) 83 { 84 return( classIsArray( o.getClass() ) ); 85 } 86 87 93 public static boolean 94 classIsArray( Class theClass ) 95 { 96 return( classnameIsArray( theClass.getName() ) ); 97 } 98 99 105 public static boolean 106 objectIsPrimitiveArray( Object o ) 107 { 108 return( getPrimitiveArrayTypeCode( o.getClass() ) != 0 ); 109 } 110 111 117 public static boolean 118 classnameIsArray( String classname ) 119 { 120 return( classname.startsWith( "[" ) ); 121 } 122 123 124 130 public static boolean 131 classnameIsPrimitiveArray( String classname ) 132 { 133 return( getPrimitiveArrayTypeCode( classname ) != 0 ); 134 } 135 136 143 public static char 144 getPrimitiveArrayTypeCode( Class theClass ) 145 { 146 char typeCode = 0; 147 148 if ( classIsArray( theClass ) ) 149 { 150 typeCode = getPrimitiveArrayTypeCode( theClass.getName() ); 151 } 152 153 return( typeCode ); 154 } 155 156 162 public static char 163 getPrimitiveArrayTypeCode( String classname ) 164 { 165 char typeCode = 0; 166 167 final int length = classname.length(); 168 169 if ( classnameIsArray( classname ) && 170 classname.charAt( length - 2 ) == '[' ) 171 { 172 typeCode = classname.charAt( length - 1 ); 173 174 switch( typeCode ) 175 { 176 default: typeCode = 0; break; 177 178 case 'Z': 179 case 'B': 180 case 'C': 181 case 'S': 182 case 'I': 183 case 'J': 184 case 'F': 185 case 'D': 186 break; 187 } 188 } 189 190 return( typeCode ); 191 } 192 193 194 200 public static String 201 getArrayMemberClassName( String classname ) 202 { 203 String result = null; 204 205 if ( ! classnameIsArray( classname ) ) 206 { 207 throw new IllegalArgumentException ( "not an array" ); 208 } 209 210 final int classnameLength = classname.length(); 211 212 213 if ( classnameIsPrimitiveArray( classname ) ) 214 { 215 final char lastChar = classname.charAt(classnameLength -1 ); 216 217 switch( lastChar ) 218 { 219 default: assert( false ); 220 221 case 'Z': result = "boolean"; break; 223 case 'B': result = "byte"; break; 224 case 'C': result = "char"; break; 225 case 'S': result = "short"; break; 226 case 'I': result = "int"; break; 227 case 'J': result = "long"; break; 228 case 'F': result = "float"; break; 229 case 'D': result = "double"; break; 230 } 231 } 232 else 233 { 234 result = classname.substring( 2, classnameLength - 1 ); 236 } 237 238 return( result ); 239 } 240 241 242 243 244 247 final static class ClassNameToClassMapping 248 { 249 String mName; 250 Class mClass; 251 252 ClassNameToClassMapping( String name, Class theClass ) 253 { 254 mName = name; 255 mClass = theClass; 256 } 257 } 258 259 private static final ClassNameToClassMapping [] sPrimitiveNameToObjectClass = 260 new ClassNameToClassMapping [] 261 { 262 new ClassNameToClassMapping( "int", int.class ), 263 new ClassNameToClassMapping( "long", long.class ), 264 new ClassNameToClassMapping( "short", short.class ), 265 new ClassNameToClassMapping( "byte", byte.class ), 266 new ClassNameToClassMapping( "boolean", boolean.class ), 267 new ClassNameToClassMapping( "float", float.class ), 268 new ClassNameToClassMapping( "double", double.class ), 269 new ClassNameToClassMapping( "char", char.class ), 270 new ClassNameToClassMapping( "void", void.class ), 271 }; 272 273 280 public static Class 281 getClassFromName( final String classname ) 282 throws ClassNotFoundException 283 { 284 Class theClass = null; 285 286 if ( classname.startsWith( "[L" )) 287 { 288 theClass = Class.forName( classname ); 290 } 291 else 292 { 293 final int numMappings = Array.getLength( sPrimitiveNameToObjectClass ); 294 for( int i = 0; i < numMappings; ++i ) 295 { 296 if ( sPrimitiveNameToObjectClass[ i ].mName.equals( classname ) ) 297 { 298 theClass = sPrimitiveNameToObjectClass[ i ].mClass; 299 break; 300 } 301 } 302 303 if ( theClass == null ) 304 { 305 theClass = theClass.forName( classname ); 306 } 307 } 308 return( theClass ); 309 } 310 311 312 private static final ClassToClassMapping [] sPrimitiveClassToObjectClass = 313 new ClassToClassMapping [] 314 { 315 new ClassToClassMapping( int.class, Integer .class ), 316 new ClassToClassMapping( long.class, Long .class ), 317 new ClassToClassMapping( short.class, Short .class ), 318 new ClassToClassMapping( byte.class, Byte .class ), 319 new ClassToClassMapping( boolean.class, Boolean .class), 320 new ClassToClassMapping( float.class, Float .class ), 321 new ClassToClassMapping( double.class, Double .class ), 322 new ClassToClassMapping( char.class, Character .class ), 323 }; 324 330 public static Class 331 PrimitiveClassToObjectClass( final Class theClass ) 332 { 333 Class result = theClass; 334 335 final int numMappings = Array.getLength( sPrimitiveClassToObjectClass ); 336 for( int i = 0; i < numMappings; ++i ) 337 { 338 final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[ i ]; 339 340 if ( mapping.mSrc.equals( theClass ) ) 341 { 342 result = mapping.mDest; 343 break; 344 } 345 } 346 347 return( result ); 348 } 349 350 356 public static boolean 357 IsPrimitiveClass( final Class theClass ) 358 { 359 boolean isSimple = false; 360 361 final int numMappings = Array.getLength( sPrimitiveClassToObjectClass ); 362 for( int i = 0; i < numMappings; ++i ) 363 { 364 final ClassToClassMapping mapping = sPrimitiveClassToObjectClass[ i ]; 365 366 if ( mapping.mSrc.equals( theClass ) ) 367 { 368 isSimple = true; 369 break; 370 } 371 } 372 373 return( isSimple ); 374 } 375 376 377 public static String 378 PrimitiveLetterToClassName( final char primitive) 379 { 380 String result = "" + primitive; 381 382 switch( primitive ) 384 { 385 case 'B': result = "byte"; break; 386 case 'C': result = "char"; break; 387 case 'D': result = "double"; break; 388 case 'F': result = "float"; break; 389 case 'I': result = "int"; break; 390 case 'J': result = "long"; break; 391 case 'S': result = "short"; break; 392 case 'Z': result = "boolean";break; 393 } 394 395 return( result ); 396 } 397 398 399 400 401 public static String [] 402 getTypes( final Object [] args ) 403 { 404 if ( args == null ) 405 return( null ); 406 407 final int numArgs = Array.getLength( args ); 408 409 final String [] types = new String [ numArgs ]; 410 411 for( int i = 0; i < numArgs; ++i ) 412 { 413 types[ i ] = args[ i ].getClass().getName(); 414 } 415 416 return( types ); 417 } 418 419 420 public static String 421 getFriendlyClassname( Class theClass ) 422 { 423 return( getFriendlyClassname( theClass.getName() ) ); 424 } 425 426 439 final static String javaLang = "java.lang."; 440 public static String 441 getFriendlyClassname( String type ) 442 { 443 String result = type; 444 445 if ( type.startsWith( "[" ) ) 446 { 447 int depth = 0; 449 while ( type.charAt( depth ) == (int)'[' ) 450 { 451 ++depth; 452 } 453 454 result = type.substring( depth, type.length() ); 456 457 if ( result.startsWith( "L" ) && result.endsWith( ";" ) ) 458 { 459 result = result.substring( 1, result.length() - 1 ); 460 } 461 else if ( result.length() == 1 ) 462 { 463 switch( result.charAt( 0 ) ) 465 { 466 case 'Z': result = "boolean"; break; 467 case 'B': result = "byte"; break; 468 case 'C': result = "char"; break; 469 case 'S': result = "short"; break; 470 case 'I': result = "int"; break; 471 case 'J': result = "long"; break; 472 case 'F': result = "float"; break; 473 case 'D': result = "double"; break; 474 } 475 } 476 477 for( int i = 0; i < depth; ++i ) 478 { 479 result = result + "[]"; 480 } 481 } 482 483 if ( result.startsWith( javaLang ) ) 484 { 485 result = result.substring( javaLang.length(), result.length() ); 486 } 487 488 return( result ); 489 } 490 491 492 494 public static Class 495 getArrayElementClass( final Class arrayClass ) 496 { 497 final String arrayClassName = arrayClass.getName(); 498 499 if ( ! classnameIsArray( arrayClassName ) ) 500 { 501 throw new IllegalArgumentException ( "not an array" ); 502 } 503 504 String name = arrayClassName; 505 506 name = name.substring( 1, name.length() ); 508 509 if ( ! name.startsWith( "[" ) ) 510 { 511 513 if ( name.startsWith( "L" ) ) 514 { 515 name = name.substring( 1, name.length() - 1); 517 } 518 else if ( name.length() == 1 ) 519 { 520 name = PrimitiveLetterToClassName( name.charAt( 0 ) ); 522 } 523 } 524 else 525 { 526 } 528 529 Class theClass = null; 530 try 531 { 532 theClass = getClassFromName( name ); 533 } 534 catch( ClassNotFoundException e ) 535 { 536 assert( false ); 537 } 538 539 return( theClass ); 540 } 541 542 public static Class 543 getInnerArrayElementClass( final Class arrayClass ) 544 throws ClassNotFoundException 545 { 546 Class elementClass = arrayClass; 547 548 do 549 { 550 elementClass = getArrayElementClass( elementClass ); 551 } 552 while ( classIsArray( elementClass ) ); 553 554 return( elementClass ); 555 } 556 557 558 559 560 private static Object 561 InstantiateObject( final String theString ) 562 throws Exception 563 { 564 Object result = null; 565 566 try 567 { 568 result = InstantiateNumber( theString ); 569 } 570 catch( NumberFormatException e ) 571 { 572 result = theString; 573 } 574 575 return( result ); 576 } 577 578 584 public static boolean 585 signaturesAreCompatible( Class [] callee, Class [] argsSignature ) 586 { 587 boolean compatible = false; 588 589 if ( callee.length == argsSignature.length ) 590 { 591 compatible = true; 592 593 for( int i = 0; i < callee.length; ++i ) 594 { 595 if ( ! callee[ i ].isAssignableFrom( argsSignature[ i ] ) ) 596 { 597 compatible = false; 598 break; 599 } 600 } 601 } 602 603 return( compatible ); 604 } 605 606 public static Object 607 InstantiateObject( final Class theClass, final Object [] args ) 608 throws Exception 609 { 610 final Class [] signature = new Class [ args.length ]; 611 612 for( int i = 0; i < signature.length; ++i ) 613 { 614 signature[ i ] = args[ i ].getClass(); 615 } 616 617 Constructor constructor = null; 618 try 619 { 620 constructor = theClass.getConstructor( signature ); 623 } 624 catch( NoSuchMethodException e ) 625 { 626 final Constructor [] constructors = theClass.getConstructors(); 627 628 int numMatches = 0; 629 for( int i = 0; i < constructors.length; ++i ) 630 { 631 final Constructor tempConstructor = constructors[ i ]; 632 633 final Class [] tempSignature = tempConstructor.getParameterTypes(); 634 635 if ( signaturesAreCompatible( tempSignature, signature ) ) 636 { 637 ++numMatches; 638 constructor = tempConstructor; 639 } 641 } 642 643 if ( numMatches != 1 ) 645 { 646 throw e; 647 } 648 } 649 650 Object result = null; 651 try 652 { 653 result = constructor.newInstance( args ); 654 } 655 catch( java.lang.reflect.InvocationTargetException e ) 656 { 657 final Throwable cause = e.getCause(); 659 660 if ( cause instanceof Exception ) 661 { 662 throw (Exception )cause; 663 } 664 else 665 { 666 throw e; 668 } 669 } 670 671 return( result ); 672 } 673 674 675 public static Object 676 InstantiateObject( final Class theClass, final String theString ) 677 throws Exception 678 { 679 final Class [] signature = new Class [] { String .class }; 680 final Constructor constructor = theClass.getConstructor( signature ); 681 682 Object result = null; 683 try 684 { 685 result = constructor.newInstance( new Object [] { theString } ); 686 } 687 catch( java.lang.reflect.InvocationTargetException e ) 688 { 689 Throwable cause = e.getCause(); 691 692 if ( cause instanceof Exception ) 693 { 694 throw (Exception )cause; 695 } 696 else 697 { 698 throw e; 700 } 701 } 702 703 return( result ); 704 } 705 706 711 private static Object 712 InstantiateNumber( final String theString ) 713 throws Exception 714 { 715 Object result = null; 716 717 if ( theString.indexOf( '.' ) >= 0 ) 718 { 719 result = InstantiateObject( Double .class, theString ); 720 } 721 else 722 { 723 try 724 { 725 result = InstantiateObject( Integer .class, theString ); 726 } 727 catch( NumberFormatException e ) 728 { 729 result = InstantiateObject( Long .class, theString ); 731 } 732 } 733 return( result ); 734 } 735 736 737 744 public static Object 745 InstantiateFromString( final Class theClass, final String theString ) 746 throws Exception 747 { 748 Object result = null; 749 750 if ( theClass == Object .class ) 752 { 753 result = InstantiateObject( theString ); 755 } 756 else if ( theClass == Number .class ) 757 { 758 result = InstantiateNumber( theString ); 760 } 761 else if ( theClass == Character .class || theClass == char.class) 762 { 763 if ( theString.length() != 1 ) 764 { 765 throw new IllegalArgumentException ( "not a character: " + theString ); 766 } 767 768 result = new Character ( theString.charAt( 0 ) ); 769 } 770 else 771 { 772 773 final Class objectClass = PrimitiveClassToObjectClass( theClass ); 774 775 result = InstantiateObject( objectClass, theString ); 776 } 777 778 return( result ); 779 } 780 781 782 789 public static Object 790 InstantiateDefault( final Class inClass ) 791 throws Exception 792 { 793 Object result = null; 794 795 final Class objectClass = PrimitiveClassToObjectClass( inClass ); 796 797 if ( Number .class.isAssignableFrom( objectClass ) ) 798 { 799 result = InstantiateFromString( objectClass, "0" ); 800 } 801 else if ( objectClass == Boolean .class) 802 { 803 result = new Boolean ( "true" ); 804 } 805 else if ( objectClass == Character .class) 806 { 807 result = new Character ( 'X' ); 808 } 809 else if ( classIsArray( objectClass ) ) 810 { 811 result = Array.newInstance( objectClass, 0 ); 812 } 813 else if ( objectClass == Object .class ) 814 { 815 result = new String ( "anyObject" ); 816 } 817 else if ( objectClass == String .class ) 818 { 819 result = new String ( "" ); 820 } 821 else if ( objectClass == java.net.URL .class ) 822 { 823 result = new java.net.URL ( "http://www.sun.com" ); 824 } 825 else if ( objectClass == java.net.URI .class ) 826 { 827 result = new java.net.URI ( "http://www.sun.com" ); 828 } 829 else if ( classIsArray( inClass ) ) 830 { 831 final int dimensions = 3; 832 result = Array.newInstance( getInnerArrayElementClass( inClass ), dimensions ); 833 } 834 else 835 { 836 result = objectClass.newInstance(); 837 } 839 return( result ); 840 } 841 842 843 848 final static String [] sJavaLangTypes = 849 { "Character", "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double", "String", "Object"}; 850 final static int sNumBaseTypes = Array.getLength( sJavaLangTypes ); 851 852 public static String 853 ExpandClassName( final String name ) 854 { 855 String fullName = name; 856 857 final int numTypes = sNumBaseTypes; 858 for( int i = 0; i < numTypes; ++i ) 859 { 860 if ( name.equals( sJavaLangTypes[ i ] ) ) 861 { 862 fullName = "java.lang." + name; 863 break; 864 } 865 } 866 867 if ( fullName == name ) { 869 if ( name.equals( "Number" ) ) 870 { 871 fullName = "java.lang." + name; 872 } 873 else if ( name.equals( "BigDecimal" ) || name.equals( "BigInteger" ) ) 874 { 875 fullName = "java.math." + name; 876 } 877 else if ( name.equals( "URL" ) || name.equals( "URI" ) ) 878 { 879 fullName = "java.net." + name; 880 } 881 else if ( name.equals( "Date" ) ) 882 { 883 fullName = "java.util." + name; 884 } 885 else if ( name.equals( "ObjectName" ) ) 886 { 887 fullName = "javax.management." + name; 888 } 889 890 } 891 892 return( fullName ); 893 } 894 895 896 897 902 public static Class 903 convertArrayClass( final Class arrayClass, final Class newInnerType ) 904 throws ClassNotFoundException 905 { 906 final String arrayClassname = arrayClass.getName(); 907 if ( ! arrayClassname.endsWith( ";" ) ) 908 { 909 throw new IllegalArgumentException ( "not an array of Object" ); 910 } 911 912 final int innerNameBegin = 1 + arrayClassname.indexOf( "L" ); 913 914 final String newClassName = arrayClassname.substring( 0, innerNameBegin ) + newInnerType.getName() + ";"; 915 916 final Class newClass = getClassFromName( newClassName ); 917 918 return( newClass ); 919 } 920 921 922 } 923 924 | Popular Tags |