KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > ClassUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28  
29 /*
30  * $Header: /cvs/glassfish/admin-core/util/src/java/com/sun/enterprise/admin/util/ClassUtil.java,v 1.2 2005/12/25 03:53:05 tcfujii Exp $
31  * $Revision: 1.2 $
32  * $Date: 2005/12/25 03:53:05 $
33  */

34
35 package com.sun.enterprise.admin.util;
36
37 import java.lang.reflect.Array JavaDoc;
38 import java.lang.reflect.Constructor JavaDoc;
39
40
41 /*
42     Used internally.
43  */

44 final class ClassToClassMapping
45 {
46     final Class JavaDoc mSrc;
47     final Class JavaDoc mDest;
48
49         public
50     ClassToClassMapping( Class JavaDoc src, Class JavaDoc dest )
51     {
52         mSrc = src;
53         mDest = dest;
54     }
55 }
56
57
58 /*
59     Various utilities used for classes.
60  */

61 public final class ClassUtil
62 {
63         private
64     ClassUtil( )
65     {
66         // disallow instantiation
67
}
68     
69         static private void
70     p( Object JavaDoc o )
71     {
72         System.out.println( o.toString() );
73     }
74     
75     /*
76         Test whether an Object is an array
77         
78         @param o object to test
79         @returns true if the object is an array, false otherwise.
80      */

81         public static boolean
82     objectIsArray( Object JavaDoc o )
83     {
84         return( classIsArray( o.getClass() ) );
85     }
86     
87     /*
88         Test whether a Class is an array
89         
90         @param theClass class to test
91         @returns true if the class is an array, false otherwise.
92      */

93         public static boolean
94     classIsArray( Class JavaDoc theClass )
95     {
96         return( classnameIsArray( theClass.getName() ) );
97     }
98     
99     /*
100         Test whether an Object is an array of primitive types
101         
102         @param o object to test
103         @returns true if the object is an array, false otherwise.
104      */

105         public static boolean
106     objectIsPrimitiveArray( Object JavaDoc o )
107     {
108         return( getPrimitiveArrayTypeCode( o.getClass() ) != 0 );
109     }
110     
111     /*
112         Test whether a classname is an array
113         
114         @param classname classname string
115         @returns true if the object is an array, false otherwise.
116      */

117         public static boolean
118     classnameIsArray( String JavaDoc classname )
119     {
120         return( classname.startsWith( "[" ) );
121     }
122     
123     
124     /*
125         Test whether a classname is a primitive array
126         
127         @param classname classname string
128         @returns true if the object is a primitive array, false otherwise.
129      */

130         public static boolean
131     classnameIsPrimitiveArray( String JavaDoc classname )
132     {
133         return( getPrimitiveArrayTypeCode( classname ) != 0 );
134     }
135     
136     /*
137         Return the primitive element type code for an array of primitive types.
138         Same as getPrimitiveArrayTypeCode( theClass.getName() )
139         
140         @param classname the Class object
141         @returns the element type code; otherwise (char)0
142      */

143         public static char
144     getPrimitiveArrayTypeCode( Class JavaDoc 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     /*
157         Return the primitive element type code for an array of primitive types.
158         
159         @param classname classname string
160         @returns the element type code; otherwise (char)0
161      */

162         public static char
163     getPrimitiveArrayTypeCode( String JavaDoc 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     /*
195         Get the classname for an array element.
196         
197         @param classname classname string
198         @returns the classname for the array element
199      */

200         public static String JavaDoc
201     getArrayMemberClassName( String JavaDoc classname )
202     {
203         String JavaDoc result = null;
204         
205         if ( ! classnameIsArray( classname ) )
206         {
207             throw new IllegalArgumentException JavaDoc( "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                 // a simple type
222
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             // strip leading "[L" and trailing ";"
235
result = classname.substring( 2, classnameLength - 1 );
236         }
237         
238         return( result );
239     }
240     
241
242     
243          
244     /*
245         Class.forName does not work for primitive types, so we need to do it ourselves here.
246      */

247     final static class ClassNameToClassMapping
248     {
249         String JavaDoc mName;
250         Class JavaDoc mClass;
251
252         ClassNameToClassMapping( String JavaDoc name, Class JavaDoc 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     /*
274         Get a Class from a classname. Class.forName does not work for primitive types;
275         this methods returns the correct Class for any type.
276         
277         @param classname classname string
278         @returns the classname for the array element
279      */

280         public static Class JavaDoc
281     getClassFromName( final String JavaDoc classname )
282         throws ClassNotFoundException JavaDoc
283     {
284         Class JavaDoc theClass = null;
285         
286         if ( classname.startsWith( "[L" ))
287         {
288             // an array
289
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 JavaDoc.class ),
316             new ClassToClassMapping( long.class, Long JavaDoc.class ),
317             new ClassToClassMapping( short.class, Short JavaDoc.class ),
318             new ClassToClassMapping( byte.class, Byte JavaDoc.class ),
319             new ClassToClassMapping( boolean.class, Boolean JavaDoc.class),
320             new ClassToClassMapping( float.class, Float JavaDoc.class ),
321             new ClassToClassMapping( double.class, Double JavaDoc.class ),
322             new ClassToClassMapping( char.class, Character JavaDoc.class ),
323          };
324     /*
325         Map primitive class Classes to Object forms eg int.class to Integer.class
326         
327         @param theClass the class to map
328         @returns the corresponding Object class or the original Class if not a primitive.
329      */

330         public static Class JavaDoc
331     PrimitiveClassToObjectClass( final Class JavaDoc theClass )
332     {
333         Class JavaDoc 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     /*
351         Test whether a class is a primitive class.
352         
353         @param theClass the class to test
354         @returns true if it's a primitive class, false otherwise.
355      */

356         public static boolean
357     IsPrimitiveClass( final Class JavaDoc 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 JavaDoc
378     PrimitiveLetterToClassName( final char primitive)
379     {
380         String JavaDoc result = "" + primitive;
381         
382         // see JavaDoc on Class.getName()
383
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 JavaDoc []
402     getTypes( final Object JavaDoc [] args )
403     {
404         if ( args == null )
405             return( null );
406             
407         final int numArgs = Array.getLength( args );
408         
409         final String JavaDoc [] types = new String JavaDoc [ 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 JavaDoc
421     getFriendlyClassname( Class JavaDoc theClass )
422     {
423         return( getFriendlyClassname( theClass.getName() ) );
424     }
425     
426     /*
427         Convert a Java class name string into a more user friendly string. Examples
428         java.lang.String => String
429         java.lang.<type> => <type>;
430         [i => int[]
431         [Lfoo.bar.ClassName; => foo.bar.ClassName[]
432         
433         The types thus correspond exactly to what a Java programmer would write, rather
434         than the internal JVM representation.
435         
436         @param type
437         @returns a friendlier string representing the type
438      */

439     final static String JavaDoc javaLang = "java.lang.";
440         public static String JavaDoc
441     getFriendlyClassname( String JavaDoc type )
442     {
443         String JavaDoc result = type;
444         
445         if ( type.startsWith( "[" ) )
446         {
447             // count how deep the array is
448
int depth = 0;
449             while ( type.charAt( depth ) == (int)'[' )
450             {
451                 ++depth;
452             }
453             
454             // strip all the '[' characters
455
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                 // a simple type
464
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     /*
493      */

494         public static Class JavaDoc
495     getArrayElementClass( final Class JavaDoc arrayClass )
496     {
497         final String JavaDoc arrayClassName = arrayClass.getName();
498             
499         if ( ! classnameIsArray( arrayClassName ) )
500         {
501             throw new IllegalArgumentException JavaDoc( "not an array" );
502         }
503         
504         String JavaDoc name = arrayClassName;
505         
506         // strip leading "["
507
name = name.substring( 1, name.length() );
508         
509         if ( ! name.startsWith( "[" ) )
510         {
511             // element is not an array
512

513             if ( name.startsWith( "L" ) )
514             {
515                 // an Object class; strip leading "L" and trailing ";"
516
name = name.substring( 1, name.length() - 1);
517             }
518             else if ( name.length() == 1 )
519             {
520                 // may be a primitive type
521
name = PrimitiveLetterToClassName( name.charAt( 0 ) );
522             }
523         }
524         else
525         {
526             // element is an array; return it
527
}
528         
529         Class JavaDoc theClass = null;
530         try
531         {
532             theClass = getClassFromName( name );
533         }
534         catch( ClassNotFoundException JavaDoc e )
535         {
536             assert( false );
537         }
538         
539         return( theClass );
540     }
541     
542         public static Class JavaDoc
543     getInnerArrayElementClass( final Class JavaDoc arrayClass )
544         throws ClassNotFoundException JavaDoc
545     {
546         Class JavaDoc 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 JavaDoc
561     InstantiateObject( final String JavaDoc theString )
562         throws Exception JavaDoc
563     {
564         Object JavaDoc result = null;
565         
566         try
567         {
568             result = InstantiateNumber( theString );
569         }
570         catch( NumberFormatException JavaDoc e )
571         {
572             result = theString;
573         }
574         
575         return( result );
576     }
577     
578     /*
579         Return true if caller signature is compatible with callee.
580         
581         @param callee the signature of the method to be called
582         @param caller the signature of the argument list
583      */

584         public static boolean
585     signaturesAreCompatible( Class JavaDoc [] callee, Class JavaDoc [] 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 JavaDoc
607     InstantiateObject( final Class JavaDoc theClass, final Object JavaDoc [] args )
608         throws Exception JavaDoc
609     {
610         final Class JavaDoc [] signature = new Class JavaDoc [ args.length ];
611         
612         for( int i = 0; i < signature.length; ++i )
613         {
614             signature[ i ] = args[ i ].getClass();
615         }
616         
617         Constructor JavaDoc constructor = null;
618         try
619         {
620             // this will fail if a constructor takes an interface;
621
// the code below will then find a compatible constructor
622
constructor = theClass.getConstructor( signature );
623         }
624         catch( NoSuchMethodException JavaDoc e )
625         {
626             final Constructor JavaDoc [] constructors = theClass.getConstructors();
627             
628             int numMatches = 0;
629             for( int i = 0; i < constructors.length; ++i )
630             {
631                 final Constructor JavaDoc tempConstructor = constructors[ i ];
632                 
633                 final Class JavaDoc [] tempSignature = tempConstructor.getParameterTypes();
634                 
635                 if ( signaturesAreCompatible( tempSignature, signature ) )
636                 {
637                     ++numMatches;
638                     constructor = tempConstructor;
639                     // keep going; there could be more than one valid match
640
}
641             }
642             
643             // to succeed, there must be exactly one match
644
if ( numMatches != 1 )
645             {
646                 throw e;
647             }
648         }
649         
650         Object JavaDoc result = null;
651         try
652         {
653             result = constructor.newInstance( args );
654         }
655         catch( java.lang.reflect.InvocationTargetException JavaDoc e )
656         {
657             // InvocationTargetException wraps the real cause
658
final Throwable JavaDoc cause = e.getCause();
659             
660             if ( cause instanceof Exception JavaDoc )
661             {
662                 throw (Exception JavaDoc)cause;
663             }
664             else
665             {
666                 // shouldn't happen, so we'll just rethrow it
667
throw e;
668             }
669         }
670         
671         return( result );
672     }
673     
674     
675         public static Object JavaDoc
676     InstantiateObject( final Class JavaDoc theClass, final String JavaDoc theString )
677         throws Exception JavaDoc
678     {
679         final Class JavaDoc [] signature = new Class JavaDoc [] { String JavaDoc.class };
680         final Constructor JavaDoc constructor = theClass.getConstructor( signature );
681         
682         Object JavaDoc result = null;
683         try
684         {
685             result = constructor.newInstance( new Object JavaDoc[] { theString } );
686         }
687         catch( java.lang.reflect.InvocationTargetException JavaDoc e )
688         {
689             // InvocationTargetException wraps the real cause
690
Throwable JavaDoc cause = e.getCause();
691             
692             if ( cause instanceof Exception JavaDoc )
693             {
694                 throw (Exception JavaDoc)cause;
695             }
696             else
697             {
698                 // shouldn't happen, so we'll just rethrow it
699
throw e;
700             }
701         }
702         
703         return( result );
704     }
705     
706     /*
707         Don't get fancy here, simple precedence:
708             Integer, Long if no decimal point, use Long if won't fit in an Integer
709             Double if decimal point (for maximum precision)
710      */

711         private static Object JavaDoc
712     InstantiateNumber( final String JavaDoc theString )
713         throws Exception JavaDoc
714     {
715         Object JavaDoc result = null;
716         
717         if ( theString.indexOf( '.' ) >= 0 )
718         {
719             result = InstantiateObject( Double JavaDoc.class, theString );
720         }
721         else
722         {
723             try
724             {
725                 result = InstantiateObject( Integer JavaDoc.class, theString );
726             }
727             catch( NumberFormatException JavaDoc e )
728             {
729                 // perhaps it wouldn't fit; try it as a long
730
result = InstantiateObject( Long JavaDoc.class, theString );
731             }
732         }
733         return( result );
734     }
735
736     
737     /*
738         Given a Class and a String, create a new instance with a constructor that accept
739         a String. Primitive types are instantiated as their equivalent Object forms.
740         
741         @param theClass the class from which an instance should be instantiated
742         @param theString the string to be supplied to the constructor
743      */

744         public static Object JavaDoc
745     InstantiateFromString( final Class JavaDoc theClass, final String JavaDoc theString )
746         throws Exception JavaDoc
747     {
748         Object JavaDoc result = null;
749         
750         // char and Character do not have a String constructor, so we must special-case it
751
if ( theClass == Object JavaDoc.class )
752         {
753             // special case, apply rules to create an object
754
result = InstantiateObject( theString );
755         }
756         else if ( theClass == Number JavaDoc.class )
757         {
758             // special case, apply rules to create a number
759
result = InstantiateNumber( theString );
760         }
761         else if ( theClass == Character JavaDoc.class || theClass == char.class)
762         {
763             if ( theString.length() != 1 )
764             {
765                 throw new IllegalArgumentException JavaDoc( "not a character: " + theString );
766             }
767             
768             result = new Character JavaDoc( theString.charAt( 0 ) );
769         }
770         else
771         {
772             
773             final Class JavaDoc objectClass = PrimitiveClassToObjectClass( theClass );
774             
775             result = InstantiateObject( objectClass, theString );
776         }
777         
778         return( result );
779     }
780     
781     
782     /*
783         Given a Class, create a new instance with an empty constructor.
784         Primitive types are instantiated as their equivalent Object forms.
785         Any value is acceptable in the newly created object.
786         
787         @param theClass the class from which an instance should be instantiated
788      */

789         public static Object JavaDoc
790     InstantiateDefault( final Class JavaDoc inClass )
791         throws Exception JavaDoc
792     {
793         Object JavaDoc result = null;
794         
795         final Class JavaDoc objectClass = PrimitiveClassToObjectClass( inClass );
796         
797         if ( Number JavaDoc.class.isAssignableFrom( objectClass ) )
798         {
799             result = InstantiateFromString( objectClass, "0" );
800         }
801         else if ( objectClass == Boolean JavaDoc.class)
802         {
803             result = new Boolean JavaDoc( "true" );
804         }
805         else if ( objectClass == Character JavaDoc.class)
806         {
807             result = new Character JavaDoc( 'X' );
808         }
809         else if ( classIsArray( objectClass ) )
810         {
811             result = Array.newInstance( objectClass, 0 );
812         }
813         else if ( objectClass == Object JavaDoc.class )
814         {
815             result = new String JavaDoc( "anyObject" );
816         }
817         else if ( objectClass == String JavaDoc.class )
818         {
819             result = new String JavaDoc( "" );
820         }
821         else if ( objectClass == java.net.URL JavaDoc.class )
822         {
823             result = new java.net.URL JavaDoc( "http://www.sun.com" );
824         }
825         else if ( objectClass == java.net.URI JavaDoc.class )
826         {
827             result = new java.net.URI JavaDoc( "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             //result = InstantiateFromString( objectClass, "0" );
838
}
839         return( result );
840     }
841     
842     
843     /*
844         We allow abbrevations of certain standard java types
845         
846         Turn "Integer" into "java.lang.Integer", etc.
847      */

848      final static String JavaDoc [] 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 JavaDoc
853     ExpandClassName( final String JavaDoc name )
854     {
855         String JavaDoc 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 ) // no match so far
868
{
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     /*
898         Convert inner element. Only works for arrays of Objects. Example:
899         
900         mapActualElementClass( "[[[LObject;", "Long" ) =>[[[LLong;
901      */

902         public static Class JavaDoc
903     convertArrayClass( final Class JavaDoc arrayClass, final Class JavaDoc newInnerType )
904         throws ClassNotFoundException JavaDoc
905     {
906         final String JavaDoc arrayClassname = arrayClass.getName();
907         if ( ! arrayClassname.endsWith( ";" ) )
908         {
909             throw new IllegalArgumentException JavaDoc( "not an array of Object" );
910         }
911         
912         final int innerNameBegin = 1 + arrayClassname.indexOf( "L" );
913         
914         final String JavaDoc newClassName = arrayClassname.substring( 0, innerNameBegin ) + newInnerType.getName() + ";";
915         
916         final Class JavaDoc newClass = getClassFromName( newClassName );
917         
918         return( newClass );
919     }
920     
921     
922 }
923
924
Popular Tags